Files
pipelines/app/lib/features/chat/stack/chat_stack_column_items.dart
T
alan efaa0d9fca 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>
2026-12-03 22:18:44 +00:00

372 lines
11 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/semantics.dart';
import '../../../ui/theme/app_design_tokens.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 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.nodes,
required this.tail,
required this.order,
required this.labelMax,
required this.drag,
required this.draggable,
required this.onOpen,
required this.onTail,
required this.onDragStart,
required this.onDragMove,
required this.onDragEnd,
required this.actionsFor,
this.tailActions,
});
final ChatStackMotion motion;
final List<ChatStackNode> nodes;
final ChatStackTail tail;
/// 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 an item.
final bool draggable;
final ValueChanged<ChatStackNode> onOpen;
final VoidCallback onTail;
final ValueChanged<ChatStackNode> onDragStart;
final ValueChanged<LongPressMoveUpdateDetails> onDragMove;
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(
clipBehavior: Clip.none,
children: [
for (final i in order) ...[_label(context, i), _item(context, i)],
],
);
ChatStackNode? _node(int i) => i < nodes.length ? nodes[i] : null;
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: node == null ? null : () => onDragEnd(node),
onCancel: drag.cancel,
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,
);
}
/// 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 node = _node(i);
final name = _keyOf(node);
return PositionedDirectional(
key: ValueKey('chat-stack-item-$name'),
end: 0,
bottom: 0,
// The transforms are outermost: the boxes under them sit at the
// stack's slot, and only a transform passes hits on to where it paints
// them.
child: ChatStackPoseTransition(
progress: motion.move[i],
from: motion.from[i],
to: motion.target[i],
child: _shift(
i,
FadeTransition(
opacity: motion.fade[i],
child: KeyedSubtree(
key: ValueKey('chat-stack-button-$name'),
child: switch (node) {
ChatStackChatNode() => _chatButton(context, node, i),
ChatStackGroupNode() => _groupButton(context, node, i),
null => _tailButton(context),
},
),
),
),
),
);
}
Widget _label(BuildContext context, int 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-${_keyOf(node)}'),
end: kChatStackSlot + kChatStackLabelGap,
bottom: 0,
height: kChatStackSlot,
child: ChatStackPoseTransition(
progress: motion.label[i],
from: motion.labelFrom[i],
to: motion.target[i],
// The pill moves with its avatar but keeps its size, so the gap
// between them stays even.
child: _shift(
i,
scales: false,
ExcludeSemantics(
child: FadeTransition(
opacity: motion.label[i],
child: Align(
alignment: AlignmentDirectional.centerEnd,
widthFactor: 1,
child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: labelMax),
child: _handle(
node,
GestureDetector(
onTap: node == null ? onTail : () => onOpen(node),
child: label,
),
),
),
),
),
),
),
),
);
}
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;
final (lift, liftRing) = _lift(context, node.key);
return _semantics(
node,
i,
label: entry.title,
selected: entry.current,
hint: entry.current ? 'Open now' : null,
child: _handle(
node,
ChatStackTappable(
autofocus: entry.current,
onTap: () => onOpen(node),
child: ChatStackDisc(
ring: ring,
lift: lift,
liftRing: liftRing,
child: chatStackAvatar(entry),
),
),
),
);
}
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,
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: onTail,
child: ChatStackDisc(
lift: lift,
liftRing: ring,
child: DecoratedBox(
decoration: ShapeDecoration(
color: tokens.flat || header ? null : scheme.primaryContainer,
shape: chatStackShape(tokens).copyWith(
side: tokens.flat
? BorderSide(color: scheme.primary.withValues(alpha: .55))
: BorderSide.none,
),
),
child: Icon(
header ? Icons.arrow_back : Icons.add,
color: tokens.flat || header
? scheme.primary
: scheme.onPrimaryContainer,
),
),
),
),
);
}
}
/// 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),
),
);
}