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,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);
});
}