03983cfd1f
A detached chat window read the chat list once, before any saved machine had connected, and never again; its pane also did not rebuild when the runtime adopted a row. Without the row the header showed a bare "Chat" with no folder or model, the window was named by the session id, and the chat's colour (bar tint, focus ring, prompt bar) was missing. The window now re-reads the list on each connection change and rebuilds on the runtime, as the Chat destination does. It also re-reads the stored design and font on the browser's storage event and on coming to the front, so a Settings change in the main window follows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
175 lines
6.0 KiB
Dart
175 lines
6.0 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../core/api_gateway.dart';
|
|
import '../../features/chat/chat_pane_content.dart';
|
|
import '../../features/chat/chat_runtime.dart';
|
|
import '../../features/chat/data/chat_recency.dart';
|
|
import '../../features/chat/ui/sessions/chat_adaptive.dart';
|
|
import '../../features/chat/workspace/chat_pane_commands.dart';
|
|
import 'detached_route.dart';
|
|
import 'detached_window_body.dart';
|
|
|
|
/// One chat alone in a detached window: the same live pane as in the Chat
|
|
/// destination (header, transcript, find, composer), with "Bring back to
|
|
/// main window" in its More menu. It has its own [ChatRuntime] over this
|
|
/// window's connection and keeps no workspace: nothing it does is saved as
|
|
/// the device's or the server's open-chat layout.
|
|
class DetachedChat extends StatefulWidget {
|
|
const DetachedChat({
|
|
super.key,
|
|
required this.sessionId,
|
|
required this.gateway,
|
|
required this.window,
|
|
this.serverKey = '',
|
|
});
|
|
|
|
final String sessionId;
|
|
final ApiGateway gateway;
|
|
final String serverKey;
|
|
final DetachedWindowControls window;
|
|
|
|
@override
|
|
State<DetachedChat> createState() => _DetachedChatState();
|
|
}
|
|
|
|
class _DetachedChatState extends State<DetachedChat> {
|
|
// An in-memory recency: the main window keeps the device's.
|
|
late final ChatRuntime _runtime = ChatRuntime(
|
|
gateway: widget.gateway,
|
|
serverKey: widget.serverKey,
|
|
recency: ChatRecency(),
|
|
);
|
|
final _paneCommands = ChatPaneCommands();
|
|
late String _sessionId = widget.sessionId;
|
|
bool _reading = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_runtime.addListener(_retitle);
|
|
_runtime.sessions.addListener(_rowsChanged);
|
|
_runtime.addSessionReplacedListener(_replacedElsewhere);
|
|
widget.window.ready.addListener(_rowsChanged);
|
|
widget.window.sync.addListener(_reconnected);
|
|
_rowsChanged();
|
|
_runtime.composerFocus.request(_sessionId);
|
|
_retitle();
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(DetachedChat oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (!identical(oldWidget.gateway, widget.gateway)) {
|
|
_runtime.gateway = widget.gateway;
|
|
}
|
|
if (oldWidget.window.ready != widget.window.ready) {
|
|
oldWidget.window.ready.removeListener(_rowsChanged);
|
|
widget.window.ready.addListener(_rowsChanged);
|
|
}
|
|
if (oldWidget.window.sync != widget.window.sync) {
|
|
oldWidget.window.sync.removeListener(_reconnected);
|
|
widget.window.sync.addListener(_reconnected);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
widget.window.ready.removeListener(_rowsChanged);
|
|
widget.window.sync.removeListener(_reconnected);
|
|
_runtime.removeListener(_retitle);
|
|
_runtime.sessions.removeListener(_rowsChanged);
|
|
_runtime.removeSessionReplacedListener(_replacedElsewhere);
|
|
_paneCommands.dispose();
|
|
_runtime.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _retitle() => widget.window.setTitle(_runtime.titleFor(_sessionId));
|
|
|
|
/// The pane's title, model and mode come from the session list, read once
|
|
/// connected (and again on every reconnect, see [_reconnected]).
|
|
void _rowsChanged() {
|
|
if (!widget.window.ready.value) return;
|
|
final sessions = _runtime.sessions;
|
|
if (_runtime.rowFor(_sessionId) == null && !_reading) {
|
|
_reading = true;
|
|
unawaited(sessions.refresh().whenComplete(() => _reading = false));
|
|
}
|
|
final rows = sessions.sessions;
|
|
scheduleMicrotask(() {
|
|
if (mounted) {
|
|
_runtime.adoptRows(rows, selected: _sessionId, retained: {_sessionId});
|
|
}
|
|
});
|
|
}
|
|
|
|
/// A machine connected (or the connection was replaced): the list is read
|
|
/// again, as the Chat list does on a new sync token. A window opened before
|
|
/// its machine connects has no row yet (the first read finds no machine),
|
|
/// and without it the header has no title, folder or model and the chat
|
|
/// none of its colour.
|
|
void _reconnected() {
|
|
if (widget.window.ready.value) unawaited(_runtime.sessions.refresh());
|
|
}
|
|
|
|
/// `/new` here, or on another device: the window follows the new session.
|
|
void _follow(String previousId, JsonMap session, String notice) {
|
|
final id = '${session['id'] ?? ''}';
|
|
if (id.isEmpty || previousId != _sessionId) return;
|
|
_runtime.movedToSession(previousId, session, notice);
|
|
setState(() => _sessionId = id);
|
|
_retitle();
|
|
}
|
|
|
|
void _startedNewSession(String previousId, JsonMap session, String notice) {
|
|
_runtime.sessions.replaceSession(previousId, session);
|
|
_follow(previousId, session, notice);
|
|
unawaited(_runtime.sessions.refresh());
|
|
}
|
|
|
|
void _replacedElsewhere(String previousId, JsonMap session) =>
|
|
_follow(previousId, session, 'This chat continues in a new session.');
|
|
|
|
@override
|
|
Widget build(BuildContext context) => ListenableBuilder(
|
|
// The runtime too, as the Chat destination rebuilds on it: the row it
|
|
// adopts (title, folder, model, colour) reaches the header only then.
|
|
listenable: Listenable.merge([
|
|
widget.window.ready,
|
|
widget.window.sync,
|
|
_runtime,
|
|
]),
|
|
builder: (context, _) {
|
|
if (!widget.window.ready.value) return const DetachedConnecting();
|
|
final sessionId = _sessionId;
|
|
return buildChatPaneContent(
|
|
runtime: _runtime,
|
|
sessionId: sessionId,
|
|
placement: ChatPanePlacement(
|
|
syncToken: widget.window.sync.value,
|
|
onBack: () {},
|
|
showBackButton: false,
|
|
// The only chat here: a paste with no field focused is its.
|
|
pasteTarget: true,
|
|
paneCommands: _paneCommands,
|
|
onComposerFocus: () => _runtime.composerFocus.request(sessionId),
|
|
onNewSession: _startedNewSession,
|
|
// `/close` was accepted: nothing is left to show.
|
|
onSessionClosed: widget.window.close,
|
|
paneControls: (_) => [
|
|
ChatBarAction(
|
|
label: detachedBringBackLabel,
|
|
identifier: 'detached-bring-back',
|
|
onPressed: () =>
|
|
widget.window.bringBack(DetachedKind.chat, sessionId),
|
|
materialIcon: Icons.login,
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|