Files
pipelines/app/lib/ui/theme/app_design.dart
T

188 lines
6.8 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 'compact_theme.dart';
import 'paper_theme.dart';
import 'terminal_theme.dart';
/// 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 = {};
/// 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:
/// 1. add a value here with a stable [id], a [label] and a one-line
/// [description] (the Settings picker lists [AppDesign.values]);
/// 2. write its theme builder (`<name>_theme.dart` beside this file, with a
/// palette and, when it needs one, its own typography) and return it from
/// [build] ([light] and [dark] cache it); keep the theme value-equal when
/// rebuilt (no fresh `Decoration`s or extensions without `==`);
/// 3. give the theme an [AppDesignTokens] extension: set only the tokens it
/// changes (corners, flat surfaces, frame, panel fill, transcript,
/// composer and selected-row styles, reading type); every token defaults
/// to the Classic look;
/// 4. give it a [swatch] for the picker and say whether it [usesInterfaceFont].
/// 5. run the design budget (app/tool/perf/README.md) and keep it passing.
/// Widgets read the tokens, so most surfaces follow without per-widget code.
enum AppDesign {
/// The app's original Material look. Its stored id stays `default` (the
/// id it had when it was the default); the picker calls it Classic so it
/// does not read as "whatever the app starts with".
standard('default', 'Classic', 'The app’s original 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.',
),
/// A calm reading look: a warm paper page, a serif for reading, ink
/// colours, hairline rules and the chat set as a typeset page.
paper(
'paper',
'Paper',
'Warm paper, serif reading text and ink colours; calm for long reads.',
),
/// The Classic look packed tight for a big monitor: smaller type, thin
/// rows, 18px icons, an icon-only rail and modest corners.
compact(
'compact',
'Compact',
'Classic’s Material look, dense: small type, thin rows, more on screen.',
);
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 a device has before it chooses one: Terminal.
static const AppDesign defaultDesign = terminal;
/// The design stored as [id], or [defaultDesign] for an unknown or missing
/// id. A stored choice (Classic's `default` included) is kept.
static AppDesign fromId(String? id) {
for (final design in values) {
if (design.id == id) return design;
}
return defaultDesign;
}
/// 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),
(paper, _) => paperTheme(brightness, fontFamily: fontFamily),
(compact, _) => compactTheme(brightness, fontFamily: fontFamily),
};
/// Whether this design packs the interface densely
/// ([DesignDensity.dense]); its theme hosts switch to and from it without
/// a blend ([AppDesignController.themeAnimationDuration]).
bool get dense =>
light().extension<AppDesignTokens>()?.density.dense ?? false;
/// Whether the device's interface font applies under this design (Paper
/// keeps it for its interface text and sets reading text in its serif).
bool get usesInterfaceFont => this != terminal;
/// The picker's preview: the design's page, ink and accent in [brightness],
/// without building its theme.
DesignSwatch swatch(Brightness brightness) {
final dark = brightness == Brightness.dark;
return switch (this) {
standard || compact =>
dark
? const DesignSwatch(
Color(0xFF141416),
Color(0xFFE6E0E9),
Color(0xFFCFBDFE),
)
: const DesignSwatch(
Color(0xFFFDF7FF),
Color(0xFF1D1B20),
Color(0xFF65558F),
),
terminal =>
dark
? const DesignSwatch(
Color(0xFF0B0B0B),
Color(0xFFD7D7D2),
Color(0xFF8CC38F),
)
: const DesignSwatch(
Color(0xFFF7F6F1),
Color(0xFF1E1E1C),
Color(0xFF2F7434),
),
paper =>
dark
? const DesignSwatch(
Color(0xFF1E1B17),
Color(0xFFE9E1D3),
Color(0xFF9DB4DC),
)
: const DesignSwatch(
Color(0xFFF8F4EB),
Color(0xFF221E19),
Color(0xFF2C4F86),
),
};
}
/// How many lines of ink the picker's preview draws: Compact's packs more
/// into the same page.
int get swatchLines => this == compact ? 4 : 2;
}
/// A design's three defining colours, for the picker's small preview.
@immutable
class DesignSwatch {
const DesignSwatch(this.page, this.ink, this.accent);
/// The page (scaffold) colour.
final Color page;
/// The main text colour.
final Color ink;
/// The accent (primary) colour.
final Color accent;
}