Skip to content

Commit

Permalink
Add FontSize markup extension
Browse files Browse the repository at this point in the history
  • Loading branch information
GeertvanHorrik committed Oct 15, 2024
1 parent ce66cfa commit eb69c1d
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/Orc.Theming.Example/ViewModels/Tools/FontSizeViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Orc.Theming.Example.ViewModels
{
using Catel.MVVM;

public class FontSizeViewModel : ViewModelBase
{
}
}
2 changes: 2 additions & 0 deletions src/Orc.Theming.Example/Views/ControlPaddingsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
xmlns:system="clr-namespace:System;assembly=System.Runtime"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">

<StackPanel Orientation="Vertical">
<orccontrols:WatermarkTextBox Watermark="WatermarkTextBox"/>
<TextBox Text="TextBox"/>
<ComboBox Text="Combobox" IsEditable="True"/>
<orccontrols:FilterBox Text="FilterBox"/>
<orccontrols:DateTimePicker Value="{x:Static system:DateTime.Now}"/>
</StackPanel>

</catel:UserControl>
8 changes: 4 additions & 4 deletions src/Orc.Theming.Example/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
98 changes: 98 additions & 0 deletions src/Orc.Theming.Example/Views/Tools/FontSizeView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<catel:UserControl x:Class="Orc.Theming.Example.Views.FontSizeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:catel="http://schemas.catelproject.com"
xmlns:ribbon="clr-namespace:Orc.Theming.Example"
xmlns:orctheming="http://schemas.wildgums.com/orc/theming"
xmlns:orccontrols="http://schemas.wildgums.com/orc/controls">

<orccontrols:StackGrid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<Label Content="Default font size"
Grid.ColumnSpan="2"
HorizontalAlignment="Center" />

<GroupBox Header="No subscriptions">
<StackPanel Orientation="Horizontal">
<GroupBox Header="Scaling">
<StackPanel Orientation="Vertical"
HorizontalAlignment="Left">
<Label Content="This is 0.6"
FontSize="{orctheming:FontSize Scale=0.6}" />
<Label Content="This is 0.8"
FontSize="{orctheming:FontSize Scale=0.8}" />
<Label Content="This is 1.0"
FontSize="{orctheming:FontSize Scale=1.0}" />
<Label Content="This is 1.2"
FontSize="{orctheming:FontSize Scale=1.2}" />
<Label Content="This is 1.4"
FontSize="{orctheming:FontSize Scale=1.4}" />
</StackPanel>
</GroupBox>

<GroupBox Header="Delta">
<StackPanel Orientation="Vertical"
HorizontalAlignment="Left">
<Label Content="This is -4"
FontSize="{orctheming:FontSize Delta=-4}" />
<Label Content="This is -2"
FontSize="{orctheming:FontSize Delta=-2}" />
<Label Content="This is 0"
FontSize="{orctheming:FontSize Delta=0}" />
<Label Content="This is +2"
FontSize="{orctheming:FontSize Delta=2}" />
<Label Content="This is +4"
FontSize="{orctheming:FontSize Delta=4}" />
</StackPanel>
</GroupBox>
</StackPanel>
</GroupBox>

<GroupBox Header="Subscriptions">
<StackPanel Orientation="Horizontal">
<GroupBox Header="Scaling">
<StackPanel Orientation="Vertical"
HorizontalAlignment="Left">
<Label Content="This is 0.6"
FontSize="{orctheming:FontSize Scale=0.6, SubscribeToEvents=True}" />
<Label Content="This is 0.8"
FontSize="{orctheming:FontSize Scale=0.8, SubscribeToEvents=True}" />
<Label Content="This is 1.0"
FontSize="{orctheming:FontSize Scale=1.0, SubscribeToEvents=True}" />
<Label Content="This is 1.2"
FontSize="{orctheming:FontSize Scale=1.2, SubscribeToEvents=True}" />
<Label Content="This is 1.4"
FontSize="{orctheming:FontSize Scale=1.4, SubscribeToEvents=True}" />
</StackPanel>
</GroupBox>

<GroupBox Header="Delta">
<StackPanel Orientation="Vertical"
HorizontalAlignment="Left">
<Label Content="This is -4"
FontSize="{orctheming:FontSize Delta=-4, SubscribeToEvents=True}" />
<Label Content="This is -2"
FontSize="{orctheming:FontSize Delta=-2, SubscribeToEvents=True}" />
<Label Content="This is 0"
FontSize="{orctheming:FontSize Delta=0, SubscribeToEvents=True}" />
<Label Content="This is +2"
FontSize="{orctheming:FontSize Delta=2, SubscribeToEvents=True}" />
<Label Content="This is +4"
FontSize="{orctheming:FontSize Delta=4, SubscribeToEvents=True}" />
</StackPanel>
</GroupBox>
</StackPanel>
</GroupBox>

</orccontrols:StackGrid>

</catel:UserControl>
9 changes: 9 additions & 0 deletions src/Orc.Theming.Example/Views/Tools/FontSizeView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Orc.Theming.Example.Views;

public partial class FontSizeView
{
public FontSizeView()
{
InitializeComponent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() { }
Expand Down
119 changes: 119 additions & 0 deletions src/Orc.Theming/Markup/FontSize.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Markup extension that can set a relative font size.
/// </summary>
public class FontSize : UpdatableMarkupExtension
{
private static readonly ILog Log = LogManager.GetCurrentClassLogger();

private IFontSizeService? _fontSizeService;

static FontSize()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="FontSize" /> class.
/// </summary>
public FontSize()
{
Scale = 1.0d;
SubscribeToEvents = false;
}

/// <summary>
/// Gets or sets the font size scaling.
/// </summary>
/// <value>The font size scaling.</value>
[ConstructorArgument("scale")]
public double? Scale { get; set; }

/// <summary>
/// Gets or sets the font size delta.
/// </summary>
/// <value>The font size delta.</value>
[ConstructorArgument("delta")]
public double? Delta { get; set; }

/// <summary>
/// Gets or sets whether this markup extension should subscribe to events to be responsive.
/// <para />
/// The default value is <c>false</c> for performance reasons.
/// </summary>
/// <value>The font size delta.</value>
[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<Control>();
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<IFontSizeService>();
}

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();
}
}
2 changes: 1 addition & 1 deletion src/Orc.Theming/ViewModels/FontSizeSwitcherViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ private void OnSelectedFontSizeChanged()
{
_fontSizeService.SetFontSize(SelectedFontSize);
}
}
}

0 comments on commit eb69c1d

Please sign in to comment.