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>
This commit is contained in:
2026-11-26 02:25:16 +00:00
parent e526150b1e
commit b59ab5fad7
27 changed files with 1141 additions and 145 deletions
@@ -14,21 +14,31 @@ const String chatShellShareLabel = 'Send to agent';
const String chatShellSharingLabel = 'Sending…';
const String chatShellSharedLabel = 'Sent to agent';
const String chatShellFullScreenLabel = 'Full screen';
const String chatShellExpandLabel = 'Expand';
const String chatShellCollapseLabel = 'Collapse';
/// The card's host buttons: Stop while running; Copy output, Send to agent
/// once finished; Full screen for the terminal.
/// once finished; Expand or Collapse (when [onExpand] is given) and Full
/// screen for the terminal.
class ChatShellCardActions extends StatefulWidget {
const ChatShellCardActions({
super.key,
required this.run,
required this.controller,
required this.onFullScreen,
this.expanded = false,
this.onExpand,
});
final ChatShellRun run;
final ChatShellRunsController controller;
final VoidCallback onFullScreen;
/// Whether the terminal is expanded, and the toggle (null: nothing to
/// expand, the output fits collapsed).
final bool expanded;
final VoidCallback? onExpand;
@override
State<ChatShellCardActions> createState() => _ChatShellCardActionsState();
}
@@ -100,6 +110,15 @@ class _ChatShellCardActionsState extends State<ChatShellCardActions> {
? () => unawaited(controller.share(run.id))
: null,
),
if (widget.onExpand case final toggle?)
_button(
key: const Key('chat-shell-run-size'),
label: widget.expanded
? chatShellCollapseLabel
: chatShellExpandLabel,
material: widget.expanded ? Icons.unfold_less : Icons.unfold_more,
onPressed: toggle,
),
if (run.running || run.output.isNotEmpty)
_button(
key: const Key('chat-shell-run-expand'),
@@ -1,5 +1,10 @@
/// The one layout bound of a shell command card: how many terminal rows its
/// output area shows. A running command's terminal has this many rows; a
/// finished one shrinks to the rows its output uses, up to this many, and
/// scrolls within them. Full screen takes the whole window instead.
/// The layout bounds of a shell command card's output area, in terminal
/// rows. Collapsed (the default), a running command's terminal has
/// [kChatShellOutputCollapsedLines] rows; a finished one shrinks to the rows
/// its output uses, up to this many, and scrolls within them. Expanded, the
/// bound is [kChatShellOutputExpandedFraction] of the window's height (never
/// fewer rows than collapsed). Full screen takes the whole window instead.
const int kChatShellOutputCollapsedLines = 12;
/// The share of the window's height an expanded card's terminal may take.
const double kChatShellOutputExpandedFraction = 0.6;
@@ -16,6 +16,7 @@ class ChatShellOutputView extends StatelessWidget {
required this.controller,
required this.viewKey,
required this.fullScreen,
this.expanded = false,
});
final ChatShellRun run;
@@ -28,6 +29,10 @@ class ChatShellOutputView extends StatelessWidget {
/// True while the full-screen view sizes the terminal.
final bool fullScreen;
/// Whether the card shows its terminal expanded (see
/// [ChatShellTerminalView.expanded]).
final bool expanded;
@override
Widget build(BuildContext context) {
if (!run.running && run.output.isEmpty) {
@@ -45,6 +50,7 @@ class ChatShellOutputView extends StatelessWidget {
ChatShellTerminalView(
key: viewKey,
session: session,
expanded: expanded,
autoResize: !fullScreen,
),
if (session.live && !fullScreen)
@@ -6,6 +6,7 @@ import '../../../ui/text/monospace.dart';
import '../conversation/chat_card_styles.dart';
import 'chat_shell_card_actions.dart';
import 'chat_shell_dismiss.dart';
import 'chat_shell_metrics.dart';
import 'chat_shell_output_view.dart';
import 'chat_shell_run.dart';
import 'chat_shell_runs_controller.dart';
@@ -38,8 +39,20 @@ class ChatShellRunCard extends StatefulWidget {
class _ChatShellRunCardState extends State<ChatShellRunCard> {
bool _fullScreen = false;
bool _expanded = false;
int _viewGeneration = 0;
/// Whether expanding would show more: a live terminal grows to the
/// window; other output only if it has more rows than the collapsed box
/// (at the width it was laid out at).
bool _expandable(ChatShellRun run) {
if (!run.running && run.output.isEmpty) return false;
final session = widget.controller.terminals.session(run);
return session.live ||
session.rowsAt(session.terminal.viewWidth) >
kChatShellOutputCollapsedLines;
}
Future<void> _openFullScreen() async {
setState(() => _fullScreen = true);
await openChatShellTerminalPage(
@@ -127,6 +140,7 @@ class _ChatShellRunCardState extends State<ChatShellRunCard> {
controller: controller,
viewKey: ValueKey<int>(_viewGeneration),
fullScreen: _fullScreen,
expanded: _expanded,
),
if (run.error.isNotEmpty && !run.running)
Padding(
@@ -138,6 +152,10 @@ class _ChatShellRunCardState extends State<ChatShellRunCard> {
run: run,
controller: controller,
onFullScreen: () => _openFullScreen(),
expanded: _expanded,
onExpand: _expanded || _expandable(run)
? () => setState(() => _expanded = !_expanded)
: null,
),
if (error != null)
Semantics(
@@ -5,6 +5,7 @@ import 'chat_shell_terminal_actions.dart';
import 'chat_shell_terminal_find.dart';
import 'chat_shell_terminal_find_bar.dart';
import 'chat_shell_terminal_session.dart';
import 'chat_shell_terminal_theme.dart';
/// A terminal view's find in the scrollback: opening the bar (or focusing
/// it), stepping between matches and scrolling each into view, and closing
@@ -29,13 +30,13 @@ mixin ChatShellTerminalFindHost<T extends StatefulWidget> on State<T> {
_findFocus.requestFocus();
return;
}
final scheme = Theme.of(context).colorScheme;
final colours = chatShellTerminalMatchColours(context);
setState(() {
_find = ChatShellTerminalFind(
terminal: session.terminal,
controller: controller,
hit: scheme.tertiary.withValues(alpha: 0.3),
current: scheme.tertiary.withValues(alpha: 0.6),
hit: colours.hit,
current: colours.current,
);
});
// The field's autofocus would not take the keyboard from the terminal,
@@ -0,0 +1,178 @@
import 'package:flutter/painting.dart';
import '../../terminal/terminal_layout.dart';
/// The contrast the sixteen ANSI colours keep, as WCAG contrast ratios.
abstract final class TerminalContrast {
/// Red, green, yellow, blue, magenta and cyan, and white on a dark
/// background (black on a light one), against the background: body text.
static const double text = 4.5;
/// The bright colours on a light background, and bright black (dim text,
/// suggestions) on either: secondary text.
static const double secondary = 3;
/// White text on the normal colours as backgrounds (`ls` setuid is white
/// on red) and black text on them (setgid, sticky directories).
static const double onColour = 3;
/// How far each bright colour stands from its normal one, lighter.
static const double brightStep = 1.2;
}
/// The normal colours programs customarily put white text on (red, blue,
/// magenta: `ls` setuid, dialog boxes); black text goes on the others
/// (green, yellow, cyan: setgid, sticky directories, status bars).
const Set<int> terminalWhiteTextColours = {1, 4, 5};
/// The sixteen ANSI colours (black, red, green, yellow, blue, magenta, cyan,
/// white, then the bright ones) of [palette] for a terminal drawn on
/// [background], adjusted to meet [TerminalContrast]: each colour keeps its
/// hue and saturation and moves only in lightness, and only as far as needed.
///
/// White stays light and black dark in both themes, so the coloured
/// backgrounds keep their customary text readable (see
/// [terminalWhiteTextColours]); as in other terminals, white text on a light
/// background and black on a dark one are faint.
List<Color> chatShellTerminalPalette(
TerminalPalette palette,
Color background,
) => _computed.putIfAbsent((
palette,
background.toARGB32(),
), () => List.unmodifiable(_adjust(palette, background)));
/// Adjusted palettes by palette and background (a handful per app run).
final Map<(TerminalPalette, int), List<Color>> _computed = {};
List<Color> _adjust(TerminalPalette palette, Color background) {
final base = [for (final value in _bases[palette]!) Color(value)];
final bg = background.computeLuminance();
final dark = bg < 0.5;
final out = List.of(base);
// Black and white first: the colours under text are bounded by them.
out[0] = _within(base[0], 0, dark ? 0.03 : 0.015);
final black = out[0].computeLuminance();
out[8] = _within(
base[8],
_above(dark ? bg : black, TerminalContrast.secondary),
_below(dark ? 1 : bg, TerminalContrast.secondary),
);
// On a light background white rises to the normal colours chosen below.
out[7] = _within(base[7], dark ? 0.78 : 0, 1);
final readable = dark
? (low: _above(bg, TerminalContrast.text), high: 1.0)
: (low: 0.0, high: _below(bg, TerminalContrast.text));
final brightHigh = dark ? 1.0 : _below(bg, TerminalContrast.secondary);
var whiteNeeded = 0.0;
for (var i = 1; i <= 6; i++) {
final white = terminalWhiteTextColours.contains(i);
final textHigh = white && dark
? _below(out[7].computeLuminance(), TerminalContrast.onColour)
: 1.0;
final textLow = white ? 0.0 : _above(black, TerminalContrast.onColour);
out[i] = _within(
base[i],
_max(readable.low, textLow),
_min(readable.high, textHigh),
);
final normal = out[i].computeLuminance();
if (white) {
whiteNeeded = _max(
whiteNeeded,
_above(normal, TerminalContrast.onColour),
);
}
out[i + 8] = _within(
base[i + 8],
_max(
_above(normal, TerminalContrast.brightStep),
dark ? readable.low : 0,
),
brightHigh,
);
}
out[7] = _within(out[7], whiteNeeded, 1);
out[15] = _within(base[15], out[7].computeLuminance(), 1);
return out;
}
double _max(double a, double b) => a > b ? a : b;
double _min(double a, double b) => a < b ? a : b;
/// The WCAG contrast ratio of two colours.
double terminalContrast(Color a, Color b) {
final la = a.computeLuminance();
final lb = b.computeLuminance();
return la > lb ? (la + 0.05) / (lb + 0.05) : (lb + 0.05) / (la + 0.05);
}
/// The luminance [ratio] above / below [luminance].
double _above(double luminance, double ratio) =>
((luminance + 0.05) * ratio - 0.05).clamp(0, 1);
double _below(double luminance, double ratio) =>
((luminance + 0.05) / ratio - 0.05).clamp(0, 1);
/// [color] moved in lightness until its luminance is within [low]..[high]
/// (at [low] when the range is empty: the lower bound wins).
Color _within(Color color, double low, double high) {
final luminance = color.computeLuminance();
if (high < low || luminance < low) return _toLuminance(color, low);
if (luminance > high) return _toLuminance(color, high);
return color;
}
/// [color] at the HSL lightness whose luminance is [target], by bisection
/// (luminance grows with lightness at a fixed hue and saturation); rounded
/// to the side that keeps the bound.
Color _toLuminance(Color color, double target) {
final hsl = HSLColor.fromColor(color);
final up = target > color.computeLuminance();
var lo = up ? hsl.lightness : 0.0;
var hi = up ? 1.0 : hsl.lightness;
for (var i = 0; i < 24; i++) {
final mid = (lo + hi) / 2;
if (hsl.withLightness(mid).toColor().computeLuminance() < target) {
lo = mid;
} else {
hi = mid;
}
}
// 8-bit channels round; step a little further so the bound holds.
final settle = up
? (hi + 0.004).clamp(0.0, 1.0)
: (lo - 0.004).clamp(0.0, 1.0);
return hsl.withLightness(settle).toColor();
}
/// Each palette's colours before adjustment, ARGB, in ANSI order.
const Map<TerminalPalette, List<int>> _bases = {
// The emulator's standard palette (the colours this app always used).
TerminalPalette.theme: [
0xFF000000, 0xFFCD3131, 0xFF0DBC79, 0xFFE5E510, //
0xFF2472C8, 0xFFBC3FBC, 0xFF11A8CD, 0xFFE5E5E5,
0xFF666666, 0xFFF14C4C, 0xFF23D18B, 0xFFF5F543,
0xFF3B8EEA, 0xFFD670D6, 0xFF29B8DB, 0xFFFFFFFF,
],
// The long-standing desktop terminal default (Tango-like).
TerminalPalette.classic: [
0xFF2E3436, 0xFFCC0000, 0xFF4E9A06, 0xFFC4A000, //
0xFF3465A4, 0xFF75507B, 0xFF06989A, 0xFFD3D7CF,
0xFF555753, 0xFFEF2929, 0xFF8AE234, 0xFFFCE94F,
0xFF729FCF, 0xFFAD7FA8, 0xFF34E2E2, 0xFFEEEEEC,
],
// Muted, warm accents of even lightness (Solarized-like).
TerminalPalette.calm: [
0xFF073642, 0xFFDC322F, 0xFF859900, 0xFFB58900, //
0xFF268BD2, 0xFFD33682, 0xFF2AA198, 0xFFEEE8D5,
0xFF586E75, 0xFFF0605C, 0xFFA8BF2A, 0xFFD9A825,
0xFF5AAEEA, 0xFFE86AA6, 0xFF4FC4B8, 0xFFFDF6E3,
],
// Soft pastel accents (One-like).
TerminalPalette.vivid: [
0xFF282C34, 0xFFE06C75, 0xFF98C379, 0xFFE5C07B, //
0xFF61AFEF, 0xFFC678DD, 0xFF56B6C2, 0xFFABB2BF,
0xFF5C6370, 0xFFFF8A93, 0xFFB5E890, 0xFFFFD68A,
0xFF8CC8FF, 0xFFDE9BF0, 0xFF7FD4DE, 0xFFFFFFFF,
],
};
@@ -6,52 +6,71 @@ 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';
/// The terminal's colours from the host theme: the card's inset surface as
/// 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. The sixteen ANSI colours are the emulator's standard palette,
/// darkened on a light surface so coloured output stays readable.
TerminalTheme chatShellTerminalTheme(BuildContext context) {
final dark = Theme.of(context).brightness == Brightness.dark;
final background = chatCardFillColor(context);
final foreground = Theme.of(context).colorScheme.onSurface;
final accent = Theme.of(context).colorScheme.primary;
final base = TerminalThemes.defaultTheme;
Color ansi(Color color) {
if (dark) return color;
final hsl = HSLColor.fromColor(color);
return hsl.lightness > 0.4 ? hsl.withLightness(0.36).toColor() : color;
}
/// 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: accent,
selection: accent.withValues(alpha: 0.35),
foreground: foreground,
// Opaque: the emulator paints cells over its own background.
background: Color.alphaBlend(
background,
Theme.of(context).colorScheme.surface,
cursor: scheme.primary,
selection: scheme.primary.withValues(
alpha: chatShellTerminalSelectionAlpha,
),
black: dark ? base.black : foreground,
red: ansi(base.red),
green: ansi(base.green),
yellow: ansi(base.yellow),
blue: ansi(base.blue),
magenta: ansi(base.magenta),
cyan: ansi(base.cyan),
white: dark ? base.white : ansi(base.brightBlack),
brightBlack: ansi(base.brightBlack),
brightRed: ansi(base.brightRed),
brightGreen: ansi(base.brightGreen),
brightYellow: ansi(base.brightYellow),
brightBlue: ansi(base.brightBlue),
brightMagenta: ansi(base.brightMagenta),
brightCyan: ansi(base.brightCyan),
brightWhite: dark ? base.brightWhite : foreground,
searchHitBackground: base.searchHitBackground,
searchHitBackgroundCurrent: base.searchHitBackgroundCurrent,
searchHitForeground: base.searchHitForeground,
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),
);
}
@@ -19,6 +19,21 @@ import '../../../ui/input/keyboard_owner.dart';
export 'chat_shell_terminal_shortcuts.dart'
show chatShellTerminalShortcuts, chatShellCopyKey;
/// The most rows a card's terminal shows in a window [windowHeight] tall
/// with rows [rowHeight] high (see [kChatShellOutputCollapsedLines]).
int chatShellCardRows(
double windowHeight,
double rowHeight, {
required bool expanded,
}) {
if (!expanded || rowHeight <= 0) return kChatShellOutputCollapsedLines;
final rows = (windowHeight * kChatShellOutputExpandedFraction / rowHeight)
.floor();
return rows < kChatShellOutputCollapsedLines
? kChatShellOutputCollapsedLines
: rows;
}
/// Whether the soft keyboard is the main way to type here, so the key
/// toolbar offers a text field: the terminal itself takes hardware keys only.
bool chatShellTouchFirst() =>
@@ -26,8 +41,10 @@ bool chatShellTouchFirst() =>
defaultTargetPlatform == TargetPlatform.iOS;
/// One run's terminal emulator view. In a card it is bounded to
/// [kChatShellOutputCollapsedLines] rows (a finished run shrinks to its
/// output); with [fill] it takes the space its parent gives (full screen).
/// [kChatShellOutputCollapsedLines] rows, or with [expanded] to
/// [kChatShellOutputExpandedFraction] of the window's height (a finished run
/// shrinks to its output); with [fill] it takes the space its parent gives
/// (full screen).
/// It sizes the terminal to its own layout; a running process is resized to
/// match. Tapping a printed web link opens it in the browser.
///
@@ -40,12 +57,14 @@ class ChatShellTerminalView extends StatefulWidget {
super.key,
required this.session,
this.fill = false,
this.expanded = false,
this.autofocus = false,
this.autoResize = true,
});
final ChatShellTerminalSession session;
final bool fill;
final bool expanded;
final bool autofocus;
/// False while another view of the same terminal (full screen) sizes it.
@@ -164,7 +183,7 @@ class _ChatShellTerminalViewState extends State<ChatShellTerminalView>
focusNode: focus,
autofocus: widget.autofocus && live,
autoResize: widget.autoResize,
theme: chatShellTerminalTheme(context),
theme: chatShellTerminalTheme(context, palette: session.layout.palette),
textStyle: style,
padding: padding,
readOnly: !live,
@@ -229,9 +248,12 @@ class _ChatShellTerminalViewState extends State<ChatShellTerminalView>
final cols = ((constraints.maxWidth - padding.horizontal) / cell.width)
.floor()
.clamp(1, 1000);
final rows = live
? kChatShellOutputCollapsedLines
: session.rowsAt(cols).clamp(1, kChatShellOutputCollapsedLines);
final most = chatShellCardRows(
MediaQuery.sizeOf(context).height,
cell.height,
expanded: widget.expanded,
);
final rows = live ? most : session.rowsAt(cols).clamp(1, most);
// A pixel of slack keeps rounding from losing the last row.
return SizedBox(
height: rows * cell.height + padding.vertical + 1,