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:
blumia committed 2026-11-21 02:49:07 +08:00
1 parent 60d02083d3
commit 5f95e2da97
14 files changed
+2347 -57

No files matched your search

+132 -36
View File
@@ -9,8 +9,11 @@ import 'package:flutter/material.dart';
import '../api/client.dart';
import '../api/models.dart';
import '../api/transport.dart';
import '../state.dart';
import '../widgets/markdown_view.dart';
import 'knowledge_panel.dart';
import 'terminal_panel.dart';
Future<void> showFilesPanel(
BuildContext context, AppState app, Session session) {
@@ -289,16 +292,28 @@ class _FilesPanelState extends State<FilesPanel> {
final client = _client;
final path = _previewPath;
if (client == null || path == null) return;
// A file already in memory needs no progress; the rest stream with one.
var bytes = _previewBytes;
if (bytes == null) {
bytes = await showDialog<Uint8List>(
context: context,
barrierDismissible: false,
builder: (_) => _DownloadDialog(
name: _previewName,
start: (onProgress, cancel) => client.downloadFile(path,
directory: _root, onProgress: onProgress, cancel: cancel),
),
);
}
if (bytes == null || !mounted) return;
try {
final bytes = _previewBytes ??
Uint8List.fromList(await client.rawFile(path, directory: _root));
final uri = await FilePicker.saveFile(
fileName: _previewName,
bytes: bytes,
);
if (uri != null) _toast('已保存');
} catch (e) {
_toast('下载失败:$e');
_toast('保存失败:$e');
}
}
@@ -516,46 +531,15 @@ class _FilesPanelState extends State<FilesPanel> {
}
switch (_tab) {
case 1:
return _placeholder(theme, Icons.menu_book_outlined, '项目知识',
'笔记、待办与计划会在这里。');
return KnowledgePanel(app: widget.app, directory: _root);
case 2:
return _placeholder(
theme, Icons.terminal, '终端', '在服务器上运行的终端会在这里。');
return TerminalPanel(app: widget.app, directory: _root);
default:
if (_previewPath != null) return _previewBody(theme);
return _browser(theme);
}
}
Widget _placeholder(
ThemeData theme, IconData icon, String title, String note) {
return Center(
child: Padding(
padding: const EdgeInsets.all(32),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(icon, size: 40, color: theme.hintColor),
const SizedBox(height: 12),
Text(title,
style: const TextStyle(
fontSize: 16, fontWeight: FontWeight.w600)),
const SizedBox(height: 6),
Text(note,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 13, color: theme.hintColor)),
const SizedBox(height: 10),
Chip(
label: const Text('即将支持'),
visualDensity: VisualDensity.compact,
side: BorderSide(color: theme.dividerColor),
),
],
),
),
);
}
Widget _browser(ThemeData theme) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
@@ -935,3 +919,115 @@ class _FilesPanelState extends State<FilesPanel> {
return '${(bytes / (1024 * 1024)).toStringAsFixed(1)} MB';
}
}
/// Streams one download and shows its progress; 取消 aborts the request and
/// the dialog closes with no bytes, so nothing is saved.
class _DownloadDialog extends StatefulWidget {
final String name;
final Future<Uint8List> Function(
void Function(int received, int? total) onProgress,
CancelToken cancel) start;
const _DownloadDialog({required this.name, required this.start});
@override
State<_DownloadDialog> createState() => _DownloadDialogState();
}
class _DownloadDialogState extends State<_DownloadDialog> {
final CancelToken _cancel = CancelToken();
int _received = 0;
int? _total;
bool _cancelling = false;
String? _error;
int _lastPaint = 0;
@override
void initState() {
super.initState();
_run();
}
Future<void> _run() async {
try {
final bytes = await widget.start((received, total) {
if (!mounted) return;
// Chunks arrive every few dozen KB; repaint a few times a second at
// most, and always on the last one.
final now = DateTime.now().millisecondsSinceEpoch;
if (total != null && received < total && now - _lastPaint < 80) return;
_lastPaint = now;
setState(() {
_received = received;
_total = total;
});
}, _cancel);
if (mounted) Navigator.of(context).pop(bytes);
} on DownloadCancelled {
if (mounted) Navigator.of(context).pop();
} catch (error) {
if (!mounted) return;
setState(() => _error = error is ApiError ? error.message : '$error');
}
}
String _size(int bytes) {
if (bytes < 1024) return '$bytes B';
if (bytes < 1024 * 1024) return '${(bytes / 1024).toStringAsFixed(1)} KB';
return '${(bytes / (1024 * 1024)).toStringAsFixed(1)} MB';
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final total = _total;
final fraction = total != null && total > 0
? (_received / total).clamp(0.0, 1.0)
: null;
return AlertDialog(
title: const Text('正在下载'),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(widget.name,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: const TextStyle(fontSize: 13)),
const SizedBox(height: 12),
if (_error == null) ...[
LinearProgressIndicator(value: fraction),
const SizedBox(height: 8),
Text(
total != null
? '${_size(_received)} / ${_size(total)}'
'(${(fraction! * 100).toStringAsFixed(0)}%)'
: '已下载 ${_size(_received)}',
style: TextStyle(fontSize: 12, color: theme.hintColor),
),
] else
Text(_error!,
style:
TextStyle(fontSize: 12.5, color: theme.colorScheme.error)),
],
),
actions: [
if (_error == null)
TextButton(
onPressed: _cancelling
? null
: () {
setState(() => _cancelling = true);
_cancel.cancel();
},
child: Text(_cancelling ? '正在取消…' : '取消'),
)
else
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('关闭'),
),
],
);
}
}