Merge: add your own fonts in Settings
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../ui/theme/app_font_controller.dart';
|
||||
import '../../ui/theme/custom_fonts/custom_font.dart';
|
||||
import '../../ui/theme/custom_fonts/custom_font_draft.dart';
|
||||
import '../../ui/theme/custom_fonts/font_file_picker.dart';
|
||||
import 'settings_custom_font_dialogs.dart';
|
||||
import 'settings_theme.dart';
|
||||
|
||||
const addCustomFontText = 'Add font…';
|
||||
const addingCustomFontText = 'Adding…';
|
||||
const renameCustomFontText = 'Rename';
|
||||
const removeCustomFontText = 'Remove';
|
||||
const customFontActionsTooltip = 'Font options';
|
||||
|
||||
/// Pick files, check them, ask for the family's name, then register, store
|
||||
/// and choose the font. Every refusal is said in a dialog; nothing is
|
||||
/// stored unless the whole family loaded.
|
||||
Future<void> addCustomFont(
|
||||
BuildContext context,
|
||||
AppFontController fonts,
|
||||
FontFilePicker pick,
|
||||
) async {
|
||||
Future<void> problem(String message) async {
|
||||
if (context.mounted) {
|
||||
await showFontProblem(context, addCustomFontFailedTitle, message);
|
||||
}
|
||||
}
|
||||
|
||||
final List<PickedFontFile>? files;
|
||||
try {
|
||||
files = await pick();
|
||||
} on CustomFontRejected catch (error) {
|
||||
return problem(error.message);
|
||||
} catch (_) {
|
||||
return problem('The chosen files could not be read.');
|
||||
}
|
||||
if (files == null || !context.mounted) return;
|
||||
final CustomFontDraft draft;
|
||||
try {
|
||||
draft = await fonts.inspect(files);
|
||||
} on CustomFontRejected catch (error) {
|
||||
return problem(error.message);
|
||||
}
|
||||
if (!context.mounted) return;
|
||||
final name = await showFontNameDialog(
|
||||
context,
|
||||
title: addCustomFontTitle,
|
||||
initial: draft.suggestedName,
|
||||
confirm: 'Add',
|
||||
detail: draft.faces.map((f) => '${f.styleName} · ${f.fileName}').join('\n'),
|
||||
);
|
||||
if (name == null) return;
|
||||
try {
|
||||
await fonts.addCustom(draft, name);
|
||||
} on CustomFontRejected catch (error) {
|
||||
return problem(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
enum _CustomFontAction { rename, remove }
|
||||
|
||||
/// The overflow control on an added font's row: Rename and Remove.
|
||||
class CustomFontMenu extends StatelessWidget {
|
||||
const CustomFontMenu({super.key, required this.fonts, required this.font});
|
||||
|
||||
final AppFontController fonts;
|
||||
final CustomFont font;
|
||||
|
||||
Future<void> _run(BuildContext context, _CustomFontAction action) async {
|
||||
switch (action) {
|
||||
case _CustomFontAction.rename:
|
||||
final name = await showFontNameDialog(
|
||||
context,
|
||||
title: renameCustomFontTitle,
|
||||
initial: font.name,
|
||||
confirm: renameCustomFontText,
|
||||
);
|
||||
if (name == null || name == font.name) return;
|
||||
try {
|
||||
await fonts.renameCustom(font, name);
|
||||
} on CustomFontRejected catch (error) {
|
||||
if (context.mounted) {
|
||||
await showFontProblem(
|
||||
context,
|
||||
renameCustomFontTitle,
|
||||
error.message,
|
||||
);
|
||||
}
|
||||
}
|
||||
case _CustomFontAction.remove:
|
||||
if (await confirmRemoveFont(context, font.name)) {
|
||||
await fonts.removeCustom(font);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _cupertinoSheet(BuildContext context) async {
|
||||
final action = await showCupertinoModalPopup<_CustomFontAction>(
|
||||
context: context,
|
||||
builder: (context) => CupertinoActionSheet(
|
||||
title: Text(font.name),
|
||||
actions: <Widget>[
|
||||
CupertinoActionSheetAction(
|
||||
onPressed: () => Navigator.pop(context, _CustomFontAction.rename),
|
||||
child: const Text(renameCustomFontText),
|
||||
),
|
||||
CupertinoActionSheetAction(
|
||||
isDestructiveAction: true,
|
||||
onPressed: () => Navigator.pop(context, _CustomFontAction.remove),
|
||||
child: const Text(removeCustomFontText),
|
||||
),
|
||||
],
|
||||
cancelButton: CupertinoActionSheetAction(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
),
|
||||
);
|
||||
if (action != null && context.mounted) await _run(context, action);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (usesCupertinoControls(context)) {
|
||||
return Semantics(
|
||||
label: customFontActionsTooltip,
|
||||
button: true,
|
||||
excludeSemantics: true,
|
||||
child: CupertinoButton(
|
||||
padding: EdgeInsets.zero,
|
||||
onPressed: () => _cupertinoSheet(context),
|
||||
child: const Icon(CupertinoIcons.ellipsis_circle),
|
||||
),
|
||||
);
|
||||
}
|
||||
return PopupMenuButton<_CustomFontAction>(
|
||||
tooltip: customFontActionsTooltip,
|
||||
onSelected: (action) => _run(context, action),
|
||||
itemBuilder: (context) => const <PopupMenuEntry<_CustomFontAction>>[
|
||||
PopupMenuItem(
|
||||
value: _CustomFontAction.rename,
|
||||
child: Text(renameCustomFontText),
|
||||
),
|
||||
PopupMenuItem(
|
||||
value: _CustomFontAction.remove,
|
||||
child: Text(removeCustomFontText),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../ui/theme/custom_fonts/custom_font_draft.dart';
|
||||
import 'settings_theme.dart';
|
||||
|
||||
const customFontNameLabel = 'Name';
|
||||
const addCustomFontTitle = 'Add font';
|
||||
const renameCustomFontTitle = 'Rename font';
|
||||
const addCustomFontFailedTitle = 'Could not add font';
|
||||
|
||||
/// Asks for a font family's name, prefilled with [initial]; null when
|
||||
/// cancelled. [detail] lists what is being added.
|
||||
Future<String?> showFontNameDialog(
|
||||
BuildContext context, {
|
||||
required String title,
|
||||
required String initial,
|
||||
required String confirm,
|
||||
String? detail,
|
||||
}) {
|
||||
final dialog = _FontNameDialog(
|
||||
title: title,
|
||||
initial: initial,
|
||||
confirm: confirm,
|
||||
detail: detail,
|
||||
);
|
||||
return usesCupertinoControls(context)
|
||||
? showCupertinoDialog<String>(context: context, builder: (_) => dialog)
|
||||
: showDialog<String>(context: context, builder: (_) => dialog);
|
||||
}
|
||||
|
||||
class _FontNameDialog extends StatefulWidget {
|
||||
const _FontNameDialog({
|
||||
required this.title,
|
||||
required this.initial,
|
||||
required this.confirm,
|
||||
this.detail,
|
||||
});
|
||||
|
||||
final String title;
|
||||
final String initial;
|
||||
final String confirm;
|
||||
final String? detail;
|
||||
|
||||
@override
|
||||
State<_FontNameDialog> createState() => _FontNameDialogState();
|
||||
}
|
||||
|
||||
class _FontNameDialogState extends State<_FontNameDialog> {
|
||||
late final TextEditingController _name = TextEditingController(
|
||||
text: widget.initial,
|
||||
);
|
||||
String? _problem;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_name.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _submit() {
|
||||
final problem = CustomFontLimits.nameProblem(_name.text);
|
||||
if (problem != null) {
|
||||
setState(() => _problem = problem);
|
||||
return;
|
||||
}
|
||||
Navigator.pop(context, _name.text.trim());
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cupertino = usesCupertinoControls(context);
|
||||
final field = cupertino
|
||||
? CupertinoTextField(
|
||||
controller: _name,
|
||||
autofocus: true,
|
||||
placeholder: customFontNameLabel,
|
||||
maxLength: CustomFontLimits.maxNameLength,
|
||||
onSubmitted: (_) => _submit(),
|
||||
)
|
||||
: TextField(
|
||||
controller: _name,
|
||||
autofocus: true,
|
||||
maxLength: CustomFontLimits.maxNameLength,
|
||||
decoration: InputDecoration(
|
||||
labelText: customFontNameLabel,
|
||||
errorText: _problem,
|
||||
),
|
||||
onSubmitted: (_) => _submit(),
|
||||
);
|
||||
final content = Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: <Widget>[
|
||||
if (widget.detail != null) ...<Widget>[
|
||||
Text(widget.detail!),
|
||||
const SizedBox(height: 12),
|
||||
],
|
||||
field,
|
||||
if (cupertino && _problem != null)
|
||||
Text(_problem!, style: TextStyle(color: settingsErrorColor(context))),
|
||||
],
|
||||
);
|
||||
if (cupertino) {
|
||||
return CupertinoAlertDialog(
|
||||
title: Text(widget.title),
|
||||
content: Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: content,
|
||||
),
|
||||
actions: <Widget>[
|
||||
CupertinoDialogAction(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
CupertinoDialogAction(
|
||||
isDefaultAction: true,
|
||||
onPressed: _submit,
|
||||
child: Text(widget.confirm),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
return AlertDialog(
|
||||
title: Text(widget.title),
|
||||
content: SingleChildScrollView(child: content),
|
||||
actions: <Widget>[
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
TextButton(onPressed: _submit, child: Text(widget.confirm)),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Confirms deleting an added font; true when the user agrees.
|
||||
Future<bool> confirmRemoveFont(BuildContext context, String name) async {
|
||||
final title = Text('Remove $name?');
|
||||
const body = Text('Its files are deleted from this device.');
|
||||
final confirmed = usesCupertinoControls(context)
|
||||
? await showCupertinoDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => CupertinoAlertDialog(
|
||||
title: title,
|
||||
content: body,
|
||||
actions: <Widget>[
|
||||
CupertinoDialogAction(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
CupertinoDialogAction(
|
||||
isDestructiveAction: true,
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('Remove'),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: title,
|
||||
content: body,
|
||||
actions: <Widget>[
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('Remove'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
return confirmed ?? false;
|
||||
}
|
||||
|
||||
/// Says why a font could not be added or changed.
|
||||
Future<void> showFontProblem(
|
||||
BuildContext context,
|
||||
String title,
|
||||
String message,
|
||||
) => usesCupertinoControls(context)
|
||||
? showCupertinoDialog<void>(
|
||||
context: context,
|
||||
builder: (context) => CupertinoAlertDialog(
|
||||
title: Text(title),
|
||||
content: Text(message),
|
||||
actions: <Widget>[
|
||||
CupertinoDialogAction(
|
||||
isDefaultAction: true,
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('OK'),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: showDialog<void>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: Text(title),
|
||||
content: Text(message),
|
||||
actions: <Widget>[
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('OK'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
@@ -5,49 +5,82 @@ 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.';
|
||||
'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(AppFont font) =>
|
||||
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. Material rows are radio tiles, Cupertino rows carry the checkmark.
|
||||
/// face, the user's added fonts below the bundled ones, then "Add font…".
|
||||
/// 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});
|
||||
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();
|
||||
_preview();
|
||||
unawaited(_preview());
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(SettingsFontSection oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.controller != widget.controller) _preview();
|
||||
if (oldWidget.controller != widget.controller) unawaited(_preview());
|
||||
}
|
||||
|
||||
/// Only the regular weight of each option, only once Settings is shown.
|
||||
void _preview() {
|
||||
Future<void> _preview() async {
|
||||
final controller = widget.controller;
|
||||
for (final font in AppFont.values) {
|
||||
unawaited(widget.controller.preview(font));
|
||||
unawaited(controller.preview(font));
|
||||
}
|
||||
await controller.loadCustomFonts();
|
||||
for (final font in controller.customFonts) {
|
||||
unawaited(controller.preview(font));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _add() async {
|
||||
setState(() => _adding = true);
|
||||
try {
|
||||
await addCustomFont(context, widget.controller, widget.pickFiles);
|
||||
} finally {
|
||||
if (mounted) setState(() => _adding = false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,6 +93,7 @@ class _SettingsFontSectionState extends State<SettingsFontSection> {
|
||||
Widget _group(BuildContext context) {
|
||||
final controller = widget.controller;
|
||||
final cupertino = usesCupertinoControls(context);
|
||||
final options = <FontChoice>[...AppFont.values, ...controller.customFonts];
|
||||
final group = SettingsGroup(
|
||||
title: fontSectionTitle,
|
||||
footer: Column(
|
||||
@@ -74,19 +108,28 @@ class _SettingsFontSectionState extends State<SettingsFontSection> {
|
||||
],
|
||||
),
|
||||
children: <Widget>[
|
||||
for (final font in AppFont.values)
|
||||
for (final font in options)
|
||||
cupertino
|
||||
? _cupertinoRow(context, font)
|
||||
: RadioListTile<AppFont>(
|
||||
key: settingsFontKey(font),
|
||||
value: font,
|
||||
title: _name(context, font),
|
||||
subtitle: _status(context, font),
|
||||
),
|
||||
: _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(cupertino ? CupertinoIcons.add : Icons.add),
|
||||
title: const Text(addCustomFontText),
|
||||
subtitle: _adding ? const Text(addingCustomFontText) : null,
|
||||
onTap: () => unawaited(_add()),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
if (cupertino) return group;
|
||||
return RadioGroup<AppFont>(
|
||||
return RadioGroup<FontChoice>(
|
||||
groupValue: controller.selected,
|
||||
onChanged: (font) {
|
||||
if (font != null) unawaited(controller.select(font));
|
||||
@@ -95,38 +138,84 @@ class _SettingsFontSectionState extends State<SettingsFontSection> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _cupertinoRow(BuildContext context, AppFont font) => SettingsRow(
|
||||
key: settingsFontKey(font),
|
||||
title: _name(context, font),
|
||||
subtitle: _status(context, font),
|
||||
trailing: widget.controller.selected == font
|
||||
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),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _cupertinoRow(BuildContext context, FontChoice font) {
|
||||
final check = widget.controller.selected == font
|
||||
? Icon(
|
||||
CupertinoIcons.check_mark,
|
||||
color: CupertinoTheme.of(context).primaryColor,
|
||||
)
|
||||
: null,
|
||||
onTap: () => unawaited(widget.controller.select(font)),
|
||||
);
|
||||
: null;
|
||||
// One node per option, announced as selected.
|
||||
final row = Semantics(
|
||||
container: true,
|
||||
selected: check != null,
|
||||
child: SettingsRow(
|
||||
key: settingsFontKey(font),
|
||||
title: _name(context, font),
|
||||
subtitle: _status(context, font),
|
||||
trailing: check,
|
||||
onTap: () => unawaited(widget.controller.select(font)),
|
||||
),
|
||||
);
|
||||
if (font is! CustomFont) return row;
|
||||
// Beside the row, not in it: the list tile merges its children's
|
||||
// semantics (see [_materialRow]).
|
||||
return Row(
|
||||
children: <Widget>[
|
||||
Expanded(child: row),
|
||||
Padding(
|
||||
padding: const EdgeInsetsDirectional.only(end: 12),
|
||||
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, AppFont font) {
|
||||
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));
|
||||
}
|
||||
|
||||
Widget? _status(BuildContext context, AppFont font) {
|
||||
/// 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) return null;
|
||||
if (controller.loadFailed == font) {
|
||||
return Text(
|
||||
fontLoadFailedText,
|
||||
style: TextStyle(color: settingsErrorColor(context)),
|
||||
);
|
||||
if (controller.selected == font) {
|
||||
if (controller.loadFailed == font) {
|
||||
return Text(
|
||||
fontLoadFailedText,
|
||||
style: TextStyle(color: settingsErrorColor(context)),
|
||||
);
|
||||
}
|
||||
if (controller.loading) return const Text(fontLoadingText);
|
||||
}
|
||||
return controller.loading ? const Text(fontLoadingText) : null;
|
||||
return font is CustomFont ? Text(font.styles) : null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user