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
+154 -189
View File
@@ -1,12 +1,15 @@
import 'dart:math' as math;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/semantics.dart';
import 'package:flutter/services.dart';
import '../../../ui/theme/app_design_tokens.dart';
import 'chat_stack_column_items.dart';
import 'chat_stack_disc.dart';
import 'chat_stack_drag.dart';
import 'chat_stack_entry.dart';
import 'chat_stack_motion.dart';
import 'chat_stack_pose.dart';
import 'chat_stack_remove_target.dart';
/// The chat stack fanned out: a scrim over the screen and the open chats in
/// a column rising from the stack's own slot, each avatar with its name in a
@@ -19,10 +22,16 @@ import 'chat_stack_pose.dart';
/// bottom slots go to the frequent action (switching), and the rarer one
/// ends the list where the operator reads "and more".
///
/// A short hold lifts a chat ([ChatStackDrag]): dragged up or down it
/// reorders the open chats, the others sliding aside to open its slot;
/// dropped on the "×" that comes in at the bottom it leaves them. Add chat
/// stays put. The same moves are semantic actions on each chat.
///
/// Nothing here rebuilds per frame: every item moves through
/// [ChatStackPoseTransition] and fades through [FadeTransition], both
/// repaint-only.
class ChatStackColumn extends StatelessWidget {
/// `ChatStackPoseTransition` and `ChatStackDragShift` and fades through
/// [FadeTransition], all repaint-only; a drag rebuilds the column only as it
/// starts and ends.
class ChatStackColumn extends StatefulWidget {
const ChatStackColumn({
super.key,
required this.motion,
@@ -34,6 +43,7 @@ class ChatStackColumn extends StatelessWidget {
required this.onAdd,
required this.onDismiss,
this.onRemove,
this.onReorder,
});
final ChatStackMotion motion;
@@ -52,23 +62,112 @@ class ChatStackColumn extends StatelessWidget {
final VoidCallback onAdd;
final VoidCallback onDismiss;
/// Long press removes a chat from the open chats; null when it cannot
/// (the only open chat).
/// Removes a chat from the open chats (dropped on the "×"); null when it
/// cannot (the only open chat).
final ValueChanged<ChatStackEntry>? onRemove;
/// Moves a chat to an index of the open-chat order; null when the chats
/// cannot be reordered.
final void Function(ChatStackEntry entry, int index)? onReorder;
static const removeLabel = 'Remove from open chats';
static const moveUpLabel = 'Move up';
static const moveDownLabel = 'Move down';
static const addLabel = 'Add chat';
@override
State<ChatStackColumn> createState() => _ChatStackColumnState();
}
class _ChatStackColumnState extends State<ChatStackColumn>
with TickerProviderStateMixin {
late final ChatStackDrag _drag = ChatStackDrag(
vsync: this,
pitch: kChatStackPitch,
)..addListener(_dragChanged);
/// The remove target's centre in this column's box, from the last layout.
Offset? _removeCenter;
List<ChatStackEntry> get _entries => widget.entries;
List<String> get _ids => [for (final entry in _entries) entry.paneId];
/// A hold lifts a chat when there is something to do with it.
bool get _draggable =>
widget.interactive &&
((widget.onReorder != null && _entries.length > 1) ||
widget.onRemove != null);
@override
void didChangeDependencies() {
super.didChangeDependencies();
_drag.reduced = MediaQuery.maybeDisableAnimationsOf(context) ?? false;
}
@override
void didUpdateWidget(covariant ChatStackColumn oldWidget) {
super.didUpdateWidget(oldWidget);
// Folding, or the open chats changing under the finger (another
// device), puts the dragged chat back.
if (!widget.interactive ||
(_drag.dragging && !listEquals(_ids, _drag.order))) {
_drag.cancel();
}
}
@override
void dispose() {
_drag.dispose();
super.dispose();
}
void _dragChanged() {
if (mounted) setState(() {});
}
void _start(ChatStackEntry entry) {
if (_draggable) _drag.start(entry.paneId, _ids);
}
void _move(LongPressMoveUpdateDetails details) {
if (!_drag.dragging) return;
final box = context.findRenderObject() as RenderBox?;
final center = _removeCenter;
final over =
widget.onRemove != null &&
box != null &&
center != null &&
(box.globalToLocal(details.globalPosition) - center).distance <=
kChatStackRemoveReach;
_drag.move(details.offsetFromOrigin, overRemove: over);
}
void _end(ChatStackEntry entry) {
if (!_drag.dragging) return;
switch (_drag.end()) {
case ChatStackDropMove(:final index):
widget.onReorder?.call(entry, index);
case ChatStackDropRemove():
widget.onRemove?.call(entry);
case ChatStackDropBack():
break;
}
}
void _escape() => _drag.dragging ? _drag.cancel() : widget.onDismiss();
@override
Widget build(BuildContext context) {
final media = MediaQuery.of(context);
final scheme = Theme.of(context).colorScheme;
final dark = Theme.of(context).brightness == Brightness.dark;
final rtl = Directionality.of(context) == TextDirection.rtl;
final anchor = widget.anchor;
final depths = widget.depths;
return LayoutBuilder(
builder: (context, constraints) {
final size = constraints.biggest;
final count = motion.count;
final count = widget.motion.count;
final height = (count - 1) * kChatStackPitch + kChatStackSlot;
final top = media.padding.top + 8;
final available = anchor.bottom - top;
@@ -80,39 +179,70 @@ class ChatStackColumn extends StatelessWidget {
final width = size.width - endInset - margin;
final labelMax = width - kChatStackSlot - kChatStackLabelGap;
// Paint the pile's order while it opens: hidden layers first, the
// top of the pile last.
// top of the pile last; a lifted chat above them all.
final order = [for (var i = 0; i < count; i++) i]
..sort((a, b) => (depths[b] ?? 99).compareTo(depths[a] ?? 99));
final items = Stack(
clipBehavior: Clip.none,
children: [
for (final i in order) ...[
_label(context, i, labelMax),
_item(context, i),
],
],
final lifted = _entries.indexWhere((e) => e.paneId == _drag.lifted);
if (lifted >= 0) order.add(order.removeAt(order.indexOf(lifted)));
// The remove target: centred in the band below the stack's slot
// (over the composer), clear of the safe area.
const r = kChatStackRemoveSize / 2;
final floor = size.height - media.padding.bottom;
final removeCenter = Offset(
size.width / 2,
math.max(
anchor.bottom + 4,
math.min((anchor.bottom + floor) / 2, floor - r - 8),
),
);
_removeCenter = removeCenter;
final items = ChatStackColumnItems(
motion: widget.motion,
entries: _entries,
order: order,
labelMax: labelMax,
drag: _drag,
draggable: _draggable,
onOpen: widget.onOpen,
onAdd: widget.onAdd,
onRemove: widget.onRemove,
onReorder: widget.onReorder,
onDragStart: _start,
onDragMove: _move,
onDragEnd: _end,
);
return CallbackShortcuts(
bindings: {
const SingleActivator(LogicalKeyboardKey.escape): onDismiss,
},
bindings: {const SingleActivator(LogicalKeyboardKey.escape): _escape},
child: FocusScope(
autofocus: true,
child: IgnorePointer(
ignoring: !interactive,
ignoring: !widget.interactive,
child: Stack(
children: [
Positioned.fill(
child: FadeTransition(
opacity: motion.scrim,
opacity: widget.motion.scrim,
child: ModalBarrier(
color: scheme.scrim.withValues(alpha: dark ? .5 : .32),
dismissible: true,
onDismiss: onDismiss,
onDismiss: widget.onDismiss,
semanticsLabel: 'Close chats',
),
),
),
// Under the column: a chat dragged onto it is drawn over it.
if (widget.onRemove != null)
Positioned(
left: removeCenter.dx - r,
top: removeCenter.dy - r,
width: kChatStackRemoveSize,
height: kChatStackRemoveSize,
child: ChatStackRemoveTarget(
shown: _drag.removeShown,
hover: _drag.hover,
reduced: _drag.reduced,
),
),
PositionedDirectional(
end: endInset,
bottom: size.height - anchor.bottom,
@@ -144,169 +274,4 @@ class ChatStackColumn extends StatelessWidget {
},
);
}
Widget _item(BuildContext context, int i) {
final entry = i < entries.length ? entries[i] : null;
return PositionedDirectional(
key: ValueKey('chat-stack-item-${entry?.paneId ?? 'add'}'),
end: 0,
bottom: 0,
// The transform is outermost: the boxes under it 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: FadeTransition(
opacity: motion.fade[i],
child: KeyedSubtree(
key: ValueKey('chat-stack-button-${entry?.paneId ?? 'add'}'),
child: entry == null ? _addButton(context) : _chatButton(entry),
),
),
),
);
}
Widget _label(BuildContext context, int i, double maxWidth) {
final entry = i < entries.length ? entries[i] : null;
return PositionedDirectional(
key: ValueKey('chat-stack-label-${entry?.paneId ?? 'add'}'),
end: kChatStackSlot + kChatStackLabelGap,
bottom: 0,
height: kChatStackSlot,
child: ChatStackPoseTransition(
progress: motion.label[i],
from: motion.labelFrom[i],
to: motion.target[i],
child: ExcludeSemantics(
child: FadeTransition(
opacity: motion.label[i],
child: Align(
alignment: AlignmentDirectional.centerEnd,
widthFactor: 1,
child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: maxWidth),
child: GestureDetector(
onTap: entry == null ? onAdd : () => onOpen(entry),
onLongPress: entry == null || onRemove == null
? null
: () => onRemove!(entry),
child: ChatStackLabel(
text: entry?.title ?? addLabel,
color: entry?.color(context),
emphasized: entry?.current ?? false,
),
),
),
),
),
),
),
);
}
Widget _chatButton(ChatStackEntry entry) => Builder(
builder: (context) {
final remove = onRemove;
final ring = entry.current
? entry.color(context) ?? Theme.of(context).colorScheme.primary
: null;
return Semantics(
container: true,
button: true,
selected: entry.current,
label: entry.title,
hint: entry.current ? 'Open now' : null,
onTap: () => onOpen(entry),
customSemanticsActions: remove == null
? null
: {
const CustomSemanticsAction(label: removeLabel): () =>
remove(entry),
},
child: _Tappable(
autofocus: entry.current,
onTap: () => onOpen(entry),
onLongPress: remove == null ? null : () => remove(entry),
child: ChatStackDisc(ring: ring, child: chatStackAvatar(entry)),
),
);
},
);
Widget _addButton(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
final tokens = AppDesignTokens.of(context);
return Semantics(
container: true,
button: true,
label: addLabel,
onTap: onAdd,
child: _Tappable(
onTap: onAdd,
child: ChatStackDisc(
child: DecoratedBox(
decoration: ShapeDecoration(
color: tokens.flat ? null : scheme.primaryContainer,
shape: chatStackShape(tokens).copyWith(
side: tokens.flat
? BorderSide(color: scheme.primary.withValues(alpha: .55))
: BorderSide.none,
),
),
child: Icon(
Icons.add,
color: tokens.flat ? scheme.primary : scheme.onPrimaryContainer,
),
),
),
),
);
}
}
/// A disc's ink and focus: the splash is drawn over the disc (the disc is
/// opaque, so ink under it would never show), in the design's shape.
class _Tappable extends StatelessWidget {
const _Tappable({
required this.child,
required this.onTap,
this.onLongPress,
this.autofocus = false,
});
final Widget child;
final VoidCallback onTap;
final VoidCallback? onLongPress;
final bool autofocus;
@override
Widget build(BuildContext context) {
final shape = chatStackShape(AppDesignTokens.of(context));
return Stack(
children: [
child,
Positioned.fill(
child: Padding(
padding: const EdgeInsets.all(
(kChatStackSlot - kChatStackDisc) / 2,
),
child: Material(
type: MaterialType.transparency,
shape: shape,
clipBehavior: Clip.antiAlias,
child: InkWell(
autofocus: autofocus,
excludeFromSemantics: true,
customBorder: shape,
onTap: onTap,
onLongPress: onLongPress,
),
),
),
),
],
);
}
}