Skip to content

Commit

Permalink
Merge branch 'hotfix/5.1.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
GeertvanHorrik committed Nov 19, 2024
2 parents 23dec94 + 90c8830 commit 6d0ccd3
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,15 @@ namespace Orc.Theming
public double? Scale { get; set; }
[System.Windows.Markup.ConstructorArgument("subscribeToEvents")]
public bool SubscribeToEvents { get; set; }
protected virtual double GetFontSizeFromParent() { }
protected virtual double GetFontSizeFromResource() { }
protected virtual double GetFontSizeFromService() { }
protected override void OnTargetObjectLoaded() { }
protected override void OnTargetObjectUnloaded() { }
protected override object? ProvideDynamicValue(System.IServiceProvider? serviceProvider) { }
public static double GetFontSize(double absolute) { }
protected static double GetFontSize(double defaultFontSize, double absoluteFontSize) { }
protected static double GetFontSizeFromTextBlockMetadata() { }
public static void ResetCache() { }
}
public enum FontSizeMode
Expand All @@ -174,6 +180,7 @@ namespace Orc.Theming
public FontSizeService() { }
public event System.EventHandler<System.EventArgs>? FontSizeChanged;
public virtual double GetFontSize() { }
protected virtual void OverrideMetadataWithFontSize(System.Windows.DependencyProperty dependencyProperty, System.Type targetType, double fontSize) { }
protected void RaiseFontSizeChanged() { }
public virtual bool SetFontSize(double fontSize) { }
}
Expand Down
32 changes: 25 additions & 7 deletions src/Orc.Theming/Markup/FontSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ public static void ResetCache()
LastUpdatedTextBlockFontSizeStopwatch.Reset();
}

/// <summary>
/// Gets the font size using the <see cref="FontSizeMode.TextBlockMetadata"/> mode.
/// </summary>
/// <param name="absolute">The absolute value based on the standard 12p font size.</param>
/// <returns></returns>
public static double GetFontSize(double absolute)
{
var defaultFontSize = GetFontSizeFromTextBlockMetadata();

return GetFontSize(defaultFontSize, absolute);
}

protected static double GetFontSize(double defaultFontSize, double absoluteFontSize)
{
// Use constant to define the ratio
var factor = absoluteFontSize / DefaultFontSize;
var finalFontSize = defaultFontSize * factor;
return finalFontSize;
}

protected override object? ProvideDynamicValue(IServiceProvider? serviceProvider)
{
var defaultFontSize = DefaultFontSize;
Expand Down Expand Up @@ -123,9 +143,7 @@ public static void ResetCache()
if (Absolute is not null)
{
// Return immediately, always use 12d as base font
var factor = Absolute.Value / DefaultFontSize;
finalFontSize = defaultFontSize * factor;
return finalFontSize;
return GetFontSize(Absolute.Value);
}

if (Delta is not null)
Expand All @@ -141,7 +159,7 @@ public static void ResetCache()
return finalFontSize;
}

private double GetFontSizeFromTextBlockMetadata()
protected static double GetFontSizeFromTextBlockMetadata()
{
var defaultValue = DefaultTextBlockFontSize;
if (defaultValue is null)
Expand All @@ -152,7 +170,7 @@ private double GetFontSizeFromTextBlockMetadata()
return defaultValue.Value;
}

private double GetFontSizeFromParent()
protected virtual double GetFontSizeFromParent()
{
var defaultFontSize = DefaultFontSize;

Expand All @@ -168,13 +186,13 @@ private double GetFontSizeFromParent()
return defaultFontSize;
}

private double GetFontSizeFromService()
protected virtual double GetFontSizeFromService()
{
var service = GetFontSizeService();
return service.GetFontSize();
}

private double GetFontSizeFromResource()
protected virtual double GetFontSizeFromResource()
{
// TODO: Based on discussion and performance, we consider
// resolving "fixed resources" based on the properties (e.g. Delta=4 resolves to Heading2)
Expand Down
47 changes: 45 additions & 2 deletions src/Orc.Theming/Services/FontSizeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using Catel.Logging;
using Catel.Reflection;

public class FontSizeService : IFontSizeService
{
Expand Down Expand Up @@ -47,8 +49,11 @@ public virtual bool SetFontSize(double fontSize)

try
{
TextElement.FontSizeProperty.OverrideMetadata(typeof(TextElement), new FrameworkPropertyMetadata(fontSize));
TextBlock.FontSizeProperty.OverrideMetadata(typeof(TextBlock), new FrameworkPropertyMetadata(fontSize));
OverrideMetadataWithFontSize(TextBlock.FontSizeProperty, typeof(TextBlock), fontSize);
OverrideMetadataWithFontSize(TextBox.FontSizeProperty, typeof(TextBox), fontSize);
OverrideMetadataWithFontSize(TextBoxBase.FontSizeProperty, typeof(TextBoxBase), fontSize);
OverrideMetadataWithFontSize(TextElement.FontSizeProperty, typeof(TextElement), fontSize);
OverrideMetadataWithFontSize(ToolTip.FontSizeProperty, typeof(ToolTip), fontSize);
}
catch (Exception ex)
{
Expand All @@ -64,4 +69,42 @@ protected void RaiseFontSizeChanged()
{
FontSizeChanged?.Invoke(this, EventArgs.Empty);
}

protected virtual void OverrideMetadataWithFontSize(DependencyProperty dependencyProperty, Type targetType, double fontSize)
{
try
{
var existingMetadata = dependencyProperty.GetMetadata(targetType);
if (existingMetadata is null)
{
dependencyProperty.OverrideMetadata(targetType, new FrameworkPropertyMetadata(fontSize));
return;
}

var dependencyObjectType = DependencyObjectType.FromSystemType(targetType);

var metadataMapField = dependencyProperty.GetType().GetFieldEx("_metadataMap")!;
var metadataMap = metadataMapField.GetValue(dependencyProperty)!;

var itemProperty = metadataMap.GetType().GetPropertyEx("Item")!;
itemProperty.SetValue(metadataMap, DependencyProperty.UnsetValue, new object?[] { dependencyObjectType.Id });

// Create new metadata
var newMetadata = new FrameworkPropertyMetadata(
fontSize,
FrameworkPropertyMetadataOptions.Inherits |
FrameworkPropertyMetadataOptions.Journal |
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
existingMetadata?.PropertyChangedCallback,
existingMetadata?.CoerceValueCallback
);

dependencyProperty.OverrideMetadata(targetType, newMetadata);
}
catch (Exception ex)
{
Log.Error(ex, $"Failed to override font size metadata for '{targetType.Name}'");
}
}
}

0 comments on commit 6d0ccd3

Please sign in to comment.