Skip to content

Commit be7b6a3

Browse files
committed
refactor: (base/FontIcon) actual glyph & font-family management
1 parent a738811 commit be7b6a3

File tree

4 files changed

+83
-82
lines changed

4 files changed

+83
-82
lines changed

samples/WpfApp1/MainWindow.xaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
</Grid>
2727
</Expander.Content>
2828
</Expander>
29+
<ui:IconAndText x:Name="s1" Icon="{x:Static ui:SegoeFluentIcons.Home}" Content="Home" />
30+
<ui:FontIcon x:Name="s2" Icon="{x:Static ui:SegoeFluentIcons.Home}"/>
31+
<Button Click="Button_Click">
32+
Update Icon
33+
</Button>
2934
</ikw:SimpleStackPanel>
3035
</Canvas>
3136
</Window>

samples/WpfApp1/MainWindow.xaml.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,12 @@ private void SettingsCard_Click(object sender, RoutedEventArgs e)
148148
{
149149
MessageBox.Show("clicked");
150150
}
151+
152+
private void Button_Click(object sender, RoutedEventArgs e)
153+
{
154+
s1.Icon = s1.Icon.Value.Glyph == SegoeFluentIcons.Home.Glyph
155+
? SegoeFluentIcons.Delete : SegoeFluentIcons.Home;
156+
s2.Icon = s1.Icon;
157+
}
151158
}
152159
}

source/iNKORE.UI.WPF.Modern/Common/FontIconSource.cs

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using iNKORE.UI.WPF.Modern.Common.IconKeys;
55
using iNKORE.UI.WPF.Modern.Controls;
66
using System;
7+
using System.Diagnostics;
78
using System.Windows;
89
using System.Windows.Markup;
910
using System.Windows.Media;
@@ -30,25 +31,17 @@ public FontIconSource()
3031
nameof(FontFamily),
3132
typeof(FontFamily),
3233
typeof(FontIconSource),
33-
new PropertyMetadata(new FontFamily(FontIcon.SegoeIconsFontFamilyName), FontFamilyProperty_ValueChanged));
34-
35-
private static void FontFamilyProperty_ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
36-
{
37-
if (d is IFontIconClass cls)
38-
{
39-
UpdateIconData(cls, false);
40-
}
41-
}
34+
new PropertyMetadata(null));
4235

4336
/// <summary>
4437
/// Gets or sets the font used to display the icon glyph.
4538
/// </summary>
4639
/// <returns>
4740
/// The font used to display the icon glyph.
4841
/// </returns>
49-
public FontFamily FontFamily
42+
public FontFamily? FontFamily
5043
{
51-
get => (FontFamily)GetValue(FontFamilyProperty);
44+
get => (FontFamily?)GetValue(FontFamilyProperty);
5245
set => SetValue(FontFamilyProperty, value);
5346
}
5447

@@ -128,25 +121,17 @@ public FontWeight FontWeight
128121
nameof(Glyph),
129122
typeof(string),
130123
typeof(FontIconSource),
131-
new PropertyMetadata(string.Empty, GlyphProperty_ValueChanged));
132-
133-
private static void GlyphProperty_ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
134-
{
135-
if(d is IFontIconClass cls)
136-
{
137-
UpdateIconData(cls, false);
138-
}
139-
}
124+
new PropertyMetadata(null));
140125

141126
/// <summary>
142127
/// Gets or sets the character code that identifies the icon glyph.
143128
/// </summary>
144129
/// <returns>
145130
/// The hexadecimal character code for the icon glyph.
146131
/// </returns>
147-
public string Glyph
132+
public string? Glyph
148133
{
149-
get => (string)GetValue(GlyphProperty);
134+
get => (string?)GetValue(GlyphProperty);
150135
set => SetValue(GlyphProperty, value);
151136
}
152137

@@ -158,7 +143,7 @@ public string Glyph
158143
nameof(Icon),
159144
typeof(FontIconData?),
160145
typeof(FontIconSource),
161-
new PropertyMetadata(null, (d, e) => UpdateIconData(d as IFontIconClass, true)));
146+
new PropertyMetadata(null));
162147

163148
/// <summary>
164149
/// Gets or sets the wrapped icon, which includes <see cref="Glyph"/> and <see cref="FontFamily"/>. You can get these instances from <see cref="iNKORE.UI.WPF.Modern.Common.IconKeys"/> namespace.
@@ -171,44 +156,6 @@ public FontIconData? Icon
171156
}
172157

