Float one chat pane over the app and toggle it with Ctrl+Y / Cmd+Y

This commit is contained in:
2026-11-10 12:56:39 +00:00
parent 19aa726e7f
commit bb669aface
18 changed files with 1957 additions and 56 deletions
@@ -0,0 +1,187 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.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/workspace/chat_float_store.dart';
import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart';
import 'package:pipelines_app/features/chat/workspace/chat_workspace_store.dart';
import 'conversation/chat_thread_fixtures.dart';
class _Gateway extends FakeChatGateway {
@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']) {'id': id, 'title': 'Chat $id'},
],
});
}
if (type == 'chat_shells') return Future.value({'chat_shells': []});
return super.call(type, fields);
}
}
Finder _thread(String id) => find.byWidgetPredicate(
(widget) => widget is ChatThreadView && widget.sessionId == id,
skipOffstage: false,
);
Finder _input(String id) => find.descendant(
of: find.byKey(ValueKey('chat-composer-$id'), skipOffstage: false),
matching: find.byType(EditableText, skipOffstage: false),
skipOffstage: false,
);
ChatWorkspaceController _workspace(WidgetTester tester) =>
tester.widget<ChatWorkspace>(find.byType(ChatWorkspace)).controller;
Future<_Gateway> _mount(
WidgetTester tester, {
Size size = const Size(1800, 1000),
}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final gateway = _Gateway()..history = (_) async => chatPage([]);
bindServerIdentity(gateway, 'server-a');
addTearDown(gateway.close);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: ChatScreen(gateway: gateway, persistWorkspace: true),
),
),
);
await tester.pumpAndSettle();
return gateway;
}
/// Opens [session]'s pane menu (wherever it is) and chooses [label].
Future<void> _menu(WidgetTester tester, String session, String label) async {
await tester.tap(
find
.descendant(
of: _thread(session),
matching: find.byTooltip('More actions'),
)
.hitTestable(),
);
await tester.pumpAndSettle();
await tester.tap(find.text(label).last);
await tester.pumpAndSettle();
}
Future<void> _saved(String a, String b) async {
final layout = ChatWorkspaceController()
..open(a)
..splitActive(b, ChatSplitAxis.horizontal);
SharedPreferences.setMockInitialValues({
scopedServerKey(chatWorkspacePreferenceKey, 'server-a'): layout
.snapshot()!
.encode(),
});
}
void main() {
testWidgets('Float and Dock move the live chat with its draft', (
tester,
) async {
await _saved('a', 'b');
final gateway = await _mount(tester);
final b = tester.state(_thread('b'));
await tester.enterText(_input('b'), 'Draft B');
await _menu(tester, 'b', 'Float (Ctrl+Y)');
final controller = _workspace(tester);
expect(controller.floatingPane?.sessionId, 'b');
expect(controller.panes.map((p) => p.sessionId), ['a']);
expect(find.byKey(const ValueKey('chat-float-title')), findsOneWidget);
expect(tester.state(_thread('b')), same(b));
expect(tester.widget<EditableText>(_input('b')).controller.text, 'Draft B');
// The window floats over the app, not inside a pane.
final window = tester.getRect(
find.byKey(const ValueKey('chat-float-title')),
);
expect(window.width, lessThan(1800 / 2));
// The lone tiled pane can still float, swapping with the floating one.
await tester.tap(
find
.descendant(
of: _thread('a'),
matching: find.byTooltip('More actions'),
)
.hitTestable(),
);
await tester.pumpAndSettle();
expect(find.text('Float (Ctrl+Y)'), findsOneWidget);
await tester.tapAt(const Offset(5, 995));
await tester.pumpAndSettle();
await _menu(tester, 'b', 'Dock');
expect(controller.floatingPane, isNull);
expect(controller.panes.map((p) => p.sessionId), ['a', 'b']);
expect(tester.state(_thread('b')), same(b));
expect(tester.widget<EditableText>(_input('b')).controller.text, 'Draft B');
expect(
gateway.calls.where(
(call) => ['chat_close', 'chat_interrupt'].contains(call.type),
),
isEmpty,
);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('the floating chat and its frame survive a reload', (
tester,
) async {
await _saved('a', 'b');
await _mount(tester);
await _menu(tester, 'b', 'Float (Ctrl+Y)');
await tester.drag(
find.byKey(const ValueKey('chat-float-title')),
const Offset(200, 40),
);
await tester.pumpAndSettle();
await _menu(tester, 'b', 'Hide floating chat (Ctrl+Y)');
expect(find.byKey(const ValueKey('chat-float-title')), findsNothing);
await tester.pump(const Duration(seconds: 1));
final preferences = await SharedPreferences.getInstance();
final record = ChatFloatRecord.decode(
preferences.getString(
scopedServerKey(chatFloatPreferenceKey, 'server-a'),
)!,
)!;
expect(record.pane?.sessionId, 'b');
expect(record.shown, isFalse);
expect(record.rect.left, chatFloatDefaultRect.left + 200);
await tester.pumpWidget(const SizedBox.shrink());
await _mount(tester);
final controller = _workspace(tester);
expect(controller.floatingPane?.sessionId, 'b');
expect(controller.floatShown, isFalse);
expect(controller.panes.map((p) => p.sessionId), ['a']);
controller.setFloatShown(true);
await tester.pumpAndSettle();
expect(
tester.getTopLeft(find.byKey(const ValueKey('chat-float-title'))).dx,
greaterThanOrEqualTo(chatFloatDefaultRect.left + 200),
);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('narrow layouts offer no Float', (tester) async {
await _saved('a', 'b');
await _mount(tester, size: const Size(430, 900));
final finder = find.byTooltip('More actions').hitTestable();
await tester.tap(finder.first);
await tester.pumpAndSettle();
expect(find.textContaining('Float'), findsNothing);
expect(find.text('Open chats (2)'), findsOneWidget);
await tester.pumpWidget(const SizedBox.shrink());
});
}
@@ -0,0 +1,229 @@
import 'package:flutter/foundation.dart';
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_float_binding.dart';
import 'package:pipelines_app/features/chat/workspace/chat_float_shortcut.dart';
import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart';
import 'package:pipelines_app/ui/input/keyboard_owner.dart';
class _Pane extends StatefulWidget {
const _Pane(this.id);
final String id;
@override
State<_Pane> createState() => _PaneState();
}
class _PaneState extends State<_Pane> {
final text = TextEditingController();
final focus = FocusNode();
@override
void dispose() {
text.dispose();
focus.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => Material(
child: TextField(
key: ValueKey('field-${widget.id}'),
controller: text,
focusNode: focus,
),
);
}
Finder _pane(String id) => find.byWidgetPredicate(
(w) => w is _Pane && w.id == id,
skipOffstage: false,
);
class _Harness {
final controller = ChatWorkspaceController()
..open('a')
..splitActive('b', ChatSplitAxis.horizontal);
final frame = ChatFloatFrame();
final typing = FocusNode(debugLabel: 'terminal');
final typed = <LogicalKeyboardKey>[];
Widget app({bool narrow = false}) => MaterialApp(
home: Scaffold(
body: Column(
children: [
Expanded(
child: ChatWorkspace(
controller: controller,
floatFrame: frame,
singlePane: narrow,
onFocusPane: (pane) => _focus(pane.sessionId),
paneBuilder: (context, pane, active) => _Pane(pane.sessionId),
),
),
TypingSurface(
child: Focus(
focusNode: typing,
onKeyEvent: (node, event) {
if (event is KeyDownEvent &&
event.logicalKey == LogicalKeyboardKey.keyY) {
typed.add(event.logicalKey);
}
return KeyEventResult.handled;
},
child: const SizedBox(height: 40, child: Text('terminal')),
),
),
],
),
),
);
WidgetTester? tester;
void _focus(String session) =>
tester!.state<_PaneState>(_pane(session)).focus.requestFocus();
void dispose() {
controller.dispose();
frame.dispose();
typing.dispose();
}
}
Future<void> _chord(
WidgetTester tester,
LogicalKeyboardKey modifier, {
LogicalKeyboardKey key = LogicalKeyboardKey.keyY,
}) async {
await tester.sendKeyDownEvent(modifier);
await tester.sendKeyEvent(key);
await tester.sendKeyUpEvent(modifier);
await tester.pumpAndSettle();
}
void main() {
test('the chord is Ctrl+Y, and Cmd+Y on Apple platforms', () {
expect(chatFloatShortcutLabel(TargetPlatform.linux), 'Ctrl+Y');
expect(chatFloatShortcutLabel(TargetPlatform.windows), 'Ctrl+Y');
expect(chatFloatShortcutLabel(TargetPlatform.macOS), 'Cmd+Y');
});
Future<_Harness> pump(WidgetTester tester, {bool narrow = false}) async {
tester.view.physicalSize = const Size(1280, 800);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final h = _Harness()..tester = tester;
addTearDown(h.dispose);
await tester.pumpWidget(h.app(narrow: narrow));
await tester.pumpAndSettle();
return h;
}
testWidgets('Ctrl+Y floats the focused pane, then hides and shows it', (
tester,
) async {
final h = await pump(tester);
final b = tester.state<_PaneState>(_pane('b'));
await tester.enterText(find.byKey(const ValueKey('field-b')), 'draft');
await tester.tap(find.byKey(const ValueKey('field-a')));
await tester.pump();
// Plain Y and Shift+Y type; only the chord acts.
await tester.sendKeyEvent(LogicalKeyboardKey.keyY);
await _chord(tester, LogicalKeyboardKey.shiftLeft);
expect(h.controller.floatingPane, isNull);
tester.state<_PaneState>(_pane('b')).focus.requestFocus();
await tester.pump();
await _chord(tester, LogicalKeyboardKey.controlLeft);
expect(h.controller.floatingPane?.sessionId, 'b');
expect(h.controller.paneCount, 1);
expect(find.byKey(const ValueKey('chat-float-title')), findsOneWidget);
// The chat moved: same State, same draft, its field has the keyboard.
expect(tester.state<_PaneState>(_pane('b')), same(b));
expect(b.text.text, 'draft');
expect(b.focus.hasFocus, isTrue);
await _chord(tester, LogicalKeyboardKey.controlLeft);
expect(h.controller.floatShown, isFalse);
expect(find.byKey(const ValueKey('chat-float-title')), findsNothing);
expect(tester.state<_PaneState>(_pane('b')), same(b));
// Showing focuses the floating chat; hiding gives the keyboard back.
final a = tester.state<_PaneState>(_pane('a'));
a.focus.requestFocus();
await tester.pump();
await _chord(tester, LogicalKeyboardKey.controlLeft);
expect(h.controller.floatShown, isTrue);
expect(b.focus.hasFocus, isTrue);
await _chord(tester, LogicalKeyboardKey.controlLeft);
expect(a.focus.hasFocus, isTrue);
// Docking returns the same chat to the tiling.
h.controller.dock();
await tester.pumpAndSettle();
expect(h.controller.paneCount, 2);
expect(tester.state<_PaneState>(_pane('b')), same(b));
expect(b.text.text, 'draft');
expect(tester.takeException(), isNull);
});
testWidgets('in a terminal, Ctrl+Y acts only when a chat floats', (
tester,
) async {
final h = await pump(tester);
h.typing.requestFocus();
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.keyY);
await _chord(tester, LogicalKeyboardKey.controlLeft);
// Nothing to float from the terminal: the shell gets Ctrl+Y (yank).
expect(h.controller.floatingPane, isNull);
expect(h.typed, contains(LogicalKeyboardKey.keyY));
expect(h.typed.where((k) => k == LogicalKeyboardKey.keyY).length, 2);
h.controller.float(h.controller.panes.last.id);
await tester.pumpAndSettle();
h.typing.requestFocus();
await tester.pump();
h.typed.clear();
await _chord(tester, LogicalKeyboardKey.controlLeft);
expect(h.controller.floatShown, isFalse);
expect(h.typed, isNot(contains(LogicalKeyboardKey.keyY)));
await tester.sendKeyEvent(LogicalKeyboardKey.keyY);
expect(h.typed, [LogicalKeyboardKey.keyY]);
});
testWidgets(
'on macOS Cmd+Y toggles and Ctrl+Y in a terminal stays with it',
(tester) async {
final h = await pump(tester);
h.controller.float(h.controller.panes.last.id);
await tester.pumpAndSettle();
h.typing.requestFocus();
await tester.pump();
await _chord(tester, LogicalKeyboardKey.controlLeft);
expect(h.controller.floatShown, isTrue);
expect(h.typed, [LogicalKeyboardKey.keyY]);
await _chord(tester, LogicalKeyboardKey.metaLeft);
expect(h.controller.floatShown, isFalse);
await _chord(tester, LogicalKeyboardKey.metaLeft);
expect(h.controller.floatShown, isTrue);
},
variant: const TargetPlatformVariant({TargetPlatform.macOS}),
);
testWidgets('narrow layouts never show the window or take the chord', (
tester,
) async {
final h = await pump(tester, narrow: true);
tester.state<_PaneState>(_pane('b')).focus.requestFocus();
await tester.pump();
await _chord(tester, LogicalKeyboardKey.controlLeft);
expect(h.controller.floatingPane, isNull);
// A float saved on a wide window stays, unpainted, with its state.
h.controller.float(h.controller.panes.last.id);
await tester.pumpAndSettle();
expect(find.byKey(const ValueKey('chat-float-title')), findsNothing);
expect(_pane('b'), findsOneWidget);
await _chord(tester, LogicalKeyboardKey.controlLeft);
expect(h.controller.floatShown, isTrue);
});
}
@@ -0,0 +1,191 @@
import 'dart:ui';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/workspace/chat_float_binding.dart';
import 'package:pipelines_app/features/chat/workspace/chat_float_store.dart';
import 'package:pipelines_app/features/chat/workspace/chat_workspace_controller.dart';
ChatWorkspaceController _three() => ChatWorkspaceController()
..open('a')
..splitActive('b', ChatSplitAxis.horizontal)
..splitActive('c', ChatSplitAxis.vertical);
List<String> _sessions(ChatWorkspaceController c) =>
c.panes.map((p) => p.sessionId).toList();
class _Store implements ChatFloatStore {
_Store([this.record]);
ChatFloatRecord? record;
int writes = 0;
@override
Future<ChatFloatRecord?> read() async => record;
@override
Future<bool> write(ChatFloatRecord value) async {
writes++;
record = value;
return true;
}
}
void main() {
test('float takes a pane out of the tiling and keeps its id', () {
final c = _three();
final b = c.panes.firstWhere((p) => p.sessionId == 'b');
c.activate(b.id);
c.float(b.id);
expect(c.floatingPane?.id, b.id);
expect(c.floatShown, isTrue);
expect(_sessions(c), ['a', 'c']);
expect(c.activePane?.sessionId, isNot('b'));
expect(c.holdsSession('b'), isTrue);
});
test('a lone pane cannot float; the tiling never empties', () {
final c = ChatWorkspaceController()..open('a');
expect(c.canFloat(c.panes.single.id), isFalse);
c.float(c.panes.single.id);
expect(c.floatingPane, isNull);
c.splitActive('b', ChatSplitAxis.horizontal);
final b = c.activePane!;
c.float(b.id);
// Closing the last tiled pane docks the floating chat in its place.
c.close(c.panes.single.id);
expect(c.floatingPane, isNull);
expect(c.panes.single.id, b.id);
expect(c.activePaneId, b.id);
});
test('floating another chat swaps it with the floating one', () {
final c = _three();
final a = c.panes.firstWhere((p) => p.sessionId == 'a');
final c3 = c.panes.firstWhere((p) => p.sessionId == 'c');
c.float(a.id);
c.setFloatShown(false);
c.float(c3.id);
expect(c.floatingPane?.id, c3.id);
expect(c.floatShown, isTrue);
expect(_sessions(c), ['b', 'a']);
expect(c.panes.last.id, a.id);
});
test('dock puts the chat right of the active pane and activates it', () {
final c = _three();
final b = c.panes.firstWhere((p) => p.sessionId == 'b');
c.float(b.id);
final active = c.activePane!;
c.dock();
expect(c.floatingPane, isNull);
expect(c.activePaneId, b.id);
final panes = c.panes.map((p) => p.id).toList();
expect(panes.indexOf(b.id), panes.indexOf(active.id) + 1);
expect(_sessions(c).toSet(), {'a', 'b', 'c'});
});
test('hide, show, reopen, close and /new follow the floating chat', () {
final c = _three();
final b = c.panes.firstWhere((p) => p.sessionId == 'b');
c.float(b.id);
c.toggleFloatShown();
expect(c.floatShown, isFalse);
c.open('b');
expect(c.floatShown, isTrue);
expect(_sessions(c), ['a', 'c']);
c.splitActive('b', ChatSplitAxis.vertical);
expect(c.paneCount, 2);
c.replaceSession('b', 'b2');
expect(c.floatingPane?.id, b.id);
expect(c.floatingPane?.sessionId, 'b2');
c.close(b.id);
expect(c.floatingPane, isNull);
expect(c.paneCount, 2);
});
test('restoreFloat skips open sessions and taken ids', () {
final c = ChatWorkspaceController()
..open('a')
..splitActive('b', ChatSplitAxis.horizontal);
final taken = c.panes.first.id;
expect(
c.restoreFloat(const ChatWorkspacePane('x', 'a'), shown: true),
isFalse,
);
expect(c.restoreFloat(ChatWorkspacePane(taken, 'z'), shown: true), isFalse);
expect(
c.restoreFloat(const ChatWorkspacePane('x', 'z'), shown: false),
isTrue,
);
expect(c.floatingPane?.sessionId, 'z');
expect(c.floatShown, isFalse);
// A new split id never collides with the floating pane's.
c.splitActive('d', ChatSplitAxis.vertical);
expect(c.panes.map((p) => p.id), isNot(contains('x')));
});
test('a restored layout drops any floating chat', () {
final c = _three();
c.float(c.panes.first.id);
final snapshot = c.snapshot()!;
c.restoreLayout(snapshot);
expect(c.floatingPane, isNull);
});
test('records round-trip and reject malformed input', () {
const record = ChatFloatRecord(
pane: ChatWorkspacePane('p1', 'server:abc'),
shown: true,
rect: Rect.fromLTWH(10, 20, 400, 300),
);
expect(ChatFloatRecord.decode(record.encode()), record);
const none = ChatFloatRecord(rect: chatFloatDefaultRect);
expect(ChatFloatRecord.decode(none.encode()), none);
for (final bad in [
'',
'[]',
'{"version":2,"rect":[0,0,1,1]}',
'{"version":1,"rect":[0,0,-1,1]}',
'{"version":1,"rect":[0,0,1]}',
'{"version":1,"rect":[0,0,1,1],"pane":{"id":" x","session_id":"a"}}',
'{"version":1,"rect":[0,0,1,1],"pane":7}',
]) {
expect(ChatFloatRecord.decode(bad), isNull, reason: bad);
}
});
test('binding restores after the layout and saves changes', () async {
final c = ChatWorkspaceController()
..open('a')
..splitActive('b', ChatSplitAxis.horizontal);
final store = _Store(
const ChatFloatRecord(
pane: ChatWorkspacePane('f', 'c'),
shown: true,
rect: Rect.fromLTWH(5, 6, 400, 300),
),
);
final frame = ChatFloatFrame();
final binding = ChatFloatBinding(
controller: c,
frame: frame,
store: store,
debounce: Duration.zero,
);
await binding.start();
expect(c.floatingPane?.sessionId, 'c');
expect(c.floatShown, isTrue);
expect(frame.rect, const Rect.fromLTWH(5, 6, 400, 300));
expect(store.writes, 0);
c.setFloatShown(false);
await Future<void>.delayed(Duration.zero);
await binding.flush();
expect(store.record?.shown, isFalse);
frame.move(const Rect.fromLTWH(50, 60, 400, 300));
final writes = store.writes;
await binding.flush();
expect(store.writes, writes + 1);
expect(store.record?.rect.left, 50);
c.dock();
binding.dispose();
await Future<void>.delayed(Duration.zero);
expect(store.record?.pane, isNull);
});
}
@@ -0,0 +1,80 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/ui/floating/floating_layer.dart';
void main() {
testWidgets('the layer last pressed or focused paints on top', (
tester,
) async {
tester.view.physicalSize = const Size(800, 600);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final hits = <String>[];
final focus = FocusNode();
addTearDown(focus.dispose);
Widget window(String name, double left, {FocusNode? node}) => Positioned(
left: left,
top: 100,
width: 200,
height: 200,
child: Focus(
focusNode: node,
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () => hits.add(name),
child: const SizedBox.expand(),
),
),
);
var behind = 0;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Stack(
children: [
Positioned.fill(
child: GestureDetector(
onTap: () => behind++,
child: const ColoredBox(color: Colors.white),
),
),
// Nested inside a page, like the chat workspace.
Positioned.fill(
child: FloatingLayer(
coverOverlay: true,
builder: (context, bounds) => [
window('chat', 100, node: focus),
],
),
),
Positioned.fill(
child: FloatingLayer(
builder: (context, bounds) => [window('terminal', 200)],
),
),
],
),
),
),
);
const overlap = Offset(250, 200);
await tester.tapAt(overlap);
expect(hits.last, 'terminal');
await tester.tapAt(const Offset(120, 200));
await tester.pump();
await tester.tapAt(overlap);
expect(hits.last, 'chat');
await tester.tapAt(const Offset(380, 200));
await tester.pump();
await tester.tapAt(overlap);
expect(hits.last, 'terminal');
focus.requestFocus();
await tester.pump();
await tester.pump();
await tester.tapAt(overlap);
expect(hits.last, 'chat');
// Space between windows passes through to the page.
await tester.tapAt(const Offset(700, 500));
expect(behind, 1);
});
}