The terminal session id lived only in memory, so the first time the panel opened after a launch it always created another PTY. The old shell stayed on the server (unattached shells live for half an hour) but the client could no longer find it. The id is now remembered per connection and directory. On first open the panel lists /api/terminal/sessions?cwd=<dir> and attaches to the remembered session while the server still has it, showing its replayed history. Without that id it adopts the newest running session of the directory, and only when the directory has nothing does it create one. An explicit "close terminal" forgets the id and forces a fresh shell, and a fatal SESSION_NOT_FOUND clears it and recovers, so a server restart cannot leave a dead panel. The panel is still one terminal per project, not a tab strip. 0.4.2 (402).
272 lines
9.1 KiB
Dart
272 lines
9.1 KiB
Dart
// 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.
|
|
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> {
|
|
TerminalSessionState? _terminal;
|
|
final FocusNode _focus = FocusNode();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_terminal = widget.app.terminalFor(widget.directory);
|
|
_terminal!.ensureOpen();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_focus.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
TerminalSessionState get _t => _terminal!;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = Theme.of(context);
|
|
return ListenableBuilder(
|
|
listenable: _t,
|
|
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',
|
|
),
|
|
padding: const EdgeInsets.fromLTRB(8, 6, 8, 6),
|
|
keyboardType: TextInputType.text,
|
|
deleteDetection: true,
|
|
backgroundOpacity: 1,
|
|
),
|
|
),
|
|
),
|
|
_keyBar(theme),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget _statusBar(ThemeData theme) {
|
|
final scheme = theme.colorScheme;
|
|
final (label, color) = switch (_t.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),
|
|
};
|
|
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),
|
|
),
|
|
),
|
|
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('关闭终端')),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
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;
|
|
}
|
|
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);
|
|
}
|
|
|
|
// ------------------------------------------------------------- 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) {
|
|
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(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)),
|
|
]),
|
|
_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)),
|
|
]),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
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(TerminalKey key) =>
|
|
_t.model.keyInput(key, ctrl: _t.ctrl, alt: _t.alt, shift: _t.shift);
|
|
|
|
void _sendText(String text) => _t.model.textInput(text);
|
|
}
|