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,
@@ -6,9 +6,10 @@ import 'settings_group.dart';
/// The text sizes the setting offers (besides Default and the current one).
const List<double> _fontSizes = [10, 11, 12, 13, 14, 15, 16, 18, 20, 24];
/// Appearance → Terminal's text size, scrollback and bell controls.
/// Appearance → Terminal's text size, colours, scrollback and bell controls.
List<Widget> settingsTerminalControls(TerminalLayout layout) => [
SettingsGroupItem(child: _FontSize(layout: layout)),
SettingsGroupItem(child: _Palette(layout: layout)),
SettingsGroupItem(child: _Scrollback(layout: layout)),
SwitchListTile(
key: const ValueKey('settings-terminal-bell'),
@@ -54,6 +55,29 @@ class _FontSize extends StatelessWidget {
}
}
class _Palette extends StatelessWidget {
const _Palette({required this.layout});
final TerminalLayout layout;
@override
Widget build(BuildContext context) => DropdownMenu<TerminalPalette>(
key: ValueKey('settings-terminal-palette-${layout.palette.name}'),
expandedInsets: EdgeInsets.zero,
label: const Text('Colours'),
helperText:
'The sixteen terminal colours, adjusted to the light or dark theme '
'so text stays readable.',
initialSelection: layout.palette,
dropdownMenuEntries: [
for (final palette in TerminalPalette.values)
DropdownMenuEntry(value: palette, label: palette.label),
],
onSelected: (palette) {
if (palette != null) layout.setPalette(palette);
},
);
}
class _Scrollback extends StatelessWidget {
const _Scrollback({required this.layout});
final TerminalLayout layout;
@@ -8,7 +8,7 @@ import 'settings_theme.dart';
/// Appearance → Terminal: whether the terminal docks at the bottom of the
/// window or floats over it (Ctrl+` shows and hides it either way), the
/// Option key, text size, scrollback length and visual bell.
/// Option key, text size, colours, scrollback length and visual bell.
class SettingsTerminalSection extends StatelessWidget {
const SettingsTerminalSection({super.key, required this.layout});
+36 -7
View File
@@ -16,10 +16,14 @@ enum TerminalMode {
final String label;
}
/// The default and smallest heights of the bottom panel, and the smallest
/// floating window; layouts clamp to what the window allows.
const double terminalPanelDefaultHeight = 300;
/// 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);
@@ -34,6 +38,19 @@ const List<int> terminalScrollbackChoices = [
];
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;
@@ -83,7 +100,10 @@ class TerminalLayout extends ChangeNotifier {
TerminalMode mode = TerminalMode.panel;
bool visible = false;
double panelHeight = terminalPanelDefaultHeight;
/// 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
@@ -103,6 +123,9 @@ class TerminalLayout extends ChangeNotifier {
/// 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;
@@ -117,7 +140,10 @@ class TerminalLayout extends ChangeNotifier {
final json = jsonDecode(raw) as Map;
mode = TerminalMode.values.asNameMap()[json['mode']] ?? mode;
visible = json['visible'] == true;
panelHeight = _finite(json['panel_height']) ?? panelHeight;
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();
@@ -139,6 +165,7 @@ class TerminalLayout extends ChangeNotifier {
scrollback = lines;
}
bell = json['bell'] != false;
palette = TerminalPalette.values.asNameMap()[json['palette']] ?? palette;
} catch (_) {
return;
}
@@ -149,6 +176,7 @@ class TerminalLayout extends ChangeNotifier {
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(
@@ -163,7 +191,7 @@ class TerminalLayout extends ChangeNotifier {
void toggle() => _change(() => visible = !visible);
void show() => _change(() => visible = true);
void hide() => _change(() => visible = false);
void setPanelHeight(double value, {bool save = true}) =>
void setPanelHeight(double? value, {bool save = true}) =>
_change(() => panelHeight = value, save: save);
/// Saves the current layout (a drag saves once, when it ends).
@@ -173,7 +201,7 @@ class TerminalLayout extends ChangeNotifier {
jsonEncode({
'mode': mode.name,
'visible': visible,
'panel_height': panelHeight,
'panel_height': ?panelHeight,
'float': [
floatRect.left,
floatRect.top,
@@ -185,6 +213,7 @@ class TerminalLayout extends ChangeNotifier {
'font_size': ?fontSize,
'scrollback': scrollback,
'bell': bell,
'palette': palette.name,
}),
);
if (revision != _revision || saved != saveFailed) return;
+53 -11
View File
@@ -1,10 +1,12 @@
import 'dart:async';
import 'dart:math' as math;
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../ui/adaptive/resize_grip.dart';
import '../chat/shell/chat_shell_terminal_theme.dart';
import 'terminal_body.dart';
import 'terminal_hub.dart';
import 'terminal_layout.dart';
@@ -16,15 +18,50 @@ const double terminalPanelKeyStep = 24;
/// Space the docked panel always leaves the app above it.
const double terminalPanelAppMin = 160;
/// [height] within what a window [available] tall allows the panel.
double terminalPanelHeight(double height, double available) {
/// The share of the window the panel takes until the operator resizes it.
const double terminalPanelDefaultFraction = 0.4;
/// The terminal rows the default panel shows at least, window permitting.
const int terminalPanelDefaultMinRows = 16;
/// The panel's height besides its rows: the resize handle, the tab strip
/// and the emulator's padding (an allowance; the rows are what count).
const double terminalPanelChrome = 96;
/// The panel's height until the operator resizes it, in a window
/// [available] tall whose terminal rows are [rowHeight] high: 40 % of the
/// window, and at least 16 rows.
double terminalPanelDefaultHeight(double available, double rowHeight) =>
math.max(
available * terminalPanelDefaultFraction,
terminalPanelDefaultMinRows * rowHeight + terminalPanelChrome,
);
/// [height] (null: the default) within what a window [available] tall
/// allows the panel. [rowHeight] is one terminal row's height.
double terminalPanelHeight(
double? height,
double available, {
required double rowHeight,
}) {
final max = (available - terminalPanelAppMin).clamp(
terminalPanelMinHeight,
double.infinity,
);
return height.clamp(terminalPanelMinHeight, max);
return (height ?? terminalPanelDefaultHeight(available, rowHeight)).clamp(
terminalPanelMinHeight,
max,
);
}
/// One terminal row's height at the terminal's text size, as the emulator
/// lays it out.
double terminalRowHeight(BuildContext context, TerminalLayout layout) =>
chatShellTerminalCellSize(
context,
chatShellTerminalStyle(context, fontSize: layout.fontSize),
).height;
/// The terminal docked at the bottom of the window, filling the height its
/// parent gives it (the layout's, see [terminalPanelHeight]): a resize
/// handle on its top edge (drag, or the arrow keys), the tabs and the
@@ -35,6 +72,7 @@ class TerminalPanel extends StatefulWidget {
required this.hub,
required this.layout,
required this.available,
required this.rowHeight,
this.focus,
});
@@ -44,6 +82,9 @@ class TerminalPanel extends StatefulWidget {
/// The height of the window the panel shares with the app.
final double available;
/// One terminal row's height (see [terminalRowHeight]).
final double rowHeight;
/// The terminal's focus scope (see [TerminalBody.focus]).
final FocusScopeNode? focus;
@@ -54,17 +95,18 @@ class TerminalPanel extends StatefulWidget {
class _TerminalPanelState extends State<TerminalPanel> {
bool _active = false;
double get _height =>
terminalPanelHeight(widget.layout.panelHeight, widget.available);
double _clamp(double? height) => terminalPanelHeight(
height,
widget.available,
rowHeight: widget.rowHeight,
);
double get _height => _clamp(widget.layout.panelHeight);
void _resize(double height, {bool save = true}) =>
widget.layout.setPanelHeight(
terminalPanelHeight(height, widget.available),
save: save,
);
widget.layout.setPanelHeight(_clamp(height), save: save);
String _describe(double height) =>
'${terminalPanelHeight(height, widget.available).round()} pixels';
String _describe(double height) => '${_clamp(height).round()} pixels';
KeyEventResult _key(FocusNode _, KeyEvent event) {
if (event is! KeyDownEvent && event is! KeyRepeatEvent) {
@@ -39,9 +39,13 @@ class TerminalStage extends StatelessWidget {
builder: (context, constraints) {
final hiding = !layout.visible;
final panel = present && layout.mode == TerminalMode.panel;
// Measured only while the panel is there (the default height is in
// rows).
final rowHeight = panel ? terminalRowHeight(context, layout) : 0.0;
final height = terminalPanelHeight(
layout.panelHeight,
constraints.maxHeight,
rowHeight: rowHeight,
);
final inset = panel ? height * reveal.value : 0.0;
Widget inert(Widget child) => IgnorePointer(
@@ -75,6 +79,7 @@ class TerminalStage extends StatelessWidget {
hub: hub,
layout: layout,
available: constraints.maxHeight,
rowHeight: rowHeight,
focus: focus,
),
),
@@ -0,0 +1,125 @@
import 'package:flutter/material.dart';
/// The widest a tab's title is drawn before it ends in an ellipsis (the
/// tooltip has it whole).
const double terminalTabTitleMaxWidth = 220;
/// One terminal's tab: its [title] (ellipsised, italic once the terminal
/// ended), a bell dot, and a close button, with the whole title and
/// [tooltip] (machine and folder) on hover or long press.
///
/// The selected tab is filled with the secondary container and outlined in
/// the accent, its title bold, so it reads as selected in either theme
/// without relying on colour alone. The close button is a standard
/// [IconButton]: at least 48 px square on touch platforms and 40 px on
/// desktops (the platform's visual density).
class TerminalTabChip extends StatelessWidget {
const TerminalTabChip({
super.key,
required this.title,
required this.tooltip,
required this.selected,
required this.running,
required this.bell,
required this.onSelect,
required this.onClose,
});
final String title;
final String tooltip;
final bool selected;
final bool running;
final bool bell;
final VoidCallback onSelect;
final VoidCallback onClose;
@override
Widget build(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
final foreground = selected
? scheme.onSecondaryContainer
: scheme.onSurfaceVariant;
final label = Text(
title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
softWrap: false,
style: Theme.of(context).textTheme.labelLarge?.copyWith(
color: foreground,
fontWeight: selected ? FontWeight.w700 : FontWeight.w500,
fontStyle: running ? null : FontStyle.italic,
),
);
final shape = RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
side: BorderSide(
color: selected ? scheme.primary : scheme.outlineVariant,
width: selected ? 2 : 1,
),
);
return Semantics(
selected: selected,
child: Material(
shape: shape,
color: selected ? scheme.secondaryContainer : Colors.transparent,
clipBehavior: Clip.antiAlias,
// The title's ink fills the tab's height (the close button's).
child: IntrinsicHeight(
child: Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Tooltip(
message: tooltip,
child: InkWell(
onTap: onSelect,
child: Semantics(
button: true,
child: Padding(
padding: const EdgeInsetsDirectional.only(
start: 12,
end: 4,
),
child: ConstrainedBox(
constraints: const BoxConstraints(
maxWidth: terminalTabTitleMaxWidth,
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(child: label),
if (bell)
Padding(
padding: const EdgeInsetsDirectional.only(
start: 6,
),
// The bell rang while the terminal was not
// shown.
child: Semantics(
label: 'Bell',
child: Badge(
key: const ValueKey('terminal-tab-bell'),
),
),
),
],
),
),
),
),
),
),
IconButton(
tooltip: 'Close terminal',
onPressed: onClose,
color: foreground,
iconSize: 18,
icon: const Icon(Icons.close),
),
],
),
),
),
);
}
}
@@ -7,6 +7,7 @@ import 'terminal_layout.dart';
import 'terminal_menu.dart';
import 'terminal_shortcuts.dart';
import 'terminal_tab.dart';
import 'terminal_tab_chip.dart';
/// The terminal's header: one tab per terminal (title, and the machine when
/// tabs come from several), New terminal (choosing the machine when more
@@ -84,39 +85,16 @@ class TerminalTabStrip extends StatelessWidget {
hub.multiMachine ? '${tab.title} · ${tab.machineName}' : tab.title;
Widget _tab(TerminalTab tab, bool selected) {
Widget label = Text(
_label(tab),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: tab.info.running
? null
: const TextStyle(fontStyle: FontStyle.italic),
);
if (tab.bellUnseen) {
// The bell rang while the terminal was not shown.
label = Row(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(child: label),
Padding(
padding: const EdgeInsetsDirectional.only(start: 6),
child: Semantics(
label: 'Bell',
child: Badge(key: const ValueKey('terminal-tab-bell')),
),
),
],
);
}
void close() => unawaited(hub.close(tab));
return InputChip(
label: label,
final title = _label(tab);
return TerminalTabChip(
title: title,
// The whole title (the tab may ellipsise it), then where it runs.
tooltip: '$title\n${tab.machineName}: ${tab.cwd}',
selected: selected,
showCheckmark: false,
tooltip: '${tab.machineName}: ${tab.cwd}',
onSelected: (_) => hub.select(tab),
onDeleted: close,
deleteButtonTooltipMessage: 'Close terminal',
running: tab.info.running,
bell: tab.bellUnseen,
onSelect: () => hub.select(tab),
onClose: () => unawaited(hub.close(tab)),
);
}
+46 -26
View File
@@ -16,6 +16,10 @@ const double kNavigationRailSpace = kNavigationRailWidth + 1;
const double kNavigationRailMinWindowWidth =
kListDetailMinWidth + kNavigationRailSpace;
/// The rail's height below which Settings scrolls with the tabs instead of
/// staying pinned to the rail's foot: room for Settings and about two tabs.
const double kNavigationRailPinnedSettingsMinHeight = 240;
/// Whether the window at [context] shows the destination rail. Read from the
/// window's own [MediaQuery], so the shell and the floating windows layer
/// (which sits above the shell) agree.
@@ -32,6 +36,10 @@ bool showsFloatingTabBarOnly(BuildContext context) =>
/// an icon and a label per tab, the selected tab marked by the rail's
/// indicator, and Settings pinned to its bottom.
///
/// The tabs scroll when the rail is too short for them (a tall terminal
/// panel shrinks the app above it), and on a very short rail Settings
/// scrolls with them, so items never overlap at any height.
///
/// [icon] builds each tab's icon (its running or unread badge included), so
/// the rail shows the same counts the floating bar did. [selected] is the
/// shell's destination: Settings (a page, not a tab) selects no tab but its
@@ -67,38 +75,50 @@ class ShellNavigationRail extends StatelessWidget {
padding: padding,
child: SizedBox(
width: kNavigationRailWidth,
child: NavigationRail(
key: const ValueKey('shell.navigation-rail'),
backgroundColor: surface,
minWidth: kNavigationRailWidth,
labelType: NavigationRailLabelType.all,
selectedIndex: index < 0 ? null : index,
onDestinationSelected: (value) =>
onSelected(shellTabDestinations[value]),
destinations: [
for (final destination in shellTabDestinations)
NavigationRailDestination(
icon: KeyedSubtree(
key: ValueKey('shell.destination.${destination.name}'),
// The bar it replaces named each tab in a tooltip too.
child: Tooltip(
message: destinationDetails(destination).label,
child: icon(destination),
),
),
label: Text(destinationDetails(destination).label),
),
],
trailingAtBottom: true,
trailing: _SettingsRailItem(
selected: selected == AppDestination.settings,
onTap: () => onSelected(AppDestination.settings),
child: LayoutBuilder(
builder: (context, constraints) => _rail(
index,
surface,
pinSettings:
constraints.maxHeight >=
kNavigationRailPinnedSettingsMinHeight,
),
),
),
),
);
}
Widget _rail(int index, Color surface, {required bool pinSettings}) =>
NavigationRail(
key: const ValueKey('shell.navigation-rail'),
backgroundColor: surface,
minWidth: kNavigationRailWidth,
labelType: NavigationRailLabelType.all,
selectedIndex: index < 0 ? null : index,
onDestinationSelected: (value) =>
onSelected(shellTabDestinations[value]),
destinations: [
for (final destination in shellTabDestinations)
NavigationRailDestination(
icon: KeyedSubtree(
key: ValueKey('shell.destination.${destination.name}'),
// The bar it replaces named each tab in a tooltip too.
child: Tooltip(
message: destinationDetails(destination).label,
child: icon(destination),
),
),
label: Text(destinationDetails(destination).label),
),
],
scrollable: true,
trailingAtBottom: pinSettings,
trailing: _SettingsRailItem(
selected: selected == AppDestination.settings,
onTap: () => onSelected(AppDestination.settings),
),
);
}
/// Settings at the rail's foot, drawn as a [NavigationRailDestination] is: an