From f18281f62e2e1957e81af6de3121fb113625445f Mon Sep 17 00:00:00 2001 From: Eliot Lew <94671792+sourcemain@users.noreply.github.com> Date: Tue, 1 Feb 2022 10:20:11 +1300 Subject: [PATCH] [web] Implement onTextScaleFactorChanged (flutter/engine#30637) --- .../lib/src/engine/platform_dispatcher.dart | 49 +++++++++++++++++++ .../test/engine/platform_dispatcher_test.dart | 34 +++++++++++++ 2 files changed, 83 insertions(+) diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/platform_dispatcher.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/platform_dispatcher.dart index 03320930bd..4f50725c77 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/platform_dispatcher.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/platform_dispatcher.dart @@ -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 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 element. + void _addFontSizeObserver() { + const String styleAttribute = 'style'; + + _fontSizeObserver = html.MutationObserver( + (List 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: [styleAttribute], + ); + registerHotRestartListener(() { + _disconnectFontSizeObserver(); + }); + } + + /// Remove the observer for font-size changes in the browser's 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 diff --git a/engine/src/flutter/lib/web_ui/test/engine/platform_dispatcher_test.dart b/engine/src/flutter/lib/web_ui/test/engine/platform_dispatcher_test.dart index 0007cc6724..e98ada7345 100644 --- a/engine/src/flutter/lib/web_ui/test/engine/platform_dispatcher_test.dart +++ b/engine/src/flutter/lib/web_ui/test/engine/platform_dispatcher_test.dart @@ -111,5 +111,39 @@ void testMain() { root.style.fontSize = null; expect(findBrowserTextScaleFactor(), 1.0); }); + + test( + 'calls onTextScaleFactorChanged when the 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.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.delayed(Duration.zero); + expect(root.style.fontSize, '16px'); + expect(isCalled, isTrue); + expect(ui.PlatformDispatcher.instance.textScaleFactor, findBrowserTextScaleFactor()); + }); }); }