Files
pipelines/app/lib/features/settings/settings_font_section.dart
alan a7c41063fa Remove Cupertino branches from Settings
Keep the Material-only settings implementation and its focused tests while preserving public cupertino parameters for later cleanup. This removes unreachable platform-specific widgets and test variants without changing the Material path.
2026-11-11 05:05:57 +00:00

195 lines
6.0 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import '../../ui/theme/app_font.dart';
import '../../ui/theme/app_font_controller.dart';
import '../../ui/theme/custom_fonts/custom_font.dart';
import '../../ui/theme/custom_fonts/font_file_picker.dart';
import '../../ui/theme/font_choice.dart';
import 'settings_custom_font_actions.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. '
'Added fonts: .ttf or .otf, up to 10 MB each.';
const fontLoadingText = 'Loading…';
const fontLoadFailedText = 'Could not load this font.';
const fontSaveFailedText = 'Not saved on this device; it applies until reload.';
ValueKey<String> settingsFontKey(FontChoice font) =>
ValueKey<String>('settings-font-${font.id}');
const ValueKey<String> settingsAddFontKey = ValueKey<String>(
'settings-font-add',
);
/// The device's interface font: one row per option, each named in its own
/// face, the user's added fonts below the bundled ones, then "Add font…".
/// Material rows are radio tiles.
/// A choice is stored locally and applies as soon as its files load.
class SettingsFontSection extends StatefulWidget {
const SettingsFontSection({
super.key,
required this.controller,
this.pickFiles = pickFontFiles,
});
final AppFontController controller;
/// The host file picker; tests substitute chosen files.
final FontFilePicker pickFiles;
@override
State<SettingsFontSection> createState() => _SettingsFontSectionState();
}
class _SettingsFontSectionState extends State<SettingsFontSection> {
bool _adding = false;
@override
void initState() {
super.initState();
unawaited(_preview());
}
@override
void didUpdateWidget(SettingsFontSection oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.controller != widget.controller) unawaited(_preview());
}
/// Only the regular weight of each option, only once Settings is shown.
Future<void> _preview() async {
final controller = widget.controller;
for (final font in AppFont.values) {
unawaited(controller.preview(font));
}
await controller.loadCustomFonts();
for (final font in controller.customFonts) {
unawaited(controller.preview(font));
}
}
Future<void> _add() async {
try {
await addCustomFont(
context,
widget.controller,
widget.pickFiles,
onPicked: () {
if (mounted) setState(() => _adding = true);
},
);
} finally {
if (mounted) setState(() => _adding = false);
}
}
@override
Widget build(BuildContext context) => ListenableBuilder(
listenable: widget.controller,
builder: (context, _) => _group(context),
);
Widget _group(BuildContext context) {
final controller = widget.controller;
final options = <FontChoice>[...AppFont.values, ...controller.customFonts];
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 options) _materialRow(context, font),
// Its own node: a bare list tile would lend its label and tap to the
// section around it.
Semantics(
container: true,
child: SettingsRow(
key: settingsAddFontKey,
action: true,
enabled: !_adding,
leading: Icon(Icons.add),
title: const Text(addCustomFontText),
subtitle: _adding ? const Text(addingCustomFontText) : null,
onTap: () => unawaited(_add()),
),
),
],
);
return RadioGroup<FontChoice>(
groupValue: controller.selected,
onChanged: (font) {
if (font != null) unawaited(controller.select(font));
},
child: group,
);
}
Widget _materialRow(BuildContext context, FontChoice font) {
final tile = RadioListTile<FontChoice>(
key: settingsFontKey(font),
value: font,
title: _name(context, font),
subtitle: _status(context, font),
);
if (font is! CustomFont) return tile;
// The options button sits beside the tile rather than in its
// `secondary` slot: the tile merges its children's semantics, which
// would leave the button unreachable on its own.
return Row(
children: <Widget>[
Expanded(child: tile),
Padding(
padding: const EdgeInsetsDirectional.only(end: 8),
child: CustomFontMenu(fonts: widget.controller, font: 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, FontChoice font) {
final family = widget.controller.canPreview(font)
? font.family ?? _systemFamily(context)
: _systemFamily(context);
return Text(font.label, style: TextStyle(fontFamily: family));
}
/// Loading or failure for the chosen option; an added font otherwise
/// lists its styles.
Widget? _status(BuildContext context, FontChoice font) {
final controller = widget.controller;
if (controller.selected == font) {
if (controller.loadFailed == font) {
return Text(
fontLoadFailedText,
style: TextStyle(color: settingsErrorColor(context)),
);
}
if (controller.loading) return const Text(fontLoadingText);
}
return font is CustomFont ? Text(font.styles) : null;
}
}
/// The platform's own interface family (Roboto), independent of the font the
/// theme currently applies.
String? _systemFamily(BuildContext context) =>
Typography.material2021(
platform: Theme.of(context).platform,
).black.bodyLarge?.fontFamily;