Skip to content

Commit fcbeafc

Browse files
author
eriklimakc
committed
chore: Warn Icons support
1 parent 1d850c4 commit fcbeafc

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

doc/material-controls-extensions.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

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

7-
| Control | Icon | LeadingIcon | TrailingIcon |
8-
|-----------------|------|-------------|--------------|
9-
| **Button** | ✔️ |||
10-
| **Combobox** | ✔️ |||
11-
| **PasswordBox** | ✔️ |||
12-
| **TextBox** | ✔️ | ✔️ | ✔️ |
13-
7+
| Control | Icon | LeadingIcon | TrailingIcon |
8+
|-----------------|-------|-------------|--------------|
9+
| **Button** | ✔️ |||
10+
| **Combobox** | ✔️ |||
11+
| **PasswordBox** | ✔️ |||
12+
| **TextBox** | ✔️* | ✔️ | ✔️ |
13+
14+
\* Setting the `Icon` for a `TextBox` will simply set the `LeadingIcon`.
1415

1516
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 />`.
1617

src/library/Uno.Material/Extensions/ControlExtensions.cs

+32-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using Windows.UI;
66
using Uno.Disposables;
77
using System.Windows.Input;
8+
using Uno.Extensions;
9+
using Microsoft.Extensions.Logging;
810

911
#if WinUI
1012
using Microsoft.UI.Xaml;
@@ -66,94 +68,105 @@ public static class ControlExtensions
6668
#endregion
6769

6870
#region DependencyProperty: LeadingIcon
69-
7071
public static DependencyProperty LeadingIconProperty { [DynamicDependency(nameof(GetLeadingIcon))] get; } = DependencyProperty.RegisterAttached(
7172
"LeadingIcon",
7273
typeof(IconElement),
7374
typeof(ControlExtensions),
74-
new PropertyMetadata(default));
75+
new PropertyMetadata(default, OnLeadingIconChanged));
7576

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

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

83+
private static void OnLeadingIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
84+
=> WarnNotSupportedProperty(d, "LeadingIcon");
8285
#endregion
8386

8487
#region DependencyProperty: IsLeadingIconVisible
85-
8688
public static DependencyProperty IsLeadingIconVisibleProperty { [DynamicDependency(nameof(GetIsLeadingIconVisible))] get; } = DependencyProperty.RegisterAttached(
8789
"IsLeadingIconVisible",
8890
typeof(bool),
8991
typeof(ControlExtensions),
90-
new PropertyMetadata(true));
92+
new PropertyMetadata(true, OnIsLeadingIconVisibleChanged));
9193

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

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

100+
private static void OnIsLeadingIconVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
101+
=> WarnNotSupportedProperty(d, "IsLeadingIconVisible");
98102
#endregion
99103

100104
#region DependencyProperty: LeadingCommand
101105
public static DependencyProperty LeadingCommandProperty { [DynamicDependency(nameof(GetLeadingCommand))] get; } = DependencyProperty.RegisterAttached(
102106
"LeadingCommand",
103107
typeof(ICommand),
104108
typeof(ControlExtensions),
105-
new PropertyMetadata(default));
109+
new PropertyMetadata(default, OnLeadingCommandChanged));
106110

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

110114
[DynamicDependency(nameof(SetLeadingCommand))]
111115
public static void SetLeadingCommand(Control obj, ICommand value) => obj.SetValue(LeadingCommandProperty, value);
116+
117+
private static void OnLeadingCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
118+
=> WarnNotSupportedProperty(d, "LeadingCommand");
112119
#endregion
113120

114121
#region DependencyProperty: TrailingIcon
115-
116122
public static DependencyProperty TrailingIconProperty { [DynamicDependency(nameof(GetTrailingIcon))] get; } = DependencyProperty.RegisterAttached(
117123
"TrailingIcon",
118124
typeof(IconElement),
119125
typeof(ControlExtensions),
120-
new PropertyMetadata(default));
126+
new PropertyMetadata(default, OnTrailingIconChanged));
121127

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

125131
[DynamicDependency(nameof(GetTrailingIcon))]
126132
public static void SetTrailingIcon(Control obj, IconElement value) => obj.SetValue(TrailingIconProperty, value);
133+
134+
private static void OnTrailingIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
135+
=> WarnNotSupportedProperty(d, "TrailingIcon");
127136
#endregion
128137

129138
#region DependencyProperty: IsTrailingIconVisible
130-
131139
public static DependencyProperty IsTrailingIconVisibleProperty { [DynamicDependency(nameof(GetIsTrailingIconVisible))] get; } = DependencyProperty.RegisterAttached(
132140
"IsTrailingIconVisible",
133141
typeof(bool),
134142
typeof(ControlExtensions),
135-
new PropertyMetadata(true));
143+
new PropertyMetadata(true, OnIsTrailingIconVisibleChanged));
136144

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

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

151+
private static void OnIsTrailingIconVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
152+
=> WarnNotSupportedProperty(d, "IsTrailingIconVisible");
143153
#endregion
144154

145155
#region DependencyProperty: TrailingCommand
146156
public static DependencyProperty TrailingCommandProperty { [DynamicDependency(nameof(GetTrailingCommand))] get; } = DependencyProperty.RegisterAttached(
147157
"TrailingCommand",
148158
typeof(ICommand),
149159
typeof(ControlExtensions),
150-
new PropertyMetadata(default));
160+
new PropertyMetadata(default, OnTrailingCommandChanged));
151161

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

155165
[DynamicDependency(nameof(SetTrailingCommand))]
156166
public static void SetTrailingCommand(Control obj, ICommand value) => obj.SetValue(TrailingCommandProperty, value);
167+
168+
private static void OnTrailingCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
169+
=> WarnNotSupportedProperty(d, "TrailingCommand");
157170
#endregion
158171

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

207220
#endregion
221+
208222
#region DependencyProperty: IsTintEnabled
209223
/// <summary>
210224
/// Gets or sets whether or not the SurfaceTintColor should be applied for elevated views
@@ -230,6 +244,14 @@ private static void OnIconChanged(DependencyObject d, DependencyPropertyChangedE
230244
}
231245
}
232246

247+
private static void WarnNotSupportedProperty(DependencyObject d, string propertyName)
248+
{
249+
if (d is not TextBox)
250+
{
251+
d.Log().LogWarning($"Warning: {propertyName} is only supported on TextBox controls.");
252+
}
253+
}
254+
233255
private static void OnElevationChanged(DependencyObject element, DependencyPropertyChangedEventArgs e)
234256
=> SurfaceTintExtensions.OnElevationChanged(element, (int)e.NewValue);
235257

0 commit comments

Comments
 (0)