Skip to content

Commit

Permalink
chore: Warn Icons support
Browse files Browse the repository at this point in the history
  • Loading branch information
eriklimakc committed Nov 27, 2023
1 parent 1d850c4 commit fcbeafc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
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
42 changes: 32 additions & 10 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 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 @@ -230,6 +244,14 @@ private static void OnIconChanged(DependencyObject d, DependencyPropertyChangedE
}
}

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

0 comments on commit fcbeafc

Please sign in to comment.