2026-11-18 19:08:06 +08:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2026-11-21 14:29:52 +08:00
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
|
import 'package:flutter_slidable/flutter_slidable.dart';
|
2026-11-18 19:08:06 +08:00
|
|
|
|
|
|
|
|
import '../api/models.dart';
|
|
|
|
|
import '../state.dart';
|
2026-11-19 12:34:25 +08:00
|
|
|
import 'servers_screen.dart';
|
2026-11-18 19:08:06 +08:00
|
|
|
|
2026-11-21 01:16:10 +08:00
|
|
|
/// The session list: a drawer on a phone, a persistent column on a wide
|
2026-11-21 14:29:52 +08:00
|
|
|
/// layout. Sessions are grouped by project, dated within it, and a row swipes
|
|
|
|
|
/// to pin, archive or delete.
|
2026-11-21 01:16:10 +08:00
|
|
|
class SessionListPane extends StatefulWidget {
|
2026-11-18 19:08:06 +08:00
|
|
|
final AppState app;
|
2026-11-21 01:16:10 +08:00
|
|
|
|
2026-11-21 14:29:52 +08:00
|
|
|
/// Called after a session (or the draft) was opened, so the drawer closes.
|
2026-11-21 01:16:10 +08:00
|
|
|
final VoidCallback? onOpened;
|
|
|
|
|
|
|
|
|
|
const SessionListPane({super.key, required this.app, this.onOpened});
|
2026-11-18 19:08:06 +08:00
|
|
|
|
|
|
|
|
@override
|
2026-11-21 01:16:10 +08:00
|
|
|
State<SessionListPane> createState() => _SessionListPaneState();
|
2026-11-18 19:08:06 +08:00
|
|
|
}
|
|
|
|
|
|
2026-11-21 01:16:10 +08:00
|
|
|
class _SessionListPaneState extends State<SessionListPane> {
|
2026-11-18 19:08:06 +08:00
|
|
|
bool _searchOpen = false;
|
|
|
|
|
Timer? _timer;
|
|
|
|
|
late final TextEditingController _searchCtrl;
|
2026-11-21 14:29:52 +08:00
|
|
|
final Set<String> _collapsed = {};
|
|
|
|
|
bool _showArchived = false;
|
2026-11-18 19:08:06 +08:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
_searchCtrl = TextEditingController();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
_timer?.cancel();
|
|
|
|
|
_searchCtrl.dispose();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-21 14:29:52 +08:00
|
|
|
AppState get _app => widget.app;
|
|
|
|
|
|
2026-11-18 19:08:06 +08:00
|
|
|
void _onSearchChanged(String v) {
|
|
|
|
|
_timer?.cancel();
|
|
|
|
|
_timer = Timer(const Duration(milliseconds: 350), () {
|
|
|
|
|
if (v.isEmpty) {
|
2026-11-21 14:29:52 +08:00
|
|
|
_app.refreshSessions();
|
2026-11-18 19:08:06 +08:00
|
|
|
} else {
|
2026-11-21 14:29:52 +08:00
|
|
|
_app.searchSessions(v);
|
2026-11-18 19:08:06 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return ListenableBuilder(
|
2026-11-21 14:29:52 +08:00
|
|
|
listenable: _app,
|
2026-11-18 19:08:06 +08:00
|
|
|
builder: (context, _) {
|
2026-11-21 01:16:10 +08:00
|
|
|
final scheme = Theme.of(context).colorScheme;
|
|
|
|
|
return SafeArea(
|
|
|
|
|
child: Column(
|
2026-11-18 19:08:06 +08:00
|
|
|
children: [
|
2026-11-21 01:16:10 +08:00
|
|
|
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();
|
2026-11-21 14:29:52 +08:00
|
|
|
_app.refreshSessions();
|
2026-11-21 01:16:10 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
IconButton(
|
|
|
|
|
icon: const Icon(Icons.add),
|
2026-11-21 14:29:52 +08:00
|
|
|
tooltip: '新会话',
|
|
|
|
|
onPressed: () => _newSession(),
|
2026-11-21 01:16:10 +08:00
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
2026-11-18 19:08:06 +08:00
|
|
|
if (_searchOpen)
|
|
|
|
|
Padding(
|
2026-11-21 01:16:10 +08:00
|
|
|
padding: const EdgeInsets.fromLTRB(12, 2, 12, 4),
|
2026-11-18 19:08:06 +08:00
|
|
|
child: TextField(
|
|
|
|
|
controller: _searchCtrl,
|
|
|
|
|
autofocus: true,
|
|
|
|
|
onChanged: _onSearchChanged,
|
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
|
hintText: '搜索会话',
|
|
|
|
|
prefixIcon: Icon(Icons.search),
|
|
|
|
|
border: OutlineInputBorder(),
|
|
|
|
|
isDense: true,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
2026-11-21 14:29:52 +08:00
|
|
|
if (_app.error != null)
|
2026-11-18 19:08:06 +08:00
|
|
|
Padding(
|
2026-11-21 01:16:10 +08:00
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 12),
|
2026-11-18 19:08:06 +08:00
|
|
|
child: Row(
|
|
|
|
|
children: [
|
|
|
|
|
Expanded(
|
2026-11-21 14:29:52 +08:00
|
|
|
child: Text(_app.error!,
|
2026-11-21 01:16:10 +08:00
|
|
|
style: TextStyle(
|
|
|
|
|
color: scheme.error, fontSize: 13)),
|
2026-11-18 19:08:06 +08:00
|
|
|
),
|
2026-11-21 01:16:10 +08:00
|
|
|
TextButton(
|
2026-11-21 14:29:52 +08:00
|
|
|
onPressed: _app.refreshSessions,
|
2026-11-21 01:16:10 +08:00
|
|
|
child: const Text('重试')),
|
2026-11-18 19:08:06 +08:00
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
2026-11-21 14:29:52 +08:00
|
|
|
Expanded(child: _body(scheme)),
|
2026-11-18 19:08:06 +08:00
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-21 14:29:52 +08:00
|
|
|
Widget _body(ColorScheme scheme) {
|
|
|
|
|
final app = _app;
|
2026-11-18 19:08:06 +08:00
|
|
|
if (app.sessionsLoading && app.sessions.isEmpty) {
|
|
|
|
|
return const Center(child: CircularProgressIndicator());
|
|
|
|
|
}
|
2026-11-21 14:29:52 +08:00
|
|
|
final draft = app.draft;
|
|
|
|
|
final topLevel = [
|
|
|
|
|
for (final s in app.sessions)
|
|
|
|
|
if (!s.isChild) s
|
|
|
|
|
];
|
|
|
|
|
final pinned = [
|
|
|
|
|
for (final s in topLevel)
|
|
|
|
|
if (s.isPinned) s
|
|
|
|
|
]..sort((a, b) => b.pinnedAt.compareTo(a.pinnedAt));
|
|
|
|
|
final unpinned = [
|
|
|
|
|
for (final s in topLevel)
|
|
|
|
|
if (!s.isPinned) s
|
|
|
|
|
];
|
|
|
|
|
if (topLevel.isEmpty && draft == null) {
|
2026-11-18 19:08:06 +08:00
|
|
|
return Center(
|
|
|
|
|
child: Column(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
2026-11-21 14:29:52 +08:00
|
|
|
Text(_searchCtrl.text.isEmpty ? '暂无会话' : '没有匹配的会话'),
|
2026-11-18 19:08:06 +08:00
|
|
|
const SizedBox(height: 8),
|
2026-11-21 01:16:10 +08:00
|
|
|
FilledButton(
|
2026-11-21 14:29:52 +08:00
|
|
|
onPressed: () => _newSession(), child: const Text('新会话')),
|
2026-11-18 19:08:06 +08:00
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return RefreshIndicator(
|
2026-11-21 14:29:52 +08:00
|
|
|
onRefresh: () async {
|
|
|
|
|
await app.refreshSessions();
|
|
|
|
|
if (app.archivedLoaded) await app.refreshArchived();
|
|
|
|
|
},
|
|
|
|
|
child: ListView(
|
|
|
|
|
padding: const EdgeInsets.only(bottom: 16),
|
|
|
|
|
children: [
|
|
|
|
|
if (draft != null) _draftRow(draft),
|
|
|
|
|
if (pinned.isNotEmpty) ..._pinnedSection(pinned),
|
|
|
|
|
for (final group in _groups(unpinned)) ..._groupSection(group),
|
|
|
|
|
_footer(scheme),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --------------------------------------------------------------- groups
|
|
|
|
|
|
|
|
|
|
bool _isChatDirectory(String directory) =>
|
|
|
|
|
directory.contains('/.config/openchamber/chats');
|
|
|
|
|
|
|
|
|
|
String _groupLabel(String key) =>
|
|
|
|
|
key == '\u0000chats' ? '聊天' : _app.projectLabelFor(key);
|
|
|
|
|
|
|
|
|
|
List<_SessionGroup> _groups(List<Session> sessions) {
|
|
|
|
|
final map = <String, List<Session>>{};
|
|
|
|
|
for (final session in sessions) {
|
|
|
|
|
final key = _isChatDirectory(session.directory)
|
|
|
|
|
? '\u0000chats'
|
|
|
|
|
: session.directory;
|
|
|
|
|
map.putIfAbsent(key, () => []).add(session);
|
|
|
|
|
}
|
|
|
|
|
final groups = [
|
|
|
|
|
for (final entry in map.entries)
|
|
|
|
|
_SessionGroup(
|
|
|
|
|
key: entry.key,
|
|
|
|
|
label: _groupLabel(entry.key),
|
|
|
|
|
path: entry.key == '\u0000chats' ? '' : entry.key,
|
|
|
|
|
sessions: entry.value
|
|
|
|
|
..sort((a, b) => b.updated.compareTo(a.updated)),
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
groups.sort((a, b) {
|
|
|
|
|
final aTime = a.sessions.isEmpty ? 0 : a.sessions.first.updated;
|
|
|
|
|
final bTime = b.sessions.isEmpty ? 0 : b.sessions.first.updated;
|
|
|
|
|
return bTime.compareTo(aTime);
|
|
|
|
|
});
|
|
|
|
|
return groups;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<Widget> _groupSection(_SessionGroup group) {
|
|
|
|
|
final collapsed = _collapsed.contains(group.key);
|
|
|
|
|
return [
|
|
|
|
|
_groupHeader(group, collapsed: collapsed),
|
|
|
|
|
if (!collapsed)
|
|
|
|
|
for (final entry in _dated(group.sessions)) ...[
|
|
|
|
|
_caption(entry.key),
|
|
|
|
|
for (final session in entry.value) _row(session),
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _groupHeader(_SessionGroup group, {required bool collapsed}) {
|
|
|
|
|
final theme = Theme.of(context);
|
|
|
|
|
return InkWell(
|
|
|
|
|
onTap: () => setState(() {
|
|
|
|
|
if (collapsed) {
|
|
|
|
|
_collapsed.remove(group.key);
|
|
|
|
|
} else {
|
|
|
|
|
_collapsed.add(group.key);
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
child: Padding(
|
|
|
|
|
padding: const EdgeInsets.fromLTRB(14, 10, 4, 4),
|
|
|
|
|
child: Row(
|
|
|
|
|
children: [
|
|
|
|
|
Icon(collapsed ? Icons.chevron_right : Icons.expand_more,
|
|
|
|
|
size: 16, color: theme.hintColor),
|
|
|
|
|
const SizedBox(width: 2),
|
|
|
|
|
Expanded(
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
Text(
|
|
|
|
|
group.label.isEmpty ? '未命名项目' : group.label,
|
|
|
|
|
maxLines: 1,
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
style: const TextStyle(
|
|
|
|
|
fontSize: 12.5, fontWeight: FontWeight.w700),
|
2026-11-18 19:08:06 +08:00
|
|
|
),
|
2026-11-21 14:29:52 +08:00
|
|
|
if (group.path.isNotEmpty)
|
|
|
|
|
Text(
|
|
|
|
|
group.path,
|
|
|
|
|
maxLines: 1,
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
style:
|
|
|
|
|
TextStyle(fontSize: 10.5, color: theme.hintColor),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
2026-11-18 19:08:06 +08:00
|
|
|
),
|
2026-11-21 14:29:52 +08:00
|
|
|
IconButton(
|
|
|
|
|
icon: const Icon(Icons.add, size: 18),
|
|
|
|
|
tooltip: '在此项目新会话',
|
|
|
|
|
visualDensity: VisualDensity.compact,
|
|
|
|
|
onPressed: () => _newSession(directory: group.path),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _caption(String label) {
|
|
|
|
|
return Padding(
|
|
|
|
|
padding: const EdgeInsets.fromLTRB(28, 6, 12, 2),
|
|
|
|
|
child: Text(label,
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: 11, color: Theme.of(context).hintColor)),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 今天 / 昨天 / 本周早些时候 / 更早, in that order.
|
|
|
|
|
List<MapEntry<String, List<Session>>> _dated(List<Session> sessions) {
|
|
|
|
|
final now = DateTime.now();
|
|
|
|
|
final today = DateTime(now.year, now.month, now.day);
|
|
|
|
|
final yesterday = today.subtract(const Duration(days: 1));
|
|
|
|
|
final weekAgo = today.subtract(const Duration(days: 7));
|
|
|
|
|
final buckets = <String, List<Session>>{
|
|
|
|
|
'今天': [],
|
|
|
|
|
'昨天': [],
|
|
|
|
|
'本周早些时候': [],
|
|
|
|
|
'更早': [],
|
|
|
|
|
};
|
|
|
|
|
for (final session in sessions) {
|
|
|
|
|
final time = session.updated > 0
|
|
|
|
|
? DateTime.fromMillisecondsSinceEpoch(session.updated)
|
|
|
|
|
: DateTime.fromMillisecondsSinceEpoch(session.created);
|
|
|
|
|
final day = DateTime(time.year, time.month, time.day);
|
|
|
|
|
final key = day == today
|
|
|
|
|
? '今天'
|
|
|
|
|
: day == yesterday
|
|
|
|
|
? '昨天'
|
|
|
|
|
: day.isAfter(weekAgo)
|
|
|
|
|
? '本周早些时候'
|
|
|
|
|
: '更早';
|
|
|
|
|
buckets[key]!.add(session);
|
|
|
|
|
}
|
|
|
|
|
return [
|
|
|
|
|
for (final entry in buckets.entries)
|
|
|
|
|
if (entry.value.isNotEmpty) entry,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------- rows
|
|
|
|
|
|
|
|
|
|
List<Widget> _pinnedSection(List<Session> pinned) {
|
|
|
|
|
final collapsed = _collapsed.contains('pinned');
|
|
|
|
|
return [
|
|
|
|
|
InkWell(
|
|
|
|
|
onTap: () => setState(() {
|
|
|
|
|
if (collapsed) {
|
|
|
|
|
_collapsed.remove('pinned');
|
|
|
|
|
} else {
|
|
|
|
|
_collapsed.add('pinned');
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
child: Padding(
|
|
|
|
|
padding: const EdgeInsets.fromLTRB(14, 10, 4, 4),
|
|
|
|
|
child: Row(
|
|
|
|
|
children: [
|
|
|
|
|
Icon(Icons.push_pin,
|
|
|
|
|
size: 13, color: Theme.of(context).hintColor),
|
|
|
|
|
const SizedBox(width: 5),
|
|
|
|
|
Expanded(
|
|
|
|
|
child: Text('已置顶',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: 12.5,
|
|
|
|
|
fontWeight: FontWeight.w700,
|
|
|
|
|
color: Theme.of(context).hintColor)),
|
|
|
|
|
),
|
|
|
|
|
Icon(collapsed ? Icons.chevron_right : Icons.expand_more,
|
|
|
|
|
size: 16, color: Theme.of(context).hintColor),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
if (!collapsed)
|
|
|
|
|
for (final session in pinned) _row(session),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _draftRow(SessionDraft draft) {
|
|
|
|
|
final theme = Theme.of(context);
|
|
|
|
|
final label = draft.firstLine.isEmpty ? '新会话' : draft.firstLine;
|
|
|
|
|
final project = _app.projectLabelFor(draft.directory);
|
|
|
|
|
return ListTile(
|
|
|
|
|
selected: _app.draftOpen,
|
|
|
|
|
leading: Icon(Icons.edit_outlined, size: 18, color: theme.hintColor),
|
|
|
|
|
title: Text(
|
|
|
|
|
label,
|
|
|
|
|
maxLines: 1,
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
style: const TextStyle(fontStyle: FontStyle.italic),
|
|
|
|
|
),
|
|
|
|
|
subtitle: Text(
|
|
|
|
|
project.isEmpty ? '草稿' : '草稿 · $project',
|
|
|
|
|
maxLines: 1,
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
),
|
|
|
|
|
trailing: IconButton(
|
|
|
|
|
icon: const Icon(Icons.close, size: 18),
|
|
|
|
|
tooltip: '丢弃草稿',
|
|
|
|
|
onPressed: () => _app.discardDraft(),
|
|
|
|
|
),
|
|
|
|
|
onTap: () {
|
|
|
|
|
_app.showDraft();
|
|
|
|
|
widget.onOpened?.call();
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool _isActive(Session session) {
|
|
|
|
|
if (_app.active.contains(session.id)) return true;
|
|
|
|
|
return _app.sessions
|
|
|
|
|
.any((s) => s.parentID == session.id && _app.active.contains(s.id));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _row(Session session, {bool archived = false}) {
|
|
|
|
|
final theme = Theme.of(context);
|
|
|
|
|
final scheme = theme.colorScheme;
|
|
|
|
|
final running = _isActive(session);
|
|
|
|
|
final selected =
|
|
|
|
|
!_app.draftOpen && _app.chat?.session.id == session.id;
|
|
|
|
|
return Slidable(
|
|
|
|
|
key: ValueKey('${archived ? 'a' : 's'}-${session.id}'),
|
|
|
|
|
endActionPane: ActionPane(
|
|
|
|
|
motion: const DrawerMotion(),
|
|
|
|
|
extentRatio: 0.62,
|
|
|
|
|
children: [
|
|
|
|
|
if (!archived)
|
|
|
|
|
SlidableAction(
|
|
|
|
|
onPressed: (_) => _app.togglePin(session),
|
|
|
|
|
icon: Icons.push_pin_outlined,
|
|
|
|
|
label: session.isPinned ? '取消置顶' : '置顶',
|
|
|
|
|
backgroundColor: scheme.secondaryContainer,
|
|
|
|
|
foregroundColor: scheme.onSecondaryContainer,
|
|
|
|
|
),
|
|
|
|
|
SlidableAction(
|
|
|
|
|
onPressed: (_) => _archive(session, archived),
|
|
|
|
|
icon: archived ? Icons.unarchive_outlined : Icons.archive_outlined,
|
|
|
|
|
label: archived ? '取消归档' : '归档',
|
|
|
|
|
backgroundColor: scheme.surfaceContainerHighest,
|
|
|
|
|
foregroundColor: scheme.onSurface,
|
|
|
|
|
),
|
|
|
|
|
SlidableAction(
|
|
|
|
|
onPressed: (_) => _confirmDelete(session),
|
|
|
|
|
icon: Icons.delete_outline,
|
|
|
|
|
label: '删除',
|
|
|
|
|
backgroundColor: scheme.errorContainer,
|
|
|
|
|
foregroundColor: scheme.onErrorContainer,
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
child: ListTile(
|
|
|
|
|
selected: selected,
|
|
|
|
|
selectedTileColor: scheme.primary.withValues(alpha: 0.10),
|
|
|
|
|
title: Text(
|
|
|
|
|
session.title.isEmpty ? '(无标题会话)' : session.title,
|
|
|
|
|
maxLines: 1,
|
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
fontWeight: selected ? FontWeight.w600 : FontWeight.w400,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
subtitle: Text(_subtitle(session),
|
|
|
|
|
maxLines: 1, overflow: TextOverflow.ellipsis),
|
|
|
|
|
trailing: Row(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
if (session.isPinned)
|
|
|
|
|
Padding(
|
|
|
|
|
padding: const EdgeInsets.only(right: 6),
|
|
|
|
|
child: Icon(Icons.push_pin,
|
|
|
|
|
size: 13, color: scheme.primary),
|
|
|
|
|
),
|
|
|
|
|
if (running)
|
|
|
|
|
Padding(
|
|
|
|
|
padding: const EdgeInsets.only(right: 6),
|
|
|
|
|
child: SizedBox(
|
|
|
|
|
width: 12,
|
|
|
|
|
height: 12,
|
|
|
|
|
child: CircularProgressIndicator(
|
|
|
|
|
strokeWidth: 1.8, color: scheme.primary),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
_menu(session, archived: archived),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
onTap: () {
|
|
|
|
|
_app.openChat(session);
|
|
|
|
|
widget.onOpened?.call();
|
2026-11-18 19:08:06 +08:00
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-21 14:29:52 +08:00
|
|
|
Widget _menu(Session session, {required bool archived}) {
|
|
|
|
|
return PopupMenuButton<String>(
|
|
|
|
|
icon: const Icon(Icons.more_vert, size: 18),
|
|
|
|
|
onSelected: (value) {
|
|
|
|
|
switch (value) {
|
|
|
|
|
case 'rename':
|
|
|
|
|
_rename(session);
|
|
|
|
|
break;
|
|
|
|
|
case 'pin':
|
|
|
|
|
_app.togglePin(session);
|
|
|
|
|
break;
|
|
|
|
|
case 'archive':
|
|
|
|
|
_archive(session, archived);
|
|
|
|
|
break;
|
|
|
|
|
case 'delete':
|
|
|
|
|
_confirmDelete(session);
|
|
|
|
|
break;
|
|
|
|
|
case 'copy':
|
|
|
|
|
Clipboard.setData(ClipboardData(text: session.id));
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
const SnackBar(content: Text('已复制会话 ID')));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
itemBuilder: (_) => [
|
|
|
|
|
if (!archived) ...[
|
|
|
|
|
const PopupMenuItem(value: 'rename', child: Text('重命名')),
|
|
|
|
|
PopupMenuItem(
|
|
|
|
|
value: 'pin',
|
|
|
|
|
child: Text(session.isPinned ? '取消置顶' : '置顶')),
|
|
|
|
|
],
|
|
|
|
|
PopupMenuItem(
|
|
|
|
|
value: 'archive',
|
|
|
|
|
child: Text(archived ? '取消归档' : '归档')),
|
|
|
|
|
const PopupMenuItem(value: 'delete', child: Text('删除')),
|
|
|
|
|
const PopupMenuItem(value: 'copy', child: Text('复制会话 ID')),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _footer(ColorScheme scheme) {
|
|
|
|
|
final app = _app;
|
|
|
|
|
final archivedCount =
|
|
|
|
|
app.archivedLoaded ? app.archivedSessions.length : null;
|
|
|
|
|
return Column(
|
|
|
|
|
children: [
|
|
|
|
|
if (app.hasMoreSessions)
|
|
|
|
|
TextButton(
|
|
|
|
|
onPressed: app.loadingMoreSessions ? null : app.loadMoreSessions,
|
|
|
|
|
child: app.loadingMoreSessions
|
|
|
|
|
? const SizedBox(
|
|
|
|
|
width: 16,
|
|
|
|
|
height: 16,
|
|
|
|
|
child: CircularProgressIndicator(strokeWidth: 2))
|
|
|
|
|
: const Text('载入更多'),
|
|
|
|
|
),
|
|
|
|
|
TextButton.icon(
|
|
|
|
|
icon: Icon(
|
|
|
|
|
_showArchived
|
|
|
|
|
? Icons.expand_less
|
|
|
|
|
: Icons.archive_outlined,
|
|
|
|
|
size: 16),
|
|
|
|
|
label: Text(
|
|
|
|
|
_showArchived
|
|
|
|
|
? '隐藏已归档'
|
|
|
|
|
: archivedCount == null
|
|
|
|
|
? '显示已归档'
|
|
|
|
|
: '显示已归档 ($archivedCount)',
|
|
|
|
|
style: const TextStyle(fontSize: 12.5),
|
|
|
|
|
),
|
|
|
|
|
onPressed: app.archivedLoading ? null : _toggleArchived,
|
|
|
|
|
),
|
|
|
|
|
if (app.archivedLoading)
|
|
|
|
|
const Padding(
|
|
|
|
|
padding: EdgeInsets.only(bottom: 8),
|
|
|
|
|
child: SizedBox(
|
|
|
|
|
width: 16,
|
|
|
|
|
height: 16,
|
|
|
|
|
child: CircularProgressIndicator(strokeWidth: 2)),
|
|
|
|
|
),
|
|
|
|
|
if (_showArchived)
|
|
|
|
|
for (final session in app.archivedSessions)
|
|
|
|
|
_row(session, archived: true),
|
|
|
|
|
if (_showArchived &&
|
|
|
|
|
app.archivedLoaded &&
|
|
|
|
|
app.archivedSessions.isEmpty)
|
|
|
|
|
Padding(
|
|
|
|
|
padding: const EdgeInsets.only(bottom: 8),
|
|
|
|
|
child: Text('没有已归档的会话',
|
|
|
|
|
style: TextStyle(fontSize: 12, color: scheme.outline)),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _toggleArchived() async {
|
|
|
|
|
if (_showArchived) {
|
|
|
|
|
setState(() => _showArchived = false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!_app.archivedLoaded) await _app.refreshArchived();
|
|
|
|
|
if (mounted) setState(() => _showArchived = true);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-18 19:08:06 +08:00
|
|
|
String _subtitle(Session s) {
|
|
|
|
|
final parts = <String>[];
|
|
|
|
|
if (s.agent.isNotEmpty) parts.add(s.agent);
|
2026-11-21 14:29:52 +08:00
|
|
|
if (s.model != null) parts.add(_app.modelName(s.model));
|
2026-11-18 19:08:06 +08:00
|
|
|
if (s.updated > 0) {
|
|
|
|
|
final t = DateTime.fromMillisecondsSinceEpoch(s.updated);
|
2026-11-21 14:29:52 +08:00
|
|
|
parts.add(
|
|
|
|
|
'${t.month.toString().padLeft(2, '0')}-${t.day.toString().padLeft(2, '0')} '
|
2026-11-18 19:08:06 +08:00
|
|
|
'${t.hour.toString().padLeft(2, '0')}:${t.minute.toString().padLeft(2, '0')}');
|
|
|
|
|
}
|
|
|
|
|
return parts.join(' · ');
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-21 14:29:52 +08:00
|
|
|
// -------------------------------------------------------------- actions
|
|
|
|
|
|
|
|
|
|
void _newSession({String? directory}) {
|
|
|
|
|
_app.openDraft(directory: directory ?? _app.chat?.session.directory);
|
|
|
|
|
widget.onOpened?.call();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _archive(Session session, bool archived) async {
|
|
|
|
|
await _app.setArchived(session, !archived);
|
2026-11-18 19:08:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _rename(Session s) {
|
|
|
|
|
final ctrl = TextEditingController(text: s.title);
|
|
|
|
|
showDialog(
|
|
|
|
|
context: context,
|
|
|
|
|
builder: (context) => AlertDialog(
|
|
|
|
|
title: const Text('重命名'),
|
|
|
|
|
content: TextField(controller: ctrl, autofocus: true),
|
|
|
|
|
actions: [
|
2026-11-21 01:16:10 +08:00
|
|
|
TextButton(
|
|
|
|
|
onPressed: () => Navigator.pop(context),
|
|
|
|
|
child: const Text('取消')),
|
2026-11-18 19:08:06 +08:00
|
|
|
FilledButton(
|
|
|
|
|
onPressed: () {
|
|
|
|
|
Navigator.pop(context);
|
2026-11-21 14:29:52 +08:00
|
|
|
_app.client?.updateSession(s.id, title: ctrl.text);
|
2026-11-18 19:08:06 +08:00
|
|
|
},
|
|
|
|
|
child: const Text('保存'),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _confirmDelete(Session s) {
|
|
|
|
|
showDialog(
|
|
|
|
|
context: context,
|
|
|
|
|
builder: (context) => AlertDialog(
|
|
|
|
|
title: const Text('删除会话?'),
|
|
|
|
|
content: Text('“${s.title}”及其子会话将被删除,且无法恢复。'),
|
|
|
|
|
actions: [
|
2026-11-21 01:16:10 +08:00
|
|
|
TextButton(
|
|
|
|
|
onPressed: () => Navigator.pop(context),
|
|
|
|
|
child: const Text('取消')),
|
2026-11-18 19:08:06 +08:00
|
|
|
FilledButton(
|
|
|
|
|
onPressed: () {
|
|
|
|
|
Navigator.pop(context);
|
2026-11-21 14:29:52 +08:00
|
|
|
_app.client?.deleteSession(s.id);
|
|
|
|
|
_app.sessions.removeWhere((x) => x.id == s.id);
|
|
|
|
|
_app.archivedSessions.removeWhere((x) => x.id == s.id);
|
|
|
|
|
_app.touch();
|
2026-11-18 19:08:06 +08:00
|
|
|
},
|
|
|
|
|
child: const Text('删除'),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _instancesSheet() {
|
2026-11-21 14:29:52 +08:00
|
|
|
final app = _app;
|
2026-11-18 19:08:06 +08:00
|
|
|
showModalBottomSheet(
|
|
|
|
|
context: context,
|
|
|
|
|
builder: (context) => SafeArea(
|
|
|
|
|
child: Column(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
const Padding(
|
|
|
|
|
padding: EdgeInsets.all(12),
|
2026-11-21 01:16:10 +08:00
|
|
|
child: Text('服务器',
|
|
|
|
|
style:
|
|
|
|
|
TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
|
2026-11-18 19:08:06 +08:00
|
|
|
),
|
|
|
|
|
for (final c in app.connections) ...[
|
|
|
|
|
ListTile(
|
|
|
|
|
title: Text(c.label),
|
2026-11-21 14:29:52 +08:00
|
|
|
subtitle:
|
|
|
|
|
Text(c.proxy.isEmpty ? c.url : '${c.url}\n经由 ${c.proxy}'),
|
2026-11-18 19:08:06 +08:00
|
|
|
trailing: app.conn?.id == c.id
|
2026-11-21 01:16:10 +08:00
|
|
|
? Text('已连接',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
color: Theme.of(context).colorScheme.primary))
|
2026-11-18 19:08:06 +08:00
|
|
|
: null,
|
|
|
|
|
onTap: () {
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
if (app.conn?.id != c.id) app.switchTo(c);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
ListTile(
|
|
|
|
|
leading: const Icon(Icons.add),
|
|
|
|
|
title: const Text('添加服务器'),
|
2026-11-19 12:34:25 +08:00
|
|
|
onTap: () {
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
Navigator.of(context).push(MaterialPageRoute(
|
2026-11-21 01:16:10 +08:00
|
|
|
builder: (_) => ServersScreen(app: app, startEditing: true),
|
2026-11-19 12:34:25 +08:00
|
|
|
));
|
|
|
|
|
},
|
2026-11-18 19:08:06 +08:00
|
|
|
),
|
|
|
|
|
if (app.client != null)
|
|
|
|
|
ListTile(
|
|
|
|
|
leading: const Icon(Icons.link_off),
|
|
|
|
|
title: const Text('断开当前连接'),
|
|
|
|
|
onTap: () {
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
app.disconnect();
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-21 14:29:52 +08:00
|
|
|
|
|
|
|
|
class _SessionGroup {
|
|
|
|
|
final String key;
|
|
|
|
|
final String label;
|
|
|
|
|
final String path;
|
|
|
|
|
final List<Session> sessions;
|
|
|
|
|
const _SessionGroup({
|
|
|
|
|
required this.key,
|
|
|
|
|
required this.label,
|
|
|
|
|
required this.path,
|
|
|
|
|
required this.sessions,
|
|
|
|
|
});
|
|
|
|
|
}
|