Build chat panes through one content builder

Phase 2.2 of docs/plans/floating-windows.md: buildChatPaneContent wraps a
conversation in the scopes it needs and keys it by the runtime, so the
tiling and (next) a floating window build the same subtree. The tiling uses
it; behaviour is unchanged.
This commit is contained in:
2026-11-10 21:05:41 +00:00
parent 558eedb410
commit 9695100faa
7 changed files with 227 additions and 136 deletions
@@ -0,0 +1,111 @@
import 'package:flutter/widgets.dart';
import '../../core/api_gateway.dart';
import '../../core/servers/server_context.dart';
import '../../shell/destination.dart';
import '../../shell/detail_page.dart';
import 'chat_runtime.dart';
import 'conversation/chat_thread_view.dart';
import 'conversation/composer_focus_request.dart';
/// Where an open chat is shown and what that place alone decides: its Back,
/// its pane menu, whether it takes pastes, and what follows `/new` and
/// `/close`. Everything else about the chat comes from its [ChatRuntime].
class ChatPanePlacement {
const ChatPanePlacement({
required this.onBack,
required this.onNewSession,
required this.onSessionClosed,
this.syncToken = 0,
this.useNativeBack = false,
this.showBackButton,
this.pasteTarget = false,
this.ownsPasteWhenFocused = false,
this.paneControls,
this.onNextChat,
});
final VoidCallback onBack;
/// `/new` moved the chat to a fresh session (previous id, the new row and
/// the line the new conversation opens on).
final void Function(String previousId, JsonMap session, String notice)
onNewSession;
/// `/close` was accepted.
final VoidCallback onSessionClosed;
/// Advances when the socket is replaced; the conversation re-subscribes.
final int syncToken;
final bool useNativeBack;
final bool? showBackButton;
/// Whether a paste with no field focused goes to this chat.
final bool pasteTarget;
/// Whether this chat takes a paste while it has the keyboard, though it is
/// not the active pane (a chat in a window).
final bool ownsPasteWhenFocused;
/// The pane menu's choices, built in the pane's own context.
final List<Widget> Function(BuildContext paneContext)? paneControls;
final VoidCallback? onNextChat;
}
/// One open chat with the scopes its conversation needs, the same in the
/// Chat destination's tiling and in a floating window. It is keyed by the
/// runtime ([ChatRuntime.viewKey]), so a chat that moves between the two in
/// one frame keeps its live view: transcript, scroll, draft and running turn.
Widget buildChatPaneContent({
required ChatRuntime runtime,
required String sessionId,
required ChatPanePlacement placement,
}) => KeyedSubtree(
key: runtime.viewKey(sessionId),
child: DetailPane(
destination: AppDestination.chat,
implyBack: false,
builder: (context) {
final row = runtime.rowFor(sessionId);
final start = runtime.startNotice;
return ChatComposerFocusScope(
sessionId: sessionId,
requests: runtime.composerFocus,
child: ServerContext.forServer(
context,
runtime.serverIdFor(sessionId),
ChatThreadView(
key: ValueKey<String>('chat-session-$sessionId'),
gateway: runtime.gatewayFor(sessionId),
sessionId: sessionId,
title: '${row?['title'] ?? ''}'.trim(),
session: row,
syncToken: placement.syncToken,
scrollPositions: runtime.scrollPositions,
transcripts: runtime.transcripts,
initialNotice: start != null && start.id == sessionId
? start.notice
: null,
onBack: placement.onBack,
onModelChanged: (applied) =>
runtime.modelChanged(sessionId, applied),
onModeChanged: (mode, name) =>
runtime.acceptMode(sessionId, mode, name),
onEffortChanged: (state) => runtime.acceptEffort(sessionId, state),
onNewSession: placement.onNewSession,
onSessionRenamed: (title) => runtime.acceptRename(sessionId, title),
onSessionClosed: placement.onSessionClosed,
onColorSelected: (color) =>
runtime.sessions.setColor(sessionId, color),
onNextChat: placement.onNextChat,
useNativeBack: placement.useNativeBack,
pasteTarget: placement.pasteTarget,
ownsPasteWhenFocused: placement.ownsPasteWhenFocused,
showBackButton: placement.showBackButton,
paneControls: placement.paneControls?.call(context) ?? const [],
),
),
);
},
),
);
+58 -3
View File
@@ -1,4 +1,6 @@
import 'package:flutter/foundation.dart';
import 'dart:async';
import 'package:flutter/widgets.dart';
import '../../core/api_gateway.dart';
import '../../core/servers/chat/chat_identity.dart';
@@ -20,9 +22,42 @@ import 'ui/sessions/chat_sessions_controller.dart';
///
/// It notifies when a row a conversation is drawn from changes.
class ChatRuntime extends ChangeNotifier {
ChatRuntime({required ApiGateway gateway})
ChatRuntime({required ApiGateway gateway, this.serverKey = ''})
: _gateway = gateway,
sessions = ChatSessionsController(ChatRepository(gateway));
sessions = ChatSessionsController(ChatRepository(gateway)) {
sessions.onSessionReplaced = (previousId, session) {
for (final listener in _replaced.toList()) {
listener(previousId, session);
}
};
}
/// The server (or set of servers) these chats belong to; windows carry it.
final String serverKey;
/// Advances when the socket is replaced, for chats shown outside the Chat
/// destination.
final ValueNotifier<int> syncToken = ValueNotifier(0);
final List<void Function(String previousId, JsonMap session)> _replaced = [];
/// Calls [listener] when another device moved a chat to a new session.
void addSessionReplacedListener(
void Function(String previousId, JsonMap session) listener,
) => _replaced.add(listener);
void removeSessionReplacedListener(
void Function(String previousId, JsonMap session) listener,
) => _replaced.remove(listener);
final Map<String, GlobalKey> _viewKeys = {};
/// The key of [sessionId]'s live view, so it can move between the tiling
/// and a window without being rebuilt.
GlobalKey viewKey(String sessionId) => _viewKeys.putIfAbsent(
sessionId,
() => GlobalKey(debugLabel: 'chat view $sessionId'),
);
ApiGateway _gateway;
@@ -128,6 +163,24 @@ class ChatRuntime extends ChangeNotifier {
return true;
}
/// An accepted mode switch also re-reads the list, so a listing already
/// in flight from before the switch is superseded rather than adopted over
/// the accepted mode.
void acceptMode(String id, String mode, String name) {
if (modeChanged(id, mode, name)) unawaited(sessions.refresh());
}
void acceptEffort(String id, ChatEffortState state) {
if (effortChanged(id, state)) unawaited(sessions.refresh());
}
/// `/rename` was accepted: the opened row follows the shared title and the
/// list re-reads it.
void acceptRename(String id, String title) {
renamed(id, title);
unawaited(sessions.refresh());
}
/// `/rename` was accepted for [id]: the opened row follows the title.
void renamed(String id, String title) {
final row = opened[id];
@@ -183,6 +236,8 @@ class ChatRuntime extends ChangeNotifier {
@override
void dispose() {
_replaced.clear();
syncToken.dispose();
sessions.dispose();
folds.dispose();
composerFocus.dispose();
@@ -14,12 +14,7 @@ mixin _ChatScreenWorkspace on State<ChatScreen> {
ChatFloatFrames get _floatFrames;
void _bindFloat(ChatFloatStore store, Future<void> restored);
void _unbindFloat();
Widget _thread(
String selected,
VoidCallback onBack, {
ChatWorkspacePane? pane,
BuildContext? paneContext,
});
Widget _thread(ChatWorkspacePane pane);
ChatSidebarMode _sidebarMode = ChatSidebarMode.expanded;
void _setSidebarMode(ChatSidebarMode mode) {
@@ -390,16 +385,9 @@ mixin _ChatScreenWorkspace on State<ChatScreen> {
// active pane the others fade theirs.
child: ChatPaneActivity(
inactive: _workspace.paneCount > 1 && !active,
child: DetailPane(
child: KeyedSubtree(
key: ValueKey('chat-pane-detail-${pane.id}'),
destination: AppDestination.chat,
implyBack: false,
builder: (context) => _thread(
pane.sessionId,
_back,
pane: pane,
paneContext: context,
),
child: _thread(pane),
),
),
),
@@ -202,7 +202,7 @@ class _ChatBackgroundTasksState extends State<ChatBackgroundTasks> {
// Level with the field's single line, beside Send.
padding: const EdgeInsetsDirectional.only(end: 4, bottom: 4),
child: Semantics(
identifier: 'chat-background-tasks-button',
identifier: 'chat-background-tasks-button',
button: true,
excludeSemantics: true,
onTap: _open,
+50 -114
View File
@@ -1,7 +1,7 @@
import 'chat_pane_content.dart';
import 'chat_runtime.dart';
import 'workspace/federated_workspace_store.dart';
import '../../core/servers/chat/federated_chat_gateway.dart';
import '../../core/servers/server_context.dart';
import '../../shell/destination.dart';
import '../../shell/shell_bar.dart';
import '../../shell/shell_destination_menu.dart';
@@ -27,8 +27,6 @@ import 'workspace/chat_workspace_binding.dart';
import 'workspace/chat_workspace_snapshot.dart';
import 'workspace/chat_workspace_store.dart';
import 'conversation/chat_bar_tint.dart';
import 'conversation/chat_efforts.dart';
import 'conversation/chat_models.dart';
import 'conversation/chat_pane_picker.dart';
import 'conversation/chat_add_pane_picker.dart';
import 'conversation/chat_sidebar_controls.dart';
@@ -40,7 +38,6 @@ import 'workspace/chat_center_stage_keys.dart';
import 'workspace/chat_pane_move_actions.dart';
import 'workspace/chat_workspace_entrance.dart';
import 'ui/sessions/chat_adaptive.dart';
import 'conversation/chat_thread_view.dart';
import 'conversation/composer_focus_request.dart';
import 'conversation/composer_selection_focus.dart';
import 'domain/chat_commands.dart';
@@ -139,7 +136,7 @@ class _ChatScreenState extends State<ChatScreen>
_runtime
..gateway = widget.gateway
..addListener(_runtimeChanged);
_sessions.onSessionReplaced = _replacedElsewhere;
_runtime.addSessionReplacedListener(_replacedElsewhere);
final initial = widget.initialSessionId;
if (initial != null && initial.isNotEmpty) {
_workspace.open(initial);
@@ -275,13 +272,6 @@ class _ChatScreenState extends State<ChatScreen>
if (_workspace.paneCount == 0) _open(session);
}
/// `/rename` was accepted for [id]: the opened row follows the shared title
/// and the list re-reads it.
void _renamed(String id, String title) {
_runtime.renamed(id, title);
unawaited(_sessions.refresh());
}
/// `/close` was accepted: the conversation leaves, back to the list, which
/// re-reads the closed row.
void _closed(String sessionId) {
@@ -328,25 +318,6 @@ class _ChatScreenState extends State<ChatScreen>
);
}
void _modelChanged(String id, ChatModelSelection applied) {
if (mounted) _runtime.modelChanged(id, applied);
}
/// An accepted mode switch updates the opened row, so reopening the chat in
/// this visit shows it before the list re-reads. The list then re-reads, so
/// a listing already in flight from before the switch is superseded rather
/// than adopted over the accepted mode.
void _modeChanged(String id, String mode, String name) {
if (!mounted || !_runtime.modeChanged(id, mode, name)) return;
unawaited(_sessions.refresh());
}
/// An accepted thinking level updates the opened row like a mode switch.
void _effortChanged(String id, ChatEffortState state) {
if (!mounted || !_runtime.effortChanged(id, state)) return;
unawaited(_sessions.refresh());
}
@override
JsonMap? _rowFor(String id) => _runtime.rowFor(id);
@@ -410,97 +381,62 @@ class _ChatScreenState extends State<ChatScreen>
_sessions.removeListener(_availableSessionsChanged);
_folds.removeListener(_nextChanged);
_runtime.removeListener(_runtimeChanged);
if (widget.runtime == null) {
_runtime.dispose();
} else {
_sessions.onSessionReplaced = null;
}
_runtime.removeSessionReplacedListener(_replacedElsewhere);
if (widget.runtime == null) _runtime.dispose();
super.dispose();
}
@override
Widget _thread(
String selected,
VoidCallback onBack, {
ChatWorkspacePane? pane,
BuildContext? paneContext,
}) {
final row = _rowFor(selected);
final start = _runtime.startNotice;
final thread = ChatThreadView(
key: ValueKey<String>('chat-session-$selected'),
gateway: _runtime.gatewayFor(selected),
Widget _thread(ChatWorkspacePane pane) {
final selected = pane.sessionId;
return buildChatPaneContent(
runtime: _runtime,
sessionId: selected,
title: '${row?['title'] ?? ''}'.trim(),
session: row,
syncToken: widget.syncToken,
scrollPositions: _runtime.scrollPositions,
transcripts: _runtime.transcripts,
initialNotice: start != null && start.id == selected
? start.notice
: null,
onBack: onBack,
onModelChanged: (applied) => _modelChanged(selected, applied),
onModeChanged: (mode, name) => _modeChanged(selected, mode, name),
onEffortChanged: (state) => _effortChanged(selected, state),
onNewSession: _startedNewSession,
onSessionRenamed: (title) => _renamed(selected, title),
onSessionClosed: () => _closed(selected),
onColorSelected: (color) => _sessions.setColor(selected, color),
onNextChat: _nextAfter(selected) != null
? () => _openNext(selected)
: null,
useNativeBack: _page.open,
pasteTarget:
_workspaceVisible &&
(pane == null || pane.id == _workspace.activePaneId),
// A floating chat is never the active pane, so its composer takes a
// paste while it has the keyboard.
ownsPasteWhenFocused: pane != null && _workspace.isFloating(pane.id),
showBackButton: pane == null
? null
: _page.open || !showsListBesideDetail(context),
paneControls: pane == null
? const []
: _workspace.isFloating(pane.id)
? _floatControls(pane)
: [
ChatBarAction(
label: 'Add chat pane',
onPressed: () => _addPane(paneContext ?? context),
materialIcon: Icons.add_box_outlined,
cupertinoIcon: CupertinoIcons.plus_square,
),
if (_workspace.paneCount > 1)
placement: ChatPanePlacement(
syncToken: widget.syncToken,
onBack: _back,
onNewSession: _startedNewSession,
onSessionClosed: () => _closed(selected),
onNextChat: _nextAfter(selected) != null
? () => _openNext(selected)
: null,
useNativeBack: _page.open,
pasteTarget: _workspaceVisible && pane.id == _workspace.activePaneId,
// A floating chat is never the active pane, so its composer takes a
// paste while it has the keyboard.
ownsPasteWhenFocused: _workspace.isFloating(pane.id),
showBackButton: _page.open || !showsListBesideDetail(context),
paneControls: (paneContext) => _workspace.isFloating(pane.id)
? _floatControls(pane)
: [
ChatBarAction(
label: 'Open chats (${_workspace.paneCount})',
onPressed: () => _pickPane(paneContext ?? context),
materialIcon: Icons.view_quilt_outlined,
cupertinoIcon: CupertinoIcons.square_split_2x2,
label: 'Add chat pane',
onPressed: () => _addPane(paneContext),
materialIcon: Icons.add_box_outlined,
cupertinoIcon: CupertinoIcons.plus_square,
),
if (_workspace.paneCount > 1 && showsListBesideDetail(context))
chatCenterStageAction(ChatCenterStageController.instance),
...chatPaneMoveActions(
ChatWorkspacePaneScope.maybeOf(paneContext ?? context),
onMove: (target) => _movePane(pane.id, target),
),
..._floatControls(pane),
if (_workspace.paneCount > 1)
ChatBarAction(
label: 'Close pane',
onPressed: () => _closePane(pane.id),
materialIcon: Icons.close,
cupertinoIcon: CupertinoIcons.xmark,
if (_workspace.paneCount > 1)
ChatBarAction(
label: 'Open chats (${_workspace.paneCount})',
onPressed: () => _pickPane(paneContext),
materialIcon: Icons.view_quilt_outlined,
cupertinoIcon: CupertinoIcons.square_split_2x2,
),
if (_workspace.paneCount > 1 && showsListBesideDetail(context))
chatCenterStageAction(ChatCenterStageController.instance),
...chatPaneMoveActions(
ChatWorkspacePaneScope.maybeOf(paneContext),
onMove: (target) => _movePane(pane.id, target),
),
],
);
return ChatComposerFocusScope(
sessionId: selected,
requests: _composerFocus,
child: ServerContext.forServer(
context,
_runtime.serverIdFor(selected),
thread,
..._floatControls(pane),
if (_workspace.paneCount > 1)
ChatBarAction(
label: 'Close pane',
onPressed: () => _closePane(pane.id),
materialIcon: Icons.close,
cupertinoIcon: CupertinoIcons.xmark,
),
],
),
);
}
@@ -142,8 +142,10 @@ class ChatWorkspaceGeometry {
};
if (gap < -slack || gap > chatPaneDividerExtent + slack) continue;
final overlap = horizontal
? math.min(origin.bottom, rect.bottom) - math.max(origin.top, rect.top)
: math.min(origin.right, rect.right) - math.max(origin.left, rect.left);
? math.min(origin.bottom, rect.bottom) -
math.max(origin.top, rect.top)
: math.min(origin.right, rect.right) -
math.max(origin.left, rect.left);
if (overlap <= slack) continue;
final centred = horizontal
? rect.top <= centre && centre < rect.bottom
@@ -1,7 +1,6 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/chat_runtime.dart';
import 'package:pipelines_app/features/chat/conversation/chat_models.dart';
class _Gateway implements ApiGateway {
@override