2026-11-21 02:49:07 +08:00
|
|
|
// 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();
|
2026-11-22 14:53:33 +08:00
|
|
|
await _t.ensureOpen(fresh: true);
|
2026-11-21 02:49:07 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
await _t.restart();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _closeSession() async {
|
|
|
|
|
await _t.closeSession();
|
2026-11-22 14:53:33 +08:00
|
|
|
// 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);
|
2026-11-21 02:49:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------- key bar
|
|
|
|
|
|
2026-11-21 11:09:44 +08:00
|
|
|
/// Two rows of seven, the way Termux lays them out: the keys a phone
|
|
|
|
|
/// keyboard cannot produce, all reachable with a thumb.
|
2026-11-21 02:49:07 +08:00
|
|
|
Widget _keyBar(ThemeData theme) {
|
|
|
|
|
return Container(
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: theme.colorScheme.surfaceContainerHigh,
|
|
|
|
|
border: Border(top: BorderSide(color: theme.dividerColor, width: 0.5)),
|
|
|
|
|
),
|
2026-11-21 11:09:44 +08:00
|
|
|
padding: const EdgeInsets.fromLTRB(3, 3, 3, 3),
|
|
|
|
|
child: Column(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
_keyRow([
|
2026-11-21 02:49:07 +08:00
|
|
|
_key('ESC', onTap: () => _sendKey(TerminalKey.escape)),
|
2026-11-21 11:09:44 +08:00
|
|
|
_key('/', onTap: () => _sendText('/')),
|
|
|
|
|
_key('—', onTap: () => _sendText('-')),
|
2026-11-21 02:49:07 +08:00
|
|
|
_key('HOME', onTap: () => _sendKey(TerminalKey.home)),
|
2026-11-21 11:09:44 +08:00
|
|
|
_key('↑', big: true, onTap: () => _sendKey(TerminalKey.arrowUp)),
|
2026-11-21 02:49:07 +08:00
|
|
|
_key('END', onTap: () => _sendKey(TerminalKey.end)),
|
|
|
|
|
_key('PGUP', onTap: () => _sendKey(TerminalKey.pageUp)),
|
2026-11-21 11:09:44 +08:00
|
|
|
]),
|
|
|
|
|
_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)),
|
2026-11-21 02:49:07 +08:00
|
|
|
_key('PGDN', onTap: () => _sendKey(TerminalKey.pageDown)),
|
2026-11-21 11:09:44 +08:00
|
|
|
]),
|
|
|
|
|
],
|
2026-11-21 02:49:07 +08:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-21 11:09:44 +08:00
|
|
|
Widget _keyRow(List<Widget> keys) => Padding(
|
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 1.5),
|
|
|
|
|
child: Row(
|
|
|
|
|
children: [for (final key in keys) Expanded(child: key)],
|
|
|
|
|
),
|
2026-11-21 02:49:07 +08:00
|
|
|
);
|
|
|
|
|
|
2026-11-21 11:09:44 +08:00
|
|
|
/// [big] draws a single glyph (an arrow) larger than a word.
|
2026-11-21 02:49:07 +08:00
|
|
|
Widget _key(String label,
|
2026-11-21 11:09:44 +08:00
|
|
|
{bool active = false, bool big = false, required VoidCallback onTap}) {
|
2026-11-21 02:49:07 +08:00
|
|
|
final theme = Theme.of(context);
|
|
|
|
|
final scheme = theme.colorScheme;
|
|
|
|
|
return GestureDetector(
|
|
|
|
|
behavior: HitTestBehavior.opaque,
|
|
|
|
|
onTap: onTap,
|
|
|
|
|
child: Container(
|
2026-11-21 11:09:44 +08:00
|
|
|
height: 30,
|
|
|
|
|
margin: const EdgeInsets.symmetric(horizontal: 1.5),
|
2026-11-21 02:49:07 +08:00
|
|
|
alignment: Alignment.center,
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: active
|
|
|
|
|
? scheme.primary.withValues(alpha: 0.22)
|
|
|
|
|
: scheme.surfaceContainerHighest,
|
2026-11-21 11:09:44 +08:00
|
|
|
borderRadius: BorderRadius.circular(6),
|
2026-11-21 02:49:07 +08:00
|
|
|
border: Border.all(
|
|
|
|
|
color: active ? scheme.primary : Colors.transparent, width: 1),
|
|
|
|
|
),
|
|
|
|
|
child: Text(
|
|
|
|
|
label,
|
2026-11-21 11:09:44 +08:00
|
|
|
maxLines: 1,
|
2026-11-21 02:49:07 +08:00
|
|
|
style: TextStyle(
|
|
|
|
|
fontFamily: 'monospace',
|
2026-11-21 11:09:44 +08:00
|
|
|
fontSize: big ? 16 : 11,
|
2026-11-21 02:49:07 +08:00
|
|
|
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);
|
|
|
|
|
}
|