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.
This commit is contained in:
2026-11-04 20:47:43 +00:00
parent 1d025bebf3
commit 081ae0957d
8 changed files with 361 additions and 32 deletions
@@ -15,13 +15,16 @@ 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.
/// 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,
) async {
FontFilePicker pick, {
VoidCallback? onPicked,
}) async {
Future<void> problem(String message) async {
if (context.mounted) {
await showFontProblem(context, addCustomFontFailedTitle, message);
@@ -33,15 +36,18 @@ Future<void> addCustomFont(
files = await pick();
} on CustomFontRejected catch (error) {
return problem(error.message);
} catch (_) {
return problem('The chosen files could not be read.');
} 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(
@@ -56,6 +62,8 @@ Future<void> addCustomFont(
await fonts.addCustom(draft, name);
} on CustomFontRejected catch (error) {
return problem(error.message);
} catch (error) {
return problem('The font could not be saved: $error');
}
}
@@ -76,9 +76,15 @@ class _SettingsFontSectionState extends State<SettingsFontSection> {
}
Future<void> _add() async {
setState(() => _adding = true);
try {
await addCustomFont(context, widget.controller, widget.pickFiles);
await addCustomFont(
context,
widget.controller,
widget.pickFiles,
onPicked: () {
if (mounted) setState(() => _adding = true);
},
);
} finally {
if (mounted) setState(() => _adding = false);
}