From 8d4c2b736014e7050bb2d88b8a9f6ef74a9e316e Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Wed, 9 Dec 2020 17:17:31 +1300 Subject: [PATCH] Add systemFontFamily to flutter/settings channel This allows a shell to set the system font to use by default. --- lib/ui/platform_dispatcher.dart | 35 ++++++++++++++++++- lib/ui/window.dart | 21 +++++++++++ lib/web_ui/lib/platform_dispatcher.dart | 9 +++++ .../lib/src/engine/platform_dispatcher.dart | 30 ++++++++++++++++ lib/web_ui/lib/window.dart | 7 ++++ 5 files changed, 101 insertions(+), 1 deletion(-) diff --git a/lib/ui/platform_dispatcher.dart b/lib/ui/platform_dispatcher.dart index 36deee6632e00..b6de42b98fc34 100644 --- a/lib/ui/platform_dispatcher.dart +++ b/lib/ui/platform_dispatcher.dart @@ -864,6 +864,26 @@ class PlatformDispatcher { _onPlatformBrightnessChangedZone = Zone.current; } + /// The setting indicating the current system font of the host platform. + String? get systemFontFamily => configuration.systemFontFamily; + + /// A callback that is invoked whenever [systemFontFamily] changes value. + /// + /// The framework invokes this callback in the same zone in which the callback + /// was set. + /// + /// See also: + /// + /// * [WidgetsBindingObserver], for a mechanism at the widgets layer to + /// observe when this callback is invoked. + VoidCallback? get onSystemFontFamilyChanged => _onSystemFontFamilyChanged; + VoidCallback? _onSystemFontFamilyChanged; + Zone _onSystemFontFamilyChangedZone = Zone.root; + set onSystemFontFamilyChanged(VoidCallback? callback) { + _onSystemFontFamilyChanged = callback; + _onSystemFontFamilyChangedZone = Zone.current; + } + // Called from the engine, via hooks.dart void _updateUserSettingsData(String jsonData) { final Map data = json.decode(jsonData) as Map; @@ -880,19 +900,23 @@ class PlatformDispatcher { } final Brightness platformBrightness = data['platformBrightness']! as String == 'dark' ? Brightness.dark : Brightness.light; + final String? systemFontFamily = data['systemFontFamily'] as String?; final PlatformConfiguration previousConfiguration = configuration; final bool platformBrightnessChanged = previousConfiguration.platformBrightness != platformBrightness; final bool textScaleFactorChanged = previousConfiguration.textScaleFactor != textScaleFactor; final bool alwaysUse24HourFormatChanged = previousConfiguration.alwaysUse24HourFormat != alwaysUse24HourFormat; - if (!platformBrightnessChanged && !textScaleFactorChanged && !alwaysUse24HourFormatChanged) { + final bool systemFontFamilyChanged = + previousConfiguration.systemFontFamily != systemFontFamily; + if (!platformBrightnessChanged && !textScaleFactorChanged && !alwaysUse24HourFormatChanged && !systemFontFamilyChanged) { return; } _configuration = previousConfiguration.copyWith( textScaleFactor: textScaleFactor, alwaysUse24HourFormat: alwaysUse24HourFormat, platformBrightness: platformBrightness, + systemFontFamily: systemFontFamily, ); _invoke(onPlatformConfigurationChanged, _onPlatformConfigurationChangedZone); if (textScaleFactorChanged) { @@ -901,6 +925,9 @@ class PlatformDispatcher { if (platformBrightnessChanged) { _invoke(onPlatformBrightnessChanged, _onPlatformBrightnessChangedZone); } + if (systemFontFamilyChanged) { + _invoke(onSystemFontFamilyChanged, _onSystemFontFamilyChangedZone); + } } /// Whether the user has requested that [updateSemantics] be called when the @@ -1032,6 +1059,7 @@ class PlatformConfiguration { this.textScaleFactor = 1.0, this.locales = const [], this.defaultRouteName, + this.systemFontFamily, }); /// Copy a [PlatformConfiguration] with some fields replaced. @@ -1043,6 +1071,7 @@ class PlatformConfiguration { double? textScaleFactor, List? locales, String? defaultRouteName, + String? systemFontFamily, }) { return PlatformConfiguration( accessibilityFeatures: accessibilityFeatures ?? this.accessibilityFeatures, @@ -1052,6 +1081,7 @@ class PlatformConfiguration { textScaleFactor: textScaleFactor ?? this.textScaleFactor, locales: locales ?? this.locales, defaultRouteName: defaultRouteName ?? this.defaultRouteName, + systemFontFamily: systemFontFamily ?? this.systemFontFamily, ); } @@ -1080,6 +1110,9 @@ class PlatformConfiguration { /// The route or path that the embedder requested when the application was /// launched. final String? defaultRouteName; + + /// The system-reported default font family. + final String? systemFontFamily; } /// An immutable view configuration. diff --git a/lib/ui/window.dart b/lib/ui/window.dart index 54d3c606ce301..f42ec7a3778cb 100644 --- a/lib/ui/window.dart +++ b/lib/ui/window.dart @@ -482,6 +482,27 @@ class SingletonFlutterWindow extends FlutterWindow { platformDispatcher.onPlatformBrightnessChanged = callback; } + /// The setting indicating the system font of the host platform. + /// + /// {@macro dart.ui.window.accessorForwardWarning} + String? get systemFontFamily => platformDispatcher.systemFontFamily; + + /// A callback that is invoked whenever [systemFontFamily] changes value. + /// + /// {@macro dart.ui.window.accessorForwardWarning} + /// + /// The framework invokes this callback in the same zone in which the + /// callback was set. + /// + /// See also: + /// + /// * [WidgetsBindingObserver], for a mechanism at the widgets layer to + /// observe when this callback is invoked. + VoidCallback? get onSystemFontFamilyChanged => platformDispatcher.onSystemFontFamilyChanged; + set onSystemFontFamilyChanged(VoidCallback? callback) { + platformDispatcher.onSystemFontFamilyChanged = callback; + } + /// A callback that is invoked to notify the window that it is an appropriate /// time to provide a scene using the [SceneBuilder] API and the [render] /// method. diff --git a/lib/web_ui/lib/platform_dispatcher.dart b/lib/web_ui/lib/platform_dispatcher.dart index b9c1e9ce85f6f..61ed62a1e288b 100644 --- a/lib/web_ui/lib/platform_dispatcher.dart +++ b/lib/web_ui/lib/platform_dispatcher.dart @@ -91,6 +91,11 @@ abstract class PlatformDispatcher { VoidCallback? get onPlatformBrightnessChanged; set onPlatformBrightnessChanged(VoidCallback? callback); + String? get systemFontFamily => configuration.systemFontFamily; + + VoidCallback? get onSystemFontFamilyChanged; + set onSystemFontFamilyChanged(VoidCallback? callback); + bool get semanticsEnabled => configuration.semanticsEnabled; VoidCallback? get onSemanticsEnabledChanged; @@ -116,6 +121,7 @@ class PlatformConfiguration { this.textScaleFactor = 1.0, this.locales = const [], this.defaultRouteName = '/', + this.systemFontFamily, }); PlatformConfiguration copyWith({ @@ -126,6 +132,7 @@ class PlatformConfiguration { double? textScaleFactor, List? locales, String? defaultRouteName, + String? systemFontFamily, }) { return PlatformConfiguration( accessibilityFeatures: accessibilityFeatures ?? this.accessibilityFeatures, @@ -135,6 +142,7 @@ class PlatformConfiguration { textScaleFactor: textScaleFactor ?? this.textScaleFactor, locales: locales ?? this.locales, defaultRouteName: defaultRouteName ?? this.defaultRouteName, + systemFontFamily: systemFontFamily ?? this.systemFontFamily, ); } @@ -145,6 +153,7 @@ class PlatformConfiguration { final double textScaleFactor; final List locales; final String defaultRouteName; + final String? systemFontFamily; } class ViewConfiguration { diff --git a/lib/web_ui/lib/src/engine/platform_dispatcher.dart b/lib/web_ui/lib/src/engine/platform_dispatcher.dart index 4f50725c779dd..c4257dbd15c6f 100644 --- a/lib/web_ui/lib/src/engine/platform_dispatcher.dart +++ b/lib/web_ui/lib/src/engine/platform_dispatcher.dart @@ -881,6 +881,10 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher { } } + /// The setting indicating the current system font of the host platform. + @override + String? get systemFontFamily => configuration.systemFontFamily; + /// Reference to css media query that indicates the user theme preference on the web. final html.MediaQueryList _brightnessMediaQuery = html.window.matchMedia('(prefers-color-scheme: dark)'); @@ -940,6 +944,32 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher { invoke(_onPlatformBrightnessChanged, _onPlatformBrightnessChangedZone); } + /// A callback that is invoked whenever [systemFontFamily] changes value. + /// + /// The framework invokes this callback in the same zone in which the + /// callback was set. + /// + /// See also: + /// + /// * [WidgetsBindingObserver], for a mechanism at the widgets layer to + /// observe when this callback is invoked. + @override + ui.VoidCallback? get onSystemFontFamilyChanged => + _onSystemFontFamilyChanged; + ui.VoidCallback? _onSystemFontFamilyChanged; + Zone? _onSystemFontFamilyChangedZone; + @override + set onSystemFontFamilyChanged(ui.VoidCallback? callback) { + _onSystemFontFamilyChanged = callback; + _onSystemFontFamilyChangedZone = Zone.current; + } + + /// Engine code should use this method instead of the callback directly. + /// Otherwise zones won't work properly. + void invokeOnSystemFontFamilyChanged() { + invoke(_onSystemFontFamilyChanged, _onSystemFontFamilyChangedZone); + } + /// Whether the user has requested that [updateSemantics] be called when /// the semantic contents of window changes. /// diff --git a/lib/web_ui/lib/window.dart b/lib/web_ui/lib/window.dart index a5dc10f17f0b4..fa2206778b870 100644 --- a/lib/web_ui/lib/window.dart +++ b/lib/web_ui/lib/window.dart @@ -64,6 +64,13 @@ abstract class SingletonFlutterWindow extends FlutterWindow { platformDispatcher.onPlatformBrightnessChanged = callback; } + String? get systemFontFamily => platformDispatcher.systemFontFamily; + + VoidCallback? get onSystemFontFamilyChanged => platformDispatcher.onSystemFontFamilyChanged; + set onSystemFontFamilyChanged(VoidCallback? callback) { + platformDispatcher.onSystemFontFamilyChanged = callback; + } + FrameCallback? get onBeginFrame => platformDispatcher.onBeginFrame; set onBeginFrame(FrameCallback? callback) { platformDispatcher.onBeginFrame = callback;