Files
pipelines/app/lib/ui/theme/app_design.dart
T
alan bc00d321d9 Designs: cache themes, value-equal tokens, and a per-design perf budget
A rebuilt Terminal theme never equalled the last one (AppDesignTokens,
AnsiAccents and the tab indicator compared by identity), so any host
rebuild (a detached window's title, a font notification) rebuilt every
Theme.of dependent and ran MaterialApp's 200 ms theme animation. Themes
are now cached per (design, brightness, font) and compare by value;
chat colour seeded schemes are cached too.

test/perf/design_bench_test.dart and tool/perf/design_bench.mjs check
every registered AppDesign against Default (tool/perf/README.md).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 19:43:10 +00:00

80 lines
2.9 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/material.dart';
import 'app_design_tokens.dart';
import 'app_theme.dart';
import 'terminal_theme.dart';
/// An app-wide design: how every surface is shaped, typed and coloured, not
/// only a colour. Each design builds a light and a dark [ThemeData] carrying
/// its [AppDesignTokens], so widgets that draw design-specific shapes read the
/// tokens instead of hard-coding one look.
///
/// Adding a design means one more value here and its theme builder; the
/// Settings picker lists [AppDesign.values].
/// Every theme a design has built, by design, brightness and interface font.
/// ThemeData is immutable; a handful of fonts and two brightnesses per design
/// keep this small.
final Map<(AppDesign, Brightness, String?), ThemeData> _themes = {};
enum AppDesign {
/// The app's own Material look.
standard('default', 'Default', 'The app’s Material look.'),
/// A terminal-style look: monospace text, near-black pages, thin frames
/// and a transcript drawn as prompt and tool lines.
terminal(
'terminal',
'Terminal',
'Monospace, flat frames and prompt-style chat, like a CLI agent.',
);
const AppDesign(this.id, this.label, this.description);
/// The stored value; stable across releases and renames.
final String id;
/// The picker's name for the design.
final String label;
/// The picker's one-line summary of the design.
final String description;
/// The design stored as [id], or [standard] for an unknown or missing id.
static AppDesign fromId(String? id) {
for (final design in values) {
if (design.id == id) return design;
}
return standard;
}
/// The design's light theme. [fontFamily] is the device's interface font;
/// a design with its own family (Terminal's monospace) keeps that instead.
/// Cached: the same object for the same design and font, so a host that
/// rebuilds for another reason hands its subtree the same theme.
ThemeData light({String? fontFamily}) =>
_cached(Brightness.light, fontFamily);
/// The design's dark theme; see [light].
ThemeData dark({String? fontFamily}) => _cached(Brightness.dark, fontFamily);
ThemeData _cached(Brightness brightness, String? fontFamily) {
final family = usesInterfaceFont ? fontFamily : null;
return _themes[(this, brightness, family)] ??= build(
brightness,
fontFamily: family,
);
}
/// Builds the design's theme for [brightness] from scratch, uncached;
/// [light] and [dark] keep the result.
ThemeData build(Brightness brightness, {String? fontFamily}) =>
switch ((this, brightness)) {
(standard, Brightness.light) => appLightTheme(fontFamily: fontFamily),
(standard, Brightness.dark) => appDarkTheme(fontFamily: fontFamily),
(terminal, _) => terminalTheme(brightness),
};
/// Whether the device's interface font applies under this design.
bool get usesInterfaceFont => this == standard;
}