368 lines
13 KiB
Dart
368 lines
13 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) {
|
|
if (_sidebarMode == mode) return;
|
|
setState(() => _sidebarMode = mode);
|
|
_workspaceBinding?.changed();
|
|
}
|
|
|
|
Widget _sidebarList(Widget full, String? selected, bool beside) {
|
|
final mode = selected == null || !beside
|
|
? ChatSidebarMode.expanded
|
|
: _sidebarMode;
|
|
final navigation = ShellBarScope.maybeOf(context);
|
|
return Stack(
|
|
fit: StackFit.expand,
|
|
children: [
|
|
Offstage(
|
|
offstage: mode != ChatSidebarMode.expanded,
|
|
child: ExcludeFocus(
|
|
excluding: mode != ChatSidebarMode.expanded,
|
|
child: full,
|
|
),
|
|
),
|
|
if (mode == ChatSidebarMode.compact)
|
|
ChatCompactSidebar(
|
|
navigation: navigation?.registry.canNavigate == true
|
|
? ShellDestinationMenu(
|
|
cupertino:
|
|
chatControlsStyleOf(context) ==
|
|
ChatControlsStyle.cupertino,
|
|
current: AppDestination.chat,
|
|
onSelected: navigation!.registry.navigateTo,
|
|
)
|
|
: null,
|
|
controller: _sessionsForSidebar,
|
|
folds: _foldsForSidebar,
|
|
activeSessionId: selected,
|
|
visibleSessionIds: _visibleSidebarSessions,
|
|
onOpen: _openSidebarSession,
|
|
onExpand: () => _setSidebarMode(ChatSidebarMode.expanded),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Set<String> _visibleWorkspaceSessions = const {};
|
|
|
|
void _workspaceVisibilityChanged(Set<String> sessions) {
|
|
if (!mounted || setEquals(_visibleWorkspaceSessions, sessions)) return;
|
|
setState(() => _visibleWorkspaceSessions = Set.unmodifiable(sessions));
|
|
}
|
|
|
|
Set<String> get _visibleSidebarSessions {
|
|
if (!_workspaceVisible) return const {};
|
|
final retained = _workspace.panes.map((pane) => pane.sessionId).toSet();
|
|
final visible = _visibleWorkspaceSessions.intersection(retained);
|
|
final active = _workspace.activePane?.sessionId;
|
|
return visible.isNotEmpty ? visible : {?active};
|
|
}
|
|
|
|
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: widget.gateway is FederatedChatGateway
|
|
? FederatedWorkspaceStore(
|
|
primary: (widget.gateway as FederatedChatGateway).primary(),
|
|
)
|
|
: SharedPreferencesChatWorkspaceStore(serverScope: scope),
|
|
sidebarMode: () => _sidebarMode.name,
|
|
onRestored: (snapshot) {
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_sidebarMode = ChatSidebarMode.values.firstWhere(
|
|
(mode) => mode.name == snapshot.sidebarMode,
|
|
);
|
|
// Older snapshots could hide the rail. Keep their recovery affordance
|
|
// now that layout changes happen at the divider instead of a menu.
|
|
if (_sidebarMode == ChatSidebarMode.hidden) {
|
|
_sidebarMode = ChatSidebarMode.compact;
|
|
}
|
|
_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: (session) {
|
|
if (!showsListBesideDetail(context)) {
|
|
// Save a horizontal desktop layout underneath the phone tape. Keep
|
|
// the rail compact so unfolding leaves room for the panes themselves.
|
|
_setSidebarMode(ChatSidebarMode.compact);
|
|
_openBeside(session);
|
|
} else {
|
|
_openBelow(session);
|
|
}
|
|
},
|
|
);
|
|
|
|
Widget _workspaceView() => SafeArea(
|
|
top: false,
|
|
child: ChatWorkspace(
|
|
key: _workspaceKey,
|
|
controller: _workspace,
|
|
onVisibleSessionIdsChanged: _workspaceVisibilityChanged,
|
|
shortcutsEnabled: _workspaceVisible,
|
|
singlePane: !showsListBesideDetail(context),
|
|
mobilePeeks: true,
|
|
paneTitle: (pane) =>
|
|
'${_rowFor(pane.sessionId)?['title'] ?? pane.sessionId}',
|
|
onFocusPane: (pane) => _composerFocus.request(pane.sessionId),
|
|
paneBuilder: (context, pane, active) {
|
|
final scope = ChatWorkspacePaneScope.maybeOf(context);
|
|
final topShared = scope?.topEdgeShared ?? false;
|
|
final bottomShared = scope?.bottomEdgeShared ?? false;
|
|
final leftShared = scope?.leftEdgeShared ?? false;
|
|
final rightShared = scope?.rightEdgeShared ?? false;
|
|
final activeColor = _workspace.paneCount > 1 && active
|
|
? chatColorShade(
|
|
context,
|
|
chatSessionColor(_rowFor(pane.sessionId)),
|
|
) ??
|
|
(chatControlsStyleOf(context) == ChatControlsStyle.cupertino
|
|
? CupertinoTheme.of(context).primaryColor
|
|
: Theme.of(context).colorScheme.primary)
|
|
: Colors.transparent;
|
|
return Semantics(
|
|
selected: active,
|
|
child: AnimatedContainer(
|
|
duration: showsListBesideDetail(context)
|
|
? Motion.of(context, Motion.standard)
|
|
: Duration.zero,
|
|
curve: Motion.curve,
|
|
foregroundDecoration: BoxDecoration(
|
|
border: showsListBesideDetail(context)
|
|
? Border(
|
|
top: bottomShared && !topShared
|
|
? BorderSide(color: activeColor)
|
|
: BorderSide.none,
|
|
bottom: BorderSide(
|
|
color: bottomShared ? Colors.transparent : activeColor,
|
|
),
|
|
left: bottomShared && topShared && !leftShared
|
|
? BorderSide(color: activeColor)
|
|
: BorderSide.none,
|
|
right:
|
|
bottomShared &&
|
|
topShared &&
|
|
leftShared &&
|
|
!rightShared
|
|
? BorderSide(color: activeColor)
|
|
: BorderSide.none,
|
|
)
|
|
: Border(
|
|
bottom: BorderSide(
|
|
color: bottomShared ? Colors.transparent : activeColor,
|
|
),
|
|
),
|
|
),
|
|
child: DetailPane(
|
|
key: ValueKey('chat-pane-detail-${pane.id}'),
|
|
destination: AppDestination.chat,
|
|
implyBack: false,
|
|
builder: (context) => _thread(
|
|
pane.sessionId,
|
|
_back,
|
|
pane: pane,
|
|
paneContext: context,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|