[web] Implement onTextScaleFactorChanged (flutter/engine#30637)

This commit is contained in:
Eliot Lew
2022-02-01 10:20:11 +13:00
committed by GitHub
parent e3be79d1d0
commit f18281f62e
2 changed files with 83 additions and 0 deletions

View File

@@ -44,6 +44,7 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
/// these.
EnginePlatformDispatcher._() {
_addBrightnessMediaQueryListener();
_addFontSizeObserver();
}
/// The [EnginePlatformDispatcher] singleton.
@@ -783,6 +784,54 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
@override
bool get alwaysUse24HourFormat => configuration.alwaysUse24HourFormat;
/// Updates [textScaleFactor] and invokes [onTextScaleFactorChanged] and
/// [onPlatformConfigurationChanged] callbacks if [textScaleFactor] changed.
void _updateTextScaleFactor(double value) {
if (configuration.textScaleFactor != value) {
_configuration = configuration.copyWith(textScaleFactor: value);
invokeOnPlatformConfigurationChanged();
invokeOnTextScaleFactorChanged();
}
}
/// Watches for font-size changes in the browser's <html> element to
/// recalculate [textScaleFactor].
///
/// Updates [textScaleFactor] with the new value.
html.MutationObserver? _fontSizeObserver;
/// Set the callback function for updating [textScaleFactor] based on
/// font-size changes in the browser's <html> element.
void _addFontSizeObserver() {
const String styleAttribute = 'style';
_fontSizeObserver = html.MutationObserver(
(List<dynamic> mutations, html.MutationObserver _) {
for (final dynamic mutation in mutations) {
final html.MutationRecord record = mutation as html.MutationRecord;
if (record.type == 'attributes' &&
record.attributeName == styleAttribute) {
final double newTextScaleFactor = findBrowserTextScaleFactor();
_updateTextScaleFactor(newTextScaleFactor);
}
}
});
_fontSizeObserver!.observe(
html.document.documentElement!,
attributes: true,
attributeFilter: <String>[styleAttribute],
);
registerHotRestartListener(() {
_disconnectFontSizeObserver();
});
}
/// Remove the observer for font-size changes in the browser's <html> element.
void _disconnectFontSizeObserver() {
_fontSizeObserver?.disconnect();
_fontSizeObserver = null;
}
/// A callback that is invoked whenever [textScaleFactor] changes value.
///
/// The framework invokes this callback in the same zone in which the

View File

@@ -111,5 +111,39 @@ void testMain() {
root.style.fontSize = null;
expect(findBrowserTextScaleFactor(), 1.0);
});
test(
'calls onTextScaleFactorChanged when the <html> element\'s font-size changes',
() async {
final html.Element root = html.document.documentElement!;
final String oldFontSize = root.style.fontSize;
final ui.VoidCallback? oldCallback = ui.PlatformDispatcher.instance.onTextScaleFactorChanged;
addTearDown(() {
root.style.fontSize = oldFontSize;
ui.PlatformDispatcher.instance.onTextScaleFactorChanged = oldCallback;
});
root.style.fontSize = '16px';
bool isCalled = false;
ui.PlatformDispatcher.instance.onTextScaleFactorChanged = () {
isCalled = true;
};
root.style.fontSize = '20px';
await Future<void>.delayed(Duration.zero);
expect(root.style.fontSize, '20px');
expect(isCalled, isTrue);
expect(ui.PlatformDispatcher.instance.textScaleFactor, findBrowserTextScaleFactor());
isCalled = false;
root.style.fontSize = '16px';
await Future<void>.delayed(Duration.zero);
expect(root.style.fontSize, '16px');
expect(isCalled, isTrue);
expect(ui.PlatformDispatcher.instance.textScaleFactor, findBrowserTextScaleFactor());
});
});
}