Files
pipelines/app/lib/features/chat/shell/chat_shell_terminal_theme.dart
T
alan b59ab5fad7 Terminal round 3: readable palettes, panel and card sizing, rail, tabs
- Colours: terminal background is the theme's lowest surface; the 16 ANSI
  colours come from a chosen palette (Theme colours, Classic, Calm, Vivid;
  Settings > Terminal > Colours, saved as `palette`) adjusted in lightness
  to the background: colours 4.5:1, bright ones lighter than normal,
  white light and black dark in both themes so white-on-red/blue/magenta
  and black-on-green/yellow/cyan (ls setuid, setgid, sticky) keep 3:1.
  Selection, find matches and the current match keep text at 4.5:1.
- Bottom panel opens at 40% of the window, at least 16 rows, until
  dragged; the dragged height is remembered (old saved 300 px default
  reads as never resized).
- Navigation rail scrolls its tabs (and below 240 px Settings too), so
  items never overlap under a tall panel.
- Chat command cards: Expand / Collapse grows a live terminal to 60% of
  the window (finished output only to its rows); collapsed stays 12 rows.
- Terminal tabs: ellipsised titles (220 px) with the whole title in the
  tooltip, selected tab filled, outlined and bold, close buttons are
  IconButtons (48 px touch, 40 px desktop).
- Capture fixture: ?terminalColors=1 and ?terminalPalette=.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 02:25:16 +00:00

122 lines
4.5 KiB
Dart

import 'dart:async';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:xterm/xterm.dart';
import '../../../ui/text/monospace.dart';
import '../../../ui/text/terminal_symbols_font.dart';
import '../../terminal/terminal_layout.dart';
import '../conversation/chat_card_styles.dart';
import 'chat_shell_terminal_palette.dart';
/// How strongly the selection and find matches tint the cells they cover
/// (painted over the text, so the text shows through).
const double chatShellTerminalSelectionAlpha = 0.35;
const double chatShellTerminalMatchAlpha = 0.2;
const double chatShellTerminalCurrentMatchAlpha = 0.4;
/// The terminal's background: the theme's lowest surface, the most
/// contrast its colours can have in either brightness.
Color chatShellTerminalBackground(BuildContext context) =>
Theme.of(context).colorScheme.surfaceContainerLowest;
/// The terminal's colours from the host theme: its lowest surface as
/// background, the body text colour as foreground, the accent as cursor and
/// selection, and [palette]'s sixteen ANSI colours adjusted to that
/// background (see [chatShellTerminalPalette]).
TerminalTheme chatShellTerminalTheme(
BuildContext context, {
TerminalPalette palette = TerminalPalette.theme,
}) {
final scheme = Theme.of(context).colorScheme;
final background = chatShellTerminalBackground(context);
final ansi = chatShellTerminalPalette(palette, background);
final matches = chatShellTerminalMatchColours(context);
return TerminalTheme(
cursor: scheme.primary,
selection: scheme.primary.withValues(
alpha: chatShellTerminalSelectionAlpha,
),
foreground: scheme.onSurface,
background: background,
black: ansi[0],
red: ansi[1],
green: ansi[2],
yellow: ansi[3],
blue: ansi[4],
magenta: ansi[5],
cyan: ansi[6],
white: ansi[7],
brightBlack: ansi[8],
brightRed: ansi[9],
brightGreen: ansi[10],
brightYellow: ansi[11],
brightBlue: ansi[12],
brightMagenta: ansi[13],
brightCyan: ansi[14],
brightWhite: ansi[15],
searchHitBackground: matches.hit,
searchHitBackgroundCurrent: matches.current,
searchHitForeground: scheme.onSurface,
);
}
/// The tints of find matches and of the current match: the theme's tertiary
/// colour, light enough over the text that it keeps 4.5:1 contrast.
({Color hit, Color current}) chatShellTerminalMatchColours(
BuildContext context,
) {
final tertiary = Theme.of(context).colorScheme.tertiary;
return (
hit: tertiary.withValues(alpha: chatShellTerminalMatchAlpha),
current: tertiary.withValues(alpha: chatShellTerminalCurrentMatchAlpha),
);
}
/// The terminal's text: the host's monospace family at [fontSize] (the
/// terminal text size setting), else a little under the card body size, with
/// the bundled symbols font first among the fallbacks for Powerline and Nerd
/// Font icons (registered on first use).
///
/// Line height 1.1: the emulator rounds a row up to whole pixels (about
/// 1.13 at the default size), close to other terminals' 1.0-1.2. Box
/// drawing, block and Powerline characters fill the whole row whatever the
/// height, so frames and bars join across rows.
TerminalStyle chatShellTerminalStyle(BuildContext context, {double? fontSize}) {
unawaited(ensureTerminalSymbolsFont());
return TerminalStyle(
fontFamily: appMonospaceFamily,
fontFamilyFallback: const [
terminalSymbolsFamily,
...kTerminalDefaultFontFamilyFallback,
],
fontSize: fontSize ?? chatShellTerminalDefaultFontSize(context),
height: 1.1,
);
}
/// The terminal's text size when the setting is the default.
double chatShellTerminalDefaultFontSize(BuildContext context) {
final body = chatCardBodyStyle(context) ?? const TextStyle();
return (body.fontSize ?? 14) * 0.95;
}
/// The size of one terminal cell in [style] at the context's text scale,
/// measured exactly as the emulator measures it.
Size chatShellTerminalCellSize(BuildContext context, TerminalStyle style) {
final text = style.toTextStyle();
final builder = ui.ParagraphBuilder(text.getParagraphStyle())
..pushStyle(text.getTextStyle(textScaler: MediaQuery.textScalerOf(context)))
..addText('mmmmmmmmmm');
final paragraph = builder.build()
..layout(const ui.ParagraphConstraints(width: double.infinity));
// Rows are whole pixels, as the emulator's painter rounds them.
final size = Size(
paragraph.maxIntrinsicWidth / 10,
paragraph.height.ceilToDouble(),
);
paragraph.dispose();
return size;
}