Merge: Add font works with a real file dialog and always reports failures
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user