c24e7f40b4
Add font… picks one or more files, checks them with a bounded sfnt reader (name table family, OS/2 weight), registers them with FontLoader before storing, and chooses the new family. Added fonts list below the bundled ones with Rename and Remove; removing the chosen one falls back to System default. Bytes live in the app support directory natively and in IndexedDB on the web (localStorage is too small); a shared_preferences index names them. Limits: 10 MB per file, 40 MB total. The capture kit gains a files= step.
154 lines
4.8 KiB
Dart
154 lines
4.8 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 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),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|