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,323 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/core/servers/server_scope.dart';
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
import 'package:pipelines_app/features/chat/screen.dart';
import 'package:pipelines_app/features/chat/stack/chat_stack_column.dart';
import 'package:pipelines_app/features/chat/stack/chat_stack_disc.dart';
import 'package:pipelines_app/features/chat/stack/chat_stack_drag.dart';
import 'package:pipelines_app/features/chat/stack/chat_stack_remove_target.dart';
import 'package:pipelines_app/features/chat/stack/mobile_chat_navigation.dart';
import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart';
import 'package:pipelines_app/shell/adaptive_shell.dart';
import 'package:pipelines_app/shell/destination.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../conversation/chat_thread_fixtures.dart';
/// A chat server keeping the open-chat set, as the sync test's does.
class _Server extends FakeChatGateway {
List<String> open = ['a', 'b', 'c'];
int revision = 1;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
if (type == 'chat_sessions') {
return Future.value({
'chat_sessions': [
for (final id in ['a', 'b', 'c', 'd'])
{'id': id, 'title': 'Chat $id', 'directory': '/w'},
],
});
}
if (type == 'chat_shells') return Future.value({'chat_shells': []});
if (type == 'chat_open_workspace') {
final request = fields['chat_open_workspace_request'] as Map?;
var rejected = false;
if (request != null && request['seed'] != true) {
if (request['base_revision'] != revision) {
rejected = true;
} else {
open = [
for (final e in (request['entries'] as List).cast<Map>())
'${e['session_id']}',
];
revision++;
}
}
return Future.value({
'type': 'chat_open_workspace',
'chat_open_workspace': {
'revision': revision,
'entries': [
for (final id in open) {'session_id': id},
],
if (rejected) 'rejected': true,
},
});
}
return super.call(type, fields);
}
}
Future<_Server> _pump(WidgetTester tester, {bool reduced = false}) async {
SharedPreferences.setMockInitialValues({});
unawaited(
MobileChatNavigationController.instance.setValue(
MobileChatNavigation.chatStack,
),
);
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final server = _Server()..history = (_) async => chatPage(const []);
bindServerIdentity(server, 'server-stack');
addTearDown(server.close);
await tester.pumpWidget(
MaterialApp(
builder: (context, child) => MediaQuery(
data: MediaQuery.of(context).copyWith(disableAnimations: reduced),
child: child!,
),
home: AdaptiveShell(
destination: AppDestination.chat,
destinationBodies: {
AppDestination.chat: (_) =>
ChatScreen(gateway: server, persistWorkspace: true),
},
),
),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('chat-row-a')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('chat-stack')));
await tester.pumpAndSettle();
return server;
}
ChatWorkspaceController _workspace(WidgetTester tester) =>
tester.widget<ChatWorkspace>(find.byType(ChatWorkspace)).controller;
List<String> _order(WidgetTester tester) => [
for (final pane in _workspace(tester).panes) pane.sessionId,
];
/// Chat [id]'s avatar in the open column.
Finder _button(WidgetTester tester, String id) {
final pane = _workspace(
tester,
).panes.firstWhere((pane) => pane.sessionId == id);
return find.byKey(ValueKey('chat-stack-button-${pane.id}'));
}
final _add = find.byKey(const ValueKey('chat-stack-button-add'));
final _target = find.byType(ChatStackRemoveTarget);
/// Holds [finder] until it lifts.
Future<TestGesture> _lift(WidgetTester tester, Finder finder) async {
final gesture = await tester.startGesture(tester.getCenter(finder));
await tester.pump(chatStackLiftDelay + const Duration(milliseconds: 20));
return gesture;
}
double _shown(WidgetTester tester) =>
tester.widget<ChatStackRemoveTarget>(_target).shown.value;
void main() {
final navigation = MobileChatNavigationController.instance;
tearDown(() {
unawaited(navigation.setValue(MobileChatNavigation.quickSwitch));
});
test('movePane moves a pane and keeps the active one', () {
final controller = ChatWorkspaceController()..open('a');
controller
..splitActive('b', ChatSplitAxis.horizontal)
..splitActive('c', ChatSplitAxis.horizontal);
final active = controller.activePaneId;
final a = controller.panes.first.id;
controller.movePane(a, 2);
expect(controller.panes.map((p) => p.sessionId), ['b', 'c', 'a']);
expect(controller.panes.last.id, a);
expect(controller.activePaneId, active);
controller.dispose();
});
testWidgets('hold and drag reorders, and the order reaches the server', (
tester,
) async {
final server = await _pump(tester);
expect(_order(tester), ['a', 'b', 'c']);
final b = tester.getCenter(_button(tester, 'b'));
final gesture = await _lift(tester, _button(tester, 'a'));
// Lifted: the remove target comes in.
await tester.pumpAndSettle();
expect(_shown(tester), 1);
// Up two slots: b and c slide down to open the gap.
await gesture.moveBy(const Offset(0, -kChatStackPitch * 2));
await tester.pumpAndSettle();
expect(tester.getCenter(_button(tester, 'b')).dy, b.dy + kChatStackPitch);
await gesture.up();
await tester.pumpAndSettle();
expect(_order(tester), ['b', 'c', 'a']);
expect(server.open, ['b', 'c', 'a']);
// Everyone settles in the new slots; the column stays open.
expect(tester.getCenter(_button(tester, 'b')).dy, closeTo(b.dy + 56, .5));
expect(_shown(tester), 0);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('dropping on the × removes; Undo restores the place', (
tester,
) async {
final server = await _pump(tester);
final gesture = await _lift(tester, _button(tester, 'b'));
await tester.pumpAndSettle();
final from = tester.getCenter(_button(tester, 'b'));
await gesture.moveBy(tester.getCenter(_target) - from);
await tester.pumpAndSettle();
final drag = tester.widget<ChatStackRemoveTarget>(_target).hover;
expect(drag.value, 1);
await gesture.up();
await tester.pumpAndSettle();
expect(find.text('Removed Chat b from open chats'), findsOneWidget);
expect(_order(tester), ['a', 'c']);
expect(server.open, ['a', 'c']);
await tester.tap(find.text('Undo'));
await tester.pumpAndSettle();
expect(_order(tester), ['a', 'b', 'c']);
expect(server.open, ['a', 'b', 'c']);
// The chat on screen stays on screen.
expect(_workspace(tester).activePane?.sessionId, 'a');
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('Escape, or a drop away from the column, puts the chat back', (
tester,
) async {
final server = await _pump(tester);
final home = tester.getCenter(_button(tester, 'a'));
var gesture = await _lift(tester, _button(tester, 'a'));
await gesture.moveBy(const Offset(0, -kChatStackPitch * 2));
await tester.pumpAndSettle();
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pumpAndSettle();
await gesture.up();
await tester.pumpAndSettle();
expect(_order(tester), ['a', 'b', 'c']);
expect(tester.getCenter(_button(tester, 'a')), home);
// Escape only cancelled the drag: the column is still open.
expect(find.byType(ChatStackColumn), findsOneWidget);
gesture = await _lift(tester, _button(tester, 'a'));
await gesture.moveBy(const Offset(-200, -kChatStackPitch * 3));
await tester.pumpAndSettle();
await gesture.up();
await tester.pumpAndSettle();
expect(_order(tester), ['a', 'b', 'c']);
expect(server.open, ['a', 'b', 'c']);
expect(tester.getCenter(_button(tester, 'a')), home);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('a tap still opens a chat; Add chat does not lift', (
tester,
) async {
await _pump(tester);
final add = tester.getCenter(_add);
final gesture = await _lift(tester, _add);
await tester.pumpAndSettle();
expect(_shown(tester), 0);
await gesture.moveBy(const Offset(0, kChatStackPitch * 2));
await tester.pumpAndSettle();
expect(tester.getCenter(_add), add);
await gesture.up();
await tester.pumpAndSettle();
expect(_order(tester), ['a', 'b', 'c']);
// A drag that moved off "+" is no tap: the column is still open.
expect(find.byType(ChatStackColumn), findsOneWidget);
await tester.tap(_button(tester, 'c'));
await tester.pumpAndSettle();
expect(
find
.byWidgetPredicate((w) => w is ChatThreadView && w.sessionId == 'c')
.hitTestable(),
findsOneWidget,
);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('Move up, Move down and Remove are semantic actions', (
tester,
) async {
final semantics = tester.ensureSemantics();
final server = await _pump(tester);
void act(String id, String label) {
final node = tester.getSemantics(_button(tester, id));
final action = node
.getSemanticsData()
.customSemanticsActionIds!
.map(CustomSemanticsAction.getAction)
.firstWhere((action) => action!.label == label)!;
tester.binding.renderViews.first.owner!.semanticsOwner!.performAction(
node.id,
SemanticsAction.customAction,
CustomSemanticsAction.getIdentifier(action),
);
}
// The bottom chat cannot move down; the top one cannot move up.
final bottom = tester.getSemantics(_button(tester, 'a'));
expect(
bottom.getSemanticsData().customSemanticsActionIds!.map(
(id) => CustomSemanticsAction.getAction(id)!.label,
),
unorderedEquals([
ChatStackColumn.moveUpLabel,
ChatStackColumn.removeLabel,
]),
);
act('a', ChatStackColumn.moveUpLabel);
await tester.pumpAndSettle();
expect(server.open, ['b', 'a', 'c']);
act('c', ChatStackColumn.moveDownLabel);
await tester.pumpAndSettle();
expect(server.open, ['b', 'c', 'a']);
act('b', ChatStackColumn.removeLabel);
await tester.pumpAndSettle();
expect(server.open, ['c', 'a']);
expect(find.text('Removed Chat b from open chats'), findsOneWidget);
semantics.dispose();
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('reduced motion: neighbours swap at once, the × only fades', (
tester,
) async {
await _pump(tester, reduced: true);
final b = tester.getCenter(_button(tester, 'b'));
final gesture = await _lift(tester, _button(tester, 'a'));
expect(_shown(tester), 1);
await gesture.moveBy(const Offset(0, -kChatStackPitch));
await tester.pump();
expect(tester.getCenter(_button(tester, 'b')).dy, b.dy + kChatStackPitch);
// No slide: the target sits in place.
expect(
find.descendant(of: _target, matching: find.byType(SlideTransition)),
findsNothing,
);
await gesture.up();
await tester.pump();
expect(_order(tester), ['b', 'a', 'c']);
expect(_shown(tester), 0);
await tester.pumpAndSettle();
await tester.pumpWidget(const SizedBox.shrink());
});
}