Files
pipelines/app/lib/features/terminal/terminal_tab_strip.dart
T
alan 61c7eee2b0 App: open a chat, the terminal or a diff in a new window
"Open in new window" sits beside Float: the chat pane and chat list row
menus, a floating chat window's title bar, the terminal options menu and
the diff bar, plus Alt+Shift+Y for the chat with the keyboard (listed in
the shortcut help). Once the window opens the item leaves the main window
as Float takes a pane out of the tiling.

The window is its own app instance at /window/chat/<id>,
/window/terminal?tab=<key> or /window/diff/<token>: only that item, named
after it, with "Bring back to main window". It has its own connection and
writes no shared layout (no open-chat workspace, window store or terminal
layout) and posts no notifications, so they stay the main window's.

Web opens a popup window (a blocked popup offers a tab); Linux and macOS
start a second process with --detached (Flutter's multi-window API is still
experimental); Android and iOS hide the action. The capture kit gains a
follow step for windows a page opens.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 12:47:42 +00:00

185 lines
5.6 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import '../../shell/detached/detached_window_scope.dart';
import 'terminal_browser_keys.dart';
import 'terminal_hub.dart';
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
/// than one is connected), the mode menu, and the host's own [trailing]
/// window buttons.
class TerminalTabStrip extends StatelessWidget {
const TerminalTabStrip({
super.key,
required this.hub,
required this.layout,
this.trailing = const [],
this.showModes = true,
});
final TerminalHub hub;
final TerminalLayout layout;
final List<Widget> trailing;
/// False on the narrow page, which has no panel or window to choose.
final bool showModes;
/// The browser keys capture the menu offers (tests replace the instance).
TerminalBrowserKeys get browserKeys => TerminalBrowserKeys.instance;
@override
Widget build(BuildContext context) {
final selected = hub.selected;
return Row(
children: [
Expanded(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
child: Row(
children: [
for (final (index, tab) in hub.tabs.indexed)
Padding(
padding: const EdgeInsetsDirectional.only(end: 4),
child: Semantics(
identifier: 'terminal-tab-$index',
child: _tab(tab, tab == selected),
),
),
],
),
),
),
_newTerminal(),
if (showModes)
ListenableBuilder(
listenable: browserKeys,
builder: (context, _) => _options(context),
),
...trailing,
],
);
}
/// The options menu: the modes, Open in new window (where the platform
/// has windows), the browser keys capture (on a browser that can) and
/// Hide terminal, with the shortcuts under them.
Widget _options(BuildContext context) {
final opener = DetachedWindowScope.maybeOf(context);
return TerminalMenuButton<_Option>(
label: 'Terminal options',
info: TerminalShortcutLabels.current().summary,
icon: Icons.more_vert,
choices: [
for (final mode in TerminalMode.values)
TerminalChoice(
_Option(mode),
mode.label,
checked: layout.mode == mode,
key: ValueKey('terminal-mode-${mode.name}'),
),
if (opener != null)
const TerminalChoice(
_Option(null, newWindow: true),
detachedWindowLabel,
key: ValueKey('terminal-new-window'),
),
if (browserKeys.supported)
TerminalChoice(
const _Option(null, browserKeys: true),
'Capture browser keys',
checked: browserKeys.active,
key: const ValueKey('terminal-browser-keys'),
),
const TerminalChoice(
_Option(null),
'Hide terminal',
key: ValueKey('terminal-hide'),
),
],
onSelected: (option) {
if (option.newWindow) {
_openInNewWindow(opener!);
} else if (option.browserKeys) {
unawaited(toggleTerminalBrowserKeys(context, browserKeys));
} else if (option.mode == null) {
layout.hide();
} else {
layout.setMode(option.mode!);
}
},
);
}
/// The terminal moves to a window of its own, on the selected tab; this
/// one hides it once that window is open. The terminals keep running on
/// the machines, so both windows reach the same shells.
void _openInNewWindow(DetachedWindowOpener opener) {
final tab = hub.selected;
opener.openTerminal(
tabKey: tab?.key ?? '',
title: tab == null ? 'Terminal' : 'Terminal: ${tab.title}',
onOpened: layout.hide,
);
}
String _label(TerminalTab tab) =>
hub.multiMachine ? '${tab.title} · ${tab.machineName}' : tab.title;
Widget _tab(TerminalTab tab, bool selected) {
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,
running: tab.info.running,
bell: tab.bellUnseen,
onSelect: () => hub.select(tab),
onClose: () => unawaited(hub.close(tab)),
);
}
Widget _newTerminal() {
final machines = hub.connected;
if (machines.length > 1) {
return TerminalMenuButton<String>(
label: 'New terminal',
icon: Icons.add,
choices: [
for (final machine in machines)
TerminalChoice(
machine.id,
machine.name,
key: ValueKey('terminal-new-${machine.id}'),
),
],
onSelected: (id) => unawaited(hub.open(machineId: id)),
);
}
final enabled = machines.isNotEmpty;
void open() => unawaited(hub.open());
return IconButton(
tooltip: 'New terminal',
onPressed: enabled ? open : null,
icon: const Icon(Icons.add),
);
}
}
/// A mode to switch to, (null) hiding the terminal, or the browser keys
/// capture.
class _Option {
const _Option(this.mode, {this.browserKeys = false, this.newWindow = false});
final TerminalMode? mode;
final bool browserKeys;
final bool newWindow;
}