Files
pipelines/app/lib/features/settings/settings_custom_font_actions.dart
T
alan 081ae0957d Add font: own web file chooser, so a picked font is never dropped silently
file_picker's web picker treated the window regaining focus as a cancel when
the files arrived more than 500 ms later, so a real dialog could return an
empty pick and Add font did nothing. The web now uses its own file input that
completes only on change or cancel; mobile browsers get no accept filter.
Picker, check and save failures are shown in a dialog, and the row is busy
only after files were chosen.
2026-11-04 20:47:43 +00:00

162 lines
5.2 KiB
Dart

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 or failure after the picker opens is
/// said in a dialog; nothing is stored unless the whole family loaded.
/// [onPicked] runs once files were chosen, not while the picker is open, so
/// a browser that never reports a cancel cannot leave the action disabled.
Future<void> addCustomFont(
BuildContext context,
AppFontController fonts,
FontFilePicker pick, {
VoidCallback? onPicked,
}) 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 (error) {
return problem('The file chooser failed: $error');
}
if (files == null || !context.mounted) return;
onPicked?.call();
final CustomFontDraft draft;
try {
draft = await fonts.inspect(files);
} on CustomFontRejected catch (error) {
return problem(error.message);
} catch (error) {
return problem('The chosen files could not be checked: $error');
}
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);
} catch (error) {
return problem('The font could not be saved: $error');
}
}
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),
),
],
);
}
}