1515
1616namespace Avalonia . Controls . Presenters
1717{
18- public class TextPresenter : Control
18+ public class TextPresenter : Control , IPlatformTextScaleable
1919 {
2020 public static readonly StyledProperty < bool > ShowSelectionHighlightProperty =
2121 AvaloniaProperty . Register < TextPresenter , bool > ( nameof ( ShowSelectionHighlight ) , defaultValue : true ) ;
@@ -47,6 +47,12 @@ public class TextPresenter : Control
4747 public static readonly StyledProperty < int > SelectionEndProperty =
4848 TextBox . SelectionEndProperty . AddOwner < TextPresenter > ( new ( coerce : TextBox . CoerceCaretIndex ) ) ;
4949
50+ /// <summary>
51+ /// Defines the <see cref="IsPlatformTextScalingEnabled"/> property.
52+ /// </summary>
53+ public static readonly StyledProperty < bool > IsPlatformTextScalingEnabledProperty =
54+ TextElement . IsPlatformTextScalingEnabledProperty . AddOwner < TextPresenter > ( ) ;
55+
5056 /// <summary>
5157 /// Defines the <see cref="Text"/> property.
5258 /// </summary>
@@ -124,6 +130,13 @@ public IBrush? Background
124130 set => SetValue ( BackgroundProperty , value ) ;
125131 }
126132
133+ /// <inheritdoc cref="TextElement.IsPlatformTextScalingEnabled"/>
134+ public bool IsPlatformTextScalingEnabled
135+ {
136+ get => GetValue ( IsPlatformTextScalingEnabledProperty ) ;
137+ set => SetValue ( IsPlatformTextScalingEnabledProperty , value ) ;
138+ }
139+
127140 /// <summary>
128141 /// Gets or sets a value that determines whether the TextPresenter shows a selection highlight.
129142 /// </summary>
@@ -181,6 +194,13 @@ public double FontSize
181194 get => TextElement . GetFontSize ( this ) ;
182195 set => TextElement . SetFontSize ( this , value ) ;
183196 }
197+
198+ double IPlatformTextScaleable . GetScaledFontSize ( double baseFontSize ) => IsPlatformTextScalingEnabled && TopLevel . GetTopLevel ( this ) is { PlatformSettings : { } platformSettings } ? platformSettings . GetScaledFontSize ( baseFontSize ) : baseFontSize ;
199+
200+ /// <summary>
201+ /// Gets <see cref="FontSize"/> scaled according to the platform's current text scaling rules.
202+ /// </summary>
203+ protected double EffectiveFontSize => ( ( IPlatformTextScaleable ) this ) . GetScaledFontSize ( FontSize ) ;
184204
185205 /// <summary>
186206 /// Gets or sets the font style.
@@ -335,6 +355,14 @@ public int SelectionEnd
335355
336356 internal TextSelectionHandleCanvas ? TextSelectionHandleCanvas { get ; set ; }
337357
358+ void IPlatformTextScaleable . OnPlatformTextScalingChanged ( )
359+ {
360+ if ( IsPlatformTextScalingEnabled )
361+ {
362+ InvalidateMeasure ( ) ;
363+ }
364+ }
365+
338366 /// <summary>
339367 /// Creates the <see cref="TextLayout"/> used to render the text.
340368 /// </summary>
@@ -350,7 +378,7 @@ private TextLayout CreateTextLayoutInternal(Size constraint, string? text, Typef
350378 var maxWidth = MathUtilities . IsZero ( constraint . Width ) ? double . PositiveInfinity : constraint . Width ;
351379 var maxHeight = MathUtilities . IsZero ( constraint . Height ) ? double . PositiveInfinity : constraint . Height ;
352380
353- var textLayout = new TextLayout ( text , typeface , FontFeatures , FontSize , foreground , TextAlignment ,
381+ var textLayout = new TextLayout ( text , typeface , FontFeatures , EffectiveFontSize , foreground , TextAlignment ,
354382 TextWrapping , maxWidth : maxWidth , maxHeight : maxHeight , textStyleOverrides : textStyleOverrides ,
355383 flowDirection : FlowDirection , lineHeight : LineHeight , letterSpacing : LetterSpacing ) ;
356384
@@ -553,7 +581,7 @@ protected virtual TextLayout CreateTextLayout()
553581 if ( ! string . IsNullOrEmpty ( preeditText ) )
554582 {
555583 var preeditHighlight = new ValueSpan < TextRunProperties > ( caretIndex , preeditText . Length ,
556- new GenericTextRunProperties ( typeface , FontFeatures , FontSize ,
584+ new GenericTextRunProperties ( typeface , FontFeatures , EffectiveFontSize ,
557585 foregroundBrush : foreground ,
558586 textDecorations : TextDecorations . Underline ) ) ;
559587
@@ -569,7 +597,7 @@ protected virtual TextLayout CreateTextLayout()
569597 textStyleOverrides = new [ ]
570598 {
571599 new ValueSpan < TextRunProperties > ( start , length ,
572- new GenericTextRunProperties ( typeface , FontFeatures , FontSize ,
600+ new GenericTextRunProperties ( typeface , FontFeatures , EffectiveFontSize ,
573601 foregroundBrush : SelectionForegroundBrush ) )
574602 } ;
575603 }
0 commit comments