Skip to content

Commit

Permalink
Use bindings for FlowDirection of markup extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
davidxuang committed Jul 3, 2024
1 parent a97e94e commit 1b46787
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 28 deletions.
8 changes: 4 additions & 4 deletions FluentIcons.Avalonia.Fluent/MarkupExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public SymbolIcon ProvideValue(IServiceProvider serviceProvider)
if (Foreground is not null) icon.Foreground = Foreground;

var service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
if (service?.TargetObject is Visual elem)
if (service?.TargetObject is Visual source)
{
icon.FlowDirection = elem.FlowDirection;
icon.Bind(Visual.FlowDirectionProperty, source.GetBindingObservable(Visual.FlowDirectionProperty));
}

return icon;
Expand All @@ -53,9 +53,9 @@ public SymbolIconSource ProvideValue(IServiceProvider serviceProvider)
if (Foreground is not null) icon.Foreground = Foreground;

var service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
if (service?.TargetObject is Visual elem)
if (service?.TargetObject is Visual source)
{
icon.FlowDirection = elem.FlowDirection;
icon.Bind(Visual.FlowDirectionProperty, source.GetBindingObservable(Visual.FlowDirectionProperty));
}

return icon;
Expand Down
4 changes: 2 additions & 2 deletions FluentIcons.Avalonia/MarkupExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public SymbolIcon ProvideValue(IServiceProvider serviceProvider)
if (Foreground is not null) icon.Foreground = Foreground;

var service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
if (service?.TargetObject is Visual elem)
if (service?.TargetObject is Visual source)
{
icon.FlowDirection = elem.FlowDirection;
icon.Bind(Visual.FlowDirectionProperty, source.GetBindingObservable(Visual.FlowDirectionProperty));
}

return icon;
Expand Down
4 changes: 2 additions & 2 deletions FluentIcons.Maui/SymbolIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ public SymbolIcon ProvideValue(IServiceProvider serviceProvider)
if (ForegroundColor is not null) icon.ForegroundColor = ForegroundColor;

var service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
if (service?.TargetObject is VisualElement elem)
if (service?.TargetObject is VisualElement source)
{
icon.FlowDirection = elem.FlowDirection;
icon.SetBinding(VisualElement.FlowDirectionProperty, new Binding(nameof(FlowDirection), source: source));
}

return icon;
Expand Down
4 changes: 2 additions & 2 deletions FluentIcons.Maui/SymbolImageSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ public SymbolImageSource ProvideValue(IServiceProvider serviceProvider)
if (Color is not null) icon.Color = Color;

var service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
if (service?.TargetObject is VisualElement elem)
if (service?.TargetObject is VisualElement source)
{
icon.FlowDirection = elem.FlowDirection;
icon.FlowDirection = source.FlowDirection;
}

return icon;
Expand Down
41 changes: 32 additions & 9 deletions FluentIcons.Uwp/SymbolIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml.Media;
using Symbol = FluentIcons.Common.Symbol;
Expand Down Expand Up @@ -49,6 +50,27 @@ public SymbolIcon()
RegisterPropertyChangedCallback(MirroredWhenRightToLeftProperty, OnMirroredWhenRightToLeftChanged);
}

#if UAP10_0_17763_0
internal SymbolIcon(bool bindFlowDirection) : this()
{
if (!bindFlowDirection)
return;

static void handler(object sender, RoutedEventArgs args)
{
if (sender is SymbolIcon icon)
{
icon.Loaded -= handler;
icon.SetBinding(
FlowDirectionProperty,
new Binding { Source = icon.Parent, Path = new PropertyPath(nameof(FlowDirection)) });
}
};

Loaded += handler;
}
#endif

