Skip to content

Commit

Permalink
Fixed Font Scaling issue when FontSize is not set explicitly (#9719)
Browse files Browse the repository at this point in the history
* Fixed Font Scaling issue for Fluent styles when Font Size is not explicity set

* Updated TextElement.FontSize metadata overrides to use ThemeMessageFontSize

* Added a condition that is used to calculate the default font size before ThemMode and Resources are synced for Application

* Fixed spelling

* Enable scaling in all the Fluent styles

* Made ThemeMessageFontSize scaling factor const
  • Loading branch information
dipeshmsft authored Sep 27, 2024
1 parent a76c2dd commit 2198097
Show file tree
Hide file tree
Showing 24 changed files with 25 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public FontFamily FontFamily
public static readonly DependencyProperty FontSizeProperty =
TextElement.FontSizeProperty.AddOwner(
typeof(Control),
new FrameworkPropertyMetadata(SystemFonts.MessageFontSize,
new FrameworkPropertyMetadata(SystemFonts.ThemeMessageFontSize,
FrameworkPropertyMetadataOptions.Inherits));

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public FontFamily FontFamily
public static readonly DependencyProperty FontSizeProperty =
TextElement.FontSizeProperty.AddOwner(
typeof(DataGridTextColumn),
new FrameworkPropertyMetadata(SystemFonts.MessageFontSize, FrameworkPropertyMetadataOptions.Inherits, DataGridColumn.NotifyPropertyChangeForRefreshContent));
new FrameworkPropertyMetadata(SystemFonts.ThemeMessageFontSize, FrameworkPropertyMetadataOptions.Inherits, DataGridColumn.NotifyPropertyChangeForRefreshContent));

/// <summary>
/// The size of the desired font.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ static MenuItem()

ForegroundProperty.OverrideMetadata(typeof(MenuItem), new FrameworkPropertyMetadata(SystemColors.MenuTextBrush));
FontFamilyProperty.OverrideMetadata(typeof(MenuItem), new FrameworkPropertyMetadata(SystemFonts.MessageFontFamily));
FontSizeProperty.OverrideMetadata(typeof(MenuItem), new FrameworkPropertyMetadata(SystemFonts.MessageFontSize));
FontSizeProperty.OverrideMetadata(typeof(MenuItem), new FrameworkPropertyMetadata(SystemFonts.ThemeMessageFontSize));
FontStyleProperty.OverrideMetadata(typeof(MenuItem), new FrameworkPropertyMetadata(SystemFonts.MessageFontStyle));
FontWeightProperty.OverrideMetadata(typeof(MenuItem), new FrameworkPropertyMetadata(SystemFonts.MessageFontWeight));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ public static FontStretch GetFontStretch(DependencyObject element)
typeof(double),
typeof(TextElement),
new FrameworkPropertyMetadata(
SystemFonts.MessageFontSize,
SystemFonts.ThemeMessageFontSize,
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits),
new ValidateValueCallback(IsValidFontSize));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2411,7 +2411,7 @@ static FrameworkElement()

// Coerce Callback for font properties for responding to system themes
TextElement.FontFamilyProperty.OverrideMetadata(_typeofThis, new FrameworkPropertyMetadata(SystemFonts.MessageFontFamily, FrameworkPropertyMetadataOptions.Inherits, null, new CoerceValueCallback(CoerceFontFamily)));
TextElement.FontSizeProperty.OverrideMetadata(_typeofThis, new FrameworkPropertyMetadata(SystemFonts.MessageFontSize, FrameworkPropertyMetadataOptions.Inherits, null, new CoerceValueCallback(CoerceFontSize)));
TextElement.FontSizeProperty.OverrideMetadata(_typeofThis, new FrameworkPropertyMetadata(SystemFonts.ThemeMessageFontSize, FrameworkPropertyMetadataOptions.Inherits, null, new CoerceValueCallback(CoerceFontSize)));
TextElement.FontStyleProperty.OverrideMetadata(_typeofThis, new FrameworkPropertyMetadata(SystemFonts.MessageFontStyle, FrameworkPropertyMetadataOptions.Inherits, null, new CoerceValueCallback(CoerceFontStyle)));
TextElement.FontWeightProperty.OverrideMetadata(_typeofThis, new FrameworkPropertyMetadata(SystemFonts.MessageFontWeight, FrameworkPropertyMetadataOptions.Inherits, null, new CoerceValueCallback(CoerceFontWeight)));

Expand Down Expand Up @@ -5564,7 +5564,7 @@ private static object CoerceFontSize(DependencyObject o, object value)
// For root elements with default values, return current system metric if local value has not been set
if (ShouldUseSystemFont((FrameworkElement)o, TextElement.FontSizeProperty))
{
return SystemFonts.MessageFontSize;
return SystemFonts.ThemeMessageFontSize;
}

return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,15 +422,25 @@ public static TextDecorationCollection StatusFontTextDecorations
/// Maps to SPI_NONCLIENTMETRICS
/// </summary>
public static double MessageFontSize
{
get
{
return ConvertFontHeight(SystemParameters.NonClientMetrics.lfMessageFont.lfHeight);
}
}

