Skip to content

Commit

Permalink
feat: CreateIconElement tracks and updates created instances
Browse files Browse the repository at this point in the history
- Addresses unoplatform#6168
  • Loading branch information
MartinZikmund committed Jun 24, 2021
1 parent 452adda commit 27a35b5
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public IconSource FallbackIconSource
}

public static DependencyProperty FallbackIconSourceProperty { get; } =
DependencyProperty.Register(nameof(FallbackIconSource), typeof(IconSource), typeof(AnimatedIconSource), new PropertyMetadata(null, OnFallbackIconSourcePropertyChanged));
DependencyProperty.Register(nameof(FallbackIconSource), typeof(IconSource), typeof(AnimatedIconSource), new PropertyMetadata(null, OnPropertyChanged));

public bool MirroredWhenRightToLeft
{
Expand All @@ -21,7 +21,7 @@ public bool MirroredWhenRightToLeft
}

public static DependencyProperty MirroredWhenRightToLeftProperty { get; } =
DependencyProperty.Register(nameof(MirroredWhenRightToLeft), typeof(bool), typeof(AnimatedIconSource), new PropertyMetadata(false, OnMirroredWhenRightToLeftPropertyChanged));
DependencyProperty.Register(nameof(MirroredWhenRightToLeft), typeof(bool), typeof(AnimatedIconSource), new PropertyMetadata(false, OnPropertyChanged));

public IAnimatedVisualSource2 Source
{
Expand All @@ -30,9 +30,9 @@ public IAnimatedVisualSource2 Source
}

public static DependencyProperty SourceProperty { get; } =
DependencyProperty.Register(nameof(Source), typeof(IAnimatedVisualSource2), typeof(AnimatedIconSource), new PropertyMetadata(null, OnSourcePropertyChanged));
DependencyProperty.Register(nameof(Source), typeof(IAnimatedVisualSource2), typeof(AnimatedIconSource), new PropertyMetadata(null, OnPropertyChanged));

public override IconElement CreateIconElement()
internal protected override IconElement CreateIconElementCore()
{
AnimatedIcon animatedIcon = new AnimatedIcon();
if (Source is { } source)
Expand All @@ -52,7 +52,7 @@ public override IconElement CreateIconElement()
return animatedIcon;
}

protected override DependencyProperty GetIconElementProperty(DependencyProperty sourceProperty)
internal protected override DependencyProperty GetIconElementPropertyCore(DependencyProperty sourceProperty)
{
if (sourceProperty == SourceProperty)
{
Expand All @@ -67,7 +67,7 @@ protected override DependencyProperty GetIconElementProperty(DependencyProperty
return AnimatedIcon.MirroredWhenRightToLeftProperty;
}

return base.GetIconElementProperty(sourceProperty);
return base.GetIconElementPropertyCore(sourceProperty);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public Uri UriSource
}

public static DependencyProperty UriSourceProperty { get; } =
DependencyProperty.Register(nameof(UriSource), typeof(Uri), typeof(BitmapIconSource), new PropertyMetadata(default(Uri)));
DependencyProperty.Register(nameof(UriSource), typeof(Uri), typeof(BitmapIconSource), new PropertyMetadata(default(Uri), OnPropertyChanged));

public bool ShowAsMonochrome
{
Expand All @@ -27,9 +27,9 @@ public bool ShowAsMonochrome
}

public static DependencyProperty ShowAsMonochromeProperty { get; } =
DependencyProperty.Register(nameof(ShowAsMonochrome), typeof(bool), typeof(BitmapIconSource), new PropertyMetadata(default(bool)));
DependencyProperty.Register(nameof(ShowAsMonochrome), typeof(bool), typeof(BitmapIconSource), new PropertyMetadata(default(bool), OnPropertyChanged));

public override IconElement CreateIconElement()
internal protected override IconElement CreateIconElementCore()
{
var bitmapIcon = new BitmapIcon();

Expand All @@ -50,5 +50,19 @@ public override IconElement CreateIconElement()

return bitmapIcon;
}

internal protected override DependencyProperty GetIconElementPropertyCore(DependencyProperty sourceProperty)
{
if (sourceProperty == ShowAsMonochromeProperty)
{
return BitmapIcon.ShowAsMonochromeProperty;
}
else if (sourceProperty == UriSourceProperty)
{
return BitmapIcon.UriSourceProperty;
}

return base.GetIconElementPropertyCore(sourceProperty);
}
}
}
50 changes: 42 additions & 8 deletions src/Uno.UI/Microsoft/UI/Xaml/Controls/IconSource/FontIconSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public bool MirroredWhenRightToLeft
}

