Files
pipelines/app/lib/ui/theme/app_font_store.dart
alan c24e7f40b4 Settings: add your own fonts (.ttf/.otf) per device
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.
2026-11-04 18:45:01 +00:00

61 lines
1.7 KiB
Dart

import 'package:shared_preferences/shared_preferences.dart';
/// The device's font choice. It lives in this device's (or browser's) own
/// preferences and is never sent to a server, so a phone and a browser signed
/// in to the same server keep their own choice.
const String appFontKey = 'pipelines.next.font.v1';
/// The persistence seam for the font choice: the chosen option's stable id
/// (a bundled `AppFont.id` or an added `CustomFont.id`).
abstract interface class AppFontStore {
/// The stored id; null when nothing is stored or it cannot be read.
Future<String?> load();
/// Whether the choice is actually stored.
Future<bool> save(String id);
}
/// `shared_preferences` storage. An unavailable or unreadable store means the
/// default; a failed save returns false rather than throwing.
class SharedPreferencesAppFontStore implements AppFontStore {
const SharedPreferencesAppFontStore();
@override
Future<String?> load() async {
try {
final preferences = await SharedPreferences.getInstance();
return preferences.getString(appFontKey);
} catch (_) {
return null;
}
}
@override
Future<bool> save(String id) async {
try {
final preferences = await SharedPreferences.getInstance();
return await preferences.setString(appFontKey, id);
} catch (_) {
return false;
}
}
}
/// An in-memory store for tests and fixtures.
class MemoryAppFontStore implements AppFontStore {
MemoryAppFontStore([this.id]);
String? id;
bool failSaves = false;
@override
Future<String?> load() async => id;
@override
Future<bool> save(String id) async {
if (failSaves) return false;
this.id = id;
return true;
}
}