public Symbol Symbol
{
get { return (Symbol)GetValue(SymbolProperty); }
Expand Down Expand Up @@ -145,9 +167,6 @@ public class SymbolIconExtension : MarkupExtension
public Symbol? Symbol { get; set; }
public IconVariant? IconVariant { get; set; }
public bool? UseSegoeMetrics { get; set; }
#if UAP10_0_17763_0
public FlowDirection? FlowDirection { get; set; }
#endif
public double? FontSize { get; set; }
public Brush? Foreground { get; set; }

Expand All @@ -157,21 +176,25 @@ protected override object ProvideValue()
protected override object ProvideValue(IXamlServiceProvider serviceProvider)
#endif
{
var icon = new SymbolIcon();
var icon = new SymbolIcon(
#if UAP10_0_17763_0
true
#endif
);

if (Symbol.HasValue) icon.Symbol = Symbol.Value;
if (IconVariant.HasValue) icon.IconVariant = IconVariant.Value;
if (UseSegoeMetrics.HasValue) icon.UseSegoeMetrics = UseSegoeMetrics.Value;
if (FontSize.HasValue) icon.FontSize = FontSize.Value;
if (Foreground is not null) icon.Foreground = Foreground;

#if UAP10_0_17763_0
if (FlowDirection.HasValue) icon.FlowDirection = FlowDirection.Value;
#else
#if !UAP10_0_17763_0
var service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
if (service?.TargetObject is FrameworkElement elem)
if (service?.TargetObject is FrameworkElement source)
{
icon.FlowDirection = elem.FlowDirection;
icon.SetBinding(
FrameworkElement.FlowDirectionProperty,
new Binding { Source = source, Path = new PropertyPath(nameof(source.FlowDirection)) });
}
#endif

Expand Down
8 changes: 6 additions & 2 deletions FluentIcons.Uwp/SymbolIconSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,13 @@ protected override object ProvideValue(IXamlServiceProvider serviceProvider)
if (FlowDirection.HasValue) icon.FlowDirection = FlowDirection.Value;
#else
var service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
if (service?.TargetObject is FrameworkElement elem)
if (service?.TargetObject is FrameworkElement source)
{
icon.FlowDirection = elem.FlowDirection;
icon.FlowDirection = source.FlowDirection;
source.RegisterPropertyChangedCallback(FrameworkElement.FlowDirectionProperty, (obj, args) =>
{
if (obj is FrameworkElement f) icon.FlowDirection = f.FlowDirection;
});
}
#endif

Expand Down
7 changes: 5 additions & 2 deletions FluentIcons.WPF/SymbolIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Media;
using FluentIcons.Common;
Expand Down Expand Up @@ -225,9 +226,11 @@ public override object ProvideValue(IServiceProvider serviceProvider)
if (Foreground is not null) icon.Foreground = Foreground;

var service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
if (service?.TargetObject is FrameworkElement elem)
if (service?.TargetObject is FrameworkElement source)
{
icon.FlowDirection = elem.FlowDirection;
icon.SetBinding(
FrameworkElement.FlowDirectionProperty,
new Binding { Source = source, Path = new PropertyPath(nameof(source.FlowDirection)) });
}

return icon;
Expand Down
7 changes: 5 additions & 2 deletions FluentIcons.WinUI/SymbolIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.UI.Text;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Markup;
using Microsoft.UI.Xaml.Media;
using Symbol = FluentIcons.Common.Symbol;
Expand Down Expand Up @@ -159,9 +160,11 @@ protected override object ProvideValue(IXamlServiceProvider serviceProvider)
if (Foreground is not null) icon.Foreground = Foreground;

var service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
if (service?.TargetObject is FrameworkElement elem)
if (service?.TargetObject is FrameworkElement source)
{
icon.FlowDirection = elem.FlowDirection;
icon.SetBinding(
FrameworkElement.FlowDirectionProperty,
new Binding { Source = source, Path = new PropertyPath(nameof(source.FlowDirection)) });
}

return icon;
Expand Down
8 changes: 6 additions & 2 deletions FluentIcons.WinUI/SymbolIconSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,13 @@ protected override object ProvideValue(IXamlServiceProvider serviceProvider)
if (Foreground is not null) icon.Foreground = Foreground;

var service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
if (service?.TargetObject is FrameworkElement elem)
if (service?.TargetObject is FrameworkElement source)
{
icon.FlowDirection = elem.FlowDirection;
icon.FlowDirection = source.FlowDirection;
source.RegisterPropertyChangedCallback(FrameworkElement.FlowDirectionProperty, (obj, args) =>
{
if (obj is FrameworkElement elem) icon.FlowDirection = elem.FlowDirection;
});
}

return icon;
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ This package features `<SymbolIcon>` element, and `<SymbolIconSource>` on platfo
</Window>
```

`SymbolIconExtension` and `SymbolIconSourceExtension` have been added since 1.1.242. These extensions will auto-detect `FlowDirection` from parent control, except on (non-Uno) UWP where `IXamlServiceProvider` is not available.
`SymbolIconExtension` and `SymbolIconSourceExtension` have been added since 1.1.242. These extensions will bind their `FlowDirection` to that of the parent control, except `SymbolIconSourceExtension` on (non-Uno) UWP where `IXamlServiceProvider` is not available.

### Avalonia

Expand Down

0 comments on commit 1b46787

Please sign in to comment.