Restore local chat layouts and add compact or hidden chat sidebar
This commit is contained in:
@@ -65,6 +65,7 @@ class ChatDestination extends StatelessWidget {
|
||||
|
||||
Widget _chatScreen() => ChatScreen(
|
||||
gateway: session.gateway,
|
||||
persistWorkspace: true,
|
||||
initialSessionId: initialSessionId,
|
||||
syncToken: session.connectionEpoch,
|
||||
onSessionSelected: onSessionSelected,
|
||||
|
||||
@@ -13,7 +13,96 @@ mixin _ChatScreenWorkspace on State<ChatScreen> {
|
||||
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)!);
|
||||
_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;
|
||||
@@ -66,7 +155,7 @@ mixin _ChatScreenWorkspace on State<ChatScreen> {
|
||||
}
|
||||
|
||||
void _workspaceChanged() {
|
||||
if (!mounted) return;
|
||||
if (!mounted || _workspaceBinding?.isRestoring == true) return;
|
||||
final signature =
|
||||
'${_workspace.activePaneId}:${_workspace.panes.map((p) => '${p.id}=${p.sessionId}').join(',')}';
|
||||
if (_workspaceSignature == signature) return;
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../ui/sessions/chat_adaptive.dart';
|
||||
|
||||
enum ChatSidebarMode { expanded, compact, hidden }
|
||||
|
||||
/// Pinned rather than overflowed: the list can always be restored from a pane.
|
||||
class ChatSidebarButton extends StatelessWidget {
|
||||
const ChatSidebarButton({
|
||||
super.key,
|
||||
required this.mode,
|
||||
required this.onChanged,
|
||||
});
|
||||
final ChatSidebarMode mode;
|
||||
final ValueChanged<ChatSidebarMode> onChanged;
|
||||
|
||||
Future<void> _choose(BuildContext context) async {
|
||||
final ChatSidebarMode? choice;
|
||||
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
|
||||
choice = await showCupertinoModalPopup<ChatSidebarMode>(
|
||||
context: context,
|
||||
builder: (context) => CupertinoActionSheet(
|
||||
title: const Text('Chat list'),
|
||||
actions: [
|
||||
for (final value in ChatSidebarMode.values)
|
||||
CupertinoActionSheetAction(
|
||||
isDefaultAction: mode == value,
|
||||
onPressed: () => Navigator.pop(context, value),
|
||||
child: Text(_label(value)),
|
||||
),
|
||||
],
|
||||
cancelButton: CupertinoActionSheetAction(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
choice = await showDialog<ChatSidebarMode>(
|
||||
context: context,
|
||||
builder: (context) => SimpleDialog(
|
||||
title: const Text('Chat list'),
|
||||
children: [
|
||||
for (final value in ChatSidebarMode.values)
|
||||
SimpleDialogOption(
|
||||
onPressed: () => Navigator.pop(context, value),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(child: Text(_label(value))),
|
||||
if (value == mode) const Icon(Icons.check),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
if (choice != null && context.mounted) onChanged(choice);
|
||||
}
|
||||
|
||||
static String _label(ChatSidebarMode value) => switch (value) {
|
||||
ChatSidebarMode.expanded => 'Expanded chat list',
|
||||
ChatSidebarMode.compact => 'Compact chat list',
|
||||
ChatSidebarMode.hidden => 'Hide chat list',
|
||||
};
|
||||
@override
|
||||
Widget build(BuildContext context) =>
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino
|
||||
? Semantics(
|
||||
label: 'Chat list layout',
|
||||
button: true,
|
||||
child: CupertinoButton(
|
||||
padding: EdgeInsets.zero,
|
||||
onPressed: () => _choose(context),
|
||||
child: const Icon(CupertinoIcons.sidebar_left),
|
||||
),
|
||||
)
|
||||
: IconButton(
|
||||
tooltip: 'Chat list layout',
|
||||
onPressed: () => _choose(context),
|
||||
icon: const Icon(Icons.view_sidebar_outlined),
|
||||
);
|
||||
}
|
||||
@@ -91,6 +91,7 @@ class ChatThreadView extends StatefulWidget {
|
||||
this.onColorSelected,
|
||||
this.onNextChat,
|
||||
this.paneControls = const [],
|
||||
this.sidebarControl,
|
||||
this.showBackButton,
|
||||
});
|
||||
|
||||
@@ -100,6 +101,7 @@ class ChatThreadView extends StatefulWidget {
|
||||
/// the bar's Next chat button; null when there is no other chat to open.
|
||||
final VoidCallback? onNextChat;
|
||||
final List<Widget> paneControls;
|
||||
final Widget? sidebarControl;
|
||||
final bool? showBackButton;
|
||||
|
||||
/// Sets the chat's shared colour (a palette name, null for none) from the
|
||||
@@ -1206,6 +1208,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
// would take more than half the pill. A session without a mode draws
|
||||
// no mode control, so no empty pill is ever drawn.
|
||||
controls: [
|
||||
?widget.sidebarControl,
|
||||
...widget.paneControls,
|
||||
if (hasModel)
|
||||
ChatModelControl(
|
||||
|
||||
@@ -11,8 +11,14 @@ import 'package:flutter/scheduler.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import '../../core/api_gateway.dart';
|
||||
import '../../core/servers/server_scope.dart';
|
||||
import 'workspace/chat_workspace_binding.dart';
|
||||
import 'workspace/chat_workspace_snapshot.dart';
|
||||
import 'workspace/chat_workspace_store.dart';
|
||||
import 'conversation/chat_models.dart';
|
||||
import 'conversation/chat_pane_picker.dart';
|
||||
import 'conversation/chat_sidebar_controls.dart';
|
||||
import 'ui/sessions/chat_compact_sidebar.dart';
|
||||
import 'workspace/chat_workspace.dart';
|
||||
import 'ui/sessions/chat_adaptive.dart';
|
||||
import 'conversation/chat_row_widgets.dart';
|
||||
@@ -46,9 +52,11 @@ class ChatScreen extends StatefulWidget {
|
||||
this.syncToken = 0,
|
||||
this.onSessionSelected,
|
||||
this.onBack,
|
||||
this.persistWorkspace = false,
|
||||
});
|
||||
|
||||
final ApiGateway gateway;
|
||||
final bool persistWorkspace;
|
||||
|
||||
/// The chat session the current route names, or null for the list.
|
||||
final String? initialSessionId;
|
||||
@@ -117,6 +125,13 @@ class _ChatScreenState extends State<ChatScreen> with _ChatScreenWorkspace {
|
||||
/// when that changes.
|
||||
bool _offeredNext = false;
|
||||
|
||||
@override
|
||||
ChatSessionsController get _sessionsForSidebar => _sessions;
|
||||
@override
|
||||
ChatSectionFolds get _foldsForSidebar => _folds;
|
||||
@override
|
||||
void _openSidebarSession(JsonMap session) => _openBySelection(session);
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -127,6 +142,7 @@ class _ChatScreenState extends State<ChatScreen> with _ChatScreenWorkspace {
|
||||
_publishedWorkspaceSession = initial;
|
||||
}
|
||||
_workspace.addListener(_workspaceChanged);
|
||||
_bindWorkspacePersistence();
|
||||
HardwareKeyboard.instance.addHandler(_cycleChatKey);
|
||||
// Seen from now on, so the press that chooses the first chat counts.
|
||||
LastPointerKind.instance;
|
||||
@@ -351,7 +367,10 @@ class _ChatScreenState extends State<ChatScreen> with _ChatScreenWorkspace {
|
||||
@override
|
||||
void didUpdateWidget(covariant ChatScreen oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.initialSessionId != widget.initialSessionId) {
|
||||
final reboundWorkspace = _updateWorkspacePersistence(oldWidget);
|
||||
if (!reboundWorkspace &&
|
||||
oldWidget.initialSessionId != widget.initialSessionId) {
|
||||
_workspaceBinding?.changed();
|
||||
final id = widget.initialSessionId;
|
||||
if (id != null && id.isNotEmpty) {
|
||||
_workspaceVisible = true;
|
||||
@@ -369,6 +388,7 @@ class _ChatScreenState extends State<ChatScreen> with _ChatScreenWorkspace {
|
||||
void dispose() {
|
||||
HardwareKeyboard.instance.removeHandler(_cycleChatKey);
|
||||
_page.dispose();
|
||||
_workspaceBinding?.dispose();
|
||||
_workspace.removeListener(_workspaceChanged);
|
||||
_workspace.dispose();
|
||||
_sessions.dispose();
|
||||
@@ -409,6 +429,9 @@ class _ChatScreenState extends State<ChatScreen> with _ChatScreenWorkspace {
|
||||
? () => _openNext(selected)
|
||||
: null,
|
||||
showBackButton: pane == null ? null : !showsListBesideDetail(context),
|
||||
sidebarControl: pane == null || !showsListBesideDetail(context)
|
||||
? null
|
||||
: ChatSidebarButton(mode: _sidebarMode, onChanged: _setSidebarMode),
|
||||
paneControls: pane == null
|
||||
? const []
|
||||
: [
|
||||
@@ -460,7 +483,7 @@ class _ChatScreenState extends State<ChatScreen> with _ChatScreenWorkspace {
|
||||
);
|
||||
// The session list pads its first row by the bar's height itself, so it
|
||||
// scrolls behind the shell's floating bar like the other lists.
|
||||
final list = ChatSessionsScreen(
|
||||
final fullList = ChatSessionsScreen(
|
||||
gateway: widget.gateway,
|
||||
embedded: true,
|
||||
syncToken: widget.syncToken,
|
||||
@@ -472,8 +495,13 @@ class _ChatScreenState extends State<ChatScreen> with _ChatScreenWorkspace {
|
||||
onShortcutOpen: _openByShortcut,
|
||||
onSessions: _adoptRows,
|
||||
activeSessionId: selected,
|
||||
publishShellBar: beside || !_usedWorkspace || selected == null,
|
||||
publishShellBar:
|
||||
(selected == null ||
|
||||
!beside ||
|
||||
_sidebarMode == ChatSidebarMode.expanded) &&
|
||||
(beside || !_usedWorkspace || selected == null),
|
||||
);
|
||||
final list = _sidebarList(fullList, selected, beside);
|
||||
if (!beside) {
|
||||
if (!_usedWorkspace) return list;
|
||||
final media = MediaQuery.of(context);
|
||||
@@ -510,18 +538,30 @@ class _ChatScreenState extends State<ChatScreen> with _ChatScreenWorkspace {
|
||||
),
|
||||
);
|
||||
}
|
||||
return ListDetailPanes(
|
||||
list: list,
|
||||
detailIdentity: selected,
|
||||
detail: selected == null
|
||||
? const EmptyDetailPane(
|
||||
destination: AppDestination.chat,
|
||||
message: 'Select a chat to open it here.',
|
||||
)
|
||||
: SizedBox.expand(
|
||||
key: const ValueKey('chat-detail-pane'),
|
||||
child: _workspaceView(),
|
||||
),
|
||||
return ShellBar(
|
||||
active: selected != null && _sidebarMode != ChatSidebarMode.expanded,
|
||||
barless: true,
|
||||
hideNavigation: true,
|
||||
child: ListDetailPanes(
|
||||
list: list,
|
||||
listWidthOverride: selected == null
|
||||
? null
|
||||
: switch (_sidebarMode) {
|
||||
ChatSidebarMode.expanded => null,
|
||||
ChatSidebarMode.compact => ChatCompactSidebar.width,
|
||||
ChatSidebarMode.hidden => 0,
|
||||
},
|
||||
detailIdentity: selected,
|
||||
detail: selected == null
|
||||
? const EmptyDetailPane(
|
||||
destination: AppDestination.chat,
|
||||
message: 'Select a chat to open it here.',
|
||||
)
|
||||
: SizedBox.expand(
|
||||
key: const ValueKey('chat-detail-pane'),
|
||||
child: _workspaceView(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../../../core/api_gateway.dart';
|
||||
import '../../domain/chat_cycle.dart';
|
||||
import '../../domain/chat_sessions.dart';
|
||||
import 'chat_adaptive.dart';
|
||||
import 'chat_sessions_body.dart';
|
||||
import 'chat_sessions_controller.dart';
|
||||
|
||||
/// A small, scrollable device-local rail. Full titles remain accessible.
|
||||
class ChatCompactSidebar extends StatelessWidget {
|
||||
const ChatCompactSidebar({
|
||||
super.key,
|
||||
required this.controller,
|
||||
required this.folds,
|
||||
required this.activeSessionId,
|
||||
required this.onOpen,
|
||||
required this.onExpand,
|
||||
});
|
||||
final ChatSessionsController controller;
|
||||
final ChatSectionFolds folds;
|
||||
final String? activeSessionId;
|
||||
final ValueChanged<JsonMap> onOpen;
|
||||
final VoidCallback onExpand;
|
||||
static const width = 72.0;
|
||||
|
||||
Widget _button(
|
||||
BuildContext context,
|
||||
String title,
|
||||
Widget icon,
|
||||
VoidCallback onPressed, {
|
||||
bool selected = false,
|
||||
}) {
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
return Semantics(
|
||||
selected: selected,
|
||||
child: cupertino
|
||||
? Tooltip(
|
||||
message: title,
|
||||
child: CupertinoButton(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
onPressed: onPressed,
|
||||
child: icon,
|
||||
),
|
||||
)
|
||||
: IconButton(
|
||||
tooltip: title,
|
||||
onPressed: onPressed,
|
||||
icon: icon,
|
||||
color: selected ? Theme.of(context).colorScheme.primary : null,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Align(
|
||||
alignment: AlignmentDirectional.topStart,
|
||||
child: SizedBox(
|
||||
width: width,
|
||||
child: Column(
|
||||
children: [
|
||||
_button(
|
||||
context,
|
||||
'Expand chat list',
|
||||
Icon(
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino
|
||||
? CupertinoIcons.sidebar_left
|
||||
: Icons.menu_open,
|
||||
),
|
||||
onExpand,
|
||||
),
|
||||
Expanded(
|
||||
child: ListenableBuilder(
|
||||
listenable: Listenable.merge([controller, folds]),
|
||||
builder: (context, _) {
|
||||
final rows = chatSessionsShown(
|
||||
controller.sessions,
|
||||
query: '',
|
||||
isOpen: folds.isOpen,
|
||||
);
|
||||
return ListView(
|
||||
children: [
|
||||
for (final row in rows)
|
||||
_button(
|
||||
context,
|
||||
chatSessionTitle(row),
|
||||
Text(
|
||||
chatSessionTitle(row).characters.take(2).toString(),
|
||||
style: TextStyle(
|
||||
fontWeight: row['id'] == activeSessionId
|
||||
? FontWeight.bold
|
||||
: null,
|
||||
),
|
||||
),
|
||||
() {
|
||||
unawaited(controller.markOpened(row));
|
||||
onOpen(row);
|
||||
},
|
||||
selected: row['id'] == activeSessionId,
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'chat_workspace_controller.dart';
|
||||
import 'chat_workspace_snapshot.dart';
|
||||
import 'chat_workspace_store.dart';
|
||||
|
||||
/// Connects one local workspace to storage, without creating server sessions.
|
||||
/// Reads cannot replace edits made while loading; writes retain their order.
|
||||
class ChatWorkspaceBinding {
|
||||
ChatWorkspaceBinding({
|
||||
required this.controller,
|
||||
required this.store,
|
||||
required this.sidebarMode,
|
||||
required this.onRestored,
|
||||
this.debounce = const Duration(milliseconds: 250),
|
||||
}) {
|
||||
controller.addListener(changed);
|
||||
}
|
||||
|
||||
final ChatWorkspaceController controller;
|
||||
final ChatWorkspaceStore store;
|
||||
final String Function() sidebarMode;
|
||||
final void Function(ChatWorkspaceSnapshot) onRestored;
|
||||
final Duration debounce;
|
||||
Timer? _timer;
|
||||
Future<void> _writes = Future.value();
|
||||
bool _loading = true;
|
||||
bool _restoring = false;
|
||||
bool _disposed = false;
|
||||
bool _dirty = false;
|
||||
bool _started = false;
|
||||
int _presentationRevision = 0;
|
||||
bool get isRestoring => _restoring;
|
||||
|
||||
Future<void> start({String? initialSessionId}) async {
|
||||
if (_started || _disposed) return;
|
||||
_started = true;
|
||||
final revision = controller.revision;
|
||||
final presentationRevision = _presentationRevision;
|
||||
ChatWorkspaceSnapshot? snapshot;
|
||||
try {
|
||||
snapshot = await store.read();
|
||||
} on Object {
|
||||
// Unavailable local preferences must never block opening chats.
|
||||
}
|
||||
if (_disposed) return;
|
||||
_loading = false;
|
||||
if (snapshot != null &&
|
||||
revision == controller.revision &&
|
||||
presentationRevision == _presentationRevision) {
|
||||
_restoring = true;
|
||||
try {
|
||||
controller.restoreLayout(snapshot, expectedRevision: revision);
|
||||
if (initialSessionId != null && initialSessionId.isNotEmpty) {
|
||||
final existing = controller.panes
|
||||
.where((pane) => pane.sessionId == initialSessionId)
|
||||
.firstOrNull;
|
||||
if (existing != null) {
|
||||
controller.activate(existing.id);
|
||||
} else {
|
||||
// A deep link keeps the restored panes and adds its own chat.
|
||||
controller.splitActive(initialSessionId, ChatSplitAxis.horizontal);
|
||||
}
|
||||
}
|
||||
onRestored(snapshot);
|
||||
} finally {
|
||||
_restoring = false;
|
||||
}
|
||||
_dirty = initialSessionId != null && initialSessionId.isNotEmpty;
|
||||
} else if (snapshot == null && controller.paneCount > 0) {
|
||||
_dirty = true;
|
||||
}
|
||||
if (_dirty) _schedule();
|
||||
}
|
||||
|
||||
/// Also called for presentation changes that don't mutate the split tree.
|
||||
void changed() {
|
||||
if (_disposed || _restoring) return;
|
||||
_presentationRevision++;
|
||||
_dirty = true;
|
||||
if (!_loading) _schedule();
|
||||
}
|
||||
|
||||
void _schedule() {
|
||||
_timer?.cancel();
|
||||
_timer = Timer(debounce, flush);
|
||||
}
|
||||
|
||||
Future<void> flush() {
|
||||
_timer?.cancel();
|
||||
_timer = null;
|
||||
if (!_dirty || _loading) return _writes;
|
||||
final snapshot = controller.snapshot(sidebarMode: sidebarMode());
|
||||
_dirty = false;
|
||||
if (snapshot == null) return _writes;
|
||||
_writes = _writes.then((_) async {
|
||||
try {
|
||||
await store.write(snapshot);
|
||||
} on Object {
|
||||
// A storage failure must not interrupt navigation or later saves.
|
||||
}
|
||||
});
|
||||
return _writes;
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
controller.removeListener(changed);
|
||||
if (_dirty) _loading = false;
|
||||
unawaited(flush());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/core/servers/server_scope.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_sidebar_controls.dart';
|
||||
import 'package:pipelines_app/features/chat/screen.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_workspace_store.dart';
|
||||
import 'package:pipelines_app/shell/adaptive_shell.dart';
|
||||
import 'package:pipelines_app/shell/destination.dart';
|
||||
|
||||
import 'conversation/chat_thread_fixtures.dart';
|
||||
|
||||
class _Gateway extends FakeChatGateway {
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
|
||||
if (type == 'chat_sessions') {
|
||||
return Future.value({
|
||||
'chat_sessions': [
|
||||
for (final id in ['a', 'b', 'linked'])
|
||||
{'id': id, 'title': 'Chat $id'},
|
||||
],
|
||||
});
|
||||
}
|
||||
if (type == 'chat_shells') return Future.value({'chat_shells': []});
|
||||
return super.call(type, fields);
|
||||
}
|
||||
}
|
||||
|
||||
ChatWorkspaceController _workspace(WidgetTester tester) =>
|
||||
tester.widget<ChatWorkspace>(find.byType(ChatWorkspace)).controller;
|
||||
|
||||
Future<void> _mount(
|
||||
WidgetTester tester,
|
||||
_Gateway gateway, {
|
||||
String? route,
|
||||
bool enabled = true,
|
||||
ValueChanged<String>? onSelected,
|
||||
}) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: AdaptiveShell(
|
||||
destination: AppDestination.chat,
|
||||
destinationBodies: {
|
||||
AppDestination.chat: (_) => ChatScreen(
|
||||
gateway: gateway,
|
||||
persistWorkspace: enabled,
|
||||
initialSessionId: route,
|
||||
onSessionSelected: onSelected,
|
||||
),
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
void main() {
|
||||
for (final width in [1800.0, 430.0]) {
|
||||
testWidgets('restores layout and saves geometry/sidebar on $width host', (
|
||||
tester,
|
||||
) async {
|
||||
tester.view.physicalSize = Size(width, 1000);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
final saved = ChatWorkspaceController()..open('a');
|
||||
saved.splitActive('b', ChatSplitAxis.vertical);
|
||||
final source = saved.snapshot(sidebarMode: 'hidden')!;
|
||||
SharedPreferences.setMockInitialValues({
|
||||
scopedServerKey(chatWorkspacePreferenceKey, 'server-a'): source
|
||||
.encode(),
|
||||
});
|
||||
final gateway = _Gateway()..history = (_) async => chatPage([]);
|
||||
bindServerIdentity(gateway, 'server-a');
|
||||
addTearDown(gateway.close);
|
||||
await _mount(tester, gateway);
|
||||
final controller = _workspace(tester);
|
||||
expect(controller.panes.map((pane) => pane.sessionId), ['a', 'b']);
|
||||
expect(controller.activePane?.sessionId, 'b');
|
||||
if (width > 1000) {
|
||||
expect(
|
||||
tester
|
||||
.widget<ChatSidebarButton>(find.byType(ChatSidebarButton).first)
|
||||
.mode,
|
||||
ChatSidebarMode.hidden,
|
||||
);
|
||||
tester
|
||||
.widget<ChatSidebarButton>(find.byType(ChatSidebarButton).first)
|
||||
.onChanged(ChatSidebarMode.compact);
|
||||
} else {
|
||||
expect(find.byType(ChatSidebarButton), findsNothing);
|
||||
}
|
||||
controller.resize(controller.root!.id, .42);
|
||||
await tester.pump(const Duration(milliseconds: 300));
|
||||
await tester.pumpAndSettle();
|
||||
final store = SharedPreferencesChatWorkspaceStore(
|
||||
serverScope: 'server-a',
|
||||
);
|
||||
final persisted = (await store.read())!;
|
||||
expect((persisted.root as ChatWorkspaceSplit).ratio, .42);
|
||||
expect(persisted.sidebarMode, width > 1000 ? 'compact' : 'hidden');
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
await tester.pumpAndSettle();
|
||||
await _mount(tester, gateway);
|
||||
expect((_workspace(tester).root as ChatWorkspaceSplit).ratio, .42);
|
||||
if (width > 1000) {
|
||||
expect(
|
||||
tester
|
||||
.widget<ChatSidebarButton>(find.byType(ChatSidebarButton).first)
|
||||
.mode,
|
||||
ChatSidebarMode.compact,
|
||||
);
|
||||
}
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
await tester.pumpAndSettle();
|
||||
});
|
||||
}
|
||||
|
||||
testWidgets('switching logical servers restores only the new server layout', (
|
||||
tester,
|
||||
) async {
|
||||
tester.view.physicalSize = const Size(1800, 1000);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
final savedA = ChatWorkspaceController()..open('a');
|
||||
final savedB = ChatWorkspaceController()..open('b');
|
||||
SharedPreferences.setMockInitialValues({
|
||||
scopedServerKey(chatWorkspacePreferenceKey, 'server-a'): savedA
|
||||
.snapshot()!
|
||||
.encode(),
|
||||
scopedServerKey(chatWorkspacePreferenceKey, 'server-b'): savedB
|
||||
.snapshot()!
|
||||
.encode(),
|
||||
});
|
||||
final gatewayA = _Gateway()..history = (_) async => chatPage([]);
|
||||
final gatewayB = _Gateway()..history = (_) async => chatPage([]);
|
||||
bindServerIdentity(gatewayA, 'server-a');
|
||||
bindServerIdentity(gatewayB, 'server-b');
|
||||
addTearDown(gatewayA.close);
|
||||
addTearDown(gatewayB.close);
|
||||
await _mount(tester, gatewayA);
|
||||
expect(_workspace(tester).activePane?.sessionId, 'a');
|
||||
await _mount(tester, gatewayB, route: 'linked');
|
||||
expect(_workspace(tester).panes.map((pane) => pane.sessionId), [
|
||||
'b',
|
||||
'linked',
|
||||
]);
|
||||
expect(_workspace(tester).activePane?.sessionId, 'linked');
|
||||
await tester.pump(const Duration(milliseconds: 300));
|
||||
final original = (await const SharedPreferencesChatWorkspaceStore(
|
||||
serverScope: 'server-a',
|
||||
).read())!;
|
||||
expect((original.root as ChatWorkspacePane).sessionId, 'a');
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
await tester.pumpAndSettle();
|
||||
});
|
||||
|
||||
testWidgets('initial deep link wins without discarding restored chats', (
|
||||
tester,
|
||||
) async {
|
||||
tester.view.physicalSize = const Size(1800, 1000);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
final saved = ChatWorkspaceController()..open('a');
|
||||
saved.splitActive('b', ChatSplitAxis.vertical);
|
||||
SharedPreferences.setMockInitialValues({
|
||||
chatWorkspacePreferenceKey: saved.snapshot()!.encode(),
|
||||
});
|
||||
final gateway = _Gateway()..history = (_) async => chatPage([]);
|
||||
addTearDown(gateway.close);
|
||||
final selected = <String>[];
|
||||
await _mount(tester, gateway, route: 'linked', onSelected: selected.add);
|
||||
expect(_workspace(tester).panes.map((pane) => pane.sessionId), [
|
||||
'a',
|
||||
'b',
|
||||
'linked',
|
||||
]);
|
||||
expect(_workspace(tester).activePane?.sessionId, 'linked');
|
||||
expect(selected, isNot(contains('b')));
|
||||
await tester.pump(const Duration(milliseconds: 300));
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
await tester.pumpAndSettle();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_workspace_binding.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_workspace_controller.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_workspace_snapshot.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_workspace_store.dart';
|
||||
|
||||
class _Store implements ChatWorkspaceStore {
|
||||
final readResult = Completer<ChatWorkspaceSnapshot?>();
|
||||
final writes = <ChatWorkspaceSnapshot>[];
|
||||
final gates = <Completer<void>>[];
|
||||
bool blockWrites = false;
|
||||
@override
|
||||
Future<ChatWorkspaceSnapshot?> read() => readResult.future;
|
||||
@override
|
||||
Future<bool> write(ChatWorkspaceSnapshot snapshot) async {
|
||||
writes.add(snapshot);
|
||||
if (blockWrites) {
|
||||
final gate = Completer<void>();
|
||||
gates.add(gate);
|
||||
await gate.future;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
ChatWorkspaceSnapshot _saved() {
|
||||
final controller = ChatWorkspaceController()..open('a');
|
||||
controller.splitActive('b', ChatSplitAxis.vertical);
|
||||
return controller.snapshot(sidebarMode: 'hidden')!;
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('deep link preserves stored panes and becomes active', () async {
|
||||
final controller = ChatWorkspaceController()..open('linked');
|
||||
final store = _Store()..readResult.complete(_saved());
|
||||
var mode = 'expanded';
|
||||
final binding = ChatWorkspaceBinding(
|
||||
controller: controller,
|
||||
store: store,
|
||||
sidebarMode: () => mode,
|
||||
onRestored: (snapshot) => mode = snapshot.sidebarMode,
|
||||
);
|
||||
await binding.start(initialSessionId: 'linked');
|
||||
expect(controller.panes.map((pane) => pane.sessionId), [
|
||||
'a',
|
||||
'b',
|
||||
'linked',
|
||||
]);
|
||||
expect(controller.activePane?.sessionId, 'linked');
|
||||
expect(mode, 'hidden');
|
||||
await binding.flush();
|
||||
expect(store.writes.single.activePaneId, controller.activePaneId);
|
||||
binding.dispose();
|
||||
controller.dispose();
|
||||
});
|
||||
|
||||
test('pending load cannot overwrite chat or sidebar user changes', () async {
|
||||
for (final sidebarEdit in [false, true]) {
|
||||
final controller = ChatWorkspaceController();
|
||||
final store = _Store();
|
||||
var mode = 'expanded';
|
||||
var restored = false;
|
||||
final binding = ChatWorkspaceBinding(
|
||||
controller: controller,
|
||||
store: store,
|
||||
sidebarMode: () => mode,
|
||||
onRestored: (_) => restored = true,
|
||||
);
|
||||
final loading = binding.start();
|
||||
if (sidebarEdit) {
|
||||
mode = 'compact';
|
||||
binding.changed();
|
||||
} else {
|
||||
controller.open('user');
|
||||
}
|
||||
store.readResult.complete(_saved());
|
||||
await loading;
|
||||
expect(restored, isFalse);
|
||||
expect(controller.activePane?.sessionId, sidebarEdit ? null : 'user');
|
||||
await binding.flush();
|
||||
expect(store.writes.single.sidebarMode, mode);
|
||||
binding.dispose();
|
||||
controller.dispose();
|
||||
}
|
||||
});
|
||||
|
||||
test(
|
||||
'ordered writes include resize and sidebar; dispose flushes edits',
|
||||
() async {
|
||||
final controller = ChatWorkspaceController();
|
||||
final store = _Store()..readResult.complete(_saved());
|
||||
var mode = 'expanded';
|
||||
final binding = ChatWorkspaceBinding(
|
||||
controller: controller,
|
||||
store: store,
|
||||
sidebarMode: () => mode,
|
||||
onRestored: (snapshot) => mode = snapshot.sidebarMode,
|
||||
);
|
||||
await binding.start();
|
||||
store.blockWrites = true;
|
||||
final root = controller.root! as ChatWorkspaceSplit;
|
||||
controller.resize(root.id, .31);
|
||||
final firstSave = binding.flush();
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
mode = 'compact';
|
||||
binding.changed();
|
||||
final secondSave = binding.flush();
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(store.writes, hasLength(1));
|
||||
store.gates.first.complete();
|
||||
await firstSave;
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(store.writes, hasLength(2));
|
||||
expect((store.writes.last.root as ChatWorkspaceSplit).ratio, .31);
|
||||
expect(store.writes.last.sidebarMode, 'compact');
|
||||
store.gates.last.complete();
|
||||
await secondSave;
|
||||
controller.close(controller.panes.last.id);
|
||||
binding.dispose();
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(store.writes, hasLength(3));
|
||||
store.gates.last.complete();
|
||||
controller.dispose();
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'debounces mutations and preserves an edit when leaving during load',
|
||||
(tester) async {
|
||||
final controller = ChatWorkspaceController();
|
||||
final store = _Store()..readResult.complete(null);
|
||||
var mode = 'expanded';
|
||||
final binding = ChatWorkspaceBinding(
|
||||
controller: controller,
|
||||
store: store,
|
||||
sidebarMode: () => mode,
|
||||
onRestored: (_) {},
|
||||
);
|
||||
await binding.start();
|
||||
controller.open('a');
|
||||
controller.splitActive('b', ChatSplitAxis.horizontal);
|
||||
mode = 'hidden';
|
||||
binding.changed();
|
||||
await tester.pump(const Duration(milliseconds: 200));
|
||||
expect(store.writes, isEmpty);
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
expect(store.writes, hasLength(1));
|
||||
expect(store.writes.single.sidebarMode, 'hidden');
|
||||
binding.dispose();
|
||||
controller.dispose();
|
||||
|
||||
final pendingController = ChatWorkspaceController();
|
||||
final pendingStore = _Store();
|
||||
final pending = ChatWorkspaceBinding(
|
||||
controller: pendingController,
|
||||
store: pendingStore,
|
||||
sidebarMode: () => 'expanded',
|
||||
onRestored: (_) => fail('disposed binding restored'),
|
||||
);
|
||||
final loading = pending.start();
|
||||
pendingController.open('user');
|
||||
pending.dispose();
|
||||
await tester.pump();
|
||||
expect(pendingStore.writes.single.root, isA<ChatWorkspacePane>());
|
||||
pendingStore.readResult.complete(_saved());
|
||||
await loading;
|
||||
pendingController.dispose();
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'leaving during unread storage does not overwrite stored workspace',
|
||||
() async {
|
||||
final controller = ChatWorkspaceController()..open('route');
|
||||
final store = _Store();
|
||||
final binding = ChatWorkspaceBinding(
|
||||
controller: controller,
|
||||
store: store,
|
||||
sidebarMode: () => 'expanded',
|
||||
onRestored: (_) => fail('disposed binding restored'),
|
||||
);
|
||||
final loading = binding.start(initialSessionId: 'route');
|
||||
binding.dispose();
|
||||
store.readResult.complete(_saved());
|
||||
await loading;
|
||||
expect(store.writes, isEmpty);
|
||||
controller.dispose();
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user