Files
pipelines/app/lib/features/chat/stack/chat_stack_column.dart
T
alan 130c1e6708 Chat stack: the default, also on a wide phone or tablet; a close button where the pile was
Chat stack is now the default phone chat navigation (a stored choice is
kept), and on Android or iOS it stays on a wide layout (an unfolded
foldable) above the active pane, where floating is ignored too. When
expanded, the pile turns into a close button at its own spot and the
column rises above it; while a chat is dragged that button is the remove
target, replacing the separate x at the bottom. Chat widget tests start
on Quick switch (they run as Android); the stack's tests choose it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 21:47:57 +00:00

276 lines
9.8 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.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_close_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
/// small pill beside it, and Add chat at the top.
///
/// The stack's own slot turns into a close button ([ChatStackCloseTarget]);
/// item 0 sits a pitch above it, nearest the thumb. The chats
/// keep the open-chat order (the order the server syncs), so a chat keeps
/// its place in the column from one visit to the next; the chat on screen
/// wears a ring in its colour. Add chat is last, at the top: the reachable
/// 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 close button (armed as a remove target while dragging)
/// 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 `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,
required this.anchor,
required this.entries,
required this.depths,
required this.interactive,
required this.onOpen,
required this.onAdd,
required this.onDismiss,
this.onRemove,
this.onReorder,
});
final ChatStackMotion motion;
/// The collapsed stack's slot in this overlay's coordinates.
final Rect anchor;
final List<ChatStackEntry> entries;
/// Each item's layer in the collapsed pile (null when it is not drawn
/// there); the add item last.
final List<int?> depths;
/// False while folding back: taps fall through to nothing.
final bool interactive;
final ValueChanged<ChatStackEntry> onOpen;
final VoidCallback onAdd;
final VoidCallback onDismiss;
/// 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 = widget.motion.count;
// The stack's own slot (the close button) and the items above it.
final height = count * kChatStackPitch + kChatStackSlot;
final top = media.padding.top + 8;
final available = anchor.bottom - top;
final fits = height <= available;
// The column's own edge is the stack's; the labels take the rest of
// the row back to a margin at the other side.
final endInset = rtl ? anchor.left : size.width - anchor.right;
const margin = 16.0;
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; 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 lifted = _entries.indexWhere((e) => e.paneId == _drag.lifted);
if (lifted >= 0) order.add(order.removeAt(order.indexOf(lifted)));
// The close button takes the pile's place; while a chat is dragged
// it is the remove target.
_removeCenter = anchor.center;
final close = Positioned.fromRect(
key: const ValueKey('chat-stack-close'),
rect: anchor,
child: IgnorePointer(
ignoring: _drag.lifted != null,
child: ChatStackCloseTarget(
open: widget.motion.close,
armed: _drag.removeShown,
hover: _drag.hover,
reduced: _drag.reduced,
onClose: widget.onDismiss,
),
),
);
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): _escape},
child: FocusScope(
autofocus: true,
child: IgnorePointer(
ignoring: !widget.interactive,
child: Stack(
children: [
Positioned.fill(
child: FadeTransition(
opacity: widget.motion.scrim,
child: ModalBarrier(
color: scheme.scrim.withValues(alpha: dark ? .5 : .32),
dismissible: true,
onDismiss: widget.onDismiss,
semanticsLabel: 'Dismiss',
),
),
),
// A dragged chat is drawn over the button (it shrinks into
// it); otherwise the button is on top, to take its tap.
if (_drag.lifted != null) close,
PositionedDirectional(
key: const ValueKey('chat-stack-column'),
end: endInset,
bottom: size.height - anchor.bottom,
top: top,
width: width,
child: Align(
alignment: AlignmentDirectional.bottomEnd,
child: SingleChildScrollView(
reverse: true,
clipBehavior: fits ? Clip.none : Clip.hardEdge,
child: SizedBox(
width: width,
height: height,
child: Semantics(
container: true,
explicitChildNodes: true,
label: 'Open chats',
child: items,
),
),
),
),
),
if (_drag.lifted == null) close,
],
),
),
),
);
},
);
}
}