public static DependencyProperty MirroredWhenRightToLeftProperty { get; } =
DependencyProperty.Register(nameof(MirroredWhenRightToLeft), typeof(bool), typeof(FontIconSource), new PropertyMetadata(false));
DependencyProperty.Register(nameof(MirroredWhenRightToLeft), typeof(bool), typeof(FontIconSource), new PropertyMetadata(false, OnPropertyChanged));

public bool IsTextScaleFactorEnabled
{
Expand All @@ -28,7 +28,7 @@ public bool IsTextScaleFactorEnabled
}

public static DependencyProperty IsTextScaleFactorEnabledProperty { get; } =
DependencyProperty.Register(nameof(IsTextScaleFactorEnabled), typeof(bool), typeof(FontIconSource), new PropertyMetadata(true));
DependencyProperty.Register(nameof(IsTextScaleFactorEnabled), typeof(bool), typeof(FontIconSource), new PropertyMetadata(true, OnPropertyChanged));

public string Glyph
{
Expand All @@ -37,7 +37,7 @@ public string Glyph
}

public static DependencyProperty GlyphProperty { get; } =
DependencyProperty.Register(nameof(Glyph), typeof(string), typeof(FontIconSource), new PropertyMetadata(default(string)));
DependencyProperty.Register(nameof(Glyph), typeof(string), typeof(FontIconSource), new PropertyMetadata(default(string), OnPropertyChanged));

public FontWeight FontWeight
{
Expand All @@ -46,7 +46,7 @@ public FontWeight FontWeight
}

public static DependencyProperty FontWeightProperty { get; } =
DependencyProperty.Register(nameof(FontWeight), typeof(FontWeight), typeof(FontIconSource), new PropertyMetadata(new FontWeight(400)));
DependencyProperty.Register(nameof(FontWeight), typeof(FontWeight), typeof(FontIconSource), new PropertyMetadata(new FontWeight(400), OnPropertyChanged));

public FontStyle FontStyle
{
Expand All @@ -55,7 +55,7 @@ public FontStyle FontStyle
}

public static DependencyProperty FontStyleProperty { get; } =
DependencyProperty.Register(nameof(FontStyle), typeof(FontStyle), typeof(FontIconSource), new PropertyMetadata(FontStyle.Normal));
DependencyProperty.Register(nameof(FontStyle), typeof(FontStyle), typeof(FontIconSource), new PropertyMetadata(FontStyle.Normal, OnPropertyChanged));

public double FontSize
{
Expand All @@ -64,7 +64,7 @@ public double FontSize
}

public static DependencyProperty FontSizeProperty { get; } =
DependencyProperty.Register(nameof(FontSize), typeof(double), typeof(FontIconSource), new PropertyMetadata(20.0));
DependencyProperty.Register(nameof(FontSize), typeof(double), typeof(FontIconSource), new PropertyMetadata(20.0, OnPropertyChanged));

public FontFamily FontFamily
{
Expand All @@ -73,10 +73,10 @@ public FontFamily FontFamily
}

public static DependencyProperty FontFamilyProperty { get; } =
DependencyProperty.Register(nameof(FontFamily), typeof(FontFamily), typeof(FontIconSource), new PropertyMetadata(new FontFamily(Uno.UI.FeatureConfiguration.Font.SymbolsFont)));
DependencyProperty.Register(nameof(FontFamily), typeof(FontFamily), typeof(FontIconSource), new PropertyMetadata(new FontFamily(Uno.UI.FeatureConfiguration.Font.SymbolsFont), OnPropertyChanged));

/// <inheritdoc />
public override IconElement CreateIconElement()
internal protected override IconElement CreateIconElementCore()
{
var fontIcon = new FontIcon()
{
Expand All @@ -100,5 +100,39 @@ public override IconElement CreateIconElement()

return fontIcon;
}

internal protected override DependencyProperty GetIconElementPropertyCore(DependencyProperty sourceProperty)
{
if (sourceProperty == FontFamilyProperty)
{
return FontIcon.FontFamilyProperty;
}
else if (sourceProperty == FontSizeProperty)
{
return FontIcon.FontSizeProperty;
}
else if (sourceProperty == FontStyleProperty)
{
return FontIcon.FontStyleProperty;
}
else if (sourceProperty == FontWeightProperty)
{
return FontIcon.FontWeightProperty;
}
else if (sourceProperty == GlyphProperty)
{
return FontIcon.GlyphProperty;
}
else if (sourceProperty == IsTextScaleFactorEnabledProperty)
{
return FontIcon.IsTextScaleFactorEnabledProperty;
}
else if (sourceProperty == MirroredWhenRightToLeftProperty)
{
return FontIcon.MirroredWhenRightToLeftProperty;
}

return base.GetIconElementPropertyCore(sourceProperty);
}
}
}
53 changes: 49 additions & 4 deletions src/Uno.UI/Microsoft/UI/Xaml/Controls/IconSource/IconSource.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#nullable enable

