Files
pipelines/app/lib/features/settings/settings_font_section.dart
T
alan 2853b18b9e Settings: per-device interface font choice
System default plus Inter, IBM Plex Sans, Source Sans 3 and Atkinson
Hyperlegible Next (OFL 1.1, bundled as on-demand assets and loaded with
FontLoader, so the web fetches only the previewed/chosen files). Stored in
shared_preferences, applied live to Material and Cupertino text themes;
RobotoMono styles unchanged. Licences registered with LicenseRegistry.
2026-11-04 02:01:49 +00:00

140 lines
4.5 KiB
Dart

import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../ui/theme/app_font.dart';
import '../../ui/theme/app_font_controller.dart';
import 'settings_group.dart';
import 'settings_row.dart';
import 'settings_theme.dart';
const fontSectionTitle = 'Font';
const fontSectionFooter =
'Saved on this device only. Code keeps its monospace font.';
const fontLoadingText = 'Loading…';
const fontLoadFailedText = 'Could not load this font.';
const fontSaveFailedText = 'Not saved on this device; it applies until reload.';
ValueKey<String> settingsFontKey(AppFont font) =>
ValueKey<String>('settings-font-${font.id}');
/// The device's interface font: one row per option, each named in its own
/// face. Material rows are radio tiles, Cupertino rows carry the checkmark.
/// A choice is stored locally and applies as soon as its files load.
class SettingsFontSection extends StatefulWidget {
const SettingsFontSection({super.key, required this.controller});
final AppFontController controller;
@override
State<SettingsFontSection> createState() => _SettingsFontSectionState();
}
class _SettingsFontSectionState extends State<SettingsFontSection> {
@override
void initState() {
super.initState();
_preview();
}
@override
void didUpdateWidget(SettingsFontSection oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.controller != widget.controller) _preview();
}
/// Only the regular weight of each option, only once Settings is shown.
void _preview() {
for (final font in AppFont.values) {
unawaited(widget.controller.preview(font));
}
}
@override
Widget build(BuildContext context) => ListenableBuilder(
listenable: widget.controller,
builder: (context, _) => _group(context),
);
Widget _group(BuildContext context) {
final controller = widget.controller;
final cupertino = usesCupertinoControls(context);
final group = SettingsGroup(
title: fontSectionTitle,
footer: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
const Text(fontSectionFooter),
if (controller.saveFailed)
Text(
fontSaveFailedText,
style: TextStyle(color: settingsErrorColor(context)),
),
],
),
children: <Widget>[
for (final font in AppFont.values)
cupertino
? _cupertinoRow(context, font)
: RadioListTile<AppFont>(
key: settingsFontKey(font),
value: font,
title: _name(context, font),
subtitle: _status(context, font),
),
],
);
if (cupertino) return group;
return RadioGroup<AppFont>(
groupValue: controller.selected,
onChanged: (font) {
if (font != null) unawaited(controller.select(font));
},
child: group,
);
}
Widget _cupertinoRow(BuildContext context, AppFont font) => SettingsRow(
key: settingsFontKey(font),
title: _name(context, font),
subtitle: _status(context, font),
trailing: widget.controller.selected == font
? Icon(
CupertinoIcons.check_mark,
color: CupertinoTheme.of(context).primaryColor,
)
: null,
onTap: () => unawaited(widget.controller.select(font)),
);
/// The option's name in its own face once its regular weight has loaded;
/// the system face until then (and always for the system option).
Widget _name(BuildContext context, AppFont font) {
final family = widget.controller.canPreview(font)
? font.family ?? _systemFamily(context)
: _systemFamily(context);
return Text(font.label, style: TextStyle(fontFamily: family));
}
Widget? _status(BuildContext context, AppFont font) {
final controller = widget.controller;
if (controller.selected != font) return null;
if (controller.loadFailed == font) {
return Text(
fontLoadFailedText,
style: TextStyle(color: settingsErrorColor(context)),
);
}
return controller.loading ? const Text(fontLoadingText) : null;
}
}
/// The platform's own interface family (Roboto, or the Cupertino system
/// face), independent of the font the theme currently applies.
String? _systemFamily(BuildContext context) => usesCupertinoControls(context)
? const CupertinoTextThemeData().textStyle.fontFamily
: Typography.material2021(
platform: Theme.of(context).platform,
).black.bodyLarge?.fontFamily;