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.
214 lines
6.1 KiB
Dart
214 lines
6.1 KiB
Dart
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'),
|
|
),
|
|
],
|
|
),
|
|
);
|