Files
pipelines/app/lib/ui/theme/app_design_tokens.dart
T
alan 2b31d6dc3a Merge branch 'main' into worktree-agent-abb9404cc062f4b30
# Conflicts:
#	app/lib/application/app.dart
#	app/lib/application/detached/detached_app.dart
#	app/lib/application/pipelines_root.dart
#	app/lib/ui/theme/app_design.dart
#	app/test/ui/theme/compact_switch_test.dart
#	app/test/ui/theme/high_contrast_design_test.dart
2026-12-04 01:19:44 +00:00

364 lines
12 KiB
Dart

import 'package:flutter/material.dart';
import 'app_design.dart';
import 'app_design_styles.dart';
import 'glass_style.dart';
export 'app_design_styles.dart';
export 'glass_border.dart';
export 'glass_style.dart';
/// The muted ANSI-style accents a design names for status and roles. The
/// Classic design leaves them to its colour scheme; Terminal sets them, and
/// Paper sets ink-toned ones.
@immutable
class AnsiAccents {
const AnsiAccents({
required this.green,
required this.cyan,
required this.yellow,
required this.red,
required this.magenta,
required this.blue,
});
final Color green;
final Color cyan;
final Color yellow;
final Color red;
final Color magenta;
final Color blue;
@override
bool operator ==(Object other) =>
other is AnsiAccents &&
other.green == green &&
other.cyan == cyan &&
other.yellow == yellow &&
other.red == red &&
other.magenta == magenta &&
other.blue == blue;
@override
int get hashCode => Object.hash(green, cyan, yellow, red, magenta, blue);
}
/// The design-specific facts widgets read instead of hard-coding one look:
/// corner shapes, whether surfaces are flat, frame colour, how the chat
/// transcript, the composer and the open list row are drawn, and the reading
/// typography. Every theme an [AppDesign] builds carries one; a theme without
/// one (a test's bare `ThemeData`) reads as [standard].
///
/// Adding a design: every field has a default that keeps the Classic look,
/// so a new design sets only what it changes. Widgets branch on the named
/// styles ([transcript], [composer], [selectedRow]) rather than on [design],
/// so two designs can share a behaviour.
@immutable
class AppDesignTokens extends ThemeExtension<AppDesignTokens> {
const AppDesignTokens({
required this.design,
this.radius,
this.flat = false,
this.frame,
this.panel,
this.ansi,
this.promptGlyph = '>',
this.transcript = TranscriptStyle.bubbles,
this.composer = ComposerStyle.pill,
this.selectedRow = SelectedRowStyle.tonal,
this.reading = const ReadingType(),
this.chatColorSaturation = 1,
this.monochrome,
this.screen,
this.stroke = 1,
this.ringStroke = 2,
this.focusRing,
this.largeTargets = false,
this.minContrast,
this.windowRadius,
this.snapSwitch = false,
this.glass,
this.density = DesignDensity.comfortable,
});
/// The Classic design: every widget keeps its own shapes.
static const AppDesignTokens standard = AppDesignTokens(
design: AppDesign.standard,
);
final AppDesign design;
/// The corner radius of framed surfaces (cards, pills, the composer,
/// bubbles, thumbnails, badges); null keeps each widget's own shape.
final double? radius;
/// Whether surfaces are flat: no frosted glass, blur, highlight or shadow.
final bool flat;
/// The 1px frame colour around flat surfaces; null keeps each widget's own.
final Color? frame;
/// The fill of a flat design's own panels (settings groups, floating bars,
/// cards a widget draws itself); null keeps `surfaceContainerLow`.
final Color? panel;
/// The design's ANSI-style accents; null for a design that uses its colour
/// scheme's roles.
final AnsiAccents? ansi;
/// The glyph before the operator's prompt line and the composer's field.
final String promptGlyph;
/// How the chat transcript draws the operator's and the agent's turns.
final TranscriptStyle transcript;
/// How the chat composer frames its writing area.
final ComposerStyle composer;
/// How a list marks its open row.
final SelectedRowStyle selectedRow;
/// The face, line height and measure of reading text (chat, documents).
final ReadingType reading;
/// How much of a chat colour's saturation the transcript keeps: 1 for the
/// palette as is, lower to quieten it towards ink.
final double chatColorSaturation;
/// The one hue of a monochrome design (Retro CRT's phosphor); null for a
/// design with colour. Under it chat colours become brightness steps of
/// this hue, and a status that only colour told apart (a failed or running
/// tool line) gets its own glyph too.
final Color? monochrome;
/// A static pattern painted once over the whole app (Retro CRT's
/// scanlines); null paints nothing.
final ScreenPattern? screen;
/// The width of the design's frames: [frameSide], flat panels, the
/// composer pill and the floating bar pieces (High contrast's 2px).
final double stroke;
/// The width of the usage ring around the composer's send button.
final double ringStroke;
/// A ring the design draws around whatever has keyboard focus, over every
/// control's own focus overlay; null leaves focus to each control.
final FocusRingStyle? focusRing;
/// Whether controls keep touch-sized (48px) targets on a desktop too,
/// rather than the pointer's compact ones.
final bool largeTargets;
/// The least contrast a design's marks keep against the page: chat
/// colours are darkened or lightened until they reach it, a chat's colour
/// marks with bars and outlines rather than translucent tints, and faint
/// tracks (the usage ring's) are drawn solid. Null keeps the palette and
/// its tints.
final double? minContrast;
/// The corner radius of floating windows; null keeps the windows' own.
final double? windowRadius;
/// Whether a live switch into or out of this design snaps rather than
/// blending: High contrast's half-blended frames would be low-contrast
/// grey on grey, and its larger component sizes have no value in other
/// designs to blend from ([AppDesignController.themeAnimationDuration]).
final bool snapSwitch;
/// A glass design's translucent surfaces: the backdrop behind the app, the
/// panes' and bars' tints, the inset cards' fill and edge, and how its
/// corners grow. Null for an opaque design (every surface keeps its fill).
final GlassStyle? glass;
/// How tightly the interface is packed: spacing, icon and avatar sizes,
/// the rail and the window title bar ([DesignDensity.compact] for Compact).
final DesignDensity density;
/// Whether the transcript draws prompt and tool lines rather than bubbles.
bool get terminal => transcript == TranscriptStyle.prompt;
/// The tokens of the theme at [context]; [standard] when it has none.
static AppDesignTokens of(BuildContext context) =>
Theme.of(context).extension<AppDesignTokens>() ?? standard;
/// The same design's theme in the opposite brightness, in [theme]'s
/// interface font: what an inverted surface (High contrast's open row)
/// draws its content under. Cached per theme.
static ThemeData inverseTheme(ThemeData theme) => _inverse[theme] ??= () {
final design = (theme.extension<AppDesignTokens>() ?? standard).design;
final family = theme.textTheme.bodyMedium?.fontFamily;
return theme.brightness == Brightness.dark
? design.light(fontFamily: family)
: design.dark(fontFamily: family);
}();
static final Expando<ThemeData> _inverse = Expando('inverse theme');
/// [designed] as this design draws it: the widget's own shape, or a
/// rectangle with [radius] corners.
/// A glass design keeps the widget's shape with its corners grown
/// ([GlassStyle.corner]); a stadium or circle stays one.
OutlinedBorder shape(OutlinedBorder designed) {
final r = radius;
if (r != null) {
return RoundedRectangleBorder(borderRadius: BorderRadius.circular(r));
}
final glass = this.glass;
if (glass != null && designed is RoundedRectangleBorder) {
final corners = designed.borderRadius;
if (corners is BorderRadius) {
return designed.copyWith(borderRadius: glass.corners(corners));
}
}
return designed;
}
/// [designed] corners, or [radius] corners under a design that sets it,
/// or (glass) [designed] with each corner grown by [GlassStyle.corner].
BorderRadius corners(BorderRadius designed) {
final r = radius;
if (r != null) return BorderRadius.circular(r);
return glass?.corners(designed) ?? designed;
}
/// [designed] corners, or [radius] corners under a dense design: for the
/// surfaces whose own corners the comfortable designs keep (floating
/// windows, search thumbnails), so only a dense design rounds them alike.
BorderRadius denseCorners(BorderRadius designed) =>
density.dense ? corners(designed) : designed;
/// [designed] (a widget's own fill) or, under a flat design, its [panel].
Color panelFill(ColorScheme scheme, Color designed) =>
flat ? panel ?? scheme.surfaceContainerLow : designed;
/// [fill] made opaque for a piece nothing may show through (a find bar
/// over the transcript, a terminal): under a glass design, whose container
/// colours are translucent, laid over its opaque [GlassStyle.solid];
/// unchanged elsewhere.
Color solidFill(Color fill) {
final glass = this.glass;
return glass == null ? fill : Color.alphaBlend(fill, glass.solid);
}
/// The hairline a flat design frames its panels with; null keeps none.
BorderSide? get frameSide {
final color = frame;
return color == null ? null : BorderSide(color: color, width: stroke);
}
@override
AppDesignTokens copyWith({
AppDesign? design,
double? radius,
bool? flat,
Color? frame,
Color? panel,
AnsiAccents? ansi,
String? promptGlyph,
TranscriptStyle? transcript,
ComposerStyle? composer,
SelectedRowStyle? selectedRow,
ReadingType? reading,
double? chatColorSaturation,
Color? monochrome,
ScreenPattern? screen,
double? stroke,
double? ringStroke,
FocusRingStyle? focusRing,
bool? largeTargets,
double? minContrast,
double? windowRadius,
bool? snapSwitch,
GlassStyle? glass,
DesignDensity? density,
}) => AppDesignTokens(
design: design ?? this.design,
radius: radius ?? this.radius,
flat: flat ?? this.flat,
frame: frame ?? this.frame,
panel: panel ?? this.panel,
ansi: ansi ?? this.ansi,
promptGlyph: promptGlyph ?? this.promptGlyph,
transcript: transcript ?? this.transcript,
composer: composer ?? this.composer,
selectedRow: selectedRow ?? this.selectedRow,
reading: reading ?? this.reading,
chatColorSaturation: chatColorSaturation ?? this.chatColorSaturation,
monochrome: monochrome ?? this.monochrome,
screen: screen ?? this.screen,
stroke: stroke ?? this.stroke,
ringStroke: ringStroke ?? this.ringStroke,
focusRing: focusRing ?? this.focusRing,
largeTargets: largeTargets ?? this.largeTargets,
minContrast: minContrast ?? this.minContrast,
windowRadius: windowRadius ?? this.windowRadius,
snapSwitch: snapSwitch ?? this.snapSwitch,
glass: glass ?? this.glass,
density: density ?? this.density,
);
// Value equality keeps a rebuilt theme equal to the one it replaces, so
// Theme dependents do not rebuild and MaterialApp starts no theme
// animation when a host rebuilds for another reason.
@override
bool operator ==(Object other) =>
other is AppDesignTokens &&
other.design == design &&
other.radius == radius &&
other.flat == flat &&
other.frame == frame &&
other.panel == panel &&
other.ansi == ansi &&
other.promptGlyph == promptGlyph &&
other.transcript == transcript &&
other.composer == composer &&
other.selectedRow == selectedRow &&
other.reading == reading &&
other.chatColorSaturation == chatColorSaturation &&
other.monochrome == monochrome &&
other.screen == screen &&
other.stroke == stroke &&
other.ringStroke == ringStroke &&
other.focusRing == focusRing &&
other.largeTargets == largeTargets &&
other.minContrast == minContrast &&
other.windowRadius == windowRadius &&
other.snapSwitch == snapSwitch &&
other.glass == glass &&
other.density == density;
@override
int get hashCode => Object.hashAll([
design,
radius,
flat,
frame,
panel,
ansi,
promptGlyph,
transcript,
composer,
selectedRow,
reading,
chatColorSaturation,
monochrome,
screen,
stroke,
ringStroke,
focusRing,
largeTargets,
minContrast,
windowRadius,
snapSwitch,
glass,
density,
]);
/// Designs switch whole: a theme animation snaps halfway rather than
/// blending two designs' shapes.
@override
AppDesignTokens lerp(AppDesignTokens? other, double t) =>
other == null || t < 0.5 ? this : other;
}