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 all commits
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
15 changes: 8 additions & 7 deletions doc/material-controls-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

Below is a summary of the icon support for different controls:

| Control | Icon | LeadingIcon | TrailingIcon |
|-----------------|------|-------------|--------------|
| **Button** | ✔️ | ❌ | ❌ |
| **Combobox** | ✔️ | ❌ | ❌ |
| **PasswordBox** | ✔️ | ❌ | ❌ |
| **TextBox** | ❌ | ✔️ | ✔️ |

| Control | Icon | LeadingIcon | TrailingIcon |
|-----------------|-------|-------------|--------------|
| **Button** | ✔️ | ❌ | ❌ |
| **Combobox** | ✔️ | ❌ | ❌ |
| **PasswordBox** | ✔️ | ❌ | ❌ |
| **TextBox** | ✔️* | ✔️ | ✔️ |

\* Setting the `Icon` for a `TextBox` will simply set the `LeadingIcon`.

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
52 changes: 41 additions & 11 deletions src/library/Uno.Material/Extensions/ControlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Windows.UI;
using Uno.Disposables;
using System.Windows.Input;
using Uno.Extensions;
using Microsoft.Extensions.Logging;

#if WinUI
using Microsoft.UI.Xaml;
Expand All @@ -29,7 +31,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 @@ -66,94 +68,105 @@ public static class ControlExtensions
#endregion

#region DependencyProperty: LeadingIcon

public static DependencyProperty LeadingIconProperty { [DynamicDependency(nameof(GetLeadingIcon))] get; } = DependencyProperty.RegisterAttached(
"LeadingIcon",
typeof(IconElement),
typeof(ControlExtensions),
new PropertyMetadata(default));
new PropertyMetadata(default, OnLeadingIconChanged));

[DynamicDependency(nameof(SetLeadingIcon))]
public static IconElement GetLeadingIcon(Control obj) => (IconElement)obj.GetValue(LeadingIconProperty);

[DynamicDependency(nameof(GetLeadingIcon))]
public static void SetLeadingIcon(Control obj, IconElement value) => obj.SetValue(LeadingIconProperty, value);

private static void OnLeadingIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
=> WarnNotSupportedProperty(d, "LeadingIcon");
#endregion

#region DependencyProperty: IsLeadingIconVisible

public static DependencyProperty IsLeadingIconVisibleProperty { [DynamicDependency(nameof(GetIsLeadingIconVisible))] get; } = DependencyProperty.RegisterAttached(
"IsLeadingIconVisible",
typeof(bool),
typeof(ControlExtensions),
new PropertyMetadata(true));
new PropertyMetadata(true, OnIsLeadingIconVisibleChanged));

[DynamicDependency(nameof(SetIsLeadingIconVisible))]
public static bool GetIsLeadingIconVisible(Control obj) => (bool)obj.GetValue(IsLeadingIconVisibleProperty);

[DynamicDependency(nameof(GetIsLeadingIconVisible))]
public static void SetIsLeadingIconVisible(Control obj, bool value) => obj.SetValue(IsLeadingIconVisibleProperty, value);

private static void OnIsLeadingIconVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
=> WarnNotSupportedProperty(d, "IsLeadingIconVisible");
#endregion

#region DependencyProperty: LeadingCommand
public static DependencyProperty LeadingCommandProperty { [DynamicDependency(nameof(GetLeadingCommand))] get; } = DependencyProperty.RegisterAttached(
"LeadingCommand",
typeof(ICommand),
typeof(ControlExtensions),
new PropertyMetadata(default));
new PropertyMetadata(default, OnLeadingCommandChanged));

[DynamicDependency(nameof(GetLeadingCommand))]
public static ICommand GetLeadingCommand(Control obj) => (ICommand)obj.GetValue(LeadingCommandProperty);

[DynamicDependency(nameof(SetLeadingCommand))]
public static void SetLeadingCommand(Control obj, ICommand value) => obj.SetValue(LeadingCommandProperty, value);

private static void OnLeadingCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
=> WarnNotSupportedProperty(d, "LeadingCommand");
#endregion

#region DependencyProperty: TrailingIcon

public static DependencyProperty TrailingIconProperty { [DynamicDependency(nameof(GetTrailingIcon))] get; } = DependencyProperty.RegisterAttached(
"TrailingIcon",
typeof(IconElement),
typeof(ControlExtensions),
new PropertyMetadata(default));
new PropertyMetadata(default, OnTrailingIconChanged));

[DynamicDependency(nameof(SetTrailingIcon))]
public static IconElement GetTrailingIcon(Control obj) => (IconElement)obj.GetValue(TrailingIconProperty);

[DynamicDependency(nameof(GetTrailingIcon))]
public static void SetTrailingIcon(Control obj, IconElement value) => obj.SetValue(TrailingIconProperty, value);

private static void OnTrailingIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
=> WarnNotSupportedProperty(d, "TrailingIcon");
#endregion

#region DependencyProperty: IsTrailingIconVisible

public static DependencyProperty IsTrailingIconVisibleProperty { [DynamicDependency(nameof(GetIsTrailingIconVisible))] get; } = DependencyProperty.RegisterAttached(
"IsTrailingIconVisible",
typeof(bool),
typeof(ControlExtensions),
new PropertyMetadata(true));
new PropertyMetadata(true, OnIsTrailingIconVisibleChanged));

[DynamicDependency(nameof(SetIsTrailingIconVisible))]
public static bool GetIsTrailingIconVisible(Control obj) => (bool)obj.GetValue(IsTrailingIconVisibleProperty);

[DynamicDependency(nameof(GetIsTrailingIconVisible))]
public static void SetIsTrailingIconVisible(Control obj, bool value) => obj.SetValue(IsTrailingIconVisibleProperty, value);

private static void OnIsTrailingIconVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
=> WarnNotSupportedProperty(d, "IsTrailingIconVisible");
#endregion

#region DependencyProperty: TrailingCommand
public static DependencyProperty TrailingCommandProperty { [DynamicDependency(nameof(GetTrailingCommand))] get; } = DependencyProperty.RegisterAttached(
"TrailingCommand",
typeof(ICommand),
typeof(ControlExtensions),
new PropertyMetadata(default));
new PropertyMetadata(default, OnTrailingCommandChanged));

[DynamicDependency(nameof(GetTrailingCommand))]
public static ICommand GetTrailingCommand(Control obj) => (ICommand)obj.GetValue(TrailingCommandProperty);

[DynamicDependency(nameof(SetTrailingCommand))]
public static void SetTrailingCommand(Control obj, ICommand value) => obj.SetValue(TrailingCommandProperty, value);

private static void OnTrailingCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
=> WarnNotSupportedProperty(d, "TrailingCommand");
#endregion

#region DependencyProperty: AlternateContent
Expand Down Expand Up @@ -205,6 +218,7 @@ public static class ControlExtensions
public static void SetTintedBackground(UIElement obj, SolidColorBrush value) => obj.SetValue(TintedBackgroundProperty, value);

#endregion

#region DependencyProperty: IsTintEnabled
/// <summary>
/// Gets or sets whether or not the SurfaceTintColor should be applied for elevated views
Expand All @@ -222,6 +236,22 @@ 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 WarnNotSupportedProperty(DependencyObject d, string propertyName)
{
if (d is not TextBox)
{
d.Log().LogWarning($"Warning: {propertyName} is only supported on TextBox controls.");
}
}

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