feat: several terminal tabs per project, with a dropdown

A directory now has a TerminalWorkspace holding a list of tabs instead of a
single session. The status bar's left side became a dropdown: it shows the
active tab and its state ("终端 2 · 运行中") and lists the other tabs (the
active one checked) plus "新建终端". New tabs take the smallest free number,
so closing one frees its name.

Only the active tab keeps a socket: switching detaches the old tab and
attaches the new one, whose screen is replayed from the server snapshot; the
shell itself keeps running. The tab's model stays in memory, so switching
back is immediate.

The tab list (ids, labels, active one) is saved per connection and directory.
On the first open the server's sessions for the directory are reconciled with
it: saved labels and order are kept for sessions that still exist, sessions
opened elsewhere (web UI) are appended in creation order, and tabs whose
session is gone are dropped. Project-action command runners are not shown as
tabs. The 0.4.2 single-session value is read as one tab.

0.4.3 (403).
This commit is contained in:
blumia committed 2026-11-22 15:23:21 +08:00
1 parent 675d19d563
commit a7bc380ac0
9 files changed
+717 -298

No files matched your search

+204 -119
View File
@@ -1,6 +1,7 @@
// The terminal tab: xterm renders the PTY, a status bar offers restart and
// close, and the key bar below sends what a phone keyboard cannot (Esc, Tab,
// Ctrl, arrows, punctuation), the way Termux does.
// The terminal tab: xterm renders the PTY, a status bar offers the tab
// dropdown (switch / new), restart and close, and the key bar below sends what
// a phone keyboard cannot (Esc, Tab, Ctrl, arrows, punctuation), the way
// Termux does.
import 'package:flutter/material.dart';
import 'package:xterm/xterm.dart';
@@ -46,14 +47,17 @@ class TerminalPanel extends StatefulWidget {
}
class _TerminalPanelState extends State<TerminalPanel> {
TerminalSessionState? _terminal;
late final TerminalWorkspace _workspace;
final FocusNode _focus = FocusNode();
/// The dropdown's value for "new terminal"; tab values are their index.
static const _newTab = -1;
@override
void initState() {
super.initState();
_terminal = widget.app.terminalFor(widget.directory);
_terminal!.ensureOpen();
_workspace = widget.app.terminalFor(widget.directory);
_workspace.ensureOpen();
}
@override
@@ -62,135 +66,208 @@ class _TerminalPanelState extends State<TerminalPanel> {
super.dispose();
}
TerminalSessionState get _t => _terminal!;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return ListenableBuilder(
listenable: _t,
listenable: _workspace,
builder: (context, _) {
return Column(
children: [
_statusBar(theme),
Expanded(
child: ColoredBox(
color: theme.brightness == Brightness.dark
? const Color(0xFF1E1E1E)
: Colors.white,
child: TerminalView(
_t.model,
focusNode: _focus,
theme: theme.brightness == Brightness.dark
? TerminalThemes.defaultTheme
: _lightTheme,
textStyle: const TerminalStyle(
fontSize: 13,
height: 1.25,
fontFamily: 'monospace',
final tab = _workspace.activeTab;
if (tab == null) return const SizedBox.shrink();
final state = tab.state;
return ListenableBuilder(
listenable: state,
builder: (context, _) {
return Column(
children: [
_statusBar(theme, tab, state),
Expanded(
child: ColoredBox(
color: theme.brightness == Brightness.dark
? const Color(0xFF1E1E1E)
: Colors.white,
child: TerminalView(
// A fresh view per tab, so scroll position and selection
// do not leak from the one before it.
key: ObjectKey(state),
state.model,
focusNode: _focus,
theme: theme.brightness == Brightness.dark
? TerminalThemes.defaultTheme
: _lightTheme,
textStyle: const TerminalStyle(
fontSize: 13,
height: 1.25,
fontFamily: 'monospace',
),
padding: const EdgeInsets.fromLTRB(8, 6, 8, 6),
keyboardType: TextInputType.text,
deleteDetection: true,
backgroundOpacity: 1,
),
),
padding: const EdgeInsets.fromLTRB(8, 6, 8, 6),
keyboardType: TextInputType.text,
deleteDetection: true,
backgroundOpacity: 1,
),
),
),
_keyBar(theme),
],
_keyBar(theme, state),
],
);
},
);
},
);
}
Widget _statusBar(ThemeData theme) {
Widget _statusBar(
ThemeData theme, TerminalTab tab, TerminalSessionState state) {
final scheme = theme.colorScheme;
final (label, color) = switch (_t.link) {
final (label, color) = switch (state.link) {
TerminalLinkState.idle => ('未启动', theme.hintColor),
TerminalLinkState.creating => ('正在创建…', theme.hintColor),
TerminalLinkState.connecting => ('正在连接…', theme.hintColor),
TerminalLinkState.reconnecting => ('连接中断,正在重连…', scheme.error),
TerminalLinkState.open => ('运行中', scheme.primary),
TerminalLinkState.exited => (_t.detail ?? '已退出', theme.hintColor),
TerminalLinkState.error => (_t.detail ?? '连接失败', scheme.error),
TerminalLinkState.exited => (state.detail ?? '已退出', theme.hintColor),
TerminalLinkState.error => (state.detail ?? '连接失败', scheme.error),
};
return Container(
padding: const EdgeInsets.fromLTRB(12, 2, 4, 2),
child: Row(
children: [
Icon(Icons.circle, size: 8, color: color),
const SizedBox(width: 6),
Expanded(
child: Text(
label,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 12, color: color),
return Row(
children: [
Expanded(
child: PopupMenuButton<int>(
tooltip: '切换终端',
position: PopupMenuPosition.under,
onSelected: _onTabMenu,
itemBuilder: (_) => [
for (var i = 0; i < _workspace.tabs.length; i++)
PopupMenuItem<int>(
value: i,
child: Row(
children: [
Icon(
i == _workspace.activeIndex
? Icons.check
: Icons.terminal_outlined,
size: 18,
color: i == _workspace.activeIndex
? scheme.primary
: theme.hintColor,
),
const SizedBox(width: 10),
Expanded(
child: Text(
_workspace.tabs[i].label,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
],
),
),
const PopupMenuDivider(),
const PopupMenuItem<int>(
value: _newTab,
child: Row(
children: [
Icon(Icons.add, size: 18),
SizedBox(width: 10),
Text('新建终端'),
],
),
),
],
child: Container(
height: 40,
padding: const EdgeInsets.fromLTRB(12, 2, 4, 2),
child: Row(
children: [
Icon(Icons.circle, size: 8, color: color),
const SizedBox(width: 6),
Flexible(
child: Text(
tab.label,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(
fontSize: 12.5, fontWeight: FontWeight.w600),
),
),
const SizedBox(width: 4),
Flexible(
child: Text(
'· $label',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 12, color: color),
),
),
Icon(Icons.arrow_drop_down, size: 20, color: theme.hintColor),
],
),
),
),
IconButton(
tooltip: '键盘',
visualDensity: VisualDensity.compact,
icon: const Icon(Icons.keyboard_outlined, size: 18),
onPressed: () => _focus.requestFocus(),
),
IconButton(
tooltip: '重新启动',
visualDensity: VisualDensity.compact,
icon: const Icon(Icons.restart_alt, size: 18),
onPressed: _t.link == TerminalLinkState.exited ||
_t.link == TerminalLinkState.error
? _restart
: _t.isRunning
? _restart
: null,
),
PopupMenuButton<String>(
tooltip: '更多',
icon: const Icon(Icons.more_vert, size: 18),
onSelected: (value) {
switch (value) {
case 'clear':
_t.model.write('\x1b[2J\x1b[H');
break;
case 'close':
_closeSession();
break;
}
},
itemBuilder: (_) => const [
PopupMenuItem(value: 'clear', child: Text('清屏')),
PopupMenuItem(value: 'close', child: Text('关闭终端')),
],
),
],
),
),
IconButton(
tooltip: '键盘',
visualDensity: VisualDensity.compact,
icon: const Icon(Icons.keyboard_outlined, size: 18),
onPressed: () => _focus.requestFocus(),
),
IconButton(
tooltip: '重新启动',
visualDensity: VisualDensity.compact,
icon: const Icon(Icons.restart_alt, size: 18),
onPressed: state.link == TerminalLinkState.exited ||
state.link == TerminalLinkState.error ||
state.isRunning
? _restart
: null,
),
PopupMenuButton<String>(
tooltip: '更多',
icon: const Icon(Icons.more_vert, size: 18),
onSelected: (value) {
switch (value) {
case 'clear':
state.model.write('\x1b[2J\x1b[H');
break;
case 'close':
_workspace.closeActive();
break;
}
},
itemBuilder: (_) => const [
PopupMenuItem(value: 'clear', child: Text('清屏')),
PopupMenuItem(value: 'close', child: Text('关闭此终端')),
],
),
],
);
}
Future<void> _restart() async {
if (_t.link == TerminalLinkState.exited ||
_t.link == TerminalLinkState.error) {
// A dead session cannot restart in place; a fresh one replaces it.
await _t.closeSession();
await _t.ensureOpen(fresh: true);
return;
void _onTabMenu(int value) {
if (value == _newTab) {
_workspace.addTab();
} else {
_workspace.select(value);
}
await _t.restart();
}
Future<void> _closeSession() async {
await _t.closeSession();
// An explicit close must not adopt one of the server's other sessions for
// this directory (or the old one, if the DELETE lagged): start a new shell.
if (mounted) await _t.ensureOpen(fresh: true);
Future<void> _restart() async {
final state = _workspace.activeTab?.state;
if (state == null) return;
if (state.link == TerminalLinkState.error) {
// A dead session cannot restart in place; a fresh shell replaces it
// inside the same tab.
await state.closeSession();
await state.ensureOpen();
return;
}
await state.restart();
}
// ------------------------------------------------------------- key bar
/// Two rows of seven, the way Termux lays them out: the keys a phone
/// keyboard cannot produce, all reachable with a thumb.
Widget _keyBar(ThemeData theme) {
Widget _keyBar(ThemeData theme, TerminalSessionState state) {
return Container(
decoration: BoxDecoration(
color: theme.colorScheme.surfaceContainerHigh,
@@ -201,22 +278,29 @@ class _TerminalPanelState extends State<TerminalPanel> {
mainAxisSize: MainAxisSize.min,
children: [
_keyRow([
_key('ESC', onTap: () => _sendKey(TerminalKey.escape)),
_key('/', onTap: () => _sendText('/')),
_key('—', onTap: () => _sendText('-')),
_key('HOME', onTap: () => _sendKey(TerminalKey.home)),
_key('↑', big: true, onTap: () => _sendKey(TerminalKey.arrowUp)),
_key('END', onTap: () => _sendKey(TerminalKey.end)),
_key('PGUP', onTap: () => _sendKey(TerminalKey.pageUp)),
_key('ESC', onTap: () => _sendKey(state, TerminalKey.escape)),
_key('/', onTap: () => _sendText(state, '/')),
_key('—', onTap: () => _sendText(state, '-')),
_key('HOME', onTap: () => _sendKey(state, TerminalKey.home)),
_key('↑',
big: true, onTap: () => _sendKey(state, TerminalKey.arrowUp)),
_key('END', onTap: () => _sendKey(state, TerminalKey.end)),
_key('PGUP', onTap: () => _sendKey(state, TerminalKey.pageUp)),
]),
_keyRow([
_key('TAB', onTap: () => _sendKey(TerminalKey.tab)),
_key('CTRL', active: _t.ctrl, onTap: _t.toggleCtrl),
_key('ALT', active: _t.alt, onTap: _t.toggleAlt),
_key('←', big: true, onTap: () => _sendKey(TerminalKey.arrowLeft)),
_key('↓', big: true, onTap: () => _sendKey(TerminalKey.arrowDown)),
_key('→', big: true, onTap: () => _sendKey(TerminalKey.arrowRight)),
_key('PGDN', onTap: () => _sendKey(TerminalKey.pageDown)),
_key('TAB', onTap: () => _sendKey(state, TerminalKey.tab)),
_key('CTRL', active: state.ctrl, onTap: state.toggleCtrl),
_key('ALT', active: state.alt, onTap: state.toggleAlt),
_key('←',
big: true,
onTap: () => _sendKey(state, TerminalKey.arrowLeft)),
_key('↓',
big: true,
onTap: () => _sendKey(state, TerminalKey.arrowDown)),
_key('→',
big: true,
onTap: () => _sendKey(state, TerminalKey.arrowRight)),
_key('PGDN', onTap: () => _sendKey(state, TerminalKey.pageDown)),
]),
],
),
@@ -264,8 +348,9 @@ class _TerminalPanelState extends State<TerminalPanel> {
);
}
void _sendKey(TerminalKey key) =>
_t.model.keyInput(key, ctrl: _t.ctrl, alt: _t.alt, shift: _t.shift);
void _sendKey(TerminalSessionState state, TerminalKey key) => state.model
.keyInput(key, ctrl: state.ctrl, alt: state.alt, shift: state.shift);
void _sendText(String text) => _t.model.textInput(text);
void _sendText(TerminalSessionState state, String text) =>
state.model.textInput(text);
}