Merge: Next chat button cycles through the list

This commit is contained in:
2026-11-04 18:22:45 +00:00
9 changed files with 425 additions and 8 deletions
@@ -0,0 +1,38 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../ui/sessions/chat_adaptive.dart';
/// The open chat's Next chat button: the platform's icon button, labelled
/// "Next chat", that opens the chat after this one in the list.
///
/// It stays in the bar rather than leaving for the More menu on a narrow
/// bar (it is not [SegmentedOverflowable]): it is the one-tap way to cycle
/// chats on a phone, so the title gives up the room instead.
class ChatNextChatButton extends StatelessWidget {
const ChatNextChatButton({super.key, required this.onPressed});
static const label = 'Next chat';
final VoidCallback onPressed;
@override
Widget build(BuildContext context) {
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
return Semantics(
label: label,
button: true,
child: CupertinoButton(
padding: EdgeInsets.zero,
onPressed: onPressed,
child: const Icon(CupertinoIcons.arrow_right_to_line),
),
);
}
return IconButton(
tooltip: label,
onPressed: onPressed,
icon: const Icon(Icons.keyboard_tab),
);
}
}
@@ -14,6 +14,7 @@ import '../domain/chat_attachment_picker.dart';
import '../domain/chat_colors.dart';
import '../domain/chat_transcript.dart';
import '../ui/sessions/chat_adaptive.dart';
import 'chat_next_chat_button.dart';
import '../ui/sessions/chat_color_picker.dart';
import '../ui/sessions/session_actions.dart';
import '../ui/transcript/chat_markdown_view.dart';
@@ -87,10 +88,15 @@ class ChatThreadView extends StatefulWidget {
this.onSessionClosed,
this.fileBridge = const ArtifactFileBridge(),
this.onColorSelected,
this.onNextChat,
});
final ApiGateway gateway;
/// Opens the chat after this one in the list (wrapping to the first), from
/// the bar's Next chat button; null when there is no other chat to open.
final VoidCallback? onNextChat;
/// Sets the chat's shared colour (a palette name, null for none) from the
/// information sheet; null leaves the sheet without the Colour choice.
final ValueChanged<String?>? onColorSelected;
@@ -1199,6 +1205,8 @@ class _ChatThreadViewState extends State<ChatThreadView> {
answeredBy: answeredBy,
),
if (hasMode) _modeControl(),
if (widget.onNextChat case final next?)
ChatNextChatButton(key: const Key('chat-next'), onPressed: next),
],
);
}
@@ -0,0 +1,24 @@
import '../../../core/api_gateway.dart';
import 'chat_sessions.dart';
/// The rows the chat list draws, top to bottom: the [query]'s matches in
/// Starred, then Chats by directory, then Closed, leaving out every section
/// [isOpen] reports folded. Ctrl+1…Ctrl+9 and Next chat both count these.
List<JsonMap> chatSessionsShown(
List<JsonMap> sessions, {
required String query,
required bool Function(String title) isOpen,
}) => chatSessionsInListOrder(
chatSessionSections(filterChatSessions(sessions, query)),
isOpen: isOpen,
);
/// The row after [currentId] in [shown], wrapping from the last row to the
/// first. A chat not among [shown] (its section is folded) moves to the
/// first row. Null when no other row is shown, so there is nothing to move to.
JsonMap? chatSessionAfter(List<JsonMap> shown, String currentId) {
final index = shown.indexWhere((row) => '${row['id'] ?? ''}' == currentId);
final others = index < 0 ? shown.length : shown.length - 1;
if (others < 1) return null;
return shown[(index + 1) % shown.length];
}
+52
View File
@@ -6,6 +6,7 @@ import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import '../../core/api_gateway.dart';
import 'conversation/chat_models.dart';
@@ -16,6 +17,8 @@ import 'conversation/chat_transcript_cache.dart';
import 'conversation/composer_focus_request.dart';
import 'data/chat_repository.dart';
import 'domain/chat_commands.dart';
import 'domain/chat_cycle.dart';
import 'ui/sessions/chat_sessions_body.dart';
import 'ui/sessions/chat_sessions_controller.dart';
import 'ui/sessions/chat_sessions_screen.dart';
@@ -95,6 +98,51 @@ class _ChatScreenState extends State<ChatScreen> {
/// Ctrl+1…Ctrl+9 put the keyboard in the opened chat's composer.
final ChatComposerFocusRequests _composerFocus = ChatComposerFocusRequests();
/// The list's folded sections, kept here so Next chat counts the rows the
/// list draws.
final ChatSectionFolds _folds = ChatSectionFolds();
/// Whether the last build offered Next chat, so a list change rebuilds only
/// when that changes.
bool _offeredNext = false;
@override
void initState() {
super.initState();
_sessions.addListener(_nextChanged);
_folds.addListener(_nextChanged);
}
/// The chat after [id] as the list draws it (Starred, then Chats by
/// directory, then Closed, skipping folded sections), wrapping around. The
/// embedded list has no search field, so no query narrows it.
JsonMap? _nextAfter(String id) => chatSessionAfter(
chatSessionsShown(_sessions.sessions, query: '', isOpen: _folds.isOpen),
id,
);
/// Next chat opens that row exactly as a tap on it would.
void _openNext(String id) {
final next = _nextAfter(id);
if (next != null) _open(next);
}
void _nextChanged() {
if (!mounted) return;
final id = _selected;
final offered = id != null && _nextAfter(id) != null;
if (offered == _offeredNext) return;
// The list may load while it builds; rebuild after that frame.
if (SchedulerBinding.instance.schedulerPhase ==
SchedulerPhase.persistentCallbacks) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) setState(() {});
});
} else {
setState(() {});
}
}
String? get _selected {
final routed = widget.initialSessionId;
if (routed != null && routed.isNotEmpty) return routed;
@@ -261,6 +309,7 @@ class _ChatScreenState extends State<ChatScreen> {
void dispose() {
_page.dispose();
_sessions.dispose();
_folds.dispose();
_composerFocus.dispose();
super.dispose();
}
@@ -287,6 +336,7 @@ class _ChatScreenState extends State<ChatScreen> {
onSessionRenamed: (title) => _renamed(selected, title),
onSessionClosed: _closed,
onColorSelected: (color) => _sessions.setColor(selected, color),
onNextChat: _offeredNext ? () => _openNext(selected) : null,
);
return ChatComposerFocusScope(
sessionId: selected,
@@ -298,6 +348,7 @@ class _ChatScreenState extends State<ChatScreen> {
@override
Widget build(BuildContext context) {
final selected = _selected;
_offeredNext = selected != null && _nextAfter(selected) != null;
// A phone opens a chat as a real Navigator page with stock Back. A wide
// window keeps the destination rail and the chat list beside the open
// chat, which has no Back: another row, `/close` or a route change swaps it.
@@ -318,6 +369,7 @@ class _ChatScreenState extends State<ChatScreen> {
embedded: true,
syncToken: widget.syncToken,
controller: _sessions,
folds: _folds,
onOpen: _open,
onShortcutOpen: _openByShortcut,
onSessions: _adoptRows,
@@ -10,14 +10,17 @@ import 'session_row_style.dart';
typedef ChatSessionSection = ({String title, List<JsonMap> sessions});
/// Which list sections are folded away, kept by the list's screen so the
/// keyboard shortcuts count only rows on screen. Closed starts folded.
class ChatSectionFolds {
/// keyboard shortcuts and Next chat count only rows on screen. Closed starts
/// folded. It notifies when a section folds or unfolds.
class ChatSectionFolds extends ChangeNotifier {
final Set<String> _folded = {'Closed'};
bool isOpen(String title) => !_folded.contains(title);
void set(String title, bool open) =>
open ? _folded.remove(title) : _folded.add(title);
void set(String title, bool open) {
final changed = open ? _folded.remove(title) : _folded.add(title);
if (changed) notifyListeners();
}
}
/// Groups the visible sessions the way the server reports them.
@@ -11,6 +11,7 @@ import '../../../../shell/shell_bar.dart';
import '../../../../shell/shell_list_shortcuts.dart';
import '../../data/chat_repository.dart';
import '../../domain/chat_colors.dart';
import '../../domain/chat_cycle.dart';
import '../../domain/chat_sessions.dart';
import 'chat_adaptive.dart';
import 'chat_color_picker.dart';
@@ -35,6 +36,7 @@ class ChatSessionsScreen extends StatefulWidget {
this.controller,
this.activeSessionId,
this.publishShellBar,
this.folds,
});
final ApiGateway gateway;
final void Function(JsonMap session) onOpen;
@@ -71,6 +73,10 @@ class ChatSessionsScreen extends StatefulWidget {
/// Null makes this screen own its own.
final ChatSessionsController? controller;
/// Which sections are folded, when the destination keeps them (Next chat
/// counts the same rows); null makes this screen keep its own.
final ChatSectionFolds? folds;
@override
State<ChatSessionsScreen> createState() => _ChatSessionsScreenState();
}
@@ -85,7 +91,7 @@ class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
..activeSessionId = widget.activeSessionId
..refresh();
final _search = TextEditingController();
final _folds = ChatSectionFolds();
late final ChatSectionFolds _folds = widget.folds ?? ChatSectionFolds();
Timer? _undoTimer;
String? _markedActiveSessionId;
@@ -140,6 +146,7 @@ class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
_undoTimer?.cancel();
_controller.removeListener(_reportSessions);
if (widget.controller == null) _controller.dispose();
if (widget.folds == null) _folds.dispose();
_search.dispose();
super.dispose();
}
@@ -203,9 +210,9 @@ class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
/// Ctrl+N: opens the Nth row the list draws (Starred first, then Chats by
/// directory, then Closed while it is unfolded), searched as shown.
bool _openIndex(int index) {
final visible = filterChatSessions(_controller.sessions, _search.text);
final rows = chatSessionsInListOrder(
chatSessionSections(visible),
final rows = chatSessionsShown(
_controller.sessions,
query: _search.text,
isOpen: _folds.isOpen,
);
if (index < 0 || index >= rows.length) return false;