173158

174-
public static bool UpdateIconData(IFontIconClass instance, bool preserveData)
175-
{
176-
bool isChanged = false;
177-
178-
if (instance.Icon.HasValue)
179-
{
180-
var icon = instance.Icon.Value;
181-
182-
if (instance.Glyph != icon.Glyph)
183-
{
184-
if (preserveData)
185-
instance.Glyph = icon.Glyph;
186-
else
187-
{
188-
instance.Icon = null;
189-
return true;
190-
}
191-
isChanged = true;
192-
}
193-
if (icon.FontFamily != null && icon.FontFamily != instance.FontFamily)
194-
{
195-
if (preserveData)
196-
instance.FontFamily = icon.FontFamily;
197-
else
198-
{
199-
instance.Icon = null;
200-
return true;
201-
}
202-
203-
isChanged = true;
204-
}
205-
}
206-
207-
return isChanged;
208-
}
209-
210-
211-
212159
/// <inheritdoc/>
213160
protected override IconElement CreateIconElementCore()
214161
{

source/iNKORE.UI.WPF.Modern/Controls/FontIcon.cs

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using iNKORE.UI.WPF.Modern.Common;
22
using iNKORE.UI.WPF.Modern.Common.IconKeys;
3+
using System;
34
using System.ComponentModel;
5+
using System.Diagnostics;
46
using System.Windows;
57
using System.Windows.Controls;
8+
using System.Windows.Data;
69
using System.Windows.Media;
710

811
namespace iNKORE.UI.WPF.Modern.Controls
@@ -46,7 +49,7 @@ public FontIcon(string glyph, FontFamily fontFamily) : this()
4649
typeof(FontFamily),
4750
typeof(FontIcon),
4851
new FrameworkPropertyMetadata(
49-
new FontFamily(SegoeIconsFontFamilyName),
52+
null,
5053
OnFontFamilyChanged));
5154

5255
/// <summary>
@@ -55,21 +58,31 @@ public FontIcon(string glyph, FontFamily fontFamily) : this()
5558
/// <returns>The font used to display the icon glyph.</returns>
5659
[Bindable(true), Category("Appearance")]
5760
[Localizability(LocalizationCategory.Font)]
58-
public FontFamily FontFamily
61+
public FontFamily? FontFamily
5962
{
60-
get { return (FontFamily)GetValue(FontFamilyProperty); }
63+
get { return (FontFamily?)GetValue(FontFamilyProperty); }
6164
set { SetValue(FontFamilyProperty, value); }
6265
}
6366

64-
private static void OnFontFamilyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
67+
protected static readonly DependencyPropertyKey ActualFontFamilyPropertyKey =
68+
DependencyProperty.RegisterReadOnly(
69+
nameof(ActualFontFamily),
70+
typeof(FontFamily),
71+
typeof(FontIcon),
72+
new FrameworkPropertyMetadata(new FontFamily(SegoeIconsFontFamilyName)));
73+
74+
public static readonly DependencyProperty ActualFontFamilyProperty = ActualFontFamilyPropertyKey.DependencyProperty;
75+
76+
public FontFamily ActualFontFamily
6577
{
66-
var fontIcon = (FontIcon)d;
67-
if (fontIcon._textBlock != null)
68-
{
69-
fontIcon._textBlock.FontFamily = (FontFamily)e.NewValue;
70-
}
78+
get { return (FontFamily)GetValue(ActualFontFamilyProperty); }
79+
private set { SetValue(ActualFontFamilyPropertyKey, value); }
80+
}
7181

72-
FontIconSource.UpdateIconData(fontIcon, false);
82+
83+
private static void OnFontFamilyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
84+
{
85+
(d as FontIcon)?.UpdateIconData();
7386
}
7487

7588
/// <summary>
@@ -177,18 +190,33 @@ private static void OnFontWeightChanged(DependencyObject d, DependencyPropertyCh
177190
nameof(Glyph),
178191
typeof(string),
179192
typeof(FontIcon),
180-
new FrameworkPropertyMetadata(string.Empty, OnGlyphChanged));
193+
new FrameworkPropertyMetadata(null, OnGlyphChanged));
181194

182195
/// <summary>
183196
/// Gets or sets the character code that identifies the icon glyph.
184197
/// </summary>
185198
/// <returns>The hexadecimal character code for the icon glyph.</returns>
186-
public string Glyph
199+
public string? Glyph
187200
{
188-
get => (string)GetValue(GlyphProperty);
201+
get => (string?)GetValue(GlyphProperty);
189202
set => SetValue(GlyphProperty, value);
190203
}
191204

