Chat stack: hold and drag to reorder the open chats, or onto x to remove

A 300 ms hold lifts a chat in the open column; dragging opens a gap and
dropping commits through ChatWorkspaceController.movePane, the pane order
the server syncs. An x comes in at the bottom while dragging; dropping on
it removes the chat with Undo, which restores its place. Escape, a
cancelled pointer or a drop away from the column put it back. Move up,
Move down and Remove are semantic actions. Repaint-only motion; reduced
motion swaps at once. Also keep floating-layout tracking out of
initState, and let the capture kit hold a press before a drag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 21:17:51 +00:00
parent f8e4d78466
commit fba3b924f1
20 changed files with 1573 additions and 254 deletions
@@ -0,0 +1,320 @@
import 'dart:ui' show lerpDouble;
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);
/// The lifted avatar's scale.
const double chatStackLiftScale = 1.1;
/// 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.
class ChatStackDrag extends ChangeNotifier {
ChatStackDrag({required TickerProvider vsync, required this.pitch})
: _vsync = vsync {
lift = AnimationController(vsync: vsync, duration: chatStackShiftDuration)
..addListener(_paintLifted);
settle =
AnimationController(vsync: vsync, duration: chatStackSettleDuration)
..addListener(_paintLifted)
..addStatusListener(_settled);
removeShown = AnimationController(
vsync: vsync,
duration: chatStackRemoveDuration,
);
hover = AnimationController(vsync: vsync, duration: chatStackHoverDuration)
..addListener(_paintLifted);
}
final TickerProvider _vsync;
/// The distance between neighbouring slots.
final double pitch;
/// Under reduced motion neighbours swap at once and nothing slides.
bool reduced = false;
late final AnimationController lift;
late final AnimationController settle;
/// The remove target's entrance, and its highlight while a chat is over
/// it.
late final AnimationController removeShown;
late final AnimationController hover;
final Map<String, ChatStackDragItem> _items = {};
final Map<String, AnimationController> _shifts = {};
/// The chat 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.
List<String>? get order => _order;
List<String>? _order;
int _from = 0;
int? _slot;
bool _over = false;
Offset _pointer = Offset.zero;
Offset _residual = Offset.zero;
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);
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);
if (from < 0) return;
if (_lifted != null) _finishSettle();
_order = List.of(order);
_from = from;
_slot = from;
_over = false;
_pointer = Offset.zero;
_residual = Offset.zero;
_lifted = id;
HapticFeedback.mediumImpact();
if (reduced) {
lift.value = 1;
removeShown.value = 1;
} else {
lift.forward(from: 0);
removeShown.forward();
}
hover.value = 0;
notifyListeners();
}
/// The pointer moved [fromOrigin] since the press; [overRemove] says
/// whether it is over the remove target.
void move(Offset fromOrigin, {required bool overRemove}) {
final order = _order;
if (order == null) return;
_pointer = fromOrigin;
if (overRemove != _over) {
_over = overRemove;
if (overRemove) HapticFeedback.selectionClick();
if (reduced) {
hover.value = overRemove ? 1 : 0;
} else {
overRemove ? hover.forward() : hover.reverse();
}
}
// 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;
_shiftNeighbours(animate: !reduced);
}
_paintLifted();
}
/// Ends the drag and says what the drop does; the motion back (or into
/// the new slot) runs from here.
ChatStackDrop end() {
final order = _order;
if (order == null) return const ChatStackDropBack();
final slot = _slot;
final ChatStackDrop drop = _over
? const ChatStackDropRemove()
: slot != null && slot != _from
? ChatStackDropMove(slot)
: const ChatStackDropBack();
_order = 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;
_residual = _pointer + Offset(0, (index - _from) * pitch);
_shiftNeighbours(animate: false);
case ChatStackDropRemove():
_slot = null;
_residual = Offset.zero;
_shiftNeighbours(animate: false);
case ChatStackDropBack():
_slot = null;
_residual = _pointer;
_shiftNeighbours(animate: !reduced);
}
_release();
return drop;
}
/// Puts the chat back where it was (Escape, a cancelled pointer, the
/// column folding).
void cancel() {
if (_order == null) return;
_order = null;
_slot = null;
_residual = _pointer;
_shiftNeighbours(animate: !reduced);
_release();
}
void _release() {
_pointer = Offset.zero;
if (_over) HapticFeedback.selectionClick();
_over = false;
if (reduced) {
hover.value = 0;
removeShown.value = 0;
lift.value = 0;
_residual = Offset.zero;
_finishSettle();
return;
}
hover.reverse();
removeShown.reverse();
lift.reverse();
settle.forward(from: 0);
}
void _settled(AnimationStatus status) {
if (status == AnimationStatus.completed) _finishSettle();
}
void _finishSettle() {
settle.stop();
final id = _lifted;
_residual = Offset.zero;
if (id != null) item(id)._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 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);
}
/// 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 slot = _slot;
for (final id in ids) {
if (id == _lifted && _order != null) continue;
final i = order.indexOf(id);
var target = 0.0;
if (slot != null && i >= 0) {
// Down (toward the thumb) when the lifted chat 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));
if (shift.value == target) continue;
if (animate) {
shift.animateTo(
target,
duration: chatStackShiftDuration,
curve: _curve,
);
} else {
shift
..stop()
..value = target;
}
}
}
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 item in _items.values) {
item.dispose();
}
super.dispose();
}
}