Skip to content

Commit 2198097

Browse files
authored
Fixed Font Scaling issue when FontSize is not set explicitly (#9719)
* 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
1 parent a76c2dd commit 2198097

File tree

24 files changed

+25
-78
lines changed

24 files changed

+25
-78
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Control.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public FontFamily FontFamily
192192
public static readonly DependencyProperty FontSizeProperty =
193193
TextElement.FontSizeProperty.AddOwner(
194194
typeof(Control),
195-
new FrameworkPropertyMetadata(SystemFonts.MessageFontSize,
195+
new FrameworkPropertyMetadata(SystemFonts.ThemeMessageFontSize,
196196
FrameworkPropertyMetadataOptions.Inherits));
197197

198198
/// <summary>

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridTextColumn.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ public FontFamily FontFamily
320320
public static readonly DependencyProperty FontSizeProperty =
321321
TextElement.FontSizeProperty.AddOwner(
322322
typeof(DataGridTextColumn),
323-
new FrameworkPropertyMetadata(SystemFonts.MessageFontSize, FrameworkPropertyMetadataOptions.Inherits, DataGridColumn.NotifyPropertyChangeForRefreshContent));
323+
new FrameworkPropertyMetadata(SystemFonts.ThemeMessageFontSize, FrameworkPropertyMetadataOptions.Inherits, DataGridColumn.NotifyPropertyChangeForRefreshContent));
324324

325325
/// <summary>
326326
/// The size of the desired font.

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/MenuItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ static MenuItem()
174174

175175
ForegroundProperty.OverrideMetadata(typeof(MenuItem), new FrameworkPropertyMetadata(SystemColors.MenuTextBrush));
176176
FontFamilyProperty.OverrideMetadata(typeof(MenuItem), new FrameworkPropertyMetadata(SystemFonts.MessageFontFamily));
177-
FontSizeProperty.OverrideMetadata(typeof(MenuItem), new FrameworkPropertyMetadata(SystemFonts.MessageFontSize));
177+
FontSizeProperty.OverrideMetadata(typeof(MenuItem), new FrameworkPropertyMetadata(SystemFonts.ThemeMessageFontSize));
178178
FontStyleProperty.OverrideMetadata(typeof(MenuItem), new FrameworkPropertyMetadata(SystemFonts.MessageFontStyle));
179179
FontWeightProperty.OverrideMetadata(typeof(MenuItem), new FrameworkPropertyMetadata(SystemFonts.MessageFontWeight));
180180

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ public static FontStretch GetFontStretch(DependencyObject element)
647647
typeof(double),
648648
typeof(TextElement),
649649
new FrameworkPropertyMetadata(
650-
SystemFonts.MessageFontSize,
650+
SystemFonts.ThemeMessageFontSize,
651651
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits),
652652
new ValidateValueCallback(IsValidFontSize));
653653

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,7 +2411,7 @@ static FrameworkElement()
24112411

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

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

55705570
return value;

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/SystemFonts.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,15 +422,25 @@ public static TextDecorationCollection StatusFontTextDecorations
422422
/// Maps to SPI_NONCLIENTMETRICS
423423
/// </summary>
424424
public static double MessageFontSize
425+
{
426+
get
427+
{
428+
return ConvertFontHeight(SystemParameters.NonClientMetrics.lfMessageFont.lfHeight);
429+
}
430+
}
431+
432+
internal static double ThemeMessageFontSize
425433
{
426434
get
427435
{
428436
// TODO : Find a better solution to this. Difference in default size of font in Fluent and other themes.
429-
if(ThemeManager.IsFluentThemeEnabled)
437+
if(ThemeManager.IsFluentThemeEnabled
438+
|| ThemeManager.IsFluentThemeDictionaryIncluded())
430439
{
431-
return ThemeManager.DefaultFluentThemeFontSize;
440+
return MessageFontSize * ThemeManager.DefaultFluentFontSizeFactor ;
432441
}
433-
return ConvertFontHeight(SystemParameters.NonClientMetrics.lfMessageFont.lfHeight);
442+
443+
return MessageFontSize;
434444
}
435445
}
436446

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThemeManager.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ internal static ResourceDictionary GetThemeDictionary(ThemeMode themeMode)
204204
return rd;
205205
}
206206

207+
internal static bool IsFluentThemeDictionaryIncluded()
208+
{
209+
return Application.Current != null && LastIndexOfFluentThemeDictionary(Application.Current.Resources) != -1;
210+
}
211+
207212
#endregion
208213

209214

@@ -323,7 +328,7 @@ internal static bool IsFluentThemeEnabled
323328

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

326-
internal static double DefaultFluentThemeFontSize => 14;
331+
internal const double DefaultFluentFontSizeFactor = 14.0 / 12.0 ;
327332

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

src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Styles/Button.xaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
<Setter Property="VerticalAlignment" Value="Center" />
2727
<Setter Property="HorizontalContentAlignment" Value="Center" />
2828
<Setter Property="VerticalContentAlignment" Value="Center" />
29-
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
3029
<Setter Property="FontWeight" Value="Normal" />
3130
<Setter Property="Border.CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
3231
<Setter Property="SnapsToDevicePixels" Value="True" />
@@ -86,7 +85,6 @@
8685
<Setter Property="VerticalAlignment" Value="Center" />
8786
<Setter Property="HorizontalContentAlignment" Value="Center" />
8887
<Setter Property="VerticalContentAlignment" Value="Center" />
89-
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
9088
<Setter Property="FontWeight" Value="Normal" />
9189
<Setter Property="Border.CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
9290
<Setter Property="SnapsToDevicePixels" Value="True" />

src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Styles/CheckBox.xaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
<Setter Property="HorizontalContentAlignment" Value="Left" />
4040
<Setter Property="VerticalContentAlignment" Value="Top" />
4141
<Setter Property="Cursor" Value="Hand" />
42-
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
4342
<Setter Property="FontWeight" Value="Normal" />
4443
<Setter Property="KeyboardNavigation.IsTabStop" Value="True" />
4544
<Setter Property="Focusable" Value="True" />

src/Microsoft.DotNet.Wpf/src/Themes/PresentationFramework.Fluent/Styles/ComboBox.xaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@
152152
<Setter Property="Background" Value="{DynamicResource ComboBoxBackground}" />
153153
<Setter Property="BorderBrush" Value="{DynamicResource ControlElevationBorderBrush}" />
154154
<Setter Property="BorderThickness" Value="{StaticResource ComboBoxBorderThemeThickness}" />
155-
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
156155
<Setter Property="ScrollViewer.CanContentScroll" Value="False" />
157156
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden" />
158157
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />

0 commit comments

Comments
 (0)