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 0000000..da305b1 --- /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 196e365..828297d 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 934c343..85cecda 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 0000000..9450d51 --- /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 0000000..67f04f4 --- /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 b848b0d..09f3bc0 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 0000000..c19aa35 --- /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 11c01b8..c098bd9 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 +}