Add resizable split chat workspace primitives

This commit is contained in:
2026-11-06 18:01:14 +00:00
parent 51ee950a5b
commit dfcdf1c154
6 changed files with 937 additions and 0 deletions
@@ -0,0 +1,308 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart';
import 'package:pipelines_app/features/chat/workspace/chat_workspace_divider.dart';
import 'package:pipelines_app/features/chat/workspace/chat_workspace_geometry.dart';
Widget host(
ChatWorkspaceController controller, {
double width = 1000,
double height = 800,
ValueChanged<ChatWorkspacePane>? onFocus,
}) => MaterialApp(
home: Scaffold(
body: Align(
alignment: Alignment.topLeft,
child: SizedBox(
width: width,
height: height,
child: ChatWorkspace(
controller: controller,
onFocusPane: onFocus,
paneBuilder: (_, pane, active) =>
_Draft(key: ValueKey(pane.sessionId), name: pane.sessionId),
),
),
),
),
);
class _Draft extends StatefulWidget {
const _Draft({super.key, required this.name});
final String name;
@override
State<_Draft> createState() => _DraftState();
}
class _DraftState extends State<_Draft> {
final text = TextEditingController();
@override
void dispose() {
text.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) =>
TextField(key: ValueKey('input-${widget.name}'), controller: text);
}
void main() {
test(
'split tree preserves IDs and closes nested siblings without losing layout',
() {
final controller = ChatWorkspaceController()..open('one');
addTearDown(controller.dispose);
final first = controller.activePaneId!;
controller.splitActive('two', ChatSplitAxis.horizontal);
final second = controller.activePaneId!;
controller.splitActive('three', ChatSplitAxis.vertical);
expect(controller.panes.map((p) => p.sessionId), ['one', 'two', 'three']);
expect(controller.panes.first.id, first);
controller.close(second);
expect(controller.panes.map((p) => p.sessionId), ['one', 'three']);
controller.close(first);
expect(controller.root, isA<ChatWorkspacePane>());
controller.close(controller.activePaneId!);
expect(controller.root, isNull);
},
);
test('open activates existing chats and replaces only the active pane', () {
final controller = ChatWorkspaceController()
..open('one')
..splitActive('two', ChatSplitAxis.horizontal);
addTearDown(controller.dispose);
final second = controller.activePaneId;
controller.open('one');
expect(controller.activePane?.sessionId, 'one');
controller.open('three');
expect(controller.panes.map((p) => p.sessionId), ['three', 'two']);
expect(controller.panes.last.id, second);
controller.replaceSession('two', 'new-two');
expect(controller.panes.last.sessionId, 'new-two');
});
test(
'geometry clamps visible sizes but retains ratios and spatial neighbors',
() {
final controller = ChatWorkspaceController()
..open('one')
..splitActive('two', ChatSplitAxis.horizontal);
addTearDown(controller.dispose);
final split = controller.root! as ChatWorkspaceSplit;
controller.resize(split.id, .9);
var geometry = ChatWorkspaceGeometry(
controller.root!,
const Size(700, 600),
);
expect(
geometry.panes.values.every((r) => r.width >= chatPaneMinimumWidth),
isTrue,
);
expect((controller.root! as ChatWorkspaceSplit).ratio, .9);
controller.splitActive('three', ChatSplitAxis.vertical);
geometry = ChatWorkspaceGeometry(controller.root!, const Size(1000, 700));
final one = controller.panes[0],
two = controller.panes[1],
three = controller.panes[2];
expect(geometry.neighbor(two.id, ChatPaneDirection.down), three.id);
expect(geometry.neighbor(three.id, ChatPaneDirection.up), two.id);
expect(geometry.neighbor(one.id, ChatPaneDirection.up), isNull);
expect(
ChatWorkspaceGeometry(controller.root!, const Size(320, 600)).collapsed,
isTrue,
);
},
);
testWidgets(
'both dividers drag, clamp and preserve draft state through nested changes',
(tester) async {
tester.view.physicalSize = const Size(1200, 1000);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final controller = ChatWorkspaceController()
..open('one')
..splitActive('two', ChatSplitAxis.horizontal);
addTearDown(controller.dispose);
await tester.pumpWidget(host(controller));
await tester.enterText(
find.byKey(const ValueKey('input-one')),
'one draft',
);
final state = tester.state(find.byKey(const ValueKey('one')));
final split = controller.root! as ChatWorkspaceSplit;
final divider = find.byType(ChatWorkspaceDivider);
await tester.drag(divider, const Offset(120, 0));
await tester.pump();
expect((controller.root! as ChatWorkspaceSplit).ratio, greaterThan(.5));
controller.activate(controller.panes.last.id);
controller.splitActive('three', ChatSplitAxis.vertical);
await tester.pump();
expect(tester.state(find.byKey(const ValueKey('one'))), same(state));
final vertical = find.byWidgetPredicate(
(w) =>
w is ChatWorkspaceDivider &&
w.geometry.split.axis == ChatSplitAxis.vertical,
);
await tester.drag(vertical, const Offset(0, 80));
await tester.pump();
final nested =
(controller.root! as ChatWorkspaceSplit).second as ChatWorkspaceSplit;
expect(nested.ratio, greaterThan(.5));
await tester.drag(
find.byWidgetPredicate(
(w) => w is ChatWorkspaceDivider && w.geometry.split.id == split.id,
),
const Offset(2000, 0),
);
await tester.pump();
await tester.pump(const Duration(milliseconds: 500));
expect(tester.takeException(), isNull);
controller.close(controller.activePaneId!);
await tester.pump();
expect(tester.state(find.byKey(const ValueKey('one'))), same(state));
expect(
tester
.widget<TextField>(find.byKey(const ValueKey('input-one')))
.controller!
.text,
'one draft',
);
},
);
testWidgets(
'divider keyboard resizing and double-click reset use the same limits',
(tester) async {
final controller = ChatWorkspaceController()
..open('one')
..splitActive('two', ChatSplitAxis.horizontal);
addTearDown(controller.dispose);
await tester.pumpWidget(host(controller, width: 700, height: 500));
final divider = find.byType(ChatWorkspaceDivider);
final gesture = find.descendant(
of: divider,
matching: find.byType(GestureDetector),
);
Focus.of(tester.element(gesture)).requestFocus();
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight);
await tester.pump();
expect((controller.root! as ChatWorkspaceSplit).ratio, greaterThan(.5));
await tester.tap(divider);
await tester.pump(const Duration(milliseconds: 50));
await tester.tap(divider);
await tester.pump(const Duration(milliseconds: 500));
expect((controller.root! as ChatWorkspaceSplit).ratio, .5);
},
);
testWidgets(
'narrow fallback keeps hidden drafts mounted and restores split ratios',
(tester) async {
final controller = ChatWorkspaceController()
..open('one')
..splitActive('two', ChatSplitAxis.horizontal);
addTearDown(controller.dispose);
await tester.pumpWidget(host(controller, width: 700, height: 500));
await tester.enterText(find.byKey(const ValueKey('input-one')), 'draft');
final original = tester.state(find.byKey(const ValueKey('one')));
controller.activate(controller.panes.last.id);
await tester.pumpWidget(host(controller, width: 320, height: 500));
expect(find.byKey(const ValueKey('input-one')), findsNothing);
expect(
find.byKey(const ValueKey('input-one'), skipOffstage: false),
findsOneWidget,
);
await tester.pumpWidget(host(controller, width: 700, height: 500));
expect(tester.state(find.byKey(const ValueKey('one'))), same(original));
expect(
tester
.widget<TextField>(find.byKey(const ValueKey('input-one')))
.controller!
.text,
'draft',
);
},
);
testWidgets(
'Ctrl Shift arrows navigate from a composer and consume edge chords',
(tester) async {
tester.view.physicalSize = const Size(1200, 1000);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final controller = ChatWorkspaceController()
..open('one')
..splitActive('two', ChatSplitAxis.horizontal);
addTearDown(controller.dispose);
final focused = <String>[];
await tester.pumpWidget(
host(controller, onFocus: (pane) => focused.add(pane.sessionId)),
);
await tester.tap(find.byKey(const ValueKey('input-one')));
await tester.pump();
expect(controller.activePane?.sessionId, 'one');
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight);
await tester.pump();
expect(controller.activePane?.sessionId, 'two');
expect(focused, ['two']);
await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight);
await tester.pump();
expect(focused, ['two']);
await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
expect(
tester
.widget<TextField>(find.byKey(const ValueKey('input-one')))
.controller!
.text,
isEmpty,
);
},
);
testWidgets(
'Command Shift arrows navigate on macOS without modifying drafts',
(tester) async {
tester.view.physicalSize = const Size(1200, 1000);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final controller = ChatWorkspaceController()
..open('one')
..splitActive('two', ChatSplitAxis.horizontal);
addTearDown(controller.dispose);
final focused = <String>[];
await tester.pumpWidget(
host(controller, onFocus: (pane) => focused.add(pane.sessionId)),
);
await tester.enterText(
find.byKey(const ValueKey('input-one')),
'original',
);
await tester.sendKeyDownEvent(LogicalKeyboardKey.metaLeft);
await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight);
await tester.pump();
await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft);
await tester.sendKeyUpEvent(LogicalKeyboardKey.metaLeft);
expect(controller.activePane?.sessionId, 'two');
expect(focused, ['two']);
expect(
tester
.widget<TextField>(find.byKey(const ValueKey('input-one')))
.controller!
.text,
'original',
);
},
variant: TargetPlatformVariant.only(TargetPlatform.macOS),
);
}