Files
pipelines/app/test/shell/windows/window_conveniences_test.dart
alan ef1c120f57 Add the Windows menu, window cycling, edge snapping and size per kind
Phase 4 of docs/plans/floating-windows.md. A bar button lists the app's
windows on every tab (show one, Show all, Minimise all, Close all). Alt+`
and Alt+Shift+` bring the next or previous window to the front. Dropping a
title at the left or right edge fills that half, at the top it maximises.
A new window of a kind opens at the size that kind was last given.
2026-11-11 00:05:16 +00:00

273 lines
9.8 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/shell/windows/window_entry.dart';
import 'package:pipelines_app/shell/windows/window_kind.dart';
import 'package:pipelines_app/shell/windows/window_layer.dart';
import 'package:pipelines_app/shell/windows/window_manager.dart';
import 'package:pipelines_app/shell/windows/window_shortcuts.dart';
import 'package:pipelines_app/shell/windows/window_store.dart';
import 'package:pipelines_app/shell/windows/windows_menu_button.dart';
import 'window_test_harness.dart';
class _Store implements WindowStore {
String? value;
@override
Future<String?> read() async => value;
@override
Future<bool> write(String record) async {
value = record;
return true;
}
}
class _Keyboard extends HardwareKeyboard {
_Keyboard(this.pressed);
final Set<LogicalKeyboardKey> pressed;
@override
bool isLogicalKeyPressed(LogicalKeyboardKey key) => pressed.contains(key);
}
const _backquote = KeyDownEvent(
physicalKey: PhysicalKeyboardKey.backquote,
logicalKey: LogicalKeyboardKey.backquote,
timeStamp: Duration.zero,
);
Future<void> _cycle(WidgetTester tester, {bool back = false}) async {
await tester.sendKeyDownEvent(LogicalKeyboardKey.altLeft);
if (back) await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.backquote);
if (back) await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft);
await tester.sendKeyUpEvent(LogicalKeyboardKey.altLeft);
await tester.pumpAndSettle();
}
void main() {
test('the cycle chord is Alt+` and Alt+Shift+`, by key position too', () {
int? step(Set<LogicalKeyboardKey> held, [KeyEvent event = _backquote]) =>
windowCycleShortcut(event, _Keyboard(held));
expect(step({LogicalKeyboardKey.altLeft}), 1);
expect(
step({LogicalKeyboardKey.altLeft, LogicalKeyboardKey.shiftLeft}),
-1,
);
expect(step({}), isNull);
// Ctrl+` is the terminal's, Cmd+` the system's.
expect(step({LogicalKeyboardKey.controlLeft}), isNull);
expect(
step({LogicalKeyboardKey.altLeft, LogicalKeyboardKey.controlLeft}),
isNull,
);
expect(
step({LogicalKeyboardKey.altLeft, LogicalKeyboardKey.metaLeft}),
isNull,
);
// Option+` is a dead key on a Mac: the logical key is not a backquote.
expect(
step(
{LogicalKeyboardKey.altLeft},
const KeyDownEvent(
physicalKey: PhysicalKeyboardKey.backquote,
logicalKey: LogicalKeyboardKey(0x0060 + 0x100000000),
timeStamp: Duration.zero,
),
),
1,
);
expect(windowCycleShortcutLabel(TargetPlatform.macOS), 'Option+`');
expect(windowCycleShortcutLabel(TargetPlatform.linux), 'Alt+`');
});
test(
'a new window of a kind takes the size that kind was last given',
() async {
final store = _Store();
final manager = WindowManager(
kinds: WindowKinds()..register(ProbeWindowKind()),
store: store,
);
final a = manager.open('probe', 'a');
manager.setFrame(
a.id,
const Rect.fromLTWH(10, 20, 500, 300),
save: false,
);
manager.commit(a.id);
final b = manager.open('probe', 'b');
expect(b.frame.size, const Size(500, 300));
// Placed by the cascade, not on top of the first.
expect(b.frame.topLeft, const Offset(100, 80) + windowCascadeStep);
// A maximised window's frame is not a size to copy.
manager.setMaximized(b.id, true);
manager.setFrame(b.id, const Rect.fromLTWH(0, 0, 900, 700), save: false);
manager.commit(b.id);
expect(manager.open('probe', 'c').frame.size, const Size(500, 300));
await pumpEventQueue();
final next = WindowManager(
kinds: WindowKinds()..register(ProbeWindowKind()),
store: store,
);
await next.restore();
expect(next.record.sizes, {'probe': const Size(500, 300)});
expect(next.open('probe', 'z').frame.size, const Size(500, 300));
},
);
test('show all, minimise all and close all', () {
final kind = ProbeWindowKind();
final manager = WindowManager(kinds: WindowKinds()..register(kind));
final a = manager.open('probe', 'a');
manager.open('probe', 'b');
manager.setFrame(a.id, const Rect.fromLTWH(7, 8, 400, 300));
manager.minimizeAll();
expect(manager.windows.where(manager.isVisible), isEmpty);
expect(manager.windows, hasLength(2));
manager.setGroupShown('probe', false);
manager.showAll();
expect(manager.windows.every(manager.isVisible), isTrue);
manager.closeAll();
expect(manager.windows, isEmpty);
expect(kind.closed_, hasLength(2));
expect(
manager.open('probe', 'a').frame,
const Rect.fromLTWH(7, 8, 400, 300),
);
});
test('snap regions and frames', () {
const bounds = Size(1280, 800);
expect(windowSnapFor(const Offset(600, 3), bounds), WindowSnap.maximize);
expect(windowSnapFor(const Offset(2, 400), bounds), WindowSnap.left);
expect(windowSnapFor(const Offset(1278, 400), bounds), WindowSnap.right);
expect(windowSnapFor(const Offset(600, 400), bounds), isNull);
final left = windowSnapRect(WindowSnap.left, bounds);
final right = windowSnapRect(WindowSnap.right, bounds);
expect(left, const Rect.fromLTWH(8, 8, 628, 784));
expect(right, const Rect.fromLTWH(644, 8, 628, 784));
expect(right.left - left.right, 8);
});
testWidgets('dropping the title at an edge fills that half; at the top it '
'maximises', (tester) async {
final manager = probeManager();
final entry = manager.open('probe', 'a');
await pumpProbe(tester, manager);
final title = find.byKey(const ValueKey('window-title'));
await tester.dragFrom(
tester.getCenter(title),
Offset(-tester.getCenter(title).dx + 2, 200),
);
await tester.pumpAndSettle();
expect(windowRect(tester, 'Probe a'), const Rect.fromLTWH(8, 8, 628, 784));
await tester.dragFrom(
tester.getCenter(title),
Offset(1278 - tester.getCenter(title).dx, 100),
);
await tester.pumpAndSettle();
expect(
windowRect(tester, 'Probe a'),
const Rect.fromLTWH(644, 8, 628, 784),
);
// A snapped half is not this kind's size for its next window.
expect(manager.open('probe', 'b').frame.size, const Size(360, 420));
manager.close(manager.find('probe', 'b')!.id);
await tester.pumpAndSettle();
await tester.dragFrom(
tester.getCenter(title),
Offset(-200, -tester.getCenter(title).dy + 2),
);
await tester.pumpAndSettle();
expect(manager.byId(entry.id)!.maximized, isTrue);
// An ordinary move does not snap.
manager.setMaximized(entry.id, false);
manager.setFrame(entry.id, const Rect.fromLTWH(300, 200, 360, 420));
await tester.pumpAndSettle();
await tester.drag(title, const Offset(50, 40));
await tester.pumpAndSettle();
expect(
windowRect(tester, 'Probe a'),
const Rect.fromLTWH(350, 240, 360, 420),
);
});
testWidgets('Alt+` brings the next window to the front, with the keyboard', (
tester,
) async {
final manager = probeManager();
manager.open('probe', 'a');
await pumpProbe(tester, manager);
// One window: the key is left alone.
await _cycle(tester);
expect(manager.active!.contentId, 'a');
manager.open('probe', 'b');
manager.open('probe', 'c');
await tester.pumpAndSettle();
await _cycle(tester);
expect(manager.active!.contentId, 'a');
final focus = FocusManager.instance.primaryFocus!.context!;
expect(
focus.findAncestorWidgetOfExactType<WindowLayer>()?.entry.contentId,
'a',
);
await _cycle(tester, back: true);
expect(manager.active!.contentId, 'c');
// Not while a dialog covers the app.
await tester.tap(find.byKey(const ValueKey('c-dialog')));
await tester.pumpAndSettle();
await _cycle(tester);
expect(manager.active!.contentId, 'c');
});
testWidgets('the Windows menu lists, shows, minimises and closes windows', (
tester,
) async {
tester.view.physicalSize = const Size(1280, 800);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final manager = probeManager();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
appBar: AppBar(actions: [WindowsMenuButton(manager: manager)]),
),
),
);
// No windows: no button.
expect(find.byKey(const ValueKey('shell.windows')), findsNothing);
final a = manager.open('probe', 'a');
final b = manager.open('probe', 'b');
manager.setMinimized(a.id, true);
await tester.pumpAndSettle();
expect(find.byTooltip('Windows (2)'), findsOneWidget);
await tester.tap(find.byKey(const ValueKey('shell.windows')));
await tester.pumpAndSettle();
expect(find.text('Probe a'), findsOneWidget);
expect(find.text('Probe b'), findsOneWidget);
expect(find.textContaining('Next window'), findsOneWidget);
await tester.tap(find.text('Probe a'));
await tester.pumpAndSettle();
expect(manager.active!.id, a.id);
expect(manager.byId(a.id)!.minimized, isFalse);
await tester.tap(find.byKey(const ValueKey('shell.windows')));
await tester.pumpAndSettle();
await tester.tap(find.text('Minimise all'));
await tester.pumpAndSettle();
expect(manager.windows.where(manager.isVisible), isEmpty);
await tester.tap(find.byKey(const ValueKey('shell.windows')));
await tester.pumpAndSettle();
await tester.tap(find.text('Show all'));
await tester.pumpAndSettle();
expect(manager.isVisible(manager.byId(b.id)!), isTrue);
await tester.tap(find.byKey(const ValueKey('shell.windows')));
await tester.pumpAndSettle();
await tester.tap(find.text('Close all'));
await tester.pumpAndSettle();
expect(manager.windows, isEmpty);
expect(find.byKey(const ValueKey('shell.windows')), findsNothing);
});
}