Add systemFontFamily to flutter/settings channel (flutter/engine#22981)

This allows a shell to set the system font to use by default.
This commit is contained in:
Robert Ancell
2022-03-14 14:07:05 +13:00
committed by GitHub
parent 7a37a3c610
commit e362802905
5 changed files with 101 additions and 1 deletions

View File

@@ -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<String, Object?> data = json.decode(jsonData) as Map<String, Object?>;
@@ -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 <Locale>[],
this.defaultRouteName,
this.systemFontFamily,
});
/// Copy a [PlatformConfiguration] with some fields replaced.
@@ -1043,6 +1071,7 @@ class PlatformConfiguration {
double? textScaleFactor,
List<Locale>? 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.

View File

@@ -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.

View File

@@ -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 <Locale>[],
this.defaultRouteName = '/',
this.systemFontFamily,
});
PlatformConfiguration copyWith({
@@ -126,6 +132,7 @@ class PlatformConfiguration {
double? textScaleFactor,
List<Locale>? 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<Locale> locales;
final String defaultRouteName;
final String? systemFontFamily;
}
class ViewConfiguration {

View File

@@ -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.
///

View File

@@ -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;