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
+119 -110
View File
@@ -4,67 +4,15 @@ import 'package:flutter/animation.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
/// How long a press is held before the chat lifts for a drag: long enough
/// that a tap still opens the chat, short enough to feel immediate.
const Duration chatStackLiftDelay = Duration(milliseconds: 300);
import 'chat_stack_drop_target.dart';
/// The lifted avatar's scale.
const double chatStackLiftScale = 1.1;
part 'chat_stack_drag_types.dart';
/// The lifted avatar's scale, relative to lifted, over the remove target.
const double chatStackOverRemoveScale = .8;
/// How long the neighbours take to slide aside, and the lifted chat to
/// settle into its slot (or back) after the drop.
const Duration chatStackShiftDuration = Duration(milliseconds: 200);
const Duration chatStackSettleDuration = Duration(milliseconds: 220);
/// How long the remove target takes to come in and to light up.
const Duration chatStackRemoveDuration = Duration(milliseconds: 180);
const Duration chatStackHoverDuration = Duration(milliseconds: 140);
/// Where one item of the open column is painted relative to its slot while
/// a chat is dragged; a render object repaints when it changes.
class ChatStackDragItem extends ChangeNotifier {
Offset _offset = Offset.zero;
double _scale = 1;
Offset get offset => _offset;
double get scale => _scale;
void _set(Offset offset, double scale) {
if (offset == _offset && scale == _scale) return;
_offset = offset;
_scale = scale;
notifyListeners();
}
}
/// What a drop does.
sealed class ChatStackDrop {
const ChatStackDrop();
}
/// Nothing: the chat settles back into its own slot.
class ChatStackDropBack extends ChatStackDrop {
const ChatStackDropBack();
}
/// The chat moves to [index] in the open-chat order.
class ChatStackDropMove extends ChatStackDrop {
const ChatStackDropMove(this.index);
final int index;
}
/// The chat leaves the open chats.
class ChatStackDropRemove extends ChatStackDrop {
const ChatStackDropRemove();
}
/// One drag in the open column: which chat is lifted, the slot it would
/// drop into, whether it is over the remove target, and the motion of every
/// item. It notifies only when a drag starts or ends (the column then
/// rebuilds once); each pointer move only repaints the items that move.
/// One drag in the open column: which item is lifted, the slot it would
/// drop into or the item it would drop onto, whether it is over the remove
/// target, and the motion of every item. It notifies only when a drag
/// starts or ends (the column then rebuilds once); each pointer move only
/// repaints the items that move.
class ChatStackDrag extends ChangeNotifier {
ChatStackDrag({required TickerProvider vsync, required this.pitch})
: _vsync = vsync {
@@ -93,23 +41,26 @@ class ChatStackDrag extends ChangeNotifier {
late final AnimationController lift;
late final AnimationController settle;
/// The remove target's entrance, and its highlight while a chat is over
/// it.
/// The remove target arming, and its highlight while the item is over it.
late final AnimationController removeShown;
late final AnimationController hover;
final Map<String, ChatStackDragItem> _items = {};
final Map<String, AnimationController> _shifts = {};
final Map<String, AnimationController> _glows = {};
/// The chat being dragged or settling; it paints above the others.
/// The item being dragged or settling; it paints above the others.
String? get lifted => _lifted;
String? _lifted;
/// The chats' order when the drag started; null when none is dragged.
/// The items' order when the drag started; null when none is dragged.
List<String>? get order => _order;
List<String>? _order;
int _from = 0;
int? _slot;
int? _onto;
bool Function(int index)? _canDropOnto;
int? _lastSlot;
bool _over = false;
Offset _pointer = Offset.zero;
Offset _residual = Offset.zero;
@@ -117,24 +68,47 @@ class ChatStackDrag extends ChangeNotifier {
bool get dragging => _order != null;
bool get overRemove => _over;
/// Where [id]'s item is painted; created on first use.
ChatStackDragItem item(String id) =>
_items.putIfAbsent(id, ChatStackDragItem.new);
/// The item the dragged one would drop onto, if any.
String? get ontoKey => _onto == null ? null : _order?[_onto!];
/// Where [key]'s item is painted; created on first use.
ChatStackDragItem item(String key) =>
_items.putIfAbsent(key, ChatStackDragItem.new);
/// 0 at rest, 1 while the dragged item is over [key]'s (to drop onto it).
Animation<double> glow(String key) => _glows.putIfAbsent(key, () {
final glow = AnimationController(
vsync: _vsync,
duration: chatStackHoverDuration,
);
glow.addListener(() => _paint(key));
return glow;
});
static const _curve = Curves.easeOutCubic;
/// Lifts [id] out of [order] (the chats, without Add chat).
void start(String id, List<String> order) {
final from = order.indexOf(id);
/// Lifts [key] out of [order] (the items, without Add chat). A drop on the
/// middle of an item [canDropOnto] accepts lands on it; the gap never
/// goes past [lastSlot] (an item after it is a target only).
void start(
String key,
List<String> order, {
bool Function(int index)? canDropOnto,
int? lastSlot,
}) {
final from = order.indexOf(key);
if (from < 0) return;
if (_lifted != null) _finishSettle();
_order = List.of(order);
_from = from;
_slot = from;
_onto = null;
_canDropOnto = canDropOnto;
_lastSlot = lastSlot;
_over = false;
_pointer = Offset.zero;
_residual = Offset.zero;
_lifted = id;
_lifted = key;
HapticFeedback.mediumImpact();
if (reduced) {
lift.value = 1;
@@ -156,18 +130,31 @@ class ChatStackDrag extends ChangeNotifier {
if (overRemove != _over) {
_over = overRemove;
if (overRemove) HapticFeedback.selectionClick();
if (reduced) {
hover.value = overRemove ? 1 : 0;
} else {
overRemove ? hover.forward() : hover.reverse();
}
_run(hover, overRemove ? 1 : 0);
}
// Far to the side of the column, or over the remove target, there is no
// slot: the gap closes and a drop there puts the item back.
int? slot;
int? onto;
if (!overRemove && fromOrigin.dx.abs() <= pitch * 1.5) {
final target = chatStackDropTarget(
position: _from - fromOrigin.dy / pitch,
from: _from,
count: order.length,
slot: _slot ?? _from,
canDropOnto: _canDropOnto,
lastSlot: _lastSlot,
);
slot = target.slot;
onto = target.onto;
}
if (onto != _onto) {
final before = _onto;
_onto = onto;
if (onto != null) HapticFeedback.selectionClick();
if (before != null) _run(glow(order[before]) as AnimationController, 0);
if (onto != null) _run(glow(order[onto]) as AnimationController, 1);
}
// Far to the side of the column, or over the target, there is no slot:
// the gap closes and a drop there puts the chat back.
final inColumn = !overRemove && fromOrigin.dx.abs() <= pitch * 1.5;
final slot = inColumn
? (_from - (fromOrigin.dy / pitch).round()).clamp(0, order.length - 1)
: null;
if (slot != _slot) {
if (slot != null && _slot != null) HapticFeedback.selectionClick();
_slot = slot;
@@ -182,33 +169,35 @@ class ChatStackDrag extends ChangeNotifier {
final order = _order;
if (order == null) return const ChatStackDropBack();
final slot = _slot;
final onto = _onto;
final ChatStackDrop drop = _over
? const ChatStackDropRemove()
: onto != null
? ChatStackDropOnto(onto)
: slot != null && slot != _from
? ChatStackDropMove(slot)
: const ChatStackDropBack();
_order = null;
_slot = null;
switch (drop) {
case ChatStackDropMove(:final index):
// The new order puts the neighbours where they are now drawn; the
// dragged chat glides from the finger into its new slot.
_slot = null;
// dragged item glides from the finger into its new slot.
_residual = _pointer + Offset(0, (index - _from) * pitch);
_shiftNeighbours(animate: false);
case ChatStackDropRemove():
_slot = null;
case ChatStackDropRemove() || ChatStackDropOnto():
// It joins the target (or leaves): no glide back.
_residual = Offset.zero;
_shiftNeighbours(animate: false);
case ChatStackDropBack():
_slot = null;
_residual = _pointer;
_shiftNeighbours(animate: !reduced);
}
_release();
_release(clearGlowAtOnce: drop is ChatStackDropOnto);
return drop;
}
/// Puts the chat back where it was (Escape, a cancelled pointer, the
/// Puts the item back where it was (Escape, a cancelled pointer, the
/// column folding).
void cancel() {
if (_order == null) return;
@@ -219,10 +208,14 @@ class ChatStackDrag extends ChangeNotifier {
_release();
}
void _release() {
void _release({bool clearGlowAtOnce = false}) {
_pointer = Offset.zero;
if (_over) HapticFeedback.selectionClick();
_over = false;
_onto = null;
for (final glow in _glows.values) {
clearGlowAtOnce ? glow.value = 0 : _run(glow, 0);
}
if (reduced) {
hover.value = 0;
removeShown.value = 0;
@@ -237,51 +230,73 @@ class ChatStackDrag extends ChangeNotifier {
settle.forward(from: 0);
}
void _run(AnimationController controller, double target) {
if (reduced) {
controller.value = target;
} else {
controller.animateTo(target, curve: _curve);
}
}
void _settled(AnimationStatus status) {
if (status == AnimationStatus.completed) _finishSettle();
}
void _finishSettle() {
settle.stop();
final id = _lifted;
final key = _lifted;
_residual = Offset.zero;
if (id != null) item(id)._set(Offset.zero, 1);
if (key != null) item(key)._set(Offset.zero, 1);
if (_order != null) return;
_lifted = null;
notifyListeners();
}
void _paintLifted() {
final id = _lifted;
if (id == null) return;
// Over the remove target the chat shrinks into it, so the target's
// lit rim shows around it.
final key = _lifted;
if (key == null) return;
// Over the remove target the item shrinks into it, so the target's lit
// rim shows around it.
final scale =
lerpDouble(1, chatStackLiftScale, _curve.transform(lift.value))! *
lerpDouble(1, chatStackOverRemoveScale, hover.value)!;
final offset = _order != null
? _pointer
: _residual * (1 - _curve.transform(settle.value));
item(id)._set(offset, scale);
item(key)._set(offset, scale);
}
/// Paints a neighbour: slid by its shift, grown by its glow.
void _paint(String key) {
if (key == _lifted) return;
final shift = _shifts[key]?.value ?? 0;
final glow = _glows[key]?.value ?? 0;
item(
key,
)._set(Offset(0, shift * pitch), lerpDouble(1, chatStackOntoScale, glow)!);
}
/// Slides each neighbour aside to open the gap at the current slot, or
/// back home when there is none.
void _shiftNeighbours({required bool animate}) {
final order = _order ?? const <String>[];
final ids = {...order, ..._shifts.keys};
final keys = {...order, ..._shifts.keys};
final slot = _slot;
for (final id in ids) {
if (id == _lifted && _order != null) continue;
final i = order.indexOf(id);
for (final key in keys) {
if (key == _lifted && _order != null) continue;
final i = order.indexOf(key);
var target = 0.0;
if (slot != null && i >= 0) {
// Down (toward the thumb) when the lifted chat goes above it, up
// Down (toward the thumb) when the lifted item goes above it, up
// when it goes below.
if (_from < slot && i > _from && i <= slot) target = 1;
if (slot < _from && i >= slot && i < _from) target = -1;
}
final shift = _shifts.putIfAbsent(id, () => _shiftFor(id));
final shift = _shifts.putIfAbsent(key, () {
final shift = AnimationController.unbounded(vsync: _vsync);
shift.addListener(() => _paint(key));
return shift;
});
if (shift.value == target) continue;
if (animate) {
shift.animateTo(
@@ -297,20 +312,14 @@ class ChatStackDrag extends ChangeNotifier {
}
}
AnimationController _shiftFor(String id) {
final shift = AnimationController.unbounded(vsync: _vsync);
shift.addListener(() => item(id)._set(Offset(0, shift.value * pitch), 1));
return shift;
}
@override
void dispose() {
lift.dispose();
settle.dispose();
removeShown.dispose();
hover.dispose();
for (final shift in _shifts.values) {
shift.dispose();
for (final controller in [..._shifts.values, ..._glows.values]) {
controller.dispose();
}
for (final item in _items.values) {
item.dispose();