Files
pipelines/app/lib/features/settings/settings_custom_font_actions.dart
alan a7c41063fa Remove Cupertino branches from Settings
Keep the Material-only settings implementation and its focused tests while preserving public cupertino parameters for later cleanup. This removes unreachable platform-specific widgets and test variants without changing the Material path.
2026-11-11 05:05:57 +00:00

123 lines
3.8 KiB
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';
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);
}
}
}
@override
Widget build(BuildContext context) {
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),
),
],
);
}
}