Chat stack: groups of open chats, synced with the open-chat set

A chat dropped onto the middle of another makes a named group (asked at
once, "Group N" by default); onto a group it joins, a group dropped onto
a chat takes it in, groups merge. The top level shows each group as a
small pile with its name and count; a tap opens it in place under a
header that leads back (also the scrim, Back and Escape). In a group,
dropping a chat on the header leaves the group; a group of one
dissolves. The close button ungroups a dropped group with Undo. Rename,
Ungroup, Close all (with Undo), Move to group and Remove from group are
in the group menu and as semantic actions.

Each open-chat entry carries its group name (protocol field "group",
omitted when ungrouped; older clients ignore it), stored and pushed by
the server like the order; names are trimmed, printable, at most 80
characters. Needs a server restart to keep groups.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 22:18:44 +00:00
parent 130c1e6708
commit efaa0d9fca
30 changed files with 2399 additions and 308 deletions
@@ -2,53 +2,81 @@ import 'package:flutter/material.dart';
import 'package:flutter/semantics.dart';
import '../../../ui/theme/app_design_tokens.dart';
import 'chat_stack_column.dart';
import 'chat_stack_disc.dart';
import 'chat_stack_drag.dart';
import 'chat_stack_drag_shift.dart';
import 'chat_stack_entry.dart';
import 'chat_stack_group_pile.dart';
import 'chat_stack_handles.dart';
import 'chat_stack_motion.dart';
import 'chat_stack_node.dart';
import 'chat_stack_pose.dart';
/// The open column's avatars and name pills, each placed at the stack's
/// slot and moved by the open motion ([ChatStackPoseTransition]) and by a
/// drag ([ChatStackDragShift]), painted in [order].
/// The item at the top of a column: Add chat on the top level, the group's
/// header (back to the top level) in an open group.
sealed class ChatStackTail {
const ChatStackTail();
}
class ChatStackAddTail extends ChatStackTail {
const ChatStackAddTail();
static const label = 'Add chat';
}
class ChatStackHeaderTail extends ChatStackTail {
const ChatStackHeaderTail(this.group, {this.onMenu});
static const key = 'tail:header';
final ChatStackGroupNode group;
/// Opens the group's menu, anchored to the given global rect.
final ValueChanged<Rect>? onMenu;
}
/// The open column's items (chats and groups) and its tail, each placed at
/// the stack's slot and moved by the open motion ([ChatStackPoseTransition])
/// and by a drag ([ChatStackDragShift]), painted in [order].
class ChatStackColumnItems extends StatelessWidget {
const ChatStackColumnItems({
super.key,
required this.motion,
required this.entries,
required this.nodes,
required this.tail,
required this.order,
required this.labelMax,
required this.drag,
required this.draggable,
required this.onOpen,
required this.onAdd,
required this.onTail,
required this.onDragStart,
required this.onDragMove,
required this.onDragEnd,
this.onRemove,
this.onReorder,
required this.actionsFor,
this.tailActions,
});
final ChatStackMotion motion;
final List<ChatStackEntry> entries;
final List<ChatStackNode> nodes;
final ChatStackTail tail;
/// Item indices in paint order, the last on top.
/// Item indices in paint order, the last on top; [nodes.length] is the
/// tail.
final List<int> order;
final double labelMax;
final ChatStackDrag drag;
/// Whether a hold lifts a chat.
/// Whether a hold lifts an item.
final bool draggable;
final ValueChanged<ChatStackEntry> onOpen;
final VoidCallback onAdd;
final ValueChanged<ChatStackEntry> onDragStart;
final ValueChanged<ChatStackNode> onOpen;
final VoidCallback onTail;
final ValueChanged<ChatStackNode> onDragStart;
final ValueChanged<LongPressMoveUpdateDetails> onDragMove;
final ValueChanged<ChatStackEntry> onDragEnd;
final ValueChanged<ChatStackEntry>? onRemove;
final void Function(ChatStackEntry entry, int index)? onReorder;
final ValueChanged<ChatStackNode> onDragEnd;
final Map<CustomSemanticsAction, VoidCallback> Function(
ChatStackNode node,
int index,
)
actionsFor;
final Map<CustomSemanticsAction, VoidCallback>? tailActions;
@override
Widget build(BuildContext context) => Stack(
@@ -58,34 +86,54 @@ class ChatStackColumnItems extends StatelessWidget {
],
);
ChatStackEntry? _entry(int i) => i < entries.length ? entries[i] : null;
ChatStackNode? _node(int i) => i < nodes.length ? nodes[i] : null;
/// [child] lifted by a hold when [entry] can be dragged.
Widget _handle(ChatStackEntry? entry, Widget child) => ChatStackDragHandle(
onStart: entry == null || !draggable ? null : () => onDragStart(entry),
String _keyOf(ChatStackNode? node) => switch (node) {
ChatStackChatNode(:final entry) => entry.paneId,
ChatStackGroupNode(:final name) => 'group-$name',
null => tail is ChatStackAddTail ? 'add' : 'header',
};
/// The drag key [i] paints with; Add chat never moves.
String? _dragKey(int i) {
final node = _node(i);
if (node != null) return node.key;
return tail is ChatStackHeaderTail ? ChatStackHeaderTail.key : null;
}
Widget _handle(ChatStackNode? node, Widget child) => ChatStackDragHandle(
onStart: node == null || !draggable ? null : () => onDragStart(node),
onMove: onDragMove,
onEnd: entry == null ? null : () => onDragEnd(entry),
onEnd: node == null ? null : () => onDragEnd(node),
onCancel: drag.cancel,
child: child,
);
/// [child] where the drag paints [entry]'s item; Add chat never moves.
Widget _shift(ChatStackEntry? entry, Widget child, {bool scales = true}) =>
entry == null
? child
: ChatStackDragShift(
item: drag.item(entry.paneId),
scales: scales,
child: child,
);
Widget _shift(int i, Widget child, {bool scales = true}) {
final key = _dragKey(i);
return key == null
? child
: ChatStackDragShift(
item: drag.item(key),
scales: scales,
child: child,
);
}
Animation<double>? _lift(ChatStackEntry? entry) =>
entry != null && entry.paneId == drag.lifted ? drag.lift : null;
/// The lifted look while dragged; the drop-target ring while a dragged
/// item would land on it.
(Animation<double>?, Color?) _lift(BuildContext context, String? key) {
if (key == null) return (null, null);
if (key == drag.lifted) return (drag.lift, null);
if (!drag.dragging) return (null, null);
return (drag.glow(key), Theme.of(context).colorScheme.primary);
}
Widget _item(BuildContext context, int i) {
final entry = _entry(i);
final node = _node(i);
final name = _keyOf(node);
return PositionedDirectional(
key: ValueKey('chat-stack-item-${entry?.paneId ?? 'add'}'),
key: ValueKey('chat-stack-item-$name'),
end: 0,
bottom: 0,
// The transforms are outermost: the boxes under them sit at the
@@ -96,14 +144,16 @@ class ChatStackColumnItems extends StatelessWidget {
from: motion.from[i],
to: motion.target[i],
child: _shift(
entry,
i,
FadeTransition(
opacity: motion.fade[i],
child: KeyedSubtree(
key: ValueKey('chat-stack-button-${entry?.paneId ?? 'add'}'),
child: entry == null
? _addButton(context)
: _chatButton(context, entry, i),
key: ValueKey('chat-stack-button-$name'),
child: switch (node) {
ChatStackChatNode() => _chatButton(context, node, i),
ChatStackGroupNode() => _groupButton(context, node, i),
null => _tailButton(context),
},
),
),
),
@@ -112,9 +162,40 @@ class ChatStackColumnItems extends StatelessWidget {
}
Widget _label(BuildContext context, int i) {
final entry = _entry(i);
final node = _node(i);
final (lift, ring) = _lift(context, _dragKey(i));
final tail = this.tail;
final Widget label = switch (node) {
ChatStackChatNode(:final entry) => ChatStackLabel(
text: entry.title,
color: entry.color(context),
emphasized: entry.current,
lift: lift,
liftRing: ring,
),
ChatStackGroupNode(:final name, :final entries) => ChatStackLabel(
text: name,
detail: '${entries.length}',
emphasized: node.current,
lift: lift,
liftRing: ring,
),
null => switch (tail) {
ChatStackAddTail() => const ChatStackLabel(
text: ChatStackAddTail.label,
),
ChatStackHeaderTail(:final group, :final onMenu) => ChatStackLabel(
text: group.name,
detail: '${group.entries.length}',
emphasized: true,
lift: lift,
liftRing: ring,
trailing: onMenu == null ? null : _MenuButton(onMenu: onMenu),
),
},
};
return PositionedDirectional(
key: ValueKey('chat-stack-label-${entry?.paneId ?? 'add'}'),
key: ValueKey('chat-stack-label-${_keyOf(node)}'),
end: kChatStackSlot + kChatStackLabelGap,
bottom: 0,
height: kChatStackSlot,
@@ -125,7 +206,7 @@ class ChatStackColumnItems extends StatelessWidget {
// The pill moves with its avatar but keeps its size, so the gap
// between them stays even.
child: _shift(
entry,
i,
scales: false,
ExcludeSemantics(
child: FadeTransition(
@@ -136,15 +217,10 @@ class ChatStackColumnItems extends StatelessWidget {
child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: labelMax),
child: _handle(
entry,
node,
GestureDetector(
onTap: entry == null ? onAdd : () => onOpen(entry),
child: ChatStackLabel(
text: entry?.title ?? ChatStackColumn.addLabel,
color: entry?.color(context),
emphasized: entry?.current ?? false,
lift: _lift(entry),
),
onTap: node == null ? onTail : () => onOpen(node),
child: label,
),
),
),
@@ -156,41 +232,27 @@ class ChatStackColumnItems extends StatelessWidget {
);
}
Widget _chatButton(BuildContext context, ChatStackEntry entry, int index) {
final remove = onRemove;
final reorder = onReorder;
final last = entries.length - 1;
Widget _chatButton(BuildContext context, ChatStackChatNode node, int i) {
final entry = node.entry;
final ring = entry.current
? entry.color(context) ?? Theme.of(context).colorScheme.primary
: null;
// Up is toward the top of the column: a later place in the order.
final actions = <CustomSemanticsAction, VoidCallback>{
if (reorder != null && index < last)
const CustomSemanticsAction(label: ChatStackColumn.moveUpLabel): () =>
reorder(entry, index + 1),
if (reorder != null && index > 0)
const CustomSemanticsAction(label: ChatStackColumn.moveDownLabel): () =>
reorder(entry, index - 1),
if (remove != null)
const CustomSemanticsAction(label: ChatStackColumn.removeLabel): () =>
remove(entry),
};
return Semantics(
container: true,
button: true,
selected: entry.current,
final (lift, liftRing) = _lift(context, node.key);
return _semantics(
node,
i,
label: entry.title,
selected: entry.current,
hint: entry.current ? 'Open now' : null,
onTap: () => onOpen(entry),
customSemanticsActions: actions.isEmpty ? null : actions,
child: _handle(
entry,
node,
ChatStackTappable(
autofocus: entry.current,
onTap: () => onOpen(entry),
onTap: () => onOpen(node),
child: ChatStackDisc(
ring: ring,
lift: _lift(entry),
lift: lift,
liftRing: liftRing,
child: chatStackAvatar(entry),
),
),
@@ -198,20 +260,75 @@ class ChatStackColumnItems extends StatelessWidget {
);
}
Widget _addButton(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
final tokens = AppDesignTokens.of(context);
Widget _groupButton(BuildContext context, ChatStackGroupNode node, int i) {
final (lift, liftRing) = _lift(context, node.key);
return _semantics(
node,
i,
label: '${node.name}, group of ${node.entries.length}',
selected: node.current,
hint: 'Open group',
child: _handle(
node,
ChatStackTappable(
onTap: () => onOpen(node),
child: ChatStackGroupPile(
entries: node.entries,
glow: lift,
ring: liftRing,
),
),
),
);
}
Widget _semantics(
ChatStackNode node,
int i, {
required String label,
required bool selected,
required Widget child,
String? hint,
}) {
final actions = actionsFor(node, i);
return Semantics(
container: true,
button: true,
label: ChatStackColumn.addLabel,
onTap: onAdd,
selected: selected,
label: label,
hint: hint,
onTap: () => onOpen(node),
customSemanticsActions: actions.isEmpty ? null : actions,
child: child,
);
}
Widget _tailButton(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
final tokens = AppDesignTokens.of(context);
final tail = this.tail;
final header = tail is ChatStackHeaderTail;
final (lift, ring) = _lift(context, _dragKey(nodes.length));
final actions = tailActions;
return Semantics(
container: true,
button: true,
label: switch (tail) {
ChatStackAddTail() => ChatStackAddTail.label,
ChatStackHeaderTail(:final group) => '${group.name}, back to all chats',
},
onTap: onTail,
customSemanticsActions: actions == null || actions.isEmpty
? null
: actions,
child: ChatStackTappable(
onTap: onAdd,
onTap: onTail,
child: ChatStackDisc(
lift: lift,
liftRing: ring,
child: DecoratedBox(
decoration: ShapeDecoration(
color: tokens.flat ? null : scheme.primaryContainer,
color: tokens.flat || header ? null : scheme.primaryContainer,
shape: chatStackShape(tokens).copyWith(
side: tokens.flat
? BorderSide(color: scheme.primary.withValues(alpha: .55))
@@ -219,8 +336,10 @@ class ChatStackColumnItems extends StatelessWidget {
),
),
child: Icon(
Icons.add,
color: tokens.flat ? scheme.primary : scheme.onPrimaryContainer,
header ? Icons.arrow_back : Icons.add,
color: tokens.flat || header
? scheme.primary
: scheme.onPrimaryContainer,
),
),
),
@@ -228,3 +347,25 @@ class ChatStackColumnItems extends StatelessWidget {
);
}
}
/// The group header's menu button, inside its pill.
class _MenuButton extends StatelessWidget {
const _MenuButton({required this.onMenu});
final ValueChanged<Rect> onMenu;
@override
Widget build(BuildContext context) => SizedBox.square(
dimension: 28,
child: IconButton(
key: const Key('chat-stack-group-menu'),
tooltip: 'Group actions',
padding: EdgeInsets.zero,
iconSize: 18,
onPressed: () {
final box = context.findRenderObject() as RenderBox;
onMenu(box.localToGlobal(Offset.zero) & box.size);
},
icon: const Icon(Icons.more_horiz),
),
);
}