Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add ControlExtensions.Icon support to TextBox #1288

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/material-controls-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Below is a summary of the icon support for different controls:
| **Button** | ✔️ | ❌ | ❌ |
| **Combobox** | ✔️ | ❌ | ❌ |
| **PasswordBox** | ✔️ | ❌ | ❌ |
| **TextBox** | | ✔️ | ✔️ |
| **TextBox** | ✔️ | ✔️ | ✔️ |
eriklimakc marked this conversation as resolved.
Show resolved Hide resolved


This feature allows for the addition of icons on the supported controls. Icons can be added in different positions, such as `Icon`, `LeadingIcon`, and `TrailingIcon`. You can choose from various [`IconElement`](https://docs.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.iconelement)s to represent your icons, including `<BitmapIcon />`, `<FontIcon />`, `<PathIcon />`, or `<SymbolIcon />`.
Expand Down
10 changes: 9 additions & 1 deletion src/library/Uno.Material/Extensions/ControlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static class ControlExtensions
"Icon",
typeof(IconElement),
typeof(ControlExtensions),
new PropertyMetadata(default));
new PropertyMetadata(default, OnIconChanged));

[DynamicDependency(nameof(SetIcon))]
public static IconElement GetIcon(Control obj) => (IconElement)obj.GetValue(IconProperty);
Expand Down Expand Up @@ -222,6 +222,14 @@ public static class ControlExtensions

#endregion

private static void OnIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TextBox textBox)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why constraint it to TextBox only? the target property accept any Control as recipient?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But only TextBox supports LeadingIcon/Command/IsVisible, right?

In that case we should probably spit out a warning if someone is trying to set those properties on things other than a TextBox

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I use System.Diagnostics.Debug.WriteLine("...") for the warning?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used d.Log().LogWarning("...")

private static void WarnNotSupportedProperty(DependencyObject d, string propertyName)
{
if (d is not TextBox)
{
d.Log().LogWarning($"Warning: {propertyName} is only supported on TextBox controls.");
}
}

{
textBox.SetValue(LeadingIconProperty, e.NewValue);
}
}

private static void OnElevationChanged(DependencyObject element, DependencyPropertyChangedEventArgs e)
=> SurfaceTintExtensions.OnElevationChanged(element, (int)e.NewValue);

Expand Down
8 changes: 4 additions & 4 deletions src/library/Uno.Material/Styles/Controls/v2/TextBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@
Content="{Binding Path=(um:ControlExtensions.LeadingIcon), RelativeSource={RelativeSource TemplatedParent}}"
Command="{Binding Path=(um:ControlExtensions.LeadingCommand), RelativeSource={RelativeSource TemplatedParent}}"
Visibility="{Binding Path=(um:ControlExtensions.IsLeadingIconVisible), RelativeSource={RelativeSource TemplatedParent}}"
Style="{ThemeResource MaterialLeadingIconStyle}"
Style="{StaticResource MaterialLeadingIconStyle}"
HorizontalAlignment="Center"
Margin="0,0,16,0"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
Expand Down Expand Up @@ -530,7 +530,7 @@
Content="{Binding Path=(um:ControlExtensions.TrailingIcon), RelativeSource={RelativeSource TemplatedParent}}"
Command="{Binding Path=(um:ControlExtensions.TrailingCommand), RelativeSource={RelativeSource TemplatedParent}}"
Visibility="{Binding Path=(um:ControlExtensions.IsTrailingIconVisible), RelativeSource={RelativeSource TemplatedParent}}"
Style="{ThemeResource MaterialTrailingIconStyle}"
Style="{StaticResource MaterialTrailingIconStyle}"
HorizontalAlignment="Center"
Margin="14,0,0,0"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
Expand Down Expand Up @@ -702,7 +702,7 @@
Content="{Binding Path=(um:ControlExtensions.LeadingIcon), RelativeSource={RelativeSource TemplatedParent}}"
Command="{Binding Path=(um:ControlExtensions.LeadingCommand), RelativeSource={RelativeSource TemplatedParent}}"
Visibility="{Binding Path=(um:ControlExtensions.IsLeadingIconVisible), RelativeSource={RelativeSource TemplatedParent}}"
Style="{ThemeResource MaterialLeadingIconStyle}"
Style="{StaticResource MaterialLeadingIconStyle}"
HorizontalAlignment="Center"
Margin="0,0,16,0"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
Expand Down Expand Up @@ -764,7 +764,7 @@
Content="{Binding Path=(um:ControlExtensions.TrailingIcon), RelativeSource={RelativeSource TemplatedParent}}"
Command="{Binding Path=(um:ControlExtensions.TrailingCommand), RelativeSource={RelativeSource TemplatedParent}}"
Visibility="{Binding Path=(um:ControlExtensions.IsTrailingIconVisible), RelativeSource={RelativeSource TemplatedParent}}"
Style="{ThemeResource MaterialTrailingIconStyle}"
Style="{StaticResource MaterialTrailingIconStyle}"
HorizontalAlignment="Center"
Margin="14,0,0,0"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
Expand Down
Loading