Skip to content

Commit fccde49

Browse files
Add systemFontFamily to flutter/settings channel (flutter#22981)
This allows a shell to set the system font to use by default.
1 parent 1dab008 commit fccde49

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed

lib/ui/platform_dispatcher.dart

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,26 @@ class PlatformDispatcher {
864864
_onPlatformBrightnessChangedZone = Zone.current;
865865
}
866866

867+
/// The setting indicating the current system font of the host platform.
868+
String? get systemFontFamily => configuration.systemFontFamily;
869+
870+
/// A callback that is invoked whenever [systemFontFamily] changes value.
871+
///
872+
/// The framework invokes this callback in the same zone in which the callback
873+
/// was set.
874+
///
875+
/// See also:
876+
///
877+
/// * [WidgetsBindingObserver], for a mechanism at the widgets layer to
878+
/// observe when this callback is invoked.
879+
VoidCallback? get onSystemFontFamilyChanged => _onSystemFontFamilyChanged;
880+
VoidCallback? _onSystemFontFamilyChanged;
881+
Zone _onSystemFontFamilyChangedZone = Zone.root;
882+
set onSystemFontFamilyChanged(VoidCallback? callback) {
883+
_onSystemFontFamilyChanged = callback;
884+
_onSystemFontFamilyChangedZone = Zone.current;
885+
}
886+
867887
// Called from the engine, via hooks.dart
868888
void _updateUserSettingsData(String jsonData) {
869889
final Map<String, Object?> data = json.decode(jsonData) as Map<String, Object?>;
@@ -880,19 +900,23 @@ class PlatformDispatcher {
880900
}
881901
final Brightness platformBrightness =
882902
data['platformBrightness']! as String == 'dark' ? Brightness.dark : Brightness.light;
903+
final String? systemFontFamily = data['systemFontFamily'] as String?;
883904
final PlatformConfiguration previousConfiguration = configuration;
884905
final bool platformBrightnessChanged =
885906
previousConfiguration.platformBrightness != platformBrightness;
886907
final bool textScaleFactorChanged = previousConfiguration.textScaleFactor != textScaleFactor;
887908
final bool alwaysUse24HourFormatChanged =
888909
previousConfiguration.alwaysUse24HourFormat != alwaysUse24HourFormat;
889-
if (!platformBrightnessChanged && !textScaleFactorChanged && !alwaysUse24HourFormatChanged) {
910+
final bool systemFontFamilyChanged =
911+
previousConfiguration.systemFontFamily != systemFontFamily;
912+
if (!platformBrightnessChanged && !textScaleFactorChanged && !alwaysUse24HourFormatChanged && !systemFontFamilyChanged) {
890913
return;
891914
}
892915
_configuration = previousConfiguration.copyWith(
893916
textScaleFactor: textScaleFactor,
894917
alwaysUse24HourFormat: alwaysUse24HourFormat,
895918
platformBrightness: platformBrightness,
919+
systemFontFamily: systemFontFamily,
896920
);
897921
_invoke(onPlatformConfigurationChanged, _onPlatformConfigurationChangedZone);
898922
if (textScaleFactorChanged) {
@@ -901,6 +925,9 @@ class PlatformDispatcher {
901925
if (platformBrightnessChanged) {
902926
_invoke(onPlatformBrightnessChanged, _onPlatformBrightnessChangedZone);
903927
}
928+
if (systemFontFamilyChanged) {
929+
_invoke(onSystemFontFamilyChanged, _onSystemFontFamilyChangedZone);
930+
}
904931
}
905932

906933
/// Whether the user has requested that [updateSemantics] be called when the
@@ -1032,6 +1059,7 @@ class PlatformConfiguration {
10321059
this.textScaleFactor = 1.0,
10331060
this.locales = const <Locale>[],
10341061
this.defaultRouteName,
1062+
this.systemFontFamily,
10351063
});
10361064

10371065
/// Copy a [PlatformConfiguration] with some fields replaced.
@@ -1043,6 +1071,7 @@ class PlatformConfiguration {
10431071
double? textScaleFactor,
10441072
List<Locale>? locales,
10451073
String? defaultRouteName,
1074+
String? systemFontFamily,
10461075
}) {
10471076
return PlatformConfiguration(
10481077
accessibilityFeatures: accessibilityFeatures ?? this.accessibilityFeatures,
@@ -1052,6 +1081,7 @@ class PlatformConfiguration {
10521081
textScaleFactor: textScaleFactor ?? this.textScaleFactor,
10531082
locales: locales ?? this.locales,
10541083
defaultRouteName: defaultRouteName ?? this.defaultRouteName,
1084+
systemFontFamily: systemFontFamily ?? this.systemFontFamily,
10551085
);
10561086
}
10571087

@@ -1080,6 +1110,9 @@ class PlatformConfiguration {
10801110
/// The route or path that the embedder requested when the application was
10811111
/// launched.
10821112
final String? defaultRouteName;
1113+
1114+
/// The system-reported default font family.
1115+
final String? systemFontFamily;
10831116
}
10841117

10851118
/// An immutable view configuration.

lib/ui/window.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,27 @@ class SingletonFlutterWindow extends FlutterWindow {
482482
platformDispatcher.onPlatformBrightnessChanged = callback;
483483
}
484484

485+
/// The setting indicating the system font of the host platform.
486+
///
487+
/// {@macro dart.ui.window.accessorForwardWarning}
488+
String? get systemFontFamily => platformDispatcher.systemFontFamily;
489+
490+
/// A callback that is invoked whenever [systemFontFamily] changes value.
491+
///
492+
/// {@macro dart.ui.window.accessorForwardWarning}
493+
///
494+
/// The framework invokes this callback in the same zone in which the
495+
/// callback was set.
496+
///
497+
/// See also:
498+
///
499+
/// * [WidgetsBindingObserver], for a mechanism at the widgets layer to
500+
/// observe when this callback is invoked.
501+
VoidCallback? get onSystemFontFamilyChanged => platformDispatcher.onSystemFontFamilyChanged;
502+
set onSystemFontFamilyChanged(VoidCallback? callback) {
503+
platformDispatcher.onSystemFontFamilyChanged = callback;
504+
}
505+
485506
/// A callback that is invoked to notify the window that it is an appropriate
486507
/// time to provide a scene using the [SceneBuilder] API and the [render]
487508
/// method.

lib/web_ui/lib/platform_dispatcher.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ abstract class PlatformDispatcher {
9191
VoidCallback? get onPlatformBrightnessChanged;
9292
set onPlatformBrightnessChanged(VoidCallback? callback);
9393

94+
String? get systemFontFamily => configuration.systemFontFamily;
95+
96+
VoidCallback? get onSystemFontFamilyChanged;
97+
set onSystemFontFamilyChanged(VoidCallback? callback);
98+
9499
bool get semanticsEnabled => configuration.semanticsEnabled;
95100

96101
VoidCallback? get onSemanticsEnabledChanged;
@@ -116,6 +121,7 @@ class PlatformConfiguration {
116121
this.textScaleFactor = 1.0,
117122
this.locales = const <Locale>[],
118123
this.defaultRouteName = '/',
124+
this.systemFontFamily,
119125
});
120126

121127
PlatformConfiguration copyWith({
@@ -126,6 +132,7 @@ class PlatformConfiguration {
126132
double? textScaleFactor,
127133
List<Locale>? locales,
128134
String? defaultRouteName,
135+
String? systemFontFamily,
129136
}) {
130137
return PlatformConfiguration(
131138
accessibilityFeatures: accessibilityFeatures ?? this.accessibilityFeatures,
@@ -135,6 +142,7 @@ class PlatformConfiguration {
135142
textScaleFactor: textScaleFactor ?? this.textScaleFactor,
136143
locales: locales ?? this.locales,
137144
defaultRouteName: defaultRouteName ?? this.defaultRouteName,
145+
systemFontFamily: systemFontFamily ?? this.systemFontFamily,
138146
);
139147
}
140148

@@ -145,6 +153,7 @@ class PlatformConfiguration {
145153
final double textScaleFactor;
146154
final List<Locale> locales;
147155
final String defaultRouteName;
156+
final String? systemFontFamily;
148157
}
149158

150159
class ViewConfiguration {

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,10 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
881881
}
882882
}
883883

884+
/// The setting indicating the current system font of the host platform.
885+
@override
886+
String? get systemFontFamily => configuration.systemFontFamily;
887+
884888
/// Reference to css media query that indicates the user theme preference on the web.
885889
final html.MediaQueryList _brightnessMediaQuery =
886890
html.window.matchMedia('(prefers-color-scheme: dark)');
@@ -940,6 +944,32 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
940944
invoke(_onPlatformBrightnessChanged, _onPlatformBrightnessChangedZone);
941945
}
942946

947+
/// A callback that is invoked whenever [systemFontFamily] changes value.
948+
///
949+
/// The framework invokes this callback in the same zone in which the
950+
/// callback was set.
951+
///
952+
/// See also:
953+
///
954+
/// * [WidgetsBindingObserver], for a mechanism at the widgets layer to
955+
/// observe when this callback is invoked.
956+
@override
957+
ui.VoidCallback? get onSystemFontFamilyChanged =>
958+
_onSystemFontFamilyChanged;
959+
ui.VoidCallback? _onSystemFontFamilyChanged;
960+
Zone? _onSystemFontFamilyChangedZone;
961+
@override
962+
set onSystemFontFamilyChanged(ui.VoidCallback? callback) {
963+
_onSystemFontFamilyChanged = callback;
964+
_onSystemFontFamilyChangedZone = Zone.current;
965+
}
966+
967+
/// Engine code should use this method instead of the callback directly.
968+
/// Otherwise zones won't work properly.
969+
void invokeOnSystemFontFamilyChanged() {
970+
invoke(_onSystemFontFamilyChanged, _onSystemFontFamilyChangedZone);
971+
}
972+
943973
/// Whether the user has requested that [updateSemantics] be called when
944974
/// the semantic contents of window changes.
945975
///

lib/web_ui/lib/window.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ abstract class SingletonFlutterWindow extends FlutterWindow {
6464
platformDispatcher.onPlatformBrightnessChanged = callback;
6565
}
6666

67+
String? get systemFontFamily => platformDispatcher.systemFontFamily;
68+
69+
VoidCallback? get onSystemFontFamilyChanged => platformDispatcher.onSystemFontFamilyChanged;
70+
set onSystemFontFamilyChanged(VoidCallback? callback) {
71+
platformDispatcher.onSystemFontFamilyChanged = callback;
72+
}
73+
6774
FrameCallback? get onBeginFrame => platformDispatcher.onBeginFrame;
6875
set onBeginFrame(FrameCallback? callback) {
6976
platformDispatcher.onBeginFrame = callback;

0 commit comments

Comments
 (0)