Skip to content

Commit 71b2bc9

Browse files
Renzo-OlivaresRenzo Olivares
authored andcommitted
Listen to text spacing overrides on the web (flutter#178081)
Original PR/Discussion: flutter#172915 # Framework: * `EditableText`/`SelectableText`, applies `lineHeightScaleFactorOverride`, `wordSpacingOverride`, and `letterSpacingOverride` to it's `TextStyle` similarly to how we already do for bold platform overrides. Note `SelectableText` is built on `EditableText` so it also applies these overrides. * `Text`, applies `lineHeightScaleFactorOverride`, `wordSpacingOverride`, and `letterSpacingOverride` to it's `TextStyle` similarly to how we already do for bold platform overrides. * Exposes line height override through `MediaQueryData.lineHeightScaleFactorOverride` and `maybeLineHeightScaleFactorOverrideOf(context)`. * Exposes letter spacing override through `MediaQueryData.letterSpacingOverride` and `maybeLetterSpacingOverrideOf(context)`. * Exposes word spacing override through `MediaQueryData.wordSpacingOverride` and `maybeWordSpacingOverrideOf(context)`. * Exposes paragraph spacing override through `MediaQueryData.paragraphSpacingOverride` and `maybeParagraphSpacingOverrideOf(context)`. * `MediaQuery.applyTextStyleOverrides()` \ `MediaQueryData.applyTextStyleOverrides()` to be able to reset/override the text spacing settings on `MediaQueryData`. # Engine: * Introduces new members on `PlatformDispatcher` API that hold the text spacing properties that are overridden on the web. * We provide the `lineHeightScaleFactorOverride`, `letterSpacingOverride`, `wordSpacingOverride`, and `paragraphSpacingOverride` on the web by attaching a `ResizeObserver` to an off-screen hidden element, when its size changes we capture its text spacing CSS properties, and notify the framework through `onMetricsChanged`. Fixes flutter#142712 https://github.com/user-attachments/assets/aaaa3e74-c232-4956-acd2-ae1a4487e415 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. --------- Co-authored-by: Renzo Olivares <roliv@google.com>
1 parent a907cb3 commit 71b2bc9

File tree

16 files changed

+1287
-42
lines changed

16 files changed

+1287
-42
lines changed

engine/src/flutter/lib/ui/platform_dispatcher.dart

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,59 @@ class PlatformDispatcher {
11211121
/// This option is used by [showTimePicker].
11221122
bool get alwaysUse24HourFormat => _configuration.alwaysUse24HourFormat;
11231123

1124+
/// The system-suggested height of the text, as a multiple of the font size.
1125+
///
1126+
/// This value takes precedence over any text height specified at the
1127+
/// application level. For example, at framework level, in the [TextStyle]
1128+
/// for [Text], [SelectableText], and [EditableText] widgets, this value
1129+
/// overrides the existing value of [TextStyle.height] and [StrutStyle.height].
1130+
///
1131+
/// Returns null when no override has been set by the system.
1132+
///
1133+
/// If this value changes, [onMetricsChanged] will be called.
1134+
double? get lineHeightScaleFactorOverride => _configuration.lineHeightScaleFactorOverride;
1135+
1136+
/// The system-suggested amount of additional space (in logical pixels)
1137+
/// to add between each letter.
1138+
///
1139+
/// A negative value can be used to bring the letters closer.
1140+
///
1141+
/// This value takes precedence over any text letter spacing specified at the
1142+
/// application level. For example, at framework level, in the [TextStyle]
1143+
/// for [Text], [SelectableText], and [EditableText] widgets, this value
1144+
/// overrides the existing value of [TextStyle.letterSpacing].
1145+
///
1146+
/// Returns null when no override has been set by the system.
1147+
///
1148+
/// If this value changes, [onMetricsChanged] will be called.
1149+
double? get letterSpacingOverride => _configuration.letterSpacingOverride;
1150+
1151+
/// The system-suggested amount of additional space (in logical pixels)
1152+
/// to add between each sequence of white-space (i.e. between each word).
1153+
///
1154+
/// A negative value can be used to bring the words closer.
1155+
///
1156+
/// This value takes precedence over any text word spacing specified at the
1157+
/// application level. For example, at framework level, in the [TextStyle]
1158+
/// for [Text], [SelectableText], and [EditableText] widgets, this value
1159+
/// overrides the existing value of [TextStyle.wordSpacing].
1160+
///
1161+
/// Returns null when no override has been set by the system.
1162+
///
1163+
/// If this value changes, [onMetricsChanged] will be called.
1164+
double? get wordSpacingOverride => _configuration.wordSpacingOverride;
1165+
1166+
/// The system-suggested amount of additional space (in logical pixels)
1167+
/// to add following each paragraph in text.
1168+
///
1169+
/// This value takes precedence over any text paragraph spacing specified at
1170+
/// the application level.
1171+
///
1172+
/// Returns null when no override has been set by the system.
1173+
///
1174+
/// If this value changes, [onMetricsChanged] will be called.
1175+
double? get paragraphSpacingOverride => _configuration.paragraphSpacingOverride;
1176+
11241177
/// The system-reported text scale.
11251178
///
11261179
/// This establishes the text scaling factor to use when rendering text,
@@ -1811,6 +1864,10 @@ class _PlatformConfiguration {
18111864
this.defaultRouteName,
18121865
this.systemFontFamily,
18131866
this.configurationId,
1867+
this.lineHeightScaleFactorOverride,
1868+
this.letterSpacingOverride,
1869+
this.wordSpacingOverride,
1870+
this.paragraphSpacingOverride,
18141871
});
18151872

18161873
_PlatformConfiguration copyWith({
@@ -1834,6 +1891,10 @@ class _PlatformConfiguration {
18341891
defaultRouteName: defaultRouteName ?? this.defaultRouteName,
18351892
systemFontFamily: systemFontFamily ?? this.systemFontFamily,
18361893
configurationId: configurationId ?? this.configurationId,
1894+
lineHeightScaleFactorOverride: lineHeightScaleFactorOverride,
1895+
letterSpacingOverride: letterSpacingOverride,
1896+
wordSpacingOverride: wordSpacingOverride,
1897+
paragraphSpacingOverride: paragraphSpacingOverride,
18371898
);
18381899
}
18391900

@@ -1881,6 +1942,25 @@ class _PlatformConfiguration {
18811942
/// configuration updates from the embedder yet. The _getScaledFontSize
18821943
/// function should not be called in either case.
18831944
final int? configurationId;
1945+
1946+
/// The system-reported height of the text, as a multiple of the font size.
1947+
final double? lineHeightScaleFactorOverride;
1948+
1949+
/// The system-reported amount of additional space (in logical pixels)
1950+
/// to add between each letter.
1951+
///
1952+
/// A negative value can be used to bring the letters closer.
1953+
final double? letterSpacingOverride;
1954+
1955+
/// The system-reported amount of additional space (in logical pixels)
1956+
/// to add between each sequence of white-space (i.e. between each word).
1957+
///
1958+
/// A negative value can be used to bring the words closer.
1959+
final double? wordSpacingOverride;
1960+
1961+
/// The system-reported amount of additional space (in logical pixels)
1962+
/// to add between each paragraph in text.
1963+
final double? paragraphSpacingOverride;
18841964
}
18851965

18861966
/// An immutable view configuration.

engine/src/flutter/lib/web_ui/lib/platform_dispatcher.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ abstract class PlatformDispatcher {
8989

9090
void setApplicationLocale(Locale locale) {}
9191

92+
double? get lineHeightScaleFactorOverride;
93+
94+
double? get letterSpacingOverride;
95+
96+
double? get wordSpacingOverride;
97+
98+
double? get paragraphSpacingOverride;
99+
92100
AccessibilityFeatures get accessibilityFeatures;
93101

94102
VoidCallback? get onAccessibilityFeaturesChanged;

0 commit comments

Comments
 (0)