From eb69c1d6f699d81596b423b3b5768d0b068ed966 Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Tue, 15 Oct 2024 23:57:37 +0200 Subject: [PATCH] Add FontSize markup extension --- .../ViewModels/Tools/FontSizeViewModel.cs | 8 ++ .../Views/ControlPaddingsView.xaml | 2 + .../Views/MainWindow.xaml.cs | 8 +- .../Views/Tools/FontSizeView.xaml | 98 +++++++++++++++ .../Views/Tools/FontSizeView.xaml.cs | 9 ++ ...ng_HasNoBreakingChanges_Async.verified.txt | 13 ++ src/Orc.Theming/Markup/FontSize.cs | 119 ++++++++++++++++++ .../ViewModels/FontSizeSwitcherViewModel.cs | 2 +- 8 files changed, 254 insertions(+), 5 deletions(-) create mode 100644 src/Orc.Theming.Example/ViewModels/Tools/FontSizeViewModel.cs create mode 100644 src/Orc.Theming.Example/Views/Tools/FontSizeView.xaml create mode 100644 src/Orc.Theming.Example/Views/Tools/FontSizeView.xaml.cs create mode 100644 src/Orc.Theming/Markup/FontSize.cs diff --git a/src/Orc.Theming.Example/ViewModels/Tools/FontSizeViewModel.cs b/src/Orc.Theming.Example/ViewModels/Tools/FontSizeViewModel.cs new file mode 100644 index 00000000..da305b10 --- /dev/null +++ b/src/Orc.Theming.Example/ViewModels/Tools/FontSizeViewModel.cs @@ -0,0 +1,8 @@ +namespace Orc.Theming.Example.ViewModels +{ + using Catel.MVVM; + + public class FontSizeViewModel : ViewModelBase + { + } +} diff --git a/src/Orc.Theming.Example/Views/ControlPaddingsView.xaml b/src/Orc.Theming.Example/Views/ControlPaddingsView.xaml index 196e3652..828297d6 100644 --- a/src/Orc.Theming.Example/Views/ControlPaddingsView.xaml +++ b/src/Orc.Theming.Example/Views/ControlPaddingsView.xaml @@ -8,6 +8,7 @@ xmlns:system="clr-namespace:System;assembly=System.Runtime" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> + @@ -15,4 +16,5 @@ + diff --git a/src/Orc.Theming.Example/Views/MainWindow.xaml.cs b/src/Orc.Theming.Example/Views/MainWindow.xaml.cs index 934c343b..85cecda6 100644 --- a/src/Orc.Theming.Example/Views/MainWindow.xaml.cs +++ b/src/Orc.Theming.Example/Views/MainWindow.xaml.cs @@ -25,10 +25,10 @@ private void LoadTabItems() }); var viewsToAdd = (from viewType in GetType().Assembly.GetTypesEx() - where typeof(UserControl).IsAssignableFromEx(viewType) && - viewType != typeof(ControlsView) - orderby viewType.Name - select viewType); + where typeof(UserControl).IsAssignableFromEx(viewType) && + viewType != typeof(ControlsView) + orderby viewType.Name + select viewType); foreach (var viewToAdd in viewsToAdd) { diff --git a/src/Orc.Theming.Example/Views/Tools/FontSizeView.xaml b/src/Orc.Theming.Example/Views/Tools/FontSizeView.xaml new file mode 100644 index 00000000..9450d513 --- /dev/null +++ b/src/Orc.Theming.Example/Views/Tools/FontSizeView.xaml @@ -0,0 +1,98 @@ + + + + + + + + + + + + + + + + diff --git a/src/Orc.Theming.Example/Views/Tools/FontSizeView.xaml.cs b/src/Orc.Theming.Example/Views/Tools/FontSizeView.xaml.cs new file mode 100644 index 00000000..67f04f49 --- /dev/null +++ b/src/Orc.Theming.Example/Views/Tools/FontSizeView.xaml.cs @@ -0,0 +1,9 @@ +namespace Orc.Theming.Example.Views; + +public partial class FontSizeView +{ + public FontSizeView() + { + InitializeComponent(); + } +} diff --git a/src/Orc.Theming.Tests/PublicApiFacts.Orc_Theming_HasNoBreakingChanges_Async.verified.txt b/src/Orc.Theming.Tests/PublicApiFacts.Orc_Theming_HasNoBreakingChanges_Async.verified.txt index b848b0dd..09f3bc0c 100644 --- a/src/Orc.Theming.Tests/PublicApiFacts.Orc_Theming_HasNoBreakingChanges_Async.verified.txt +++ b/src/Orc.Theming.Tests/PublicApiFacts.Orc_Theming_HasNoBreakingChanges_Async.verified.txt @@ -142,6 +142,19 @@ namespace Orc.Theming public double Width { get; set; } protected override object ProvideDynamicValue(System.IServiceProvider? serviceProvider) { } } + public class FontSize : Catel.Windows.Markup.UpdatableMarkupExtension + { + public FontSize() { } + [System.Windows.Markup.ConstructorArgument("delta")] + public double? Delta { get; set; } + [System.Windows.Markup.ConstructorArgument("scale")] + public double? Scale { get; set; } + [System.Windows.Markup.ConstructorArgument("subscribeToEvents")] + public bool SubscribeToEvents { get; set; } + protected override void OnTargetObjectLoaded() { } + protected override void OnTargetObjectUnloaded() { } + protected override object? ProvideDynamicValue(System.IServiceProvider? serviceProvider) { } + } public class FontSizeService : Orc.Theming.IFontSizeService { public FontSizeService() { } diff --git a/src/Orc.Theming/Markup/FontSize.cs b/src/Orc.Theming/Markup/FontSize.cs new file mode 100644 index 00000000..c19aa353 --- /dev/null +++ b/src/Orc.Theming/Markup/FontSize.cs @@ -0,0 +1,119 @@ +namespace Orc.Theming; + +using System; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Markup; +using Catel.IoC; +using Catel.Logging; +using Catel.Windows; +using Catel.Windows.Markup; + +/// +/// Markup extension that can set a relative font size. +/// +public class FontSize : UpdatableMarkupExtension +{ + private static readonly ILog Log = LogManager.GetCurrentClassLogger(); + + private IFontSizeService? _fontSizeService; + + static FontSize() + { + } + + /// + /// Initializes a new instance of the class. + /// + public FontSize() + { + Scale = 1.0d; + SubscribeToEvents = false; + } + + /// + /// Gets or sets the font size scaling. + /// + /// The font size scaling. + [ConstructorArgument("scale")] + public double? Scale { get; set; } + + /// + /// Gets or sets the font size delta. + /// + /// The font size delta. + [ConstructorArgument("delta")] + public double? Delta { get; set; } + + /// + /// Gets or sets whether this markup extension should subscribe to events to be responsive. + /// + /// The default value is false for performance reasons. + /// + /// The font size delta. + [ConstructorArgument("subscribeToEvents")] + public bool SubscribeToEvents { get; set; } + + protected override object? ProvideDynamicValue(IServiceProvider? serviceProvider) + { + var defaultFontSize = 12d; + + if (TargetObject is DependencyObject dependencyObject) + { + var parentControl = dependencyObject.GetLogicalParent()?.FindLogicalAncestorByType(); + if (parentControl is not null) + { + defaultFontSize = (double)parentControl.GetValue(Control.FontSizeProperty); + } + } + + var finalFontSize = defaultFontSize; + + if (Delta is not null) + { + finalFontSize += Delta.Value; + } + + if (Scale is not null) + { + finalFontSize *= Scale.Value; + } + + return finalFontSize; + } + + protected override void OnTargetObjectLoaded() + { + base.OnTargetObjectLoaded(); + + if (SubscribeToEvents) + { + var fontSizeService = _fontSizeService; + if (fontSizeService is null) + { + _fontSizeService = fontSizeService = ServiceLocator.Default.ResolveType(); + } + + if (fontSizeService is not null) + { + fontSizeService.FontSizeChanged += OnFontSizeServiceFontSizeChanged; + } + } + } + + protected override void OnTargetObjectUnloaded() + { + var fontSizeService = _fontSizeService; + if (fontSizeService is not null) + { + fontSizeService.FontSizeChanged -= OnFontSizeServiceFontSizeChanged; + } + + base.OnTargetObjectUnloaded(); + } + + private void OnFontSizeServiceFontSizeChanged(object? sender, EventArgs e) + { + UpdateValue(); + } +} diff --git a/src/Orc.Theming/ViewModels/FontSizeSwitcherViewModel.cs b/src/Orc.Theming/ViewModels/FontSizeSwitcherViewModel.cs index 11c01b86..c098bd9c 100644 --- a/src/Orc.Theming/ViewModels/FontSizeSwitcherViewModel.cs +++ b/src/Orc.Theming/ViewModels/FontSizeSwitcherViewModel.cs @@ -65,4 +65,4 @@ private void OnSelectedFontSizeChanged() { _fontSizeService.SetFontSize(SelectedFontSize); } -} \ No newline at end of file +}