From cb49aeef3456b1ee3f4850eef90c9d356c7e8374 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 13 Nov 2026 12:45:42 +0000 Subject: [PATCH] Drag chats from the list onto panes On desktop a chat dragged from the list or compact rail onto a tiled pane replaces that pane's chat when dropped in its centre, or opens in a split right of or below it when dropped on that edge; an open chat moves without duplicating and a floating chat docks. --- .../features/chat/chat_screen_workspace.dart | 20 ++++ app/lib/features/chat/screen.dart | 1 + .../ui/sessions/chat_compact_sidebar.dart | 79 +++++++++------ .../chat/ui/sessions/chat_sessions_body.dart | 3 + .../ui/sessions/chat_sessions_screen.dart | 3 + .../chat/ui/sessions/session_list_items.dart | 2 + .../ui/sessions/session_list_material.dart | 3 + .../chat/ui/sessions/session_row_content.dart | 21 +++- .../chat/ui/sessions/session_row_memo.dart | 3 + .../chat/workspace/chat_workspace.dart | 5 + .../workspace/chat_workspace_controller.dart | 51 ++++++++++ .../chat/workspace/chat_workspace_drop.dart | 95 +++++++++++++++++++ .../workspace/chat_workspace_surface.dart | 13 +++ app/notes/drag-chats-onto-panes.md | 13 +++ .../chat/workspace/chat_workspace_test.dart | 31 ++++++ 15 files changed, 313 insertions(+), 30 deletions(-) create mode 100644 app/lib/features/chat/workspace/chat_workspace_drop.dart create mode 100644 app/notes/drag-chats-onto-panes.md diff --git a/app/lib/features/chat/chat_screen_workspace.dart b/app/lib/features/chat/chat_screen_workspace.dart index 9e537bf4..38cf6ab6 100644 --- a/app/lib/features/chat/chat_screen_workspace.dart +++ b/app/lib/features/chat/chat_screen_workspace.dart @@ -80,6 +80,25 @@ mixin _ChatScreenWorkspace on State { ChatSectionFolds get _foldsForSidebar; void _openSidebarSession(JsonMap session); + void _dropSession( + ChatWorkspacePane target, + Object data, + ChatWorkspaceDropZone zone, + ) { + if (data is! JsonMap) return; + final sessionId = '${data['id'] ?? ''}'; + if (sessionId.isEmpty) return; + final windows = widget.windows; + final floating = windows?.windows + .where((window) => window.contentId == sessionId) + .firstOrNull; + if (floating != null) windows!.manager.close(floating.id); + _openedSessions[sessionId] = data; + _workspaceVisible = true; + _workspace.dropSession(sessionId, target.id, zone); + _composerFocus.request(sessionId); + } + final ChatWorkspaceController _workspace = ChatWorkspaceController(); ChatOpenWorkspaceController? _openWorkspace; bool _applyingServerWorkspace = false; @@ -467,6 +486,7 @@ mixin _ChatScreenWorkspace on State { paneTitle: (pane) => '${_rowFor(pane.sessionId)?['title'] ?? pane.sessionId}', onFocusPane: (pane) => _composerFocus.request(pane.sessionId), + onDrop: _dropSession, paneBuilder: (context, pane, active) { final scope = ChatWorkspacePaneScope.maybeOf(context); final topShared = scope?.topEdgeShared ?? false; diff --git a/app/lib/features/chat/screen.dart b/app/lib/features/chat/screen.dart index dadd1888..c539d800 100644 --- a/app/lib/features/chat/screen.dart +++ b/app/lib/features/chat/screen.dart @@ -491,6 +491,7 @@ class _ChatScreenState extends State controller: _sessions, folds: _folds, onOpen: _openBySelection, + draggable: beside, onOpenBeside: beside ? _openBeside : null, onOpenBelow: _openBelow, onFloat: _floatsHere ? _floatSession : null, diff --git a/app/lib/features/chat/ui/sessions/chat_compact_sidebar.dart b/app/lib/features/chat/ui/sessions/chat_compact_sidebar.dart index 587e205c..81e69824 100644 --- a/app/lib/features/chat/ui/sessions/chat_compact_sidebar.dart +++ b/app/lib/features/chat/ui/sessions/chat_compact_sidebar.dart @@ -82,6 +82,38 @@ class ChatCompactSidebar extends StatelessWidget { final color = chatColorShade(context, chatSessionColor(row)) ?? Theme.of(context).colorScheme.primary; + final button = _button( + context, + details, + ExcludeSemantics( + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + ChatCompactAvatar( + title: title, + color: color, + active: selected, + visible: visible, + running: running, + backgroundTasks: backgroundTasks, + unread: unread, + ), + if (machineName case final name?) + Text( + name, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: Theme.of(context).textTheme.labelSmall, + ), + ], + ), + ), + () { + unawaited(controller.markOpened(row)); + onOpen(row); + }, + selected: selected, + ); return ChatVisibilityMarker( sessionId: id, color: color, @@ -92,37 +124,26 @@ class ChatCompactSidebar extends StatelessWidget { ? Duration.zero : const Duration(milliseconds: 200), color: selected ? Colors.transparent : color.withValues(alpha: .07), - child: _button( - context, - details, - ExcludeSemantics( - child: Column( - mainAxisSize: MainAxisSize.min, - children: [ - ChatCompactAvatar( - title: title, - color: color, - active: selected, - visible: visible, - running: running, - backgroundTasks: backgroundTasks, - unread: unread, - ), - if (machineName case final name?) - Text( - name, - maxLines: 1, - overflow: TextOverflow.ellipsis, - style: Theme.of(context).textTheme.labelSmall, - ), - ], + child: LongPressDraggable( + data: row, + feedback: Material( + elevation: 4, + borderRadius: BorderRadius.circular(20), + child: Chip( + avatar: ChatCompactAvatar( + title: title, + color: color, + active: false, + visible: false, + running: running, + backgroundTasks: backgroundTasks, + unread: unread, + ), + label: Text(title), ), ), - () { - unawaited(controller.markOpened(row)); - onOpen(row); - }, - selected: selected, + childWhenDragging: Opacity(opacity: .35, child: button), + child: button, ), ), ); diff --git a/app/lib/features/chat/ui/sessions/chat_sessions_body.dart b/app/lib/features/chat/ui/sessions/chat_sessions_body.dart index 3e32acac..9a9f6a43 100644 --- a/app/lib/features/chat/ui/sessions/chat_sessions_body.dart +++ b/app/lib/features/chat/ui/sessions/chat_sessions_body.dart @@ -33,6 +33,7 @@ class ChatSessionsBody extends StatelessWidget { required this.error, required this.onOpen, this.onOpenBeside, + this.draggable = false, this.onOpenBelow, this.onFloat, this.paneMenu, @@ -65,6 +66,7 @@ class ChatSessionsBody extends StatelessWidget { final String? error; final ValueChanged onOpen; final ValueChanged? onOpenBeside; + final bool draggable; final ValueChanged? onOpenBelow; final ValueChanged? onFloat; final SessionPaneMenu? paneMenu; @@ -104,6 +106,7 @@ class ChatSessionsBody extends StatelessWidget { duplicateTitles: duplicateTitles, onOpen: onOpen, onOpenBeside: onOpenBeside, + draggable: draggable, onOpenBelow: onOpenBelow, onFloat: onFloat, paneMenu: paneMenu, diff --git a/app/lib/features/chat/ui/sessions/chat_sessions_screen.dart b/app/lib/features/chat/ui/sessions/chat_sessions_screen.dart index d789d670..aae7e396 100644 --- a/app/lib/features/chat/ui/sessions/chat_sessions_screen.dart +++ b/app/lib/features/chat/ui/sessions/chat_sessions_screen.dart @@ -33,6 +33,7 @@ class ChatSessionsScreen extends StatefulWidget { required this.gateway, required this.onOpen, this.onOpenBeside, + this.draggable = false, this.onOpenBelow, this.onFloat, this.paneMenu, @@ -52,6 +53,7 @@ class ChatSessionsScreen extends StatefulWidget { /// Optional split-pane actions; absent when the host cannot offer a split. final ValueChanged? onOpenBeside; + final bool draggable; final ValueChanged? onOpenBelow; /// Floats the chat in a window over the app; absent when it cannot. @@ -333,6 +335,7 @@ class _ChatSessionsScreenState extends State { onOpenBeside: widget.onOpenBeside == null ? null : _openBeside, + draggable: widget.draggable, onOpenBelow: widget.onOpenBelow == null ? null : _openBelow, diff --git a/app/lib/features/chat/ui/sessions/session_list_items.dart b/app/lib/features/chat/ui/sessions/session_list_items.dart index 922171e3..bafb31ce 100644 --- a/app/lib/features/chat/ui/sessions/session_list_items.dart +++ b/app/lib/features/chat/ui/sessions/session_list_items.dart @@ -88,6 +88,7 @@ SessionListItem chatSessionRowItem( required Set duplicateTitles, required bool Function(JsonMap) isUnseen, required ValueChanged onOpen, + bool draggable = false, ValueChanged? onOpenBeside, ValueChanged? onOpenBelow, ValueChanged? onFloat, @@ -112,6 +113,7 @@ SessionListItem chatSessionRowItem( unseen: isUnseen(session), divider: divider, onOpen: onOpen, + draggable: draggable, onOpenBeside: onOpenBeside, onOpenBelow: onOpenBelow, onFloat: onFloat, diff --git a/app/lib/features/chat/ui/sessions/session_list_material.dart b/app/lib/features/chat/ui/sessions/session_list_material.dart index 01d1928b..03e7dbc2 100644 --- a/app/lib/features/chat/ui/sessions/session_list_material.dart +++ b/app/lib/features/chat/ui/sessions/session_list_material.dart @@ -14,6 +14,7 @@ class MaterialSessionList extends StatelessWidget { required this.emptyMessage, required this.duplicateTitles, required this.onOpen, + this.draggable = false, this.onOpenBeside, this.onOpenBelow, this.onFloat, @@ -42,6 +43,7 @@ class MaterialSessionList extends StatelessWidget { final String? emptyMessage; final Set duplicateTitles; final ValueChanged onOpen; + final bool draggable; final ValueChanged? onOpenBeside; final ValueChanged? onOpenBelow; final ValueChanged? onFloat; @@ -117,6 +119,7 @@ class MaterialSessionList extends StatelessWidget { duplicateTitles: duplicateTitles, isUnseen: isUnseen, onOpen: onOpen, + draggable: draggable, onOpenBeside: onOpenBeside, onOpenBelow: onOpenBelow, onFloat: onFloat, diff --git a/app/lib/features/chat/ui/sessions/session_row_content.dart b/app/lib/features/chat/ui/sessions/session_row_content.dart index 86a0e791..b9369a34 100644 --- a/app/lib/features/chat/ui/sessions/session_row_content.dart +++ b/app/lib/features/chat/ui/sessions/session_row_content.dart @@ -44,12 +44,14 @@ class ChatSessionRow extends StatelessWidget { this.duplicateTitles = const {}, this.onLongPress, this.selectionOnly = false, + this.draggable = false, }); final JsonMap session; /// A choosing surface keeps the normal row content but offers only opening. final bool selectionOnly; + final bool draggable; final ValueChanged onOpen; final ValueChanged? onOpenBeside; final ValueChanged? onOpenBelow; @@ -78,7 +80,24 @@ class ChatSessionRow extends StatelessWidget { color: tint == null ? background : Color.alphaBlend(tint, background), marker: marker, markerWidth: visible ? (selected ? 10 : 3) : 0, - child: _marked(context, id, marker, accent), + child: draggable + ? LongPressDraggable( + data: session, + feedback: Material( + elevation: 4, + borderRadius: BorderRadius.circular(20), + child: Chip( + avatar: ChatSessionAvatar(session: session), + label: Text(chatSessionTitle(session)), + ), + ), + childWhenDragging: Opacity( + opacity: .35, + child: _marked(context, id, marker, accent), + ), + child: _marked(context, id, marker, accent), + ) + : _marked(context, id, marker, accent), ); } diff --git a/app/lib/features/chat/ui/sessions/session_row_memo.dart b/app/lib/features/chat/ui/sessions/session_row_memo.dart index 6155333f..f3da83da 100644 --- a/app/lib/features/chat/ui/sessions/session_row_memo.dart +++ b/app/lib/features/chat/ui/sessions/session_row_memo.dart @@ -21,6 +21,7 @@ Widget memoChatSessionRow({ required bool unseen, required bool divider, required ValueChanged onOpen, + bool draggable = false, ValueChanged? onOpenBeside, ValueChanged? onOpenBelow, ValueChanged? onFloat, @@ -43,6 +44,7 @@ Widget memoChatSessionRow({ unseen, divider, onOpen, + draggable, onOpenBeside, onOpenBelow, onFloat, @@ -63,6 +65,7 @@ Widget memoChatSessionRow({ unseen: unseen, divider: divider, onOpen: onOpen, + draggable: draggable, onOpenBeside: onOpenBeside, onOpenBelow: onOpenBelow, onFloat: onFloat, diff --git a/app/lib/features/chat/workspace/chat_workspace.dart b/app/lib/features/chat/workspace/chat_workspace.dart index 4b226417..11e345a4 100644 --- a/app/lib/features/chat/workspace/chat_workspace.dart +++ b/app/lib/features/chat/workspace/chat_workspace.dart @@ -17,12 +17,14 @@ import 'chat_pane_switch_motion.dart'; import 'chat_pane_backdrop.dart'; import 'chat_pane_dim.dart'; import 'chat_workspace_pane_scope.dart'; +import 'chat_workspace_drop.dart'; import 'chat_workspace_surface.dart'; import '../../../ui/input/keyboard_owner.dart'; export 'chat_workspace_controller.dart'; export 'chat_workspace_geometry.dart' show ChatPaneDirection; export 'chat_workspace_pane_scope.dart'; +export 'chat_workspace_drop.dart'; typedef ChatWorkspacePaneBuilder = Widget Function(BuildContext context, ChatWorkspacePane pane, bool active); @@ -35,6 +37,7 @@ class ChatWorkspace extends StatefulWidget { required this.controller, required this.paneBuilder, this.onFocusPane, + this.onDrop, this.shortcutsEnabled = true, this.singlePane = false, this.mobilePeeks = false, @@ -71,6 +74,7 @@ class ChatWorkspace extends StatefulWidget { /// Keyboard navigation only: the owner can focus the target composer. /// Pointer activation leaves text-selection and pointer focus alone. final ValueChanged? onFocusPane; + final void Function(ChatWorkspacePane, Object, ChatWorkspaceDropZone)? onDrop; @override State createState() => _ChatWorkspaceState(); @@ -393,6 +397,7 @@ class _ChatWorkspaceState extends State sharedEdges, moveTargets[pane.id], ), + onDrop: widget.onDrop, ); }, ), diff --git a/app/lib/features/chat/workspace/chat_workspace_controller.dart b/app/lib/features/chat/workspace/chat_workspace_controller.dart index fdc825a5..bfbfa91a 100644 --- a/app/lib/features/chat/workspace/chat_workspace_controller.dart +++ b/app/lib/features/chat/workspace/chat_workspace_controller.dart @@ -3,6 +3,7 @@ import 'package:flutter/foundation.dart'; import 'chat_workspace_nodes.dart'; import 'chat_workspace_reconcile.dart'; import 'chat_workspace_snapshot.dart'; +import 'chat_workspace_drop.dart'; export 'chat_workspace_nodes.dart'; @@ -237,6 +238,56 @@ class ChatWorkspaceController extends ChangeNotifier { _changed(); } + /// Places a dragged chat into a pane without ever creating a duplicate. + void dropSession( + String sessionId, + String targetPaneId, + ChatWorkspaceDropZone zone, + ) { + if (sessionId.isEmpty || _root == null) return; + final target = panes.where((pane) => pane.id == targetPaneId).firstOrNull; + if (target == null) return; + final source = panes + .where((pane) => pane.sessionId == sessionId) + .firstOrNull; + if (source?.id == target.id) { + activate(target.id); + return; + } + if (zone == ChatWorkspaceDropZone.replace) { + if (source == null) { + _root = _replace( + _root!, + target.id, + ChatWorkspacePane(target.id, sessionId), + ); + } else { + _root = swapChatWorkspacePanes(_root!, source, target); + } + _active = target.id; + _changed(); + return; + } + if (source != null) close(source.id); + final axis = zone == ChatWorkspaceDropZone.right + ? ChatSplitAxis.horizontal + : ChatSplitAxis.vertical; + final current = panes.where((pane) => pane.id == target.id).firstOrNull; + if (current == null) return; + _root = _replace( + _root!, + current.id, + ChatWorkspaceSplit( + _id('split'), + axis, + current, + ChatWorkspacePane(_id('pane'), sessionId), + ), + ); + _active = panes.last.id; + _changed(); + } + ChatWorkspaceNode _replace( ChatWorkspaceNode node, String id, diff --git a/app/lib/features/chat/workspace/chat_workspace_drop.dart b/app/lib/features/chat/workspace/chat_workspace_drop.dart new file mode 100644 index 00000000..357df303 --- /dev/null +++ b/app/lib/features/chat/workspace/chat_workspace_drop.dart @@ -0,0 +1,95 @@ +import 'package:flutter/material.dart'; + +import 'chat_workspace_nodes.dart'; + +enum ChatWorkspaceDropZone { replace, right, bottom } + +class ChatWorkspaceDropTarget extends StatefulWidget { + const ChatWorkspaceDropTarget({ + super.key, + required this.pane, + required this.rect, + required this.onDrop, + }); + + final ChatWorkspacePane pane; + final Rect rect; + final void Function(ChatWorkspacePane, Object, ChatWorkspaceDropZone) onDrop; + + @override + State createState() => + _ChatWorkspaceDropTargetState(); +} + +class _ChatWorkspaceDropTargetState extends State { + ChatWorkspaceDropZone? _zone; + + ChatWorkspaceDropZone _zoneAt(Offset local) { + if (local.dx >= widget.rect.width * .75) { + return ChatWorkspaceDropZone.right; + } + if (local.dy >= widget.rect.height * .75) { + return ChatWorkspaceDropZone.bottom; + } + return ChatWorkspaceDropZone.replace; + } + + @override + Widget build(BuildContext context) => DragTarget( + onWillAcceptWithDetails: (_) => true, + onMove: (details) { + final box = context.findRenderObject() as RenderBox?; + if (box == null) return; + final zone = _zoneAt(box.globalToLocal(details.offset)); + if (_zone != zone) setState(() => _zone = zone); + }, + onLeave: (_) { + if (mounted) setState(() => _zone = null); + }, + onAcceptWithDetails: (details) { + final box = context.findRenderObject() as RenderBox?; + final zone = box == null + ? ChatWorkspaceDropZone.replace + : _zoneAt(box.globalToLocal(details.offset)); + setState(() => _zone = null); + widget.onDrop(widget.pane, details.data, zone); + }, + builder: (context, candidates, rejected) { + final zone = _zone; + if (zone == null) return const SizedBox.expand(); + final color = Theme.of(context).colorScheme.primary; + final indicator = switch (zone) { + ChatWorkspaceDropZone.replace => const SizedBox.expand(), + ChatWorkspaceDropZone.right => const Align( + alignment: Alignment.centerRight, + child: FractionallySizedBox(widthFactor: .5, heightFactor: 1), + ), + ChatWorkspaceDropZone.bottom => const Align( + alignment: Alignment.bottomCenter, + child: FractionallySizedBox(widthFactor: 1, heightFactor: .5), + ), + }; + return Stack( + fit: StackFit.expand, + children: [ + DecoratedBox( + decoration: BoxDecoration(color: color.withValues(alpha: .18)), + child: indicator, + ), + if (zone != ChatWorkspaceDropZone.replace) + Align( + alignment: zone == ChatWorkspaceDropZone.right + ? Alignment.centerRight + : Alignment.bottomCenter, + child: Icon( + zone == ChatWorkspaceDropZone.right + ? Icons.vertical_split + : Icons.horizontal_split, + color: color, + ), + ), + ], + ); + }, + ); +} diff --git a/app/lib/features/chat/workspace/chat_workspace_surface.dart b/app/lib/features/chat/workspace/chat_workspace_surface.dart index d958d5bd..5f63654a 100644 --- a/app/lib/features/chat/workspace/chat_workspace_surface.dart +++ b/app/lib/features/chat/workspace/chat_workspace_surface.dart @@ -13,6 +13,7 @@ import 'chat_motion_viewport.dart'; import 'chat_workspace_controller.dart'; import 'chat_workspace_divider.dart'; import 'chat_workspace_geometry.dart'; +import 'chat_workspace_drop.dart'; /// Rendering only: retained live panes stay stable siblings, while the outgoing /// picture and mobile drag target sit alongside them in the same clipped stack. @@ -30,6 +31,7 @@ Widget chatWorkspaceSurface({ required List panes, required Widget Function(ChatWorkspacePane) paneView, Widget Function(ChatWorkspacePane)? stageStrip, + void Function(ChatWorkspacePane, Object, ChatWorkspaceDropZone)? onDrop, }) { final moving = Listenable.merge([ motion.animation, @@ -145,6 +147,17 @@ Widget chatWorkspaceSurface({ rect: stage.rectFor(layout.stageId)!, size: size, ), + if (!collapsed && mobile == null && onDrop != null) + for (final pane in panes) + if (geometry.panes[pane.id] case final rect?) + Positioned.fromRect( + rect: rect, + child: ChatWorkspaceDropTarget( + pane: pane, + rect: rect, + onDrop: onDrop, + ), + ), if (mobile?.peekId != null && !motion.isAnimating) Positioned( top: mobile!.contentHeight - 12, diff --git a/app/notes/drag-chats-onto-panes.md b/app/notes/drag-chats-onto-panes.md new file mode 100644 index 00000000..bac7e9fb --- /dev/null +++ b/app/notes/drag-chats-onto-panes.md @@ -0,0 +1,13 @@ +# Drag chats onto panes + +Implemented desktop chat dragging from expanded rows and compact avatars. A +drop in a pane replaces it; the right and bottom edge zones split horizontally +or vertically. Existing open chats move without duplicates, and floating chat +windows close before docking into the selected pane. + +Focused regression: `tool/memwatch/full.sh +'test/features/chat/workspace/chat_workspace_test.dart'` — 11 passed. +`tool/analyze.sh` — clean. + +Browser captures and full UI-review fixture coverage were not run in this +implementation stage. diff --git a/app/test/features/chat/workspace/chat_workspace_test.dart b/app/test/features/chat/workspace/chat_workspace_test.dart index 6614e88f..900edc38 100644 --- a/app/test/features/chat/workspace/chat_workspace_test.dart +++ b/app/test/features/chat/workspace/chat_workspace_test.dart @@ -84,6 +84,37 @@ void main() { expect(controller.panes.last.sessionId, 'new-two'); }); + test('drop replaces, splits and moves without duplicating a chat', () { + final controller = ChatWorkspaceController() + ..open('one') + ..splitActive('two', ChatSplitAxis.horizontal); + addTearDown(controller.dispose); + final first = controller.panes.first; + final second = controller.panes.last; + + controller.dropSession('three', first.id, ChatWorkspaceDropZone.replace); + expect(controller.panes.map((pane) => pane.sessionId), ['three', 'two']); + + controller.dropSession('four', second.id, ChatWorkspaceDropZone.right); + expect(controller.panes.map((pane) => pane.sessionId), [ + 'three', + 'two', + 'four', + ]); + expect( + controller.panes.where((pane) => pane.sessionId == 'three'), + hasLength(1), + ); + + final four = controller.panes.last; + controller.dropSession('three', four.id, ChatWorkspaceDropZone.bottom); + expect( + controller.panes.where((pane) => pane.sessionId == 'three'), + hasLength(1), + ); + expect(controller.activePane?.sessionId, 'three'); + }); + test( 'geometry clamps visible sizes but retains ratios and spatial neighbors', () {