Restore local chat layouts and add compact or hidden chat sidebar
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'chat_workspace_controller.dart';
|
||||
import 'chat_workspace_snapshot.dart';
|
||||
import 'chat_workspace_store.dart';
|
||||
|
||||
/// Connects one local workspace to storage, without creating server sessions.
|
||||
/// Reads cannot replace edits made while loading; writes retain their order.
|
||||
class ChatWorkspaceBinding {
|
||||
ChatWorkspaceBinding({
|
||||
required this.controller,
|
||||
required this.store,
|
||||
required this.sidebarMode,
|
||||
required this.onRestored,
|
||||
this.debounce = const Duration(milliseconds: 250),
|
||||
}) {
|
||||
controller.addListener(changed);
|
||||
}
|
||||
|
||||
final ChatWorkspaceController controller;
|
||||
final ChatWorkspaceStore store;
|
||||
final String Function() sidebarMode;
|
||||
final void Function(ChatWorkspaceSnapshot) onRestored;
|
||||
final Duration debounce;
|
||||
Timer? _timer;
|
||||
Future<void> _writes = Future.value();
|
||||
bool _loading = true;
|
||||
bool _restoring = false;
|
||||
bool _disposed = false;
|
||||
bool _dirty = false;
|
||||
bool _started = false;
|
||||
int _presentationRevision = 0;
|
||||
bool get isRestoring => _restoring;
|
||||
|
||||
Future<void> start({String? initialSessionId}) async {
|
||||
if (_started || _disposed) return;
|
||||
_started = true;
|
||||
final revision = controller.revision;
|
||||
final presentationRevision = _presentationRevision;
|
||||
ChatWorkspaceSnapshot? snapshot;
|
||||
try {
|
||||
snapshot = await store.read();
|
||||
} on Object {
|
||||
// Unavailable local preferences must never block opening chats.
|
||||
}
|
||||
if (_disposed) return;
|
||||
_loading = false;
|
||||
if (snapshot != null &&
|
||||
revision == controller.revision &&
|
||||
presentationRevision == _presentationRevision) {
|
||||
_restoring = true;
|
||||
try {
|
||||
controller.restoreLayout(snapshot, expectedRevision: revision);
|
||||
if (initialSessionId != null && initialSessionId.isNotEmpty) {
|
||||
final existing = controller.panes
|
||||
.where((pane) => pane.sessionId == initialSessionId)
|
||||
.firstOrNull;
|
||||
if (existing != null) {
|
||||
controller.activate(existing.id);
|
||||
} else {
|
||||
// A deep link keeps the restored panes and adds its own chat.
|
||||
controller.splitActive(initialSessionId, ChatSplitAxis.horizontal);
|
||||
}
|
||||
}
|
||||
onRestored(snapshot);
|
||||
} finally {
|
||||
_restoring = false;
|
||||
}
|
||||
_dirty = initialSessionId != null && initialSessionId.isNotEmpty;
|
||||
} else if (snapshot == null && controller.paneCount > 0) {
|
||||
_dirty = true;
|
||||
}
|
||||
if (_dirty) _schedule();
|
||||
}
|
||||
|
||||
/// Also called for presentation changes that don't mutate the split tree.
|
||||
void changed() {
|
||||
if (_disposed || _restoring) return;
|
||||
_presentationRevision++;
|
||||
_dirty = true;
|
||||
if (!_loading) _schedule();
|
||||
}
|
||||
|
||||
void _schedule() {
|
||||
_timer?.cancel();
|
||||
_timer = Timer(debounce, flush);
|
||||
}
|
||||
|
||||
Future<void> flush() {
|
||||
_timer?.cancel();
|
||||
_timer = null;
|
||||
if (!_dirty || _loading) return _writes;
|
||||
final snapshot = controller.snapshot(sidebarMode: sidebarMode());
|
||||
_dirty = false;
|
||||
if (snapshot == null) return _writes;
|
||||
_writes = _writes.then((_) async {
|
||||
try {
|
||||
await store.write(snapshot);
|
||||
} on Object {
|
||||
// A storage failure must not interrupt navigation or later saves.
|
||||
}
|
||||
});
|
||||
return _writes;
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
controller.removeListener(changed);
|
||||
if (_dirty) _loading = false;
|
||||
unawaited(flush());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user