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).
357 lines
12 KiB
Dart
357 lines
12 KiB
Dart
// 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';
|
|
|
|
import '../api/terminal.dart';
|
|
import '../state.dart';
|
|
import '../terminal_state.dart';
|
|
|
|
/// A light palette, so the terminal does not turn into a black hole in the
|
|
/// light theme. (xterm ships only dark ones.)
|
|
const _lightTheme = TerminalTheme(
|
|
cursor: Color(0xFF3A3A3A),
|
|
selection: Color(0x553A6EA5),
|
|
foreground: Color(0xFF1F1F1F),
|
|
background: Color(0xFFFFFFFF),
|
|
black: Color(0xFF000000),
|
|
red: Color(0xFFCD3131),
|
|
green: Color(0xFF0D7A44),
|
|
yellow: Color(0xFF7A6200),
|
|
blue: Color(0xFF1B5FA6),
|
|
magenta: Color(0xFFA626A4),
|
|
cyan: Color(0xFF0E7490),
|
|
white: Color(0xFFBDBDBD),
|
|
brightBlack: Color(0xFF666666),
|
|
brightRed: Color(0xFFD73A49),
|
|
brightGreen: Color(0xFF1A8A4E),
|
|
brightYellow: Color(0xFF8A6D00),
|
|
brightBlue: Color(0xFF2E6FBF),
|
|
brightMagenta: Color(0xFFB23BB0),
|
|
brightCyan: Color(0xFF12859B),
|
|
brightWhite: Color(0xFFEEEEEE),
|
|
searchHitBackground: Color(0xFFFFF176),
|
|
searchHitBackgroundCurrent: Color(0xFFAED581),
|
|
searchHitForeground: Color(0xFF000000),
|
|
);
|
|
|
|
class TerminalPanel extends StatefulWidget {
|
|
final AppState app;
|
|
final String directory;
|
|
const TerminalPanel({super.key, required this.app, this.directory = ''});
|
|
|
|
@override
|
|
State<TerminalPanel> createState() => _TerminalPanelState();
|
|
}
|
|
|
|
class _TerminalPanelState extends State<TerminalPanel> {
|
|
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();
|
|
_workspace = widget.app.terminalFor(widget.directory);
|
|
_workspace.ensureOpen();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_focus.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = Theme.of(context);
|
|
return ListenableBuilder(
|
|
listenable: _workspace,
|
|
builder: (context, _) {
|
|
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,
|
|
),
|
|
),
|
|
),
|
|
_keyBar(theme, state),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget _statusBar(
|
|
ThemeData theme, TerminalTab tab, TerminalSessionState state) {
|
|
final scheme = theme.colorScheme;
|
|
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 => (state.detail ?? '已退出', theme.hintColor),
|
|
TerminalLinkState.error => (state.detail ?? '连接失败', scheme.error),
|
|
};
|
|
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: 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('关闭此终端')),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
void _onTabMenu(int value) {
|
|
if (value == _newTab) {
|
|
_workspace.addTab();
|
|
} else {
|
|
_workspace.select(value);
|
|
}
|
|
}
|
|
|
|
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, TerminalSessionState state) {
|
|
return Container(
|
|
decoration: BoxDecoration(
|
|
color: theme.colorScheme.surfaceContainerHigh,
|
|
border: Border(top: BorderSide(color: theme.dividerColor, width: 0.5)),
|
|
),
|
|
padding: const EdgeInsets.fromLTRB(3, 3, 3, 3),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
_keyRow([
|
|
_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(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)),
|
|
]),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _keyRow(List<Widget> keys) => Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 1.5),
|
|
child: Row(
|
|
children: [for (final key in keys) Expanded(child: key)],
|
|
),
|
|
);
|
|
|
|
/// [big] draws a single glyph (an arrow) larger than a word.
|
|
Widget _key(String label,
|
|
{bool active = false, bool big = false, required VoidCallback onTap}) {
|
|
final theme = Theme.of(context);
|
|
final scheme = theme.colorScheme;
|
|
return GestureDetector(
|
|
behavior: HitTestBehavior.opaque,
|
|
onTap: onTap,
|
|
child: Container(
|
|
height: 30,
|
|
margin: const EdgeInsets.symmetric(horizontal: 1.5),
|
|
alignment: Alignment.center,
|
|
decoration: BoxDecoration(
|
|
color: active
|
|
? scheme.primary.withValues(alpha: 0.22)
|
|
: scheme.surfaceContainerHighest,
|
|
borderRadius: BorderRadius.circular(6),
|
|
border: Border.all(
|
|
color: active ? scheme.primary : Colors.transparent, width: 1),
|
|
),
|
|
child: Text(
|
|
label,
|
|
maxLines: 1,
|
|
style: TextStyle(
|
|
fontFamily: 'monospace',
|
|
fontSize: big ? 16 : 11,
|
|
fontWeight: active ? FontWeight.w700 : FontWeight.w500,
|
|
color: active ? scheme.primary : scheme.onSurface,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
void _sendKey(TerminalSessionState state, TerminalKey key) => state.model
|
|
.keyInput(key, ctrl: state.ctrl, alt: state.alt, shift: state.shift);
|
|
|
|
void _sendText(TerminalSessionState state, String text) =>
|
|
state.model.textInput(text);
|
|
}
|