205+
protected static readonly DependencyPropertyKey ActualGlyphPropertyKey =
206+
DependencyProperty.RegisterReadOnly(
207+
nameof(ActualGlyph),
208+
typeof(string),
209+
typeof(FontIcon),
210+
new FrameworkPropertyMetadata(string.Empty));
211+
212+
public static readonly DependencyProperty ActualGlyphProperty = ActualGlyphPropertyKey.DependencyProperty;
213+
214+
public string ActualGlyph
215+
{
216+
get => (string)GetValue(ActualGlyphProperty);
217+
private set => SetValue(ActualGlyphPropertyKey, value);
218+
}
219+
192220
/// <summary>
193221
/// Identifies the <see cref="Icon"/> dependency property.
194222
/// </summary>
@@ -197,7 +225,7 @@ public string Glyph
197225
nameof(Icon),
198226
typeof(FontIconData?),
199227
typeof(FontIcon),
200-
new PropertyMetadata(null, (d, e) => FontIconSource.UpdateIconData(d as IFontIconClass, true)));
228+
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure, OnIconChanged));
201229

202230
/// <summary>
203231
/// Gets or sets the wrapped icon, which includes <see cref="Glyph"/> and <see cref="FontFamily"/>. You can get these instances from <see cref="iNKORE.UI.WPF.Modern.Common.IconKeys"/> namespace.
@@ -218,9 +246,21 @@ private static void OnGlyphChanged(DependencyObject d, DependencyPropertyChanged
218246
fontIcon._textBlock.Text = (string)e.NewValue;
219247
}
220248

221-
FontIconSource.UpdateIconData(fontIcon, false);
249+
(d as FontIcon)?.UpdateIconData();
250+
}
251+
252+
private static void OnIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
253+
{
254+
(d as FontIcon)?.UpdateIconData();
255+
}
256+
257+
private void UpdateIconData()
258+
{
259+
this.ActualGlyph = this.Glyph ?? this.Icon?.Glyph ?? null;
260+
this.ActualFontFamily = this.FontFamily ?? this.Icon?.FontFamily ?? new FontFamily(SegoeIconsFontFamilyName);
222261
}
223262

263+
224264
private protected override void InitializeChildren()
225265
{
226266
_textBlock = new TextBlock
@@ -229,13 +269,15 @@ private protected override void InitializeChildren()
229269
HorizontalAlignment = HorizontalAlignment.Stretch,
230270
VerticalAlignment = VerticalAlignment.Center,
231271
TextAlignment = TextAlignment.Center,
232-
FontFamily = FontFamily,
233-
FontSize = FontSize,
234-
FontStyle = FontStyle,
235-
FontWeight = FontWeight,
236-
Text = Glyph
237272
};
238273

274+
// Setup bindings
275+
_textBlock.SetBinding(TextBlock.FontFamilyProperty, new Binding { Path = new PropertyPath(nameof(ActualFontFamily)), Source = this });
276+
_textBlock.SetBinding(TextBlock.FontSizeProperty, new Binding { Path = new PropertyPath(nameof(FontSize)), Source = this });
277+
_textBlock.SetBinding(TextBlock.FontStyleProperty, new Binding { Path = new PropertyPath(nameof(FontStyle)), Source = this });
278+
_textBlock.SetBinding(TextBlock.FontWeightProperty, new Binding { Path = new PropertyPath(nameof(FontWeight)), Source = this });
279+
_textBlock.SetBinding(TextBlock.TextProperty, new Binding { Path = new PropertyPath(nameof(ActualGlyph)), Source = this });
280+
239281
if (ShouldInheritForegroundFromVisualParent)
240282
{
241283
_textBlock.Foreground = VisualParentForeground;
@@ -287,12 +329,12 @@ protected override IconSource CreateIconSourceCore()
287329
FontFamily = new FontFamily(SegoeIconsFontFamilyName);
288330
}
289331
iconSource.FontFamily = FontFamily;
332+
iconSource.Icon = Icon;
290333

291334
iconSource.FontWeight = FontWeight;
292335
iconSource.FontStyle = FontStyle;
293336

294337
return iconSource;
295-
296338
}
297339
}
298340
}

0 commit comments

Comments
 (0)