Host floating chats on the shell window system

Phase 2.3 and 2.4 of docs/plans/floating-windows.md. A floating chat is a
window of the chat kind over the app's WindowManager, built from the
app-lifetime ChatRuntime, so it shows over every destination and at start
without the Chat destination being built. Float and Dock move the same
keyed view between the tiling and the window host. The pipelines app keeps
one runtime and one ChatWindows; saved chatFloat records (v1 and v2) are
brought over once.

ChatFloatHost, ChatFloatWindow, the float frames and binding, the
controller's float list and FloatingFocusRoot are gone; the workspace only
asks whether a chat is out in a window. Every MenuAnchor targets the root
overlay (guarded by a test), and menus and tooltips name the Alt chords.
This commit is contained in:
2026-11-10 21:17:40 +00:00
parent 9695100faa
commit fdc730422d
44 changed files with 1369 additions and 1792 deletions
@@ -281,7 +281,7 @@ void main() {
addTearDown(workspace.dispose);
addTearDown(stage.dispose);
await tester.pumpWidget(host(workspace, stage));
workspace.float(workspace.panes.first.id);
workspace.detach(workspace.panes.first.id);
await tester.pumpAndSettle();
expect(workspace.activePane?.sessionId, 'three');
expect(widthOf(tester, 'three'), 950);
@@ -289,7 +289,7 @@ void main() {
expect(widthOf(tester, 'two'), 1368 - 12 - 950);
expect(find.byType(ChatCenterStageStrip), findsNothing);
workspace.dock(workspace.floatingPanes.single.id);
workspace.dock('one');
await tester.pumpAndSettle();
expect(workspace.activePane?.sessionId, 'one');
expect(widthOf(tester, 'one'), 950);
@@ -1,245 +1,54 @@
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,
const _y = KeyDownEvent(
physicalKey: PhysicalKeyboardKey.keyY,
logicalKey: LogicalKeyboardKey.keyY,
timeStamp: Duration.zero,
);
class _Harness {
final controller = ChatWorkspaceController()
..open('a')
..splitActive('b', ChatSplitAxis.horizontal);
final frames = ChatFloatFrames();
final typing = FocusNode(debugLabel: 'terminal');
final typed = <LogicalKeyboardKey>[];
Widget app({bool narrow = false}) => MaterialApp(
home: Scaffold(
body: Column(
children: [
Expanded(
child: ChatWorkspace(
controller: controller,
floatFrames: frames,
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();
frames.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');
test('menus and tooltips name the Alt chord', () {
expect(chatFloatShortcutLabel(TargetPlatform.linux), 'Alt+Y');
expect(chatFloatShortcutLabel(TargetPlatform.windows), 'Alt+Y');
expect(chatFloatShortcutLabel(TargetPlatform.macOS), 'Option+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.floatingPanes, isEmpty);
tester.state<_PaneState>(_pane('b')).focus.requestFocus();
await tester.pump();
await _chord(tester, LogicalKeyboardKey.controlLeft);
expect(h.controller.floatingPanes.firstOrNull?.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(h.controller.floatingPanes.single.id);
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.floatingPanes, isEmpty);
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.floatingPanes, isEmpty);
// 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);
});
testWidgets('Alt+Y floats, hides and shows like Ctrl+Y, also from a '
'terminal', (tester) async {
final h = await pump(tester);
tester.state<_PaneState>(_pane('b')).focus.requestFocus();
await tester.pump();
await _chord(tester, LogicalKeyboardKey.altLeft);
expect(h.controller.floatingPanes.firstOrNull?.sessionId, 'b');
expect(h.controller.floatShown, isTrue);
h.typing.requestFocus();
await tester.pump();
h.typed.clear();
await _chord(tester, LogicalKeyboardKey.altLeft);
expect(h.controller.floatShown, isFalse);
expect(h.typed, isEmpty);
test('Alt+Y, Ctrl+Y and Cmd+Y on Apple platforms; a plain Y types', () {
bool chord(Set<LogicalKeyboardKey> held, TargetPlatform platform) =>
chatFloatShortcut(_y, _FakeKeyboard(held), platform: platform);
expect(chord({}, TargetPlatform.linux), isFalse);
expect(
chord({LogicalKeyboardKey.shiftLeft}, TargetPlatform.linux),
isFalse,
);
expect(chord({LogicalKeyboardKey.altLeft}, TargetPlatform.linux), isTrue);
expect(chord({LogicalKeyboardKey.altLeft}, TargetPlatform.macOS), isTrue);
expect(
chord({LogicalKeyboardKey.controlLeft}, TargetPlatform.linux),
isTrue,
);
expect(chord({LogicalKeyboardKey.metaLeft}, TargetPlatform.linux), isFalse);
expect(chord({LogicalKeyboardKey.metaLeft}, TargetPlatform.macOS), isTrue);
expect(
chord({
LogicalKeyboardKey.controlLeft,
LogicalKeyboardKey.metaLeft,
}, TargetPlatform.macOS),
isFalse,
);
expect(
chatFloatShortcut(
const KeyUpEvent(
physicalKey: PhysicalKeyboardKey.keyY,
logicalKey: LogicalKeyboardKey.keyY,
timeStamp: Duration.zero,
),
_FakeKeyboard({LogicalKeyboardKey.altLeft}),
),
isFalse,
);
});
test('Option+Y matches by key position; extra modifiers do not', () {
@@ -255,11 +64,6 @@ void main() {
chatFloatShortcut(yen, keyboard, platform: TargetPlatform.macOS),
isTrue,
);
const y = KeyDownEvent(
physicalKey: PhysicalKeyboardKey.keyY,
logicalKey: LogicalKeyboardKey.keyY,
timeStamp: Duration.zero,
);
for (final extra in [
LogicalKeyboardKey.controlLeft,
LogicalKeyboardKey.metaLeft,
@@ -267,14 +71,13 @@ void main() {
]) {
expect(
chatFloatShortcut(
y,
_y,
_FakeKeyboard({LogicalKeyboardKey.altLeft, extra}),
),
isFalse,
reason: '$extra',
);
}
expect(chatFloatShortcut(y, _FakeKeyboard({})), isFalse);
});
}
@@ -125,7 +125,9 @@ void main() {
addTearDown(controller.dispose);
final outer = controller.root as ChatWorkspaceSplit;
controller.resize(outer.id, .3);
final ids = {for (final s in ['a', 'b', 'c', 'd']) s: _id(controller, s)};
final ids = {
for (final s in ['a', 'b', 'c', 'd']) s: _id(controller, s),
};
final serial = controller.moveSerial;
controller.swap(ids['a']!, ids['c']!);
expect(controller.panes.map((p) => p.sessionId), ['c', 'b', 'a', 'd']);
@@ -1,7 +1,6 @@
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';
@@ -13,156 +12,63 @@ ChatWorkspaceController _three() => ChatWorkspaceController()
List<String> _sessions(ChatWorkspaceController c) =>
c.panes.map((p) => p.sessionId).toList();
List<String> _floating(ChatWorkspaceController c) =>
c.floatingPanes.map((p) => p.sessionId).toList();
ChatWorkspacePane _pane(ChatWorkspaceController c, String session) =>
c.panes.firstWhere((p) => p.sessionId == session);
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', () {
test('detach takes a pane out of the tiling and activates a neighbour', () {
final c = _three();
final b = _pane(c, 'b');
c.activate(b.id);
c.float(b.id);
expect(c.floatingPanes.single.id, b.id);
expect(c.isFloating(b.id), isTrue);
expect(c.floatShown, isTrue);
expect(c.canFloat(b.id), isTrue);
expect(c.detach(b.id)?.sessionId, 'b');
expect(_sessions(c), ['a', 'c']);
expect(c.activePane?.sessionId, isNot('b'));
expect(c.holdsSession('b'), isTrue);
expect(c.activePane, isNotNull);
expect(c.holdsSession('b'), isFalse);
});
test('a lone pane cannot float; the tiling never empties', () {
test('a lone pane cannot leave: the tiling never empties by floating', () {
final c = ChatWorkspaceController()..open('a');
expect(c.canFloat(c.panes.single.id), isFalse);
c.float(c.panes.single.id);
expect(c.floatingPanes, isEmpty);
c.splitActive('b', ChatSplitAxis.horizontal);
final b = c.activePane!;
c.float(b.id);
// The last tiled chat stays: it cannot float too.
expect(c.canFloat(c.panes.single.id), isFalse);
// Closing the last tiled pane docks the oldest floating chat in its place.
c.close(c.panes.single.id);
expect(c.floatingPanes, isEmpty);
expect(c.panes.single.id, b.id);
expect(c.activePaneId, b.id);
});
test('several chats float together and show or hide together', () {
final c = _three();
c.float(_pane(c, 'a').id);
c.setFloatShown(false);
c.float(_pane(c, 'c').id);
expect(_floating(c), ['a', 'c']);
// Floating another one shows them all.
expect(c.floatShown, isTrue);
expect(_sessions(c), ['b']);
c.toggleFloatShown();
expect(c.floatShown, isFalse);
c.toggleFloatShown();
expect(c.floatShown, isTrue);
// Docking one leaves the other floating.
final a = c.floatingPanes.first;
c.dock(a.id);
expect(_floating(c), ['c']);
expect(c.activePaneId, a.id);
expect(_sessions(c), ['b', 'a']);
final only = c.panes.single;
expect(c.canFloat(only.id), isFalse);
expect(c.detach(only.id), isNull);
expect(c.detach('nope'), isNull);
expect(_sessions(c), ['a']);
});
test('dock puts the chat right of the active pane and activates it', () {
final c = _three();
final b = _pane(c, 'b');
c.float(b.id);
final active = c.activePane!;
c.dock(b.id);
expect(c.floatingPanes, isEmpty);
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'});
c.detach(_pane(c, 'b').id);
c.activate(_pane(c, 'a').id);
c.dock('b');
expect(_sessions(c), ['a', 'b', 'c']);
expect(c.activePane?.sessionId, 'b');
// Docking a chat the tiling already shows only activates it.
c.dock('c');
expect(_sessions(c), ['a', 'b', 'c']);
expect(c.activePane?.sessionId, 'c');
final empty = ChatWorkspaceController()..dock('z');
expect(_sessions(empty), ['z']);
});
test('hide, show, reopen, close and /new follow the floating chat', () {
test('a chat out in a window is shown there instead of opened', () {
final c = _three();
final b = _pane(c, '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.floatingPanes.single.id, b.id);
expect(c.floatingPanes.single.sessionId, 'b2');
c.close(b.id);
expect(c.floatingPanes, isEmpty);
expect(c.paneCount, 2);
});
test('/new into a session another float holds closes the duplicate', () {
final c = _three();
c.float(_pane(c, 'a').id);
c.float(_pane(c, 'b').id);
c.replaceSession('a', 'b');
expect(_floating(c), ['b']);
});
test('restoreFloats skips open sessions and taken ids', () {
final c = ChatWorkspaceController()
..open('a')
..splitActive('b', ChatSplitAxis.horizontal);
final taken = c.panes.first.id;
expect(
c.restoreFloats([
const ChatWorkspacePane('x', 'a'),
ChatWorkspacePane(taken, 'z'),
], shown: true),
0,
);
expect(c.floatShown, isFalse);
expect(
c.restoreFloats([
const ChatWorkspacePane('x', 'z'),
const ChatWorkspacePane('y', 'w'),
const ChatWorkspacePane('x', 'v'),
], shown: false),
2,
);
expect(_floating(c), ['z', 'w']);
expect(c.floatShown, isFalse);
// A new split id never collides with a floating pane's.
c.splitActive('d', ChatSplitAxis.vertical);
expect(
c.panes.map((p) => p.id),
isNot(anyOf(contains('x'), contains('y'))),
);
});
test('a restored layout drops every floating chat', () {
final c = _three();
c.float(c.panes.first.id);
c.float(c.panes.first.id);
final snapshot = c.snapshot()!;
c.restoreLayout(snapshot);
expect(c.floatingPanes, isEmpty);
final shown = <String>[];
c.showOutside = (id) {
if (id != 'w') return false;
shown.add(id);
return true;
};
c.open('w');
c.splitActive('w', ChatSplitAxis.horizontal);
expect(_sessions(c), ['a', 'b', 'c']);
expect(shown, ['w', 'w']);
c.open('d');
expect(c.holdsSession('d'), isTrue);
// /new into a session a window holds closes the tiled duplicate.
c.replaceSession('a', 'w');
expect(c.holdsSession('a'), isFalse);
expect(c.holdsSession('w'), isFalse);
});
test('records round-trip, read version 1 and reject malformed input', () {
@@ -213,63 +119,4 @@ void main() {
expect(ChatFloatRecord.decode(bad), isNull, reason: bad);
}
});
test('frames are kept per pane, cascade by default and restore', () {
final frames = ChatFloatFrames();
expect(frames.rectFor('a'), chatFloatDefaultRect);
expect(
frames.rectFor('b', index: 2),
chatFloatDefaultRect.shift(const Offset(64, 64)),
);
frames.move('a', const Rect.fromLTWH(1, 2, 400, 300));
expect(frames.rectFor('a', index: 3), const Rect.fromLTWH(1, 2, 400, 300));
frames.restore({'b': const Rect.fromLTWH(9, 9, 400, 300)});
expect(frames.rectFor('a'), chatFloatDefaultRect);
expect(frames.rectFor('b'), const Rect.fromLTWH(9, 9, 400, 300));
});
test('binding restores after the layout and saves changes', () async {
final c = ChatWorkspaceController()
..open('a')
..splitActive('b', ChatSplitAxis.horizontal);
final store = _Store(
const ChatFloatRecord(
panes: [ChatWorkspacePane('f', 'c'), ChatWorkspacePane('g', 'd')],
shown: true,
frames: {
'f': Rect.fromLTWH(5, 6, 400, 300),
'g': Rect.fromLTWH(50, 60, 400, 300),
},
),
);
final frames = ChatFloatFrames();
final binding = ChatFloatBinding(
controller: c,
frames: frames,
store: store,
debounce: Duration.zero,
);
await binding.start();
expect(_floating(c), ['c', 'd']);
expect(c.floatShown, isTrue);
expect(frames.rectFor('f'), const Rect.fromLTWH(5, 6, 400, 300));
expect(frames.rectFor('g'), const Rect.fromLTWH(50, 60, 400, 300));
expect(store.writes, 0);
c.setFloatShown(false);
await Future<void>.delayed(Duration.zero);
await binding.flush();
expect(store.record?.shown, isFalse);
frames.move('f', const Rect.fromLTWH(70, 80, 400, 300));
final writes = store.writes;
await binding.flush();
expect(store.writes, writes + 1);
expect(store.record?.frames['f']?.left, 70);
// A docked chat keeps its frame for the next float; a closed one drops it.
c.dock('f');
c.close('g');
binding.dispose();
await Future<void>.delayed(Duration.zero);
expect(store.record?.panes, isEmpty);
expect(store.record?.frames.keys, ['f']);
});
}