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
@@ -4,12 +4,18 @@ 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/chat_runtime.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/windows/chat_windows.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 'package:pipelines_app/ui/floating/floating_layer.dart';
import 'package:pipelines_app/shell/windows/window_frame.dart';
import 'package:pipelines_app/shell/windows/window_host.dart';
import 'package:pipelines_app/shell/windows/window_kind.dart';
import 'package:pipelines_app/shell/windows/window_manager.dart';
import 'package:pipelines_app/shell/windows/window_store.dart';
import 'package:pipelines_app/ui/motion/fade_through_stack.dart';
import 'conversation/chat_thread_fixtures.dart';
@@ -29,6 +35,17 @@ class _Gateway extends FakeChatGateway {
}
}
class _Store implements WindowStore {
String? value;
@override
Future<String?> read() async => value;
@override
Future<bool> write(String record) async {
value = record;
return true;
}
}
Finder _thread(String id) => find.byWidgetPredicate(
(widget) => widget is ChatThreadView && widget.sessionId == id,
skipOffstage: false,
@@ -38,45 +55,102 @@ Finder _input(String id) => find.descendant(
matching: find.byType(EditableText, skipOffstage: false),
skipOffstage: false,
);
ChatWorkspaceController _workspace(WidgetTester tester) =>
tester.widget<ChatWorkspace>(find.byType(ChatWorkspace)).controller;
ChatWorkspaceController _workspace(WidgetTester tester) => tester
.widget<ChatWorkspace>(find.byType(ChatWorkspace, skipOffstage: false))
.controller;
Future<_Gateway> _mount(
WidgetTester tester, {
Size size = const Size(1800, 1000),
ValueNotifier<int>? destination,
}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
/// The window whose title is [title].
Finder _window(String title) =>
find.ancestor(of: find.text(title), matching: find.byType(WindowFrame));
/// That window's title bar, where it shows and takes input.
Finder _shown(String title) => find
.descendant(
of: _window(title),
matching: find.byKey(const ValueKey('window-title')),
)
.hitTestable();
/// The app in miniature: the Chat destination beside another one, and the
/// window host above both, with app-lifetime chat state.
class _App {
_App(this.store);
final _Store store;
final gateway = _Gateway()..history = (_) async => chatPage([]);
bindServerIdentity(gateway, 'server-a');
addTearDown(gateway.close);
final chat = ChatScreen(gateway: gateway, persistWorkspace: true);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: destination == null
? chat
// As the shell keeps destinations: the hidden one is muted,
// unfocusable and takes no input.
: FloatingFocusRoot(
child: ValueListenableBuilder(
valueListenable: destination,
builder: (context, index, _) => FadeThroughStack(
index: index,
children: [
chat,
const Center(child: Text('Runs page')),
],
),
final destination = ValueNotifier(0);
final chatBuilt = ValueNotifier(true);
final opened = <String>[];
late final ChatRuntime runtime;
late final WindowManager manager;
late final ChatWindows windows;
Future<void> start(
WidgetTester tester, {
Size size = const Size(1800, 1000),
}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
bindServerIdentity(gateway, 'server-a');
runtime = ChatRuntime(gateway: gateway, serverKey: 'server-a');
manager = WindowManager(
kinds: WindowKinds(),
store: store,
server: 'server-a',
);
windows = ChatWindows(
manager: manager,
runtime: runtime,
openInChat: opened.add,
)..start();
await manager.restore();
await windows.migrate(
const SharedPreferencesChatFloatStore(serverScope: 'server-a'),
);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Stack(
fit: StackFit.expand,
children: [
ListenableBuilder(
listenable: Listenable.merge([destination, chatBuilt]),
builder: (context, _) => FadeThroughStack(
index: destination.value,
children: [
if (chatBuilt.value)
ChatScreen(
gateway: gateway,
persistWorkspace: true,
runtime: runtime,
windows: windows,
)
else
const Center(child: Text('Not connected')),
const Center(child: Text('Runs page')),
],
),
),
Positioned.fill(child: WindowHost(manager: manager)),
],
),
),
),
),
);
await tester.pumpAndSettle();
return gateway;
);
await tester.pumpAndSettle();
}
Future<void> stop(WidgetTester tester) async {
await tester.pumpWidget(const SizedBox.shrink());
windows.dispose();
runtime.dispose();
manager.dispose();
destination.dispose();
chatBuilt.dispose();
gateway.close();
}
List<String> get floating => [for (final w in windows.windows) w.contentId];
}
/// Opens [session]'s pane menu (wherever it is) and chooses [label].
@@ -106,26 +180,34 @@ Future<void> _saved(String a, String b, [String? c]) async {
});
}
Future<void> _chord(WidgetTester tester) async {
await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyY);
await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft);
await tester.pumpAndSettle();
}
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 app = _App(_Store());
await app.start(tester);
final b = tester.state(_thread('b'));
await tester.enterText(_input('b'), 'Draft B');
await _menu(tester, 'b', 'Float (Ctrl+Y)');
await _menu(tester, 'b', 'Float (Alt+Y)');
final controller = _workspace(tester);
expect(controller.floatingPanes.single.sessionId, 'b');
expect(app.floating, ['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')),
// A window over the app, titled by its chat, not a pane.
expect(tester.getSize(_window('Chat b')).width, lessThan(1800 / 2));
expect(
find.descendant(of: _window('Chat b'), matching: find.byTooltip('Dock')),
findsOneWidget,
);
expect(window.width, lessThan(1800 / 2));
// The lone tiled pane cannot float: the tiling would be empty.
await tester.tap(
@@ -137,157 +219,164 @@ void main() {
.hitTestable(),
);
await tester.pumpAndSettle();
expect(find.text('Float (Ctrl+Y)'), findsNothing);
expect(find.textContaining('Float'), findsNothing);
await tester.tapAt(const Offset(5, 995));
await tester.pumpAndSettle();
await _menu(tester, 'b', 'Dock');
expect(controller.floatingPanes, isEmpty);
await tester.tap(find.byTooltip('Dock'));
await tester.pumpAndSettle();
expect(app.floating, isEmpty);
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(
app.gateway.calls.where(
(call) => ['chat_close', 'chat_interrupt'].contains(call.type),
),
isEmpty,
);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox.shrink());
await app.stop(tester);
});
testWidgets('the floating chat and its frame survive a reload', (
testWidgets('X closes the window and returns nothing to the tiling', (
tester,
) async {
await _saved('a', 'b');
await _mount(tester);
await _menu(tester, 'b', 'Float (Ctrl+Y)');
final app = _App(_Store());
await app.start(tester);
await _menu(tester, 'b', 'Float (Alt+Y)');
await tester.tap(find.byTooltip('Close'));
await tester.pumpAndSettle();
expect(app.floating, isEmpty);
expect(_workspace(tester).panes.map((p) => p.sessionId), ['a']);
expect(_thread('b'), findsNothing);
expect(app.gateway.calls.where((c) => c.type == 'chat_close'), isEmpty);
await app.stop(tester);
});
testWidgets('popups from a floating chat open above its window', (
tester,
) async {
await _saved('a', 'b');
final app = _App(_Store());
await app.start(tester);
await _menu(tester, 'b', 'Float (Alt+Y)');
// The More actions menu, opened from inside the window.
await tester.tap(
find
.descendant(
of: _thread('b'),
matching: find.byTooltip('More actions'),
)
.hitTestable(),
);
await tester.pumpAndSettle();
expect(find.text('Chat details').hitTestable(), findsOneWidget);
// Chat details, the sheet that used to open behind the window.
await tester.tap(find.text('Chat details'));
await tester.pumpAndSettle();
final sheet = find.byType(BottomSheet);
expect(sheet.hitTestable(), findsOneWidget);
// On the root navigator: wider than the window, and over it.
expect(
tester.getSize(sheet).width,
greaterThan(tester.getSize(_window('Chat b')).width),
);
expect(_shown('Chat b'), findsNothing);
expect(tester.takeException(), isNull);
await app.stop(tester);
});
testWidgets('windows and their frames survive a reload, without the Chat '
'destination being built', (tester) async {
await _saved('a', 'b');
final store = _Store();
var app = _App(store);
await app.start(tester);
await _menu(tester, 'b', 'Float (Alt+Y)');
await tester.drag(
find.byKey(const ValueKey('chat-float-title')),
find.descendant(
of: _window('Chat b'),
matching: find.byKey(const ValueKey('window-title')),
),
const Offset(200, 40),
);
await tester.pumpAndSettle();
await _menu(tester, 'b', 'Hide floating chats (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'),
)!,
)!;
final floated = record.panes.single;
expect(floated.sessionId, 'b');
expect(record.shown, isFalse);
expect(record.frames[floated.id]?.left, chatFloatDefaultRect.left + 200);
await tester.pumpWidget(const SizedBox.shrink());
await _mount(tester);
final controller = _workspace(tester);
expect(controller.floatingPanes.single.sessionId, 'b');
expect(controller.floatShown, isFalse);
expect(controller.panes.map((p) => p.sessionId), ['a']);
controller.setFloatShown(true);
await tester.tap(find.byTooltip('Minimise'));
await tester.pumpAndSettle();
expect(
tester.getTopLeft(find.byKey(const ValueKey('chat-float-title'))).dx,
greaterThanOrEqualTo(chatFloatDefaultRect.left + 200),
);
await tester.pumpWidget(const SizedBox.shrink());
expect(_shown('Chat b'), findsNothing);
await app.stop(tester);
app = _App(store)..chatBuilt.value = false;
await app.start(tester);
expect(find.text('Not connected'), findsOneWidget);
final entry = app.windows.windows.single;
expect(entry.contentId, 'b');
expect(entry.minimized, isTrue);
expect(entry.frame.left, chatFloatDefaultRect.left + 200);
await _chord(tester);
expect(_shown('Chat b'), findsOneWidget);
expect(tester.getTopLeft(_window('Chat b')).dx, entry.frame.left);
expect(_thread('b').hitTestable(), findsOneWidget);
// Dock with no tiling opens the chat in the Chat destination.
await tester.tap(find.byTooltip('Dock'));
await tester.pumpAndSettle();
expect(app.opened, ['b']);
expect(app.floating, isEmpty);
await app.stop(tester);
});
Future<void> chord(WidgetTester tester) async {
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyY);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
await tester.pumpAndSettle();
}
final titles = find.byKey(const ValueKey('chat-float-title'));
testWidgets('several chats float; the chord hides and shows them all '
'where they were', (tester) async {
await _saved('a', 'b', 'c');
await _mount(tester);
await _menu(tester, 'b', 'Float (Ctrl+Y)');
await _menu(tester, 'c', 'Float (Ctrl+Y)');
final app = _App(_Store());
await app.start(tester);
await _menu(tester, 'b', 'Float (Alt+Y)');
await _menu(tester, 'c', 'Float (Alt+Y)');
final controller = _workspace(tester);
expect(controller.floatingPanes.map((p) => p.sessionId), ['b', 'c']);
expect(app.floating, ['b', 'c']);
expect(controller.panes.map((p) => p.sessionId), ['a']);
expect(titles, findsNWidgets(2));
// The second window opens stepped from the first, so both show.
final first = tester.getTopLeft(titles.first);
final second = tester.getTopLeft(titles.last);
final first = tester.getTopLeft(_window('Chat b'));
final second = tester.getTopLeft(_window('Chat c'));
expect(second.dx, greaterThan(first.dx));
await tester.drag(titles.first, const Offset(300, 100));
await tester.pumpAndSettle();
final moved = tester.getTopLeft(titles.first);
expect(moved.dx, greaterThan(first.dx + 200));
await chord(tester);
expect(controller.floatShown, isFalse);
expect(titles, findsNothing);
await chord(tester);
expect(controller.floatShown, isTrue);
expect(titles, findsNWidgets(2));
expect(tester.getTopLeft(titles.first), moved);
expect(tester.getTopLeft(titles.last), second);
await _chord(tester);
expect(
find.byKey(const ValueKey('window-title')).hitTestable(),
findsNothing,
);
await _chord(tester);
expect(tester.getTopLeft(_window('Chat b')), first);
expect(tester.getTopLeft(_window('Chat c')), second);
// Docking one leaves the other floating.
await _menu(tester, 'b', 'Dock');
expect(controller.floatingPanes.map((p) => p.sessionId), ['c']);
expect(titles, findsOneWidget);
await tester.pump(const Duration(seconds: 1));
final preferences = await SharedPreferences.getInstance();
final record = ChatFloatRecord.decode(
preferences.getString(
scopedServerKey(chatFloatPreferenceKey, 'server-a'),
)!,
)!;
expect(record.panes.map((p) => p.sessionId), ['c']);
// The docked chat keeps its frame for its next float.
expect(record.frames, hasLength(2));
await tester.tap(
find.descendant(of: _window('Chat b'), matching: find.byTooltip('Dock')),
);
await tester.pumpAndSettle();
expect(app.floating, ['c']);
expect(controller.panes.map((p) => p.sessionId), ['a', 'b']);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox.shrink());
await app.stop(tester);
});
testWidgets('a floating chat moves, types, hides and shows over another '
'destination', (tester) async {
testWidgets('a floating chat types, runs its tickers, hides and shows over '
'another destination', (tester) async {
await _saved('a', 'b');
final destination = ValueNotifier(0);
addTearDown(destination.dispose);
await _mount(tester, destination: destination);
await _menu(tester, 'b', 'Float (Ctrl+Y)');
final controller = _workspace(tester);
destination.value = 1;
final app = _App(_Store());
await app.start(tester);
await _menu(tester, 'b', 'Float (Alt+Y)');
app.destination.value = 1;
await tester.pumpAndSettle();
expect(find.text('Runs page'), findsOneWidget);
// The chat page is offstage, so finders look past it; the window itself
// paints and takes input in the root overlay.
final title = find.byKey(
const ValueKey('chat-float-title'),
skipOffstage: false,
);
bool hits(Finder finder) {
final box = tester.renderObject(finder);
return tester
.hitTestOnBinding(tester.getCenter(finder))
.path
.any((entry) => identical(entry.target, box));
}
expect(hits(title), isTrue);
final before = tester.getTopLeft(title);
await tester.drag(title, const Offset(150, 60));
await tester.pumpAndSettle();
expect(tester.getTopLeft(title).dx, greaterThan(before.dx + 100));
expect(tester.getTopLeft(title).dy, greaterThan(before.dy + 20));
expect(_shown('Chat b'), findsOneWidget);
// Not muted by the hidden Chat destination.
expect(TickerMode.valuesOf(tester.element(_thread('b'))).enabled, isTrue);
// The composer takes the keyboard there.
final input = _input('b');
expect(hits(input), isTrue);
await tester.tapAt(tester.getCenter(input));
await tester.pump();
final field = tester.widget<EditableText>(input);
@@ -296,35 +385,65 @@ void main() {
await tester.pump();
expect(field.controller.text, 'From runs');
// The chord and the window's own hide button both work there.
// The window itself animates away and back, not only the state.
await chord(tester);
expect(controller.floatShown, isFalse);
expect(hits(title), isFalse);
await chord(tester);
expect(controller.floatShown, isTrue);
expect(hits(title), isTrue);
final hide = find.byTooltip(
'Hide floating chats (Ctrl+Y)',
skipOffstage: false,
);
expect(hits(hide), isTrue);
await tester.tapAt(tester.getCenter(hide));
await tester.pumpAndSettle();
expect(controller.floatShown, isFalse);
expect(hits(title), isFalse);
await _chord(tester);
expect(_shown('Chat b'), findsNothing);
await _chord(tester);
expect(_shown('Chat b'), findsOneWidget);
// Shown again by the chord, the composer has the keyboard at once.
expect(tester.widget<EditableText>(_input('b')).focusNode.hasFocus, isTrue);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox.shrink());
await app.stop(tester);
});
testWidgets('narrow layouts offer no Float', (tester) async {
testWidgets('floating chats saved by an older build come back as windows', (
tester,
) async {
await _saved('a', 'c');
final preferences = await SharedPreferences.getInstance();
await preferences.setString(
scopedServerKey(chatFloatPreferenceKey, 'server-a'),
const ChatFloatRecord(
panes: [ChatWorkspacePane('chat-pane-9', 'b')],
shown: true,
frames: {'chat-pane-9': Rect.fromLTWH(400, 300, 500, 420)},
).encode(),
);
final app = _App(_Store());
await app.start(tester);
expect(app.floating, ['b']);
expect(tester.getTopLeft(_window('Chat b')), const Offset(400, 300));
expect(_workspace(tester).panes.map((p) => p.sessionId), ['a', 'c']);
await app.stop(tester);
});
testWidgets('a chat is drawn once: opening a floating chat from the list '
'shows its window', (tester) async {
await _saved('a', 'b');
await _mount(tester, size: const Size(430, 900));
final app = _App(_Store());
await app.start(tester);
await _menu(tester, 'b', 'Float (Alt+Y)');
app.manager.setGroupShown(chatWindowKind, false);
await tester.pumpAndSettle();
_workspace(tester).open('b');
await tester.pumpAndSettle();
expect(_shown('Chat b'), findsOneWidget);
expect(_workspace(tester).panes.map((p) => p.sessionId), ['a']);
expect(_thread('b'), findsOneWidget);
expect(tester.takeException(), isNull);
await app.stop(tester);
});
testWidgets('narrow layouts offer no Float and show no window', (
tester,
) async {
await _saved('a', 'b');
final app = _App(_Store());
await app.start(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());
await app.stop(tester);
});
}