part of 'screen.dart'; /// Floating a tiled pane into a window and docking it back (see /// [ChatWindows] and `notes/chat-floating-pane.md`). mixin _ChatScreenFloat on State, _ChatScreenWorkspace { ChatTiling? _tiling; void _bindWindows() { final windows = widget.windows; if (windows == null) return; _workspace.showOutside = _showOutside; windows.manager.addListener(_windowsChanged); windows.tiling = _tiling = ChatTiling( holds: _workspace.holdsSession, dock: _dockSession, canFloatActive: () { final pane = _workspace.activePane; return mounted && pane != null && _floatsHere && _workspaceVisible && _workspace.canFloat(pane.id) && _chatOwnsKeyboard(); }, floatActive: () { final pane = _workspace.activePane; if (pane != null) _floatPane(pane.id); }, release: _releaseSession, activeSession: () => _workspace.activePane?.sessionId, ); } void _unbindWindows() { final windows = widget.windows; if (windows != null && identical(windows.tiling, _tiling)) { windows.tiling = null; } windows?.manager.removeListener(_windowsChanged); _workspace.showOutside = null; } /// A chat out in a window comes to the top on a wide layout. A phone /// ignores floating: the chat leaves its (hidden) window and opens as a /// pane, still floating in the server's set. bool _showOutside(String sessionId) { final windows = widget.windows; if (windows == null) return false; if (!_phoneIgnoresFloat) return windows.show(sessionId); _holdForPhone(windows, sessionId); return false; } /// Takes [sessionId] out of its window, to be tiled on this phone layout. void _holdForPhone(ChatWindows windows, String sessionId) { final window = windows.windows .where((window) => window.contentId == sessionId) .firstOrNull; if (window == null) return; _phoneDocked.add(sessionId); windows.manager.close(window.id); } /// Whether the last build was a phone's layout, which tiles floating chats. bool? _builtPhoneFloat; bool _followingFloat = false; /// Called from build: a change between a phone and a wide layout moves /// the floating chats between panes and windows after the frame. void _trackFloatLayout() { _phoneLayout = !showsListBesideDetail(context); if (widget.windows == null) return; final phone = _phoneIgnoresFloat; if (phone == _builtPhoneFloat) return; _builtPhoneFloat = phone; _followFloatLayoutSoon(); } void _windowsChanged() { if (_builtPhoneFloat == true && (widget.windows?.windows.isNotEmpty ?? false)) { _followFloatLayoutSoon(); } } void _followFloatLayoutSoon() { if (_followingFloat) return; _followingFloat = true; WidgetsBinding.instance.addPostFrameCallback((_) { _followingFloat = false; if (mounted) _followFloatLayout(); }); } /// On a phone every floating chat becomes a pane (the chat on screen /// stays); on a wide layout the chats held that way float again. void _followFloatLayout() { final windows = widget.windows; if (windows == null) return; final phone = _phoneIgnoresFloat; final ids = phone ? windows.floatingSessionIds.toList() : _phoneDocked.toList(); if (ids.isEmpty) return; // One write for the whole move: the chats stay floating in the set. _applyingServerWorkspace = true; try { if (phone) { final active = _workspace.activePaneId; for (final id in ids) { _holdForPhone(windows, id); _workspace.dock(id); } if (active != null) _workspace.activate(active); } else { for (final id in ids) { final pane = _workspace.panes .where((pane) => pane.sessionId == id) .firstOrNull; if (pane == null) continue; _workspace.detach(pane.id); windows.float(id); } _phoneDocked.clear(); } } finally { _applyingServerWorkspace = false; } _syncOpenWorkspace(); if (!phone && _workspace.paneCount == 0) _back(); } /// Panes float only on a wide layout, beside the chat list. bool get _floatsHere => widget.windows != null && !_pageOpen && showsListBesideDetail(context); /// The pane leaves the tiling and its live view moves into a window in /// the same frame. void _floatPane(String paneId) { final pane = _workspace.detach(paneId); if (pane == null) return; widget.windows?.float(pane.sessionId); _syncOpenWorkspace(); // Floating the last tiled chat leaves the list, as closing it does. if (_workspace.paneCount == 0) _back(); } /// The chat list's Float choice: a tiled chat leaves the tiling, a chat /// already in a window comes to the top, any other opens in a new window. void _floatSession(JsonMap session) { final id = '${session['id'] ?? ''}'; final windows = widget.windows; if (id.isEmpty || windows == null || windows.show(id)) return; final pane = _workspace.panes.where((p) => p.sessionId == id).firstOrNull; if (pane != null) { _floatPane(pane.id); } else { windows.float(id); _syncOpenWorkspace(); } } /// [sessionId] went to a window of its own: its panes leave the tiling, /// as a floated pane does, and the list shows when none is left. void _releaseSession(String sessionId) { if (!mounted) return; final panes = _workspace.panes.where((p) => p.sessionId == sessionId); if (panes.isEmpty) return; for (final pane in panes.toList()) { _workspace.detach(pane.id); } _syncOpenWorkspace(); if (_workspace.paneCount == 0) _back(); } void _dockSession(String sessionId) { if (!mounted) return; _workspaceVisible = true; if (!_pageOpen && showsListBesideDetail(context)) _usedWorkspace = true; _workspace.dock(sessionId); _syncOpenWorkspace(); _composerFocus.request(sessionId); } /// The pane menu's Float choice, on a tiled pane that can, and Open in /// new window where the platform has windows. List _floatControls(ChatWorkspacePane pane) { final windows = widget.windows; if (windows == null || !_workspace.canFloat(pane.id)) return const []; return [ if (_floatsHere) ChatBarAction( label: 'Float (${chatFloatShortcutLabel()})', onPressed: () => _floatPane(pane.id), materialIcon: Icons.picture_in_picture_alt_outlined, ), if (windows.detached != null) ChatBarAction( label: detachedChatWindowLabel(), identifier: 'chat-open-new-window', onPressed: () => windows.detach(pane.sessionId), materialIcon: Icons.open_in_browser, ), ]; } }