2026-11-22 15:23:21 +08:00
|
|
|
// 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.
|
2026-11-21 02:49:07 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:xterm/xterm.dart';
|
|
|
|
|
|
|
|
|
|
import '../api/terminal.dart';
|
|
|
|
|
import '../state.dart';
|
|
|
|
|
import '../terminal_state.dart';
|
2026-11-26 09:04:25 +08:00
|
|
|
import '../l10n.dart';
|
2026-11-21 02:49:07 +08:00
|
|
|
|
|
|
|
|
/// 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> {
|
2026-11-22 15:23:21 +08:00
|
|
|
late final TerminalWorkspace _workspace;
|
2026-11-21 02:49:07 +08:00
|
|
|
final FocusNode _focus = FocusNode();
|
|
|
|
|
|
2026-11-22 15:23:21 +08:00
|
|
|
/// The dropdown's value for "new terminal"; tab values are their index.
|
|
|
|
|
static const _newTab = -1;
|
|
|
|
|
|
2026-11-21 02:49:07 +08:00
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
2026-11-22 15:23:21 +08:00
|
|
|
_workspace = widget.app.terminalFor(widget.directory);
|
|
|
|
|
_workspace.ensureOpen();
|
2026-11-21 02:49:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
_focus.dispose();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
final theme = Theme.of(context);
|
|
|
|
|
return ListenableBuilder(
|
2026-11-22 15:23:21 +08:00
|
|
|
listenable: _workspace,
|
2026-11-21 02:49:07 +08:00
|
|
|
builder: (context, _) {
|
2026-11-22 15:23:21 +08:00
|
|
|
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,
|
|
|
|
|
),
|
2026-11-21 02:49:07 +08:00
|
|
|
),
|
|
|
|
|
),
|
2026-11-22 15:23:21 +08:00
|
|
|
_keyBar(theme, state),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
},
|
2026-11-21 02:49:07 +08:00
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 15:23:21 +08:00
|
|
|
Widget _statusBar(
|
|
|
|
|
ThemeData theme, TerminalTab tab, TerminalSessionState state) {
|
2026-11-21 02:49:07 +08:00
|
|
|
final scheme = theme.colorScheme;
|
2026-11-22 15:23:21 +08:00
|
|
|
final (label, color) = switch (state.link) {
|
2026-11-26 09:04:25 +08:00
|
|
|
TerminalLinkState.idle => (tr('未启动'), theme.hintColor),
|
|
|
|
|
TerminalLinkState.creating => (tr('正在创建…'), theme.hintColor),
|
|
|
|
|
TerminalLinkState.connecting => (tr('正在连接…'), theme.hintColor),
|
|
|
|
|
TerminalLinkState.reconnecting =>
|
|
|
|
|
(tr('连接中断,正在重连…'), scheme.error),
|
|
|
|
|
TerminalLinkState.open => (tr('运行中'), scheme.primary),
|
|
|
|
|
TerminalLinkState.exited => (state.detail ?? tr('已退出'), theme.hintColor),
|
|
|
|
|
TerminalLinkState.error => (state.detail ?? tr('连接失败'), scheme.error),
|
2026-11-21 02:49:07 +08:00
|
|
|
};
|
2026-11-22 15:23:21 +08:00
|
|
|
return Row(
|
|
|
|
|
children: [
|
|
|
|
|
Expanded(
|
|
|
|
|
child: PopupMenuButton<int>(
|
2026-11-26 09:04:25 +08:00
|
|
|
tooltip: tr('切换终端'),
|
2026-11-22 15:23:21 +08:00
|
|
|
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(
|
2026-11-26 09:04:25 +08:00
|
|
|
child: AppText(
|
2026-11-22 15:23:21 +08:00
|
|
|
_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),
|
2026-11-26 09:04:25 +08:00
|
|
|
AppText('新建终端'),
|
2026-11-22 15:23:21 +08:00
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
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(
|
2026-11-26 09:04:25 +08:00
|
|
|
child: AppText(
|
2026-11-22 15:23:21 +08:00
|
|
|
tab.label,
|
|
|
|
|
maxLines: 1,
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
style: const TextStyle(
|
|
|
|
|
fontSize: 12.5, fontWeight: FontWeight.w600),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(width: 4),
|
|
|
|
|
Flexible(
|
2026-11-26 09:04:25 +08:00
|
|
|
child: AppText(
|
2026-11-22 15:23:21 +08:00
|
|
|
'· $label',
|
|
|
|
|
maxLines: 1,
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
style: TextStyle(fontSize: 12, color: color),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
Icon(Icons.arrow_drop_down, size: 20, color: theme.hintColor),
|
|
|
|
|
],
|
|
|
|
|
),
|
2026-11-21 02:49:07 +08:00
|
|
|
),
|
|
|
|
|
),
|
2026-11-22 15:23:21 +08:00
|
|
|
),
|
|
|
|
|
IconButton(
|
2026-11-26 09:04:25 +08:00
|
|
|
tooltip: tr('键盘'),
|
2026-11-22 15:23:21 +08:00
|
|
|
visualDensity: VisualDensity.compact,
|
|
|
|
|
icon: const Icon(Icons.keyboard_outlined, size: 18),
|
|
|
|
|
onPressed: () => _focus.requestFocus(),
|
|
|
|
|
),
|
|
|
|
|
IconButton(
|
2026-11-26 09:04:25 +08:00
|
|
|
tooltip: tr('重新启动'),
|
2026-11-22 15:23:21 +08:00
|
|
|
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>(
|
2026-11-26 09:04:25 +08:00
|
|
|
tooltip: tr('更多'),
|
2026-11-22 15:23:21 +08:00
|
|
|
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 [
|
2026-11-26 09:04:25 +08:00
|
|
|
PopupMenuItem(value: 'clear', child: AppText('清屏')),
|
|
|
|
|
PopupMenuItem(value: 'close', child: AppText('关闭此终端')),
|
2026-11-22 15:23:21 +08:00
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
2026-11-21 02:49:07 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 15:23:21 +08:00
|
|
|
void _onTabMenu(int value) {
|
|
|
|
|
if (value == _newTab) {
|
|
|
|
|
_workspace.addTab();
|
|
|
|
|
} else {
|
|
|
|
|
_workspace.select(value);
|
2026-11-21 02:49:07 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 15:23:21 +08:00
|
|
|
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();
|
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-22 15:23:21 +08:00
|
|
|
Widget _keyBar(ThemeData theme, TerminalSessionState state) {
|
2026-11-21 02:49:07 +08:00
|
|
|
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-22 15:23:21 +08:00
|
|
|
_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)),
|
2026-11-21 11:09:44 +08:00
|
|
|
]),
|
|
|
|
|
_keyRow([
|
2026-11-22 15:23:21 +08:00
|
|
|
_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)),
|
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),
|
|
|
|
|
),
|
2026-11-26 09:04:25 +08:00
|
|
|
child: AppText(
|
2026-11-21 02:49:07 +08:00
|
|
|
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,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 15:23:21 +08:00
|
|
|
void _sendKey(TerminalSessionState state, TerminalKey key) => state.model
|
|
|
|
|
.keyInput(key, ctrl: state.ctrl, alt: state.alt, shift: state.shift);
|
2026-11-21 02:49:07 +08:00
|
|
|
|
2026-11-22 15:23:21 +08:00
|
|
|
void _sendText(TerminalSessionState state, String text) =>
|
|
|
|
|
state.model.textInput(text);
|
2026-11-26 09:04:25 +08:00
|
|
|
}
|