Files
pipelines/app/lib/features/chat/chat_screen_workspace.dart
T
alan de0c06cec1 Chat: slash focuses the composer; Ctrl+Tab over one pane toggles recent chats
A bare / typed outside any text field puts the keyboard in the active
chat's composer through the existing focus request, after the frame, so
the slash is not typed into it. Ctrl+Tab with a single pane now swaps in
the chat shown before the current one and back; two or more panes keep
cycling panes. The visit remembers its shown chats most-recent-first,
dropping closed, /new-moved and other-server ids.
2026-11-07 00:46:02 +00:00

279 lines
9.1 KiB
Dart

part of 'screen.dart';
/// Pane layout, focus and route publication for one Chat destination visit.
mixin _ChatScreenWorkspace on State<ChatScreen> {
Map<String, JsonMap> get _openedSessions;
ChatComposerFocusRequests get _composerFocus;
bool _chatOwnsKeyboard();
bool _toggleRecentChat();
void _noteRecentSession();
void _forgetRecentSessions();
JsonMap? _rowFor(String id);
void _back();
bool get _pageOpen;
Widget _thread(
String selected,
VoidCallback onBack, {
ChatWorkspacePane? pane,
BuildContext? paneContext,
});
ChatSidebarMode _sidebarMode = ChatSidebarMode.expanded;
void _setSidebarMode(ChatSidebarMode mode) {
setState(() => _sidebarMode = mode);
_workspaceBinding?.changed();
}
Widget _sidebarList(Widget full, String? selected, bool beside) {
final mode = selected == null || !beside
? ChatSidebarMode.expanded
: _sidebarMode;
return Stack(
fit: StackFit.expand,
children: [
Offstage(
offstage: mode != ChatSidebarMode.expanded,
child: ExcludeFocus(
excluding: mode != ChatSidebarMode.expanded,
child: full,
),
),
if (mode == ChatSidebarMode.compact)
ChatCompactSidebar(
controller: _sessionsForSidebar,
folds: _foldsForSidebar,
activeSessionId: selected,
onOpen: _openSidebarSession,
onExpand: () => _setSidebarMode(ChatSidebarMode.expanded),
),
],
);
}
ChatSessionsController get _sessionsForSidebar;
ChatSectionFolds get _foldsForSidebar;
void _openSidebarSession(JsonMap session);
final ChatWorkspaceController _workspace = ChatWorkspaceController();
ChatWorkspaceBinding? _workspaceBinding;
String? _workspaceScope;
void _bindWorkspacePersistence() {
if (!widget.persistWorkspace) return;
final scope = serverStorageScope(serverIdentity(widget.gateway));
_workspaceScope = scope;
_workspaceBinding = ChatWorkspaceBinding(
controller: _workspace,
store: SharedPreferencesChatWorkspaceStore(serverScope: scope),
sidebarMode: () => _sidebarMode.name,
onRestored: (snapshot) {
if (!mounted) return;
setState(() {
_sidebarMode = ChatSidebarMode.values.firstWhere(
(mode) => mode.name == snapshot.sidebarMode,
);
_usedWorkspace = true;
_workspaceVisible = _workspace.paneCount > 0;
});
_publishWorkspaceRoute();
},
);
unawaited(
_workspaceBinding!.start(initialSessionId: widget.initialSessionId),
);
}
bool _updateWorkspacePersistence(ChatScreen oldWidget) {
final scope = serverStorageScope(serverIdentity(widget.gateway));
if (oldWidget.persistWorkspace == widget.persistWorkspace &&
(!widget.persistWorkspace || scope == _workspaceScope)) {
return false;
}
_workspaceBinding?.dispose();
_workspaceBinding = null;
if (_workspaceScope != null && scope != _workspaceScope) {
_workspace.restoreLayout(ChatWorkspaceSnapshot.fromLayout(null, null)!);
_forgetRecentSessions();
_workspaceVisible = false;
_usedWorkspace = true;
_sidebarMode = ChatSidebarMode.expanded;
_publishedWorkspaceSession = null;
final initial = widget.initialSessionId;
if (initial != null && initial.isNotEmpty) {
_workspace.open(initial);
_workspaceVisible = true;
}
}
_workspaceScope = null;
_bindWorkspacePersistence();
return true;
}
final GlobalKey _workspaceKey = GlobalKey();
bool _workspaceVisible = false;
bool _usedWorkspace = false;
String? _publishedWorkspaceSession;
String _workspaceSignature = '';
/// Ctrl+Tab cycles retained panes even when the sidebar or toolbar owns
/// focus. Over a single pane it swaps in the chat shown before this one
/// instead, and back again (see [_toggleRecentChat]). Command+Tab stays
/// with the operating system's application switcher.
bool _cycleChatKey(KeyEvent event) {
final keyboard = HardwareKeyboard.instance;
if (event is! KeyDownEvent ||
event.logicalKey != LogicalKeyboardKey.tab ||
!keyboard.isControlPressed ||
keyboard.isMetaPressed ||
keyboard.isAltPressed ||
!_workspaceVisible ||
_workspace.paneCount == 0 ||
!_chatOwnsKeyboard()) {
return false;
}
final panes = _workspace.panes;
if (panes.length == 1 && _toggleRecentChat()) return true;
final active = panes.indexWhere(
(pane) => pane.id == _workspace.activePaneId,
);
final direction = keyboard.isShiftPressed ? -1 : 1;
final target =
panes[((active < 0 ? 0 : active) + direction) % panes.length];
_workspace.activate(target.id);
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted ||
!_workspaceVisible ||
_workspace.activePaneId != target.id) {
return;
}
final destination = ShellBarScope.maybeOf(context)?.registry.focus;
if (destination == null || destination == AppDestination.chat) {
_composerFocus.request(target.sessionId);
}
});
WidgetsBinding.instance.ensureVisualUpdate();
return true;
}
void _workspaceChanged() {
if (!mounted) return;
_noteRecentSession();
if (_workspaceBinding?.isRestoring == true) return;
final signature =
'${_workspace.activePaneId}:${_workspace.panes.map((p) => '${p.id}=${p.sessionId}').join(',')}';
if (_workspaceSignature == signature) return;
_workspaceSignature = signature;
void update() {
if (!mounted) return;
setState(() {});
_publishWorkspaceRoute();
}
if (SchedulerBinding.instance.schedulerPhase ==
SchedulerPhase.persistentCallbacks) {
WidgetsBinding.instance.addPostFrameCallback((_) => update());
} else {
update();
}
}
void _publishWorkspaceRoute() {
if (!mounted) return;
final id = _workspace.activePane?.sessionId;
final destination = ShellBarScope.maybeOf(context)?.registry.focus;
if (!_workspaceVisible ||
id == null ||
id == _publishedWorkspaceSession ||
(destination != null && destination != AppDestination.chat)) {
return;
}
_publishedWorkspaceSession = id;
widget.onSessionSelected?.call(id);
}
void _split(JsonMap session, ChatSplitAxis axis) {
final id = '${session['id'] ?? ''}';
if (id.isEmpty) return;
_openedSessions[id] = session;
_workspaceVisible = true;
if (!_pageOpen && showsListBesideDetail(context)) _usedWorkspace = true;
_workspace.splitActive(id, axis);
if (showsListBesideDetail(context) ||
chatSelectionFocusesComposer(const {})) {
_composerFocus.request(id);
} else {
_composerFocus.cancel();
FocusManager.instance.primaryFocus?.unfocus();
}
}
void _openBeside(JsonMap session) =>
_split(session, ChatSplitAxis.horizontal);
void _openBelow(JsonMap session) => _split(session, ChatSplitAxis.vertical);
void _closePane(String paneId) {
_workspace.close(paneId);
if (_workspace.paneCount == 0) _back();
}
void _pickPane(BuildContext context) => showChatPanePicker(
context,
panes: [
for (final pane in _workspace.panes)
(
id: pane.id,
title: '${_rowFor(pane.sessionId)?['title'] ?? pane.sessionId}',
),
],
activeId: _workspace.activePaneId,
onSelected: (id) {
_workspace.activate(id);
final session = _workspace.activePane?.sessionId;
if (session != null && chatSelectionFocusesComposer(const {})) {
_composerFocus.request(session);
}
},
);
void _addPane(BuildContext context) => showAddChatPanePicker(
context,
controller: _sessionsForSidebar,
sidebarFolds: _foldsForSidebar,
excludedSessionIds: _workspace.panes.map((pane) => pane.sessionId).toSet(),
onSelected: _openBelow,
);
Widget _workspaceView() => ChatWorkspace(
key: _workspaceKey,
controller: _workspace,
shortcutsEnabled: _workspaceVisible,
singlePane: !showsListBesideDetail(context),
mobilePeeks: !showsListBesideDetail(context),
paneTitle: (pane) =>
'${_rowFor(pane.sessionId)?['title'] ?? pane.sessionId}',
onFocusPane: (pane) => _composerFocus.request(pane.sessionId),
paneBuilder: (context, pane, active) => Semantics(
selected: active,
child: DecoratedBox(
position: DecorationPosition.foreground,
decoration: BoxDecoration(
border: Border.all(
color: _workspace.paneCount > 1 && active
? (chatControlsStyleOf(context) == ChatControlsStyle.cupertino
? CupertinoTheme.of(context).primaryColor
: Theme.of(context).colorScheme.primary)
: Colors.transparent,
),
),
child: DetailPane(
key: ValueKey('chat-pane-detail-${pane.id}'),
destination: AppDestination.chat,
implyBack: false,
builder: (context) =>
_thread(pane.sessionId, _back, pane: pane, paneContext: context),
),
),
),
);
}