Files
pipelines/app/lib/application/detached/detached_chat.dart
T
alan 61c7eee2b0 App: open a chat, the terminal or a diff in a new window
"Open in new window" sits beside Float: the chat pane and chat list row
menus, a floating chat window's title bar, the terminal options menu and
the diff bar, plus Alt+Shift+Y for the chat with the keyboard (listed in
the shortcut help). Once the window opens the item leaves the main window
as Float takes a pane out of the tiling.

The window is its own app instance at /window/chat/<id>,
/window/terminal?tab=<key> or /window/diff/<token>: only that item, named
after it, with "Bring back to main window". It has its own connection and
writes no shared layout (no open-chat workspace, window store or terminal
layout) and posts no notifications, so they stay the main window's.

Web opens a popup window (a blocked popup offers a tab); Linux and macOS
start a second process with --detached (Flutter's multi-window API is still
experimental); Android and iOS hide the action. The capture kit gains a
follow step for windows a page opens.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 12:47:42 +00:00

154 lines
5.1 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);
_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,
),
],
),
);
},
);
}