Files
pipelines/app/lib/features/terminal/terminal_layout.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

234 lines
7.4 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';
const terminalLayoutKey = 'pipelines.terminal-layout.v1';
/// Where the terminal shows on a wide window.
enum TerminalMode {
panel('Bottom panel'),
float('Floating window');
const TerminalMode(this.label);
final String label;
}
/// The smallest height of the bottom panel, and the smallest floating
/// window; layouts clamp to what the window allows. The panel's default
/// height depends on the window (see `terminalPanelDefaultHeight`).
const double terminalPanelMinHeight = 120;
/// The fixed default panel height layouts saved before the default followed
/// the window: a saved height of exactly this is read as never resized.
const double terminalPanelLegacyDefaultHeight = 300;
const Size terminalFloatMinSize = Size(320, 200);
const Rect terminalFloatDefaultRect = Rect.fromLTWH(80, 80, 720, 420);
/// The scrollback lengths offered, and the default.
const List<int> terminalScrollbackChoices = [
1000,
5000,
10000,
25000,
50000,
100000,
];
const int terminalScrollbackDefault = 10000;
/// The terminal's ANSI colour palette. Every palette is adjusted to the
/// light or dark theme's terminal background so text stays readable (see
/// `chatShellTerminalPalette`); the names describe them, not products.
enum TerminalPalette {
theme('Theme colours'),
classic('Classic'),
calm('Calm'),
vivid('Vivid');
const TerminalPalette(this.label);
final String label;
}
/// The terminal's text size limits (logical pixels) and one zoom step.
const double terminalFontSizeMin = 8;
const double terminalFontSizeMax = 32;
const double terminalFontSizeStep = 1;
abstract interface class TerminalLayoutStore {
Future<String?> load();
Future<bool> save(String value);
}
class SharedTerminalLayoutStore implements TerminalLayoutStore {
const SharedTerminalLayoutStore();
@override
Future<String?> load() async {
try {
return (await SharedPreferences.getInstance()).getString(
terminalLayoutKey,
);
} catch (_) {
return null;
}
}
@override
Future<bool> save(String value) async {
try {
return await (await SharedPreferences.getInstance()).setString(
terminalLayoutKey,
value,
);
} catch (_) {
return false;
}
}
}
/// The terminal surface's device-local layout: the mode, whether it is
/// shown and the bottom panel's height. Every change is saved, so a reload
/// reopens it as it was. The floating window's frame is the window
/// system's.
class TerminalLayout extends ChangeNotifier {
TerminalLayout({TerminalLayoutStore? store})
: _store = store ?? const SharedTerminalLayoutStore();
static final instance = TerminalLayout();
final TerminalLayoutStore _store;
int _revision = 0;
TerminalMode mode = TerminalMode.panel;
bool visible = false;
/// The bottom panel's height as the operator last dragged it, or null for
/// the default, which follows the window.
double? panelHeight;
/// The floating window's frame and maximised state as saved before the
/// window moved to the app's window system (`TerminalWindows`), which takes
/// them for its first window. They are still written back unchanged.
Rect floatRect = terminalFloatDefaultRect;
bool maximized = false;
/// Whether a Mac's Option key is Meta in the terminal (Option+B sends
/// ESC b, readline's word back) rather than typing its symbol (∫). Other
/// platforms' Alt is always Meta.
bool optionAsMeta = true;
/// The terminal's text size, or null for the app's default (the chat
/// card's body size). Ctrl/Cmd+=, - and 0 zoom it.
double? fontSize;
/// Lines each terminal keeps, scrollback included.
int scrollback = terminalScrollbackDefault;
/// The sixteen ANSI colours' palette.
TerminalPalette palette = TerminalPalette.theme;
/// Whether a bell (BEL) flashes the terminal and dots a hidden tab.
bool bell = true;
/// Whether the last change could not be saved.
bool saveFailed = false;
Future<void> restore() async {
final revision = _revision;
final raw = await _store.load();
if (revision != _revision || raw == null) return;
try {
final json = jsonDecode(raw) as Map;
mode = TerminalMode.values.asNameMap()[json['mode']] ?? mode;
visible = json['visible'] == true;
final height = _finite(json['panel_height']);
panelHeight = height == terminalPanelLegacyDefaultHeight
? null
: height ?? panelHeight;
final rect = json['float'];
if (rect is List && rect.length == 4) {
final values = rect.map(_finite).toList();
if (!values.contains(null)) {
floatRect = Rect.fromLTWH(
values[0]!,
values[1]!,
values[2]!,
values[3]!,
);
}
}
maximized = json['maximized'] == true;
optionAsMeta = json['option_as_meta'] != false;
final size = _finite(json['font_size']);
fontSize = size?.clamp(terminalFontSizeMin, terminalFontSizeMax);
final lines = json['scrollback'];
if (lines is int && terminalScrollbackChoices.contains(lines)) {
scrollback = lines;
}
bell = json['bell'] != false;
palette = TerminalPalette.values.asNameMap()[json['palette']] ?? palette;
} catch (_) {
return;
}
notifyListeners();
}
void setMode(TerminalMode value) => _change(() => mode = value);
void setOptionAsMeta(bool value) => _change(() => optionAsMeta = value);
void setScrollback(int value) => _change(() => scrollback = value);
void setBell(bool value) => _change(() => bell = value);
void setPalette(TerminalPalette value) => _change(() => palette = value);
/// Sets the text size (null: the default), within the limits.
void setFontSize(double? value) => _change(
() => fontSize = value?.clamp(terminalFontSizeMin, terminalFontSizeMax),
);
/// One zoom step in or out ([steps] > 0 larger) from [current], the size
/// the terminal draws at now.
void zoom(int steps, double current) => setFontSize(
(fontSize ?? current).roundToDouble() + steps * terminalFontSizeStep,
);
void toggle() => _change(() => visible = !visible);
void show() => _change(() => visible = true);
void hide() => _change(() => visible = false);
void setPanelHeight(double? value, {bool save = true}) =>
_change(() => panelHeight = value, save: save);
/// Saves the current layout (a drag saves once, when it ends).
Future<void> save() async {
final revision = _revision;
final saved = await _store.save(
jsonEncode({
'mode': mode.name,
'visible': visible,
'panel_height': ?panelHeight,
'float': [
floatRect.left,
floatRect.top,
floatRect.width,
floatRect.height,
],
'maximized': maximized,
'option_as_meta': optionAsMeta,
'font_size': ?fontSize,
'scrollback': scrollback,
'bell': bell,
'palette': palette.name,
}),
);
if (revision != _revision || saved != saveFailed) return;
saveFailed = !saved;
notifyListeners();
}
void _change(VoidCallback change, {bool save = true}) {
_revision++;
change();
notifyListeners();
if (save) unawaited(this.save());
}
static double? _finite(Object? value) =>
value is num && value.isFinite ? value.toDouble() : null;
}