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 createState() => _DetachedChatState(); } class _DetachedChatState extends State { // 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); _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); } } @override void dispose() { widget.window.ready.removeListener(_rowsChanged); _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 [build]). 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}); } }); } /// `/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( listenable: Listenable.merge([widget.window.ready, widget.window.sync]), 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, ), ], ), ); }, ); }