Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 772faf0

Browse files
committed
Add systemFontFamily to flutter/settings channel
This allows a shell to set the system font to use by default.
1 parent 4f1ea25 commit 772faf0

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

lib/ui/platform_dispatcher.dart

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,26 @@ class PlatformDispatcher {
858858
_onPlatformBrightnessChangedZone = Zone.current;
859859
}
860860

861+
/// The setting indicating the current system font of the host platform.
862+
String? get systemFontFamily => configuration.systemFontFamily;
863+
864+
/// A callback that is invoked whenever [systemFontFamily] changes value.
865+
///
866+
/// The framework invokes this callback in the same zone in which the callback
867+
/// was set.
868+
///
869+
/// See also:
870+
///
871+
/// * [WidgetsBindingObserver], for a mechanism at the widgets layer to
872+
/// observe when this callback is invoked.
873+
VoidCallback? get onSystemFontFamilyChanged => _onSystemFontFamilyChanged;
874+
VoidCallback? _onSystemFontFamilyChanged;
875+
Zone _onSystemFontFamilyChangedZone = Zone.root;
876+
set onSystemFontFamilyChanged(VoidCallback? callback) {
877+
_onSystemFontFamilyChanged = callback;
878+
_onSystemFontFamilyChangedZone = Zone.current;
879+
}
880+
861881
// Called from the engine, via hooks.dart
862882
void _updateUserSettingsData(String jsonData) {
863883
final Map<String, dynamic> data = json.decode(jsonData) as Map<String, dynamic>;
@@ -874,19 +894,23 @@ class PlatformDispatcher {
874894
}
875895
final Brightness platformBrightness =
876896
data['platformBrightness'] as String == 'dark' ? Brightness.dark : Brightness.light;
897+
final String? systemFontFamily = data['systemFontFamily'] as String?;
877898
final PlatformConfiguration previousConfiguration = configuration;
878899
final bool platformBrightnessChanged =
879900
previousConfiguration.platformBrightness != platformBrightness;
880901
final bool textScaleFactorChanged = previousConfiguration.textScaleFactor != textScaleFactor;
881902
final bool alwaysUse24HourFormatChanged =
882903
previousConfiguration.alwaysUse24HourFormat != alwaysUse24HourFormat;
883-
if (!platformBrightnessChanged && !textScaleFactorChanged && !alwaysUse24HourFormatChanged) {
904+
final bool systemFontFamilyChanged =
905+
previousConfiguration.systemFontFamily != systemFontFamily;
906+
if (!platformBrightnessChanged && !textScaleFactorChanged && !alwaysUse24HourFormatChanged && !systemFontFamilyChanged) {
884907
return;
885908
}
886909
_configuration = previousConfiguration.copyWith(
887910
textScaleFactor: textScaleFactor,
888911
alwaysUse24HourFormat: alwaysUse24HourFormat,
889912
platformBrightness: platformBrightness,
913+
systemFontFamily: systemFontFamily,
890914
);
891915
_invoke(onPlatformConfigurationChanged, _onPlatformConfigurationChangedZone);
892916
if (textScaleFactorChanged) {
@@ -895,6 +919,9 @@ class PlatformDispatcher {
895919
if (platformBrightnessChanged) {
896920
_invoke(onPlatformBrightnessChanged, _onPlatformBrightnessChangedZone);
897921
}
922+
if (systemFontFamilyChanged) {
923+
_invoke(onSystemFontFamilyChanged, _onSystemFontFamilyChangedZone);
924+
}
898925
}
899926

900927
/// Whether the user has requested that [updateSemantics] be called when the
@@ -1026,6 +1053,7 @@ class PlatformConfiguration {
10261053
this.textScaleFactor = 1.0,
10271054
this.locales = const <Locale>[],
10281055
this.defaultRouteName,
1056+
this.systemFontFamily,
10291057
});
10301058

10311059
/// Copy a [PlatformConfiguration] with some fields replaced.
@@ -1037,6 +1065,7 @@ class PlatformConfiguration {
10371065
double? textScaleFactor,
10381066
List<Locale>? locales,
10391067
String? defaultRouteName,
1068+
String? systemFontFamily,
10401069
}) {
10411070
return PlatformConfiguration(
10421071
accessibilityFeatures: accessibilityFeatures ?? this.accessibilityFeatures,
@@ -1046,6 +1075,7 @@ class PlatformConfiguration {
10461075
textScaleFactor: textScaleFactor ?? this.textScaleFactor,
10471076
locales: locales ?? this.locales,
10481077
defaultRouteName: defaultRouteName ?? this.defaultRouteName,
1078+
systemFontFamily: systemFontFamily ?? this.systemFontFamily,
10491079
);
10501080
}
10511081

@@ -1074,6 +1104,9 @@ class PlatformConfiguration {
10741104
/// The route or path that the embedder requested when the application was
10751105
/// launched.
10761106
final String? defaultRouteName;
1107+
1108+
/// The system-reported default font family.
1109+
final String? systemFontFamily;
10771110
}
10781111

10791112
/// An immutable view configuration.

lib/ui/window.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,11 @@ class SingletonFlutterWindow extends FlutterWindow {
466466
/// [Brightness.light].
467467
Brightness get platformBrightness => platformDispatcher.platformBrightness;
468468

469+
/// The setting indicating the system font of the host platform.
470+
///
471+
/// {@macro dart.ui.window.accessorForwardWarning}
472+
String? get systemFontFamily => platformDispatcher.systemFontFamily;
473+
469474
/// A callback that is invoked whenever [platformBrightness] changes value.
470475
///
471476
/// {@macro dart.ui.window.accessorForwardWarning}

0 commit comments

Comments
 (0)