feat: a transport seam, image previews, native forms, a phone drawer
- lib/api/transport.dart: TransportRequest/TransportResponse/Transport, with DirectTransport holding the old behaviour (HTTP CONNECT, SOCKS5/socks5h, self-signed and pinned certificates). Client goes through it, so a relay tunnel only adds an implementation. - User rows decode the server's inline attachment data and open the shared image preview on tap; the local path stays the fallback. - The agent's native forms: models from @opencode/client, the form list, replies and cancels, form.* events, and a dock above the composer with when-rules and required-field validation (a switch must be on). - HomeScreen: the session list is the phone's left drawer and a persistent column from 840 px, the chat beside it; the newest session opens on start. - 0.3.10 (310).
This commit is contained in:
1 parent
9b53338908
commit
60d02083d3
11 files changed
+1272
-320
No files matched your search
+479
-14
@@ -18,7 +18,16 @@ import 'files_panel.dart';
|
||||
class ChatScreen extends StatefulWidget {
|
||||
final AppState app;
|
||||
final Session session;
|
||||
const ChatScreen({super.key, required this.app, required this.session});
|
||||
|
||||
/// Opens the session drawer. Null where the list is always on screen (a
|
||||
/// wide layout), so no menu button is shown there.
|
||||
final VoidCallback? onOpenSessions;
|
||||
const ChatScreen({
|
||||
super.key,
|
||||
required this.app,
|
||||
required this.session,
|
||||
this.onOpenSessions,
|
||||
});
|
||||
|
||||
@override
|
||||
State<ChatScreen> createState() => _ChatScreenState();
|
||||
@@ -36,6 +45,9 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
List<_Attach> _attachments = [];
|
||||
bool _attaching = false;
|
||||
|
||||
/// Decoded user attachments, so a rebuild does not base64-decode again.
|
||||
final Map<String, Uint8List> _userImages = {};
|
||||
|
||||
/// `/` palette: the highlighted row, and the draft Esc closed it at.
|
||||
int _slashIndex = 0;
|
||||
String? _slashDismissed;
|
||||
@@ -90,6 +102,13 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
final theme = Theme.of(context);
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
leading: widget.onOpenSessions == null
|
||||
? null
|
||||
: IconButton(
|
||||
icon: const Icon(Icons.menu),
|
||||
tooltip: '会话',
|
||||
onPressed: widget.onOpenSessions,
|
||||
),
|
||||
title: Text(widget.session.title.isEmpty ? '会话' : widget.session.title,
|
||||
maxLines: 1, overflow: TextOverflow.ellipsis),
|
||||
actions: [
|
||||
@@ -131,6 +150,7 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
),
|
||||
Expanded(child: _messageList()),
|
||||
_permissions(),
|
||||
_forms(),
|
||||
_composer(),
|
||||
],
|
||||
),
|
||||
@@ -376,8 +396,15 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
|
||||
Widget _userRow(String id) {
|
||||
final m = chat.byId[id]!;
|
||||
final images = chat.imagePaths[id] ?? [];
|
||||
final theme = Theme.of(context);
|
||||
// The server keeps attached images in the message; the paths only cover
|
||||
// the moment between sending and the next refresh.
|
||||
final attachedImages = [
|
||||
for (var i = 0; i < m.files.length; i++)
|
||||
if (m.files[i].isImage) i,
|
||||
];
|
||||
final localImages = attachedImages.isEmpty ? chat.imagePaths[id] ?? [] : const <String>[];
|
||||
final otherFiles = m.files.where((f) => !f.isImage).toList();
|
||||
return Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child: Container(
|
||||
@@ -393,20 +420,43 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
children: [
|
||||
if (m.text.isNotEmpty)
|
||||
Text(m.text, style: TextStyle(color: theme.colorScheme.onPrimary)),
|
||||
for (final p in images)
|
||||
for (final i in attachedImages)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 4),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
child: Image.file(File(p), width: 200, height: 150, fit: BoxFit.cover),
|
||||
padding: const EdgeInsets.only(top: 6),
|
||||
child: GestureDetector(
|
||||
onTap: () => _previewAttachment(m.files[i]),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
child: _attachmentImage(id, i, m.files[i]),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (m.files.isNotEmpty)
|
||||
for (final p in localImages)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 4),
|
||||
child: Text(
|
||||
m.files.map((f) => '📎 ${f.name}').join('\n'),
|
||||
style: TextStyle(color: theme.colorScheme.onPrimary, fontSize: 13),
|
||||
child: GestureDetector(
|
||||
onTap: () => _openPreview(_ImageRef(p, p.split('/').last)),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
child: Image.file(File(p),
|
||||
width: 200, height: 150, fit: BoxFit.cover),
|
||||
),
|
||||
),
|
||||
),
|
||||
for (final f in otherFiles)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 4),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: theme.colorScheme.onPrimary.withValues(alpha: 0.14),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text('📎 ${f.name.isEmpty ? '附件' : f.name}',
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
color: theme.colorScheme.onPrimary, fontSize: 12.5)),
|
||||
),
|
||||
),
|
||||
],
|
||||
@@ -415,6 +465,28 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _attachmentImage(String messageId, int index, FileAttachment f) {
|
||||
final key = '$messageId/$index';
|
||||
Uint8List? bytes = _userImages[key];
|
||||
if (bytes == null) {
|
||||
try {
|
||||
bytes = base64Decode(f.data);
|
||||
_userImages[key] = bytes;
|
||||
} catch (_) {
|
||||
return const SizedBox(
|
||||
width: 200, height: 60, child: Center(child: Text('图片无法显示')));
|
||||
}
|
||||
}
|
||||
return Image.memory(bytes, width: 200, height: 150, fit: BoxFit.cover);
|
||||
}
|
||||
|
||||
void _previewAttachment(FileAttachment f) {
|
||||
_showImageDialog(
|
||||
f.name.isEmpty ? '图片' : f.name,
|
||||
source: 'data:${f.mime};base64,${f.data}',
|
||||
);
|
||||
}
|
||||
|
||||
Widget _userActions(String id) {
|
||||
final m = chat.byId[id]!;
|
||||
final stamp = m.created > 0
|
||||
@@ -1747,6 +1819,14 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
|
||||
// ---------------------------------------------------------- permissions
|
||||
|
||||
/// The agent's blocking questions sit just above the composer, like the
|
||||
/// permissions dock: answering one lets the turn continue.
|
||||
Widget _forms() {
|
||||
if (chat.forms.isEmpty) return const SizedBox.shrink();
|
||||
final form = chat.forms.first;
|
||||
return _FormDock(key: ValueKey(form.id), chat: chat, form: form);
|
||||
}
|
||||
|
||||
Widget _permissions() {
|
||||
final perms = widget.app.perms
|
||||
.where((p) => p.sessionId.isEmpty || p.sessionId == chat.id)
|
||||
@@ -1922,12 +2002,15 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
|
||||
// ------------------------------------------------------------ previews
|
||||
|
||||
Future<void> _openPreview(_ImageRef ref) async {
|
||||
Future<void> _openPreview(_ImageRef ref) =>
|
||||
_showImageDialog(ref.label, source: ref.source);
|
||||
|
||||
Future<void> _showImageDialog(String title, {required String source}) async {
|
||||
await showDialog(
|
||||
context: context,
|
||||
builder: (_) => _ImagePreviewDialog(
|
||||
title: ref.label,
|
||||
source: ref.source,
|
||||
title: title,
|
||||
source: source,
|
||||
directory: chat.dir,
|
||||
client: widget.app.client!,
|
||||
),
|
||||
@@ -2300,3 +2383,385 @@ class _ImagePreviewDialogState extends State<_ImagePreviewDialog> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The native form/question card: every visible field at once, one answer.
|
||||
/// Field visibility follows the form's `when` clauses, and required fields
|
||||
/// must be filled before it can be sent.
|
||||
class _FormDock extends StatefulWidget {
|
||||
final ChatState chat;
|
||||
final FormInfo form;
|
||||
const _FormDock({super.key, required this.chat, required this.form});
|
||||
|
||||
@override
|
||||
State<_FormDock> createState() => _FormDockState();
|
||||
}
|
||||
|
||||
class _FormDockState extends State<_FormDock> {
|
||||
final Map<String, Object?> _values = {};
|
||||
final Map<String, TextEditingController> _controllers = {};
|
||||
String? _error;
|
||||
bool _sending = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
for (final f in widget.form.fields) {
|
||||
switch (f.type) {
|
||||
case 'boolean':
|
||||
_values[f.key] = f.defaultValue is bool ? f.defaultValue : false;
|
||||
break;
|
||||
case 'multiselect':
|
||||
_values[f.key] = f.defaultValue is List
|
||||
? List<String>.from((f.defaultValue as List).whereType<String>())
|
||||
: <String>[];
|
||||
break;
|
||||
case 'external':
|
||||
_values[f.key] = true;
|
||||
break;
|
||||
default:
|
||||
if (f.defaultValue != null) {
|
||||
_values[f.key] = f.defaultValue;
|
||||
_controllers[f.key] =
|
||||
TextEditingController(text: '${f.defaultValue}');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
for (final c in _controllers.values) {
|
||||
c.dispose();
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
bool _visible(AgentFormField f) {
|
||||
if (f.hidden) return false;
|
||||
for (final w in f.when) {
|
||||
final v = _values[w.key];
|
||||
final eq = v == w.value ||
|
||||
(v is num && w.value is num && v == w.value) ||
|
||||
(v is bool && w.value is bool && v == w.value);
|
||||
if (w.op == 'neq' ? eq : !eq) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
TextEditingController _controller(AgentFormField f) =>
|
||||
_controllers.putIfAbsent(f.key, () => TextEditingController());
|
||||
|
||||
void _set(AgentFormField f, Object? value) {
|
||||
setState(() {
|
||||
_values[f.key] = value;
|
||||
_error = null;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _submit() async {
|
||||
final visible = widget.form.fields.where(_visible).toList();
|
||||
final missing = <String>[];
|
||||
for (final f in visible) {
|
||||
if (!f.required || f.type == 'external') continue;
|
||||
final v = _values[f.key];
|
||||
// A required switch or acknowledgement has to be on, not merely set.
|
||||
final empty = v == null ||
|
||||
v == false ||
|
||||
(v is String && v.trim().isEmpty) ||
|
||||
(v is List && v.isEmpty);
|
||||
if (empty) missing.add(f.label);
|
||||
}
|
||||
if (missing.isNotEmpty) {
|
||||
setState(() => _error = '请填写:${missing.join('、')}');
|
||||
return;
|
||||
}
|
||||
final answer = <String, Object?>{};
|
||||
for (final f in visible) {
|
||||
if (f.type == 'external') {
|
||||
answer[f.key] = true;
|
||||
continue;
|
||||
}
|
||||
final v = _values[f.key];
|
||||
if (v == null) continue;
|
||||
if (v is String && v.trim().isEmpty) continue;
|
||||
if (v is List && v.isEmpty) continue;
|
||||
answer[f.key] = v;
|
||||
}
|
||||
setState(() {
|
||||
_sending = true;
|
||||
_error = null;
|
||||
});
|
||||
await widget.chat.replyForm(widget.form, answer);
|
||||
if (mounted) setState(() => _sending = false);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final scheme = theme.colorScheme;
|
||||
final fields = widget.form.fields.where(_visible).toList();
|
||||
return Card(
|
||||
margin: const EdgeInsets.fromLTRB(12, 4, 12, 4),
|
||||
color: scheme.surfaceContainerHigh,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(12, 10, 12, 8),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(Icons.help_outline, size: 18, color: scheme.primary),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
widget.form.title.isEmpty ? '请选择' : widget.form.title,
|
||||
style: const TextStyle(
|
||||
fontSize: 14, fontWeight: FontWeight.w600),
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
style: TextButton.styleFrom(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8),
|
||||
minimumSize: const Size(0, 32),
|
||||
),
|
||||
onPressed: _sending
|
||||
? null
|
||||
: () => widget.chat.cancelForm(widget.form),
|
||||
child: const Text('取消', style: TextStyle(fontSize: 12.5)),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (fields.isNotEmpty)
|
||||
ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxHeight: 360),
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
for (final f in fields) _field(f, theme),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
if (_error != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 4),
|
||||
child: Text(_error!,
|
||||
style: TextStyle(fontSize: 12, color: scheme.error)),
|
||||
),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
FilledButton(
|
||||
onPressed: _sending ? null : _submit,
|
||||
child: _sending
|
||||
? const SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(strokeWidth: 2))
|
||||
: const Text('发送'),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _field(AgentFormField f, ThemeData theme) {
|
||||
final scheme = theme.colorScheme;
|
||||
final title = Row(
|
||||
children: [
|
||||
Flexible(
|
||||
child: Text(f.label,
|
||||
style:
|
||||
const TextStyle(fontSize: 13, fontWeight: FontWeight.w600)),
|
||||
),
|
||||
if (f.required)
|
||||
Text(' *', style: TextStyle(color: scheme.error, fontSize: 13)),
|
||||
],
|
||||
);
|
||||
Widget hint() => f.description == null || f.description!.isEmpty
|
||||
? const SizedBox.shrink()
|
||||
: Padding(
|
||||
padding: const EdgeInsets.only(top: 2, bottom: 2),
|
||||
child: Text(f.description!,
|
||||
style: TextStyle(fontSize: 12, color: theme.hintColor)),
|
||||
);
|
||||
switch (f.type) {
|
||||
case 'external':
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
title,
|
||||
hint(),
|
||||
if (f.url != null && f.url!.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 2),
|
||||
child: Text(f.url!,
|
||||
style: TextStyle(
|
||||
fontSize: 12, color: scheme.primary),
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
case 'boolean':
|
||||
return SwitchListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
dense: true,
|
||||
title: title,
|
||||
subtitle: hint(),
|
||||
value: _values[f.key] == true,
|
||||
onChanged: (v) => _set(f, v),
|
||||
);
|
||||
case 'multiselect':
|
||||
final selected = List<String>.from(
|
||||
(_values[f.key] as List?)?.whereType<String>() ?? const []);
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
title,
|
||||
hint(),
|
||||
Wrap(
|
||||
spacing: 6,
|
||||
runSpacing: -6,
|
||||
children: [
|
||||
for (final o in f.options)
|
||||
FilterChip(
|
||||
label: Text(o.label, style: const TextStyle(fontSize: 12.5)),
|
||||
selected: selected.contains(o.value),
|
||||
onSelected: (on) {
|
||||
final next = [...selected];
|
||||
if (on) {
|
||||
next.add(o.value);
|
||||
} else {
|
||||
next.remove(o.value);
|
||||
}
|
||||
_set(f, next);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
case 'string':
|
||||
if (f.options.isNotEmpty) {
|
||||
final current = _values[f.key]?.toString();
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
title,
|
||||
hint(),
|
||||
Wrap(
|
||||
spacing: 6,
|
||||
runSpacing: -6,
|
||||
children: [
|
||||
for (final o in f.options)
|
||||
ChoiceChip(
|
||||
label: Text(o.label,
|
||||
style: const TextStyle(fontSize: 12.5)),
|
||||
selected: current == o.value,
|
||||
onSelected: (_) => _set(f, o.value),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (f.custom)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 6),
|
||||
child: TextField(
|
||||
controller: _controller(f),
|
||||
onChanged: (v) => _set(f, v),
|
||||
style: const TextStyle(fontSize: 13),
|
||||
decoration: InputDecoration(
|
||||
isDense: true,
|
||||
hintText: '其他…',
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8)),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
final numeric = f.format == 'number';
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
title,
|
||||
hint(),
|
||||
const SizedBox(height: 4),
|
||||
TextField(
|
||||
controller: _controller(f),
|
||||
onChanged: (v) => _set(f, v),
|
||||
keyboardType: f.format == 'email'
|
||||
? TextInputType.emailAddress
|
||||
: f.format == 'uri'
|
||||
? TextInputType.url
|
||||
: numeric
|
||||
? TextInputType.number
|
||||
: TextInputType.text,
|
||||
style: const TextStyle(fontSize: 13),
|
||||
decoration: InputDecoration(
|
||||
isDense: true,
|
||||
hintText: f.placeholder ??
|
||||
(f.format == 'date' ? 'YYYY-MM-DD' : null),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8)),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
case 'number':
|
||||
case 'integer':
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
title,
|
||||
hint(),
|
||||
const SizedBox(height: 4),
|
||||
TextField(
|
||||
controller: _controller(f),
|
||||
keyboardType:
|
||||
TextInputType.numberWithOptions(decimal: f.type == 'number'),
|
||||
onChanged: (v) {
|
||||
final n = f.type == 'integer'
|
||||
? int.tryParse(v)
|
||||
: num.tryParse(v);
|
||||
_set(f, n ?? (v.isEmpty ? null : v));
|
||||
},
|
||||
style: const TextStyle(fontSize: 13),
|
||||
decoration: InputDecoration(
|
||||
isDense: true,
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8)),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
default:
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text('${f.label}(${f.type})',
|
||||
style: const TextStyle(fontSize: 13)),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
// The connected shell. A phone treats the session list as the left drawer and
|
||||
// the chat as the screen; a wide layout shows both side by side, which is
|
||||
// where a tablet or a desktop window lands.
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../state.dart';
|
||||
import 'chat_screen.dart';
|
||||
import 'sessions_screen.dart';
|
||||
|
||||
class HomeScreen extends StatefulWidget {
|
||||
final AppState app;
|
||||
const HomeScreen({super.key, required this.app});
|
||||
|
||||
@override
|
||||
State<HomeScreen> createState() => _HomeScreenState();
|
||||
}
|
||||
|
||||
class _HomeScreenState extends State<HomeScreen> {
|
||||
final GlobalKey<ScaffoldState> _scaffold = GlobalKey();
|
||||
bool _opening = false;
|
||||
|
||||
static const _wideBreakpoint = 840.0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
widget.app.addListener(_maybeOpenFirst);
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => _maybeOpenFirst());
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
widget.app.removeListener(_maybeOpenFirst);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
/// Like the first-party client, a phone lands in a conversation rather than
|
||||
/// on an empty pane: open the newest one when nothing is open yet.
|
||||
void _maybeOpenFirst() {
|
||||
final app = widget.app;
|
||||
if (!mounted || _opening) return;
|
||||
if (app.chat != null || app.sessions.isEmpty) return;
|
||||
_opening = true;
|
||||
app.openChat(app.sessions.first).whenComplete(() => _opening = false);
|
||||
}
|
||||
|
||||
void _closeDrawer() {
|
||||
_scaffold.currentState?.closeDrawer();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListenableBuilder(
|
||||
listenable: widget.app,
|
||||
builder: (context, _) {
|
||||
return LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final wide = constraints.maxWidth >= _wideBreakpoint;
|
||||
final chat = widget.app.chat;
|
||||
final chatView = chat == null
|
||||
? _empty(wide)
|
||||
: ChatScreen(
|
||||
key: ValueKey(chat.session.id),
|
||||
app: widget.app,
|
||||
session: chat.session,
|
||||
onOpenSessions: wide ? null : () =>
|
||||
_scaffold.currentState?.openDrawer(),
|
||||
);
|
||||
if (wide) {
|
||||
return Scaffold(
|
||||
body: Row(
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 320,
|
||||
child: SessionListPane(app: widget.app),
|
||||
),
|
||||
const VerticalDivider(width: 1),
|
||||
Expanded(child: chatView),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
return Scaffold(
|
||||
key: _scaffold,
|
||||
drawer: Drawer(
|
||||
width: constraints.maxWidth * 0.86 > 380
|
||||
? 380
|
||||
: constraints.maxWidth * 0.86,
|
||||
child: SessionListPane(
|
||||
app: widget.app,
|
||||
onOpened: _closeDrawer,
|
||||
),
|
||||
),
|
||||
body: chatView,
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _empty(bool wide) {
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.forum_outlined, size: 40, color: scheme.onSurfaceVariant),
|
||||
const SizedBox(height: 12),
|
||||
Text('选择一个会话',
|
||||
style: TextStyle(fontSize: 16, color: scheme.onSurfaceVariant)),
|
||||
if (!wide) ...[
|
||||
const SizedBox(height: 12),
|
||||
FilledButton(
|
||||
onPressed: () => _scaffold.currentState?.openDrawer(),
|
||||
child: const Text('打开会话列表'),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -5,18 +5,23 @@ import 'package:flutter/material.dart';
|
||||
import '../api/models.dart';
|
||||
import '../state.dart';
|
||||
import 'servers_screen.dart';
|
||||
import 'chat_screen.dart';
|
||||
|
||||
/// The connected screen: the live session list.
|
||||
class SessionsScreen extends StatefulWidget {
|
||||
/// The session list: a drawer on a phone, a persistent column on a wide
|
||||
/// layout. Selecting a session opens it in the chat beside it (the shell owns
|
||||
/// that), so this only reports the choice through [onOpened].
|
||||
class SessionListPane extends StatefulWidget {
|
||||
final AppState app;
|
||||
const SessionsScreen({super.key, required this.app});
|
||||
|
||||
/// Called after a session was opened, so the drawer can close itself.
|
||||
final VoidCallback? onOpened;
|
||||
|
||||
const SessionListPane({super.key, required this.app, this.onOpened});
|
||||
|
||||
@override
|
||||
State<SessionsScreen> createState() => _SessionsScreenState();
|
||||
State<SessionListPane> createState() => _SessionListPaneState();
|
||||
}
|
||||
|
||||
class _SessionsScreenState extends State<SessionsScreen> {
|
||||
class _SessionListPaneState extends State<SessionListPane> {
|
||||
bool _searchOpen = false;
|
||||
Timer? _timer;
|
||||
late final TextEditingController _searchCtrl;
|
||||
@@ -51,40 +56,48 @@ class _SessionsScreenState extends State<SessionsScreen> {
|
||||
return ListenableBuilder(
|
||||
listenable: app,
|
||||
builder: (context, _) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('会话'),
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.dns_outlined),
|
||||
tooltip: '服务器',
|
||||
onPressed: _instancesSheet,
|
||||
),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: Icon(_searchOpen ? Icons.close : Icons.search),
|
||||
tooltip: '搜索',
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_searchOpen = !_searchOpen;
|
||||
if (!_searchOpen) {
|
||||
_searchCtrl.clear();
|
||||
app.refreshSessions();
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.add),
|
||||
tooltip: '新建会话',
|
||||
onPressed: _newSessionSheet,
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Column(
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
return SafeArea(
|
||||
child: Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(4, 6, 4, 0),
|
||||
child: Row(
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.dns_outlined),
|
||||
tooltip: '服务器',
|
||||
onPressed: _instancesSheet,
|
||||
),
|
||||
const Expanded(
|
||||
child: Text('会话',
|
||||
style: TextStyle(
|
||||
fontSize: 20, fontWeight: FontWeight.w600)),
|
||||
),
|
||||
IconButton(
|
||||
icon: Icon(_searchOpen ? Icons.close : Icons.search),
|
||||
tooltip: '搜索',
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_searchOpen = !_searchOpen;
|
||||
if (!_searchOpen) {
|
||||
_searchCtrl.clear();
|
||||
app.refreshSessions();
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.add),
|
||||
tooltip: '新建会话',
|
||||
onPressed: _newSessionSheet,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (_searchOpen)
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 4, 16, 4),
|
||||
padding: const EdgeInsets.fromLTRB(12, 2, 12, 4),
|
||||
child: TextField(
|
||||
controller: _searchCtrl,
|
||||
autofocus: true,
|
||||
@@ -99,14 +112,17 @@ class _SessionsScreenState extends State<SessionsScreen> {
|
||||
),
|
||||
if (app.error != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(app.error!,
|
||||
style: TextStyle(color: Theme.of(context).colorScheme.error, fontSize: 13)),
|
||||
style: TextStyle(
|
||||
color: scheme.error, fontSize: 13)),
|
||||
),
|
||||
TextButton(onPressed: app.refreshSessions, child: const Text('重试')),
|
||||
TextButton(
|
||||
onPressed: app.refreshSessions,
|
||||
child: const Text('重试')),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -130,7 +146,8 @@ class _SessionsScreenState extends State<SessionsScreen> {
|
||||
children: [
|
||||
const Text('暂无会话'),
|
||||
const SizedBox(height: 8),
|
||||
FilledButton(onPressed: _newSessionSheet, child: const Text('新建会话')),
|
||||
FilledButton(
|
||||
onPressed: _newSessionSheet, child: const Text('新建会话')),
|
||||
],
|
||||
),
|
||||
);
|
||||
@@ -142,7 +159,9 @@ class _SessionsScreenState extends State<SessionsScreen> {
|
||||
itemBuilder: (context, i) {
|
||||
final s = app.sessions[i];
|
||||
final running = app.active.contains(s.id);
|
||||
final selected = widget.app.chat?.session.id == s.id;
|
||||
return ListTile(
|
||||
selected: selected,
|
||||
title: Text(s.title.isEmpty ? '(无标题会话)' : s.title,
|
||||
maxLines: 1, overflow: TextOverflow.ellipsis),
|
||||
subtitle: Text(_subtitle(s),
|
||||
@@ -153,7 +172,9 @@ class _SessionsScreenState extends State<SessionsScreen> {
|
||||
if (running)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 8),
|
||||
child: Icon(Icons.circle, size: 10, color: Theme.of(context).colorScheme.primary),
|
||||
child: Icon(Icons.circle,
|
||||
size: 10,
|
||||
color: Theme.of(context).colorScheme.primary),
|
||||
),
|
||||
PopupMenuButton<String>(
|
||||
onSelected: (v) => _sessionAction(s, v),
|
||||
@@ -167,10 +188,7 @@ class _SessionsScreenState extends State<SessionsScreen> {
|
||||
),
|
||||
onTap: () {
|
||||
app.openChat(s);
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (_) => ChatScreen(app: app, session: s)),
|
||||
);
|
||||
widget.onOpened?.call();
|
||||
},
|
||||
);
|
||||
},
|
||||
@@ -212,7 +230,9 @@ class _SessionsScreenState extends State<SessionsScreen> {
|
||||
title: const Text('重命名'),
|
||||
content: TextField(controller: ctrl, autofocus: true),
|
||||
actions: [
|
||||
TextButton(onPressed: () => Navigator.pop(context), child: const Text('取消')),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('取消')),
|
||||
FilledButton(
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
@@ -232,7 +252,9 @@ class _SessionsScreenState extends State<SessionsScreen> {
|
||||
title: const Text('删除会话?'),
|
||||
content: Text('“${s.title}”及其子会话将被删除,且无法恢复。'),
|
||||
actions: [
|
||||
TextButton(onPressed: () => Navigator.pop(context), child: const Text('取消')),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('取消')),
|
||||
FilledButton(
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
@@ -257,12 +279,14 @@ class _SessionsScreenState extends State<SessionsScreen> {
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
const Text('新建会话', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
|
||||
const Text('新建会话',
|
||||
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: ctrl,
|
||||
autofocus: true,
|
||||
decoration: const InputDecoration(hintText: '标题(可选)', border: OutlineInputBorder()),
|
||||
decoration: const InputDecoration(
|
||||
hintText: '标题(可选)', border: OutlineInputBorder()),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
FilledButton(
|
||||
@@ -274,12 +298,7 @@ class _SessionsScreenState extends State<SessionsScreen> {
|
||||
widget.app.sessions.insert(0, s);
|
||||
widget.app.touch();
|
||||
widget.app.openChat(s);
|
||||
if (mounted) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (_) => ChatScreen(app: widget.app, session: s)),
|
||||
);
|
||||
}
|
||||
widget.onOpened?.call();
|
||||
}
|
||||
},
|
||||
child: const Text('创建'),
|
||||
@@ -300,14 +319,18 @@ class _SessionsScreenState extends State<SessionsScreen> {
|
||||
children: [
|
||||
const Padding(
|
||||
padding: EdgeInsets.all(12),
|
||||
child: Text('服务器', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
|
||||
child: Text('服务器',
|
||||
style:
|
||||
TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
|
||||
),
|
||||
for (final c in app.connections) ...[
|
||||
ListTile(
|
||||
title: Text(c.label),
|
||||
subtitle: Text(c.proxy.isEmpty ? c.url : '${c.url}\n经由 ${c.proxy}'),
|
||||
trailing: app.conn?.id == c.id
|
||||
? Text('已连接', style: TextStyle(color: Theme.of(context).colorScheme.primary))
|
||||
? Text('已连接',
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).colorScheme.primary))
|
||||
: null,
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
@@ -321,8 +344,7 @@ class _SessionsScreenState extends State<SessionsScreen> {
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
Navigator.of(context).push(MaterialPageRoute(
|
||||
builder: (_) =>
|
||||
ServersScreen(app: app, startEditing: true),
|
||||
builder: (_) => ServersScreen(app: app, startEditing: true),
|
||||
));
|
||||
},
|
||||
),
|
||||
|
||||
Reference in new issue
Block a user