using System;
using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
Expand All @@ -6,6 +10,8 @@ namespace Microsoft.UI.Xaml.Controls
{
public partial class IconSource : DependencyObject
{
private List<WeakReference<IconElement>> m_createdIconElements;

protected IconSource()
{
}
Expand All @@ -17,12 +23,51 @@ public Brush Foreground
}

public static DependencyProperty ForegroundProperty { get; } =
DependencyProperty.Register(nameof(Foreground), typeof(Brush), typeof(IconSource), new PropertyMetadata(null));
DependencyProperty.Register(nameof(Foreground), typeof(Brush), typeof(IconSource), new PropertyMetadata(null, OnPropertyChanged));

#nullable enable
public IconElement? CreateIconElement()
{
var element = CreateIconElementCore();
if (element != null)
{
m_createdIconElements.Add(new WeakReference<IconElement>(element));
}
return element;
}

public virtual IconElement? CreateIconElement() => default;
internal static void OnPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
var iconSource = sender as IconSource;
iconSource?.OnPropertyChanged(args);
}

protected virtual DependencyProperty? GetIconElementProperty(DependencyProperty sourceProperty) => default;
private void OnPropertyChanged(DependencyPropertyChangedEventArgs args)
{
if (GetIconElementPropertyCore(args.Property) is { } iconProp)
{
m_createdIconElements.RemoveAll(
weakElement =>
{
if (weakElement.TryGetTarget(out var target))
{
target.SetValue(iconProp, args.NewValue);
return false;
}
return true;
});
}
}

internal protected virtual IconElement? CreateIconElementCore() => default;

internal protected virtual DependencyProperty? GetIconElementPropertyCore(DependencyProperty sourceProperty)
{
if (sourceProperty == ForegroundProperty)
{
return IconElement.ForegroundProperty;
}

return null;
}
}
}
14 changes: 12 additions & 2 deletions src/Uno.UI/Microsoft/UI/Xaml/Controls/IconSource/PathIconSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public Geometry Data
}

public static DependencyProperty DataProperty { get; } =
DependencyProperty.Register(nameof(Data), typeof(Geometry), typeof(PathIconSource), new PropertyMetadata(null));
DependencyProperty.Register(nameof(Data), typeof(Geometry), typeof(PathIconSource), new PropertyMetadata(null, OnPropertyChanged));

public override IconElement CreateIconElement()
internal protected override IconElement CreateIconElementCore()
{
var pathIcon = new PathIcon();

Expand All @@ -35,5 +35,15 @@ public override IconElement CreateIconElement()

return pathIcon;
}

internal protected override DependencyProperty GetIconElementPropertyCore(DependencyProperty sourceProperty)
{
if (sourceProperty == DataProperty)
{
return PathIcon.DataProperty;
}

return base.GetIconElementPropertyCore(sourceProperty);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public Symbol Symbol
}

public static DependencyProperty SymbolProperty { get; } =
DependencyProperty.Register(nameof(Symbol), typeof(Symbol), typeof(SymbolIconSource), new PropertyMetadata(Symbol.Emoji));
DependencyProperty.Register(nameof(Symbol), typeof(Symbol), typeof(SymbolIconSource), new PropertyMetadata(Symbol.Emoji, OnPropertyChanged));

public override IconElement CreateIconElement()
internal protected override IconElement CreateIconElementCore()
{
var symbolIcon = new SymbolIcon()
{
Expand All @@ -32,5 +32,15 @@ public override IconElement CreateIconElement()

return symbolIcon;
}

internal protected override DependencyProperty GetIconElementPropertyCore(DependencyProperty sourceProperty)
{
if (sourceProperty == SymbolProperty)
{
return SymbolIcon.SymbolProperty;
}

return base.GetIconElementPropertyCore(sourceProperty);
}
}
}

0 comments on commit 27a35b5

Please sign in to comment.