internal static double ThemeMessageFontSize
{
get
{
// TODO : Find a better solution to this. Difference in default size of font in Fluent and other themes.
if(ThemeManager.IsFluentThemeEnabled)
if(ThemeManager.IsFluentThemeEnabled
|| ThemeManager.IsFluentThemeDictionaryIncluded())
{
return ThemeManager.DefaultFluentThemeFontSize;
return MessageFontSize * ThemeManager.DefaultFluentFontSizeFactor ;
}
return ConvertFontHeight(SystemParameters.NonClientMetrics.lfMessageFont.lfHeight);

return MessageFontSize;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ internal static ResourceDictionary GetThemeDictionary(ThemeMode themeMode)
return rd;
}

internal static bool IsFluentThemeDictionaryIncluded()
{
return Application.Current != null && LastIndexOfFluentThemeDictionary(Application.Current.Resources) != -1;
}

#endregion


Expand Down Expand Up @@ -323,7 +328,7 @@ internal static bool IsFluentThemeEnabled

internal static bool IgnoreWindowResourcesChange { get; set; } = false;

internal static double DefaultFluentThemeFontSize => 14;
internal const double DefaultFluentFontSizeFactor = 14.0 / 12.0 ;

internal static WindowCollection FluentEnabledWindows { get; set; } = new WindowCollection();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="Border.CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
<Setter Property="SnapsToDevicePixels" Value="True" />
Expand Down Expand Up @@ -86,7 +85,6 @@
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="Border.CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
<Setter Property="SnapsToDevicePixels" Value="True" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Top" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="KeyboardNavigation.IsTabStop" Value="True" />
<Setter Property="Focusable" Value="True" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@
<Setter Property="Background" Value="{DynamicResource ComboBoxBackground}" />
<Setter Property="BorderBrush" Value="{DynamicResource ControlElevationBorderBrush}" />
<Setter Property="BorderThickness" Value="{StaticResource ComboBoxBorderThemeThickness}" />
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
<Setter Property="ScrollViewer.CanContentScroll" Value="False" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,6 @@
<Setter Property="HorizontalGridLinesBrush" Value="{DynamicResource ControlElevationBorderBrush}" />
<Setter Property="VerticalGridLinesBrush" Value="{DynamicResource ControlElevationBorderBrush}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="FontSize" Value="{StaticResource DefaultDataGridFontSize}" />
<Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected" />
<Setter Property="ScrollViewer.CanContentScroll" Value="True" />
<Setter Property="ScrollViewer.PanningMode" Value="Both" />
Expand Down Expand Up @@ -666,7 +665,6 @@
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="KeyboardNavigation.IsTabStop" Value="True" />
<Setter Property="Focusable" Value="True" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
<Setter Property="Background" Value="{DynamicResource DatePickerBackground}" />
<Setter Property="BorderBrush" Value="{DynamicResource ControlElevationBorderBrush}" />
<Setter Property="BorderThickness" Value="{StaticResource DatePickerBorderThemeThickness}" />
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="Border.CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
<Setter Property="IsExpanded" Value="False" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.CanContentScroll" Value="True" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
<Setter Property="Background" Value="{DynamicResource TextControlBackground}" />
<Setter Property="BorderBrush" Value="{DynamicResource TextControlElevationBorderBrush}" />
<Setter Property="BorderThickness" Value="{StaticResource PasswordBoxBorderThemeThickness}" />
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Top" />
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="MinWidth" Value="120" />
<Setter Property="KeyboardNavigation.IsTabStop" Value="True" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="Border.CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
<Setter Property="SnapsToDevicePixels" Value="True" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
<Setter Property="MinHeight" Value="34" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Padding" Value="6,4" />
<Setter Property="FontSize" Value="14" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Template">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
<Setter Property="Background" Value="{DynamicResource TextControlBackground}" />
<Setter Property="BorderBrush" Value="{DynamicResource TextControlElevationBorderBrush}" />
<Setter Property="BorderThickness" Value="{StaticResource TextBoxBorderThemeThickness}" />
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
<Setter Property="ScrollViewer.CanContentScroll" Value="False" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
Expand Down Expand Up @@ -246,7 +245,6 @@
<Setter Property="Background" Value="{DynamicResource TextControlBackground}" />
<Setter Property="BorderBrush" Value="{DynamicResource TextControlElevationBorderBrush}" />
<Setter Property="BorderThickness" Value="{StaticResource TextBoxBorderThemeThickness}" />
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
<Setter Property="ScrollViewer.CanContentScroll" Value="False" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="Border.CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
<Setter Property="SnapsToDevicePixels" Value="True" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
<Setter Property="Background" Value="{DynamicResource TreeViewItemBackground}" />
<Setter Property="Margin" Value="0,0,0,2" />
<Setter Property="Padding" Value="4" />
<Setter Property="FontSize" Value="{StaticResource TreeViewItemFontSize}" />
<Setter Property="Border.CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
<Setter Property="IsTabStop" Value="True" />
<Setter Property="OverridesDefaultStyle" Value="True" />
Expand Down
Loading

0 comments on commit 2198097

Please # to comment.