feat: the terminal, project knowledge, and downloads that can be cancelled
- lib/api/terminal.dart: the PTY protocol (create/resize/restart/close, and the /api/terminal/ws binary control frames) with a reconnecting socket that replays the server snapshot at the size it was drawn for. - lib/terminal_state.dart: an xterm model plus that socket, kept per workspace on AppState, so closing the panel or switching tabs keeps the shell; the app reattaches when it returns from the background. - lib/screens/terminal_panel.dart: TerminalView, a status bar (keyboard, restart, clear, close) and a Termux-style key bar whose CTRL/ALT/SHIFT modifiers also apply to what the phone keyboard types. - lib/screens/knowledge_panel.dart: notes, todos and plans from /api/project-context, searchable, with a full page for a plan's Markdown and its raw editor. - Downloads stream with a progress dialog (bytes, total, percent) and a cancel that aborts the request; the save dialog only opens on success. The request asks for `accept-encoding: identity` so Content-Length exists. - MarkdownView renders task lists as ✅/⬜ instead of material_ui's Checkbox, which wants a material_ui Material ancestor the app lacks. - Dialogs own their TextEditingControllers, so closing one no longer trips a framework assertion in debug builds. - 0.3.11 (311).
This commit is contained in:
1 parent
60d02083d3
commit
5f95e2da97
14 files changed
+2347
-57
No files matched your search
@@ -0,0 +1,279 @@
|
||||
// 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();
|
||||
return;
|
||||
}
|
||||
await _t.restart();
|
||||
}
|
||||
|
||||
Future<void> _closeSession() async {
|
||||
await _t.closeSession();
|
||||
if (mounted) await _t.ensureOpen();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------- key bar
|
||||
|
||||
static const _symbols = [
|
||||
'/', '-', '|', '~', '*', r'$', '#', '_', '=', '+', ':', ';',
|
||||
'"', "'", '(', ')', '[', ']', '{', '}', '<', '>', '?', '!',
|
||||
'&', r'\', '^', '%', '@',
|
||||
];
|
||||
|
||||
Widget _keyBar(ThemeData theme) {
|
||||
return Container(
|
||||
height: 46,
|
||||
decoration: BoxDecoration(
|
||||
color: theme.colorScheme.surfaceContainerHigh,
|
||||
border: Border(top: BorderSide(color: theme.dividerColor, width: 0.5)),
|
||||
),
|
||||
child: SingleChildScrollView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6),
|
||||
child: Row(
|
||||
children: [
|
||||
_key('CTRL', active: _t.ctrl, onTap: _t.toggleCtrl),
|
||||
_key('ALT', active: _t.alt, onTap: _t.toggleAlt),
|
||||
_key('SHIFT', active: _t.shift, onTap: _t.toggleShift),
|
||||
_divider(theme),
|
||||
_key('ESC', onTap: () => _sendKey(TerminalKey.escape)),
|
||||
_key('TAB', onTap: () => _sendKey(TerminalKey.tab)),
|
||||
_divider(theme),
|
||||
_key('←', onTap: () => _sendKey(TerminalKey.arrowLeft)),
|
||||
_key('↑', onTap: () => _sendKey(TerminalKey.arrowUp)),
|
||||
_key('↓', onTap: () => _sendKey(TerminalKey.arrowDown)),
|
||||
_key('→', onTap: () => _sendKey(TerminalKey.arrowRight)),
|
||||
_divider(theme),
|
||||
_key('HOME', onTap: () => _sendKey(TerminalKey.home)),
|
||||
_key('END', onTap: () => _sendKey(TerminalKey.end)),
|
||||
_key('PGUP', onTap: () => _sendKey(TerminalKey.pageUp)),
|
||||
_key('PGDN', onTap: () => _sendKey(TerminalKey.pageDown)),
|
||||
_key('DEL', onTap: () => _sendKey(TerminalKey.delete)),
|
||||
_divider(theme),
|
||||
for (final symbol in _symbols)
|
||||
_key(symbol, onTap: () => _sendText(symbol)),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _divider(ThemeData theme) => Container(
|
||||
width: 1,
|
||||
height: 20,
|
||||
margin: const EdgeInsets.symmetric(horizontal: 4),
|
||||
color: theme.dividerColor,
|
||||
);
|
||||
|
||||
Widget _key(String label,
|
||||
{bool active = false, required VoidCallback onTap}) {
|
||||
final theme = Theme.of(context);
|
||||
final scheme = theme.colorScheme;
|
||||
return GestureDetector(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
onTap: onTap,
|
||||
child: Container(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 2, vertical: 8),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10),
|
||||
alignment: Alignment.center,
|
||||
constraints: const BoxConstraints(minWidth: 34),
|
||||
decoration: BoxDecoration(
|
||||
color: active
|
||||
? scheme.primary.withValues(alpha: 0.22)
|
||||
: scheme.surfaceContainerHighest,
|
||||
borderRadius: BorderRadius.circular(7),
|
||||
border: Border.all(
|
||||
color: active ? scheme.primary : Colors.transparent, width: 1),
|
||||
),
|
||||
child: Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontFamily: 'monospace',
|
||||
fontSize: 12,
|
||||
fontWeight: active ? FontWeight.w700 : FontWeight.w500,
|
||||
color: active ? scheme.primary : scheme.onSurface,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// The terminal state applies the pending modifiers (and clears them) as the
|
||||
// text leaves for the PTY, so both the bar and the phone keyboard take them.
|
||||
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);
|
||||
}
|
||||
Reference in new issue
Block a user