diff --git a/app/lib/application/pipelines_app.dart b/app/lib/application/pipelines_app.dart index d8f411ed..c64d9c4b 100644 --- a/app/lib/application/pipelines_app.dart +++ b/app/lib/application/pipelines_app.dart @@ -38,6 +38,7 @@ import '../shell/windows/window_host.dart'; import '../shell/windows/window_kind.dart'; import '../shell/windows/window_manager.dart'; import '../shell/windows/window_store.dart'; +import '../shell/windows/windows_menu_button.dart'; import 'app_terminals.dart'; import '../features/chat/chat_runtime.dart'; import '../features/chat/windows/chat_windows.dart'; @@ -819,6 +820,8 @@ class _PipelinesAppState extends State // The terminal button sits on the Chat tab only; the keyboard // shortcut still toggles the terminal everywhere. barActions: [ + // Shown on every tab while the app has floating windows. + WindowsMenuButton(manager: _windows), if (_destinationOf(route.kind) == AppDestination.chat) _terminals.toggle(cupertino: _cupertino), ], diff --git a/app/lib/shell/windows/window_entry.dart b/app/lib/shell/windows/window_entry.dart index efaa83af..584c861f 100644 --- a/app/lib/shell/windows/window_entry.dart +++ b/app/lib/shell/windows/window_entry.dart @@ -99,3 +99,36 @@ Rect drawnWindowFrame(WindowEntry entry, Size bounds, Size minSize) => windowMaximizedMargin.clamp(0.0, bounds.shortestSide / 2), ) : fitWindowFrame(entry.frame, bounds, minSize); + +/// Where a dragged window snaps when its title is dropped at an edge. +enum WindowSnap { left, right, maximize } + +/// How close to an edge of the window area the pointer must be to snap. +const double windowSnapDistance = 6; + +/// The snap for a title dropped with the pointer at [pointer] inside +/// [bounds], or null away from the edges. +WindowSnap? windowSnapFor(Offset pointer, Size bounds) { + if (pointer.dy <= windowSnapDistance) return WindowSnap.maximize; + if (pointer.dx <= windowSnapDistance) return WindowSnap.left; + if (pointer.dx >= bounds.width - windowSnapDistance) return WindowSnap.right; + return null; +} + +/// The frame of a window snapped to a half of [bounds]. +Rect windowSnapRect(WindowSnap snap, Size bounds) { + final area = (Offset.zero & bounds).deflate( + windowMaximizedMargin.clamp(0.0, bounds.shortestSide / 2), + ); + final half = (area.width - windowMaximizedMargin) / 2; + return switch (snap) { + WindowSnap.left => Rect.fromLTWH(area.left, area.top, half, area.height), + WindowSnap.right => Rect.fromLTWH( + area.right - half, + area.top, + half, + area.height, + ), + WindowSnap.maximize => area, + }; +} diff --git a/app/lib/shell/windows/window_frame.dart b/app/lib/shell/windows/window_frame.dart index 79789dc4..001076aa 100644 --- a/app/lib/shell/windows/window_frame.dart +++ b/app/lib/shell/windows/window_frame.dart @@ -32,7 +32,9 @@ class WindowFrame extends StatelessWidget { final IconData icon; final List actions; final bool maximized; - final ValueChanged onMove; + + /// The drag's delta and the pointer's global position. + final void Function(Offset delta, Offset globalPosition) onMove; final VoidCallback onMoveEnd; final VoidCallback onMinimize; final VoidCallback onToggleMaximized; @@ -100,7 +102,9 @@ class WindowFrame extends StatelessWidget { behavior: HitTestBehavior.opaque, // The window follows the pointer from where it went down. dragStartBehavior: DragStartBehavior.down, - onPanUpdate: maximized ? null : (details) => onMove(details.delta), + onPanUpdate: maximized + ? null + : (details) => onMove(details.delta, details.globalPosition), onPanEnd: maximized ? null : (_) => onMoveEnd(), onDoubleTap: onToggleMaximized, child: Padding( diff --git a/app/lib/shell/windows/window_host.dart b/app/lib/shell/windows/window_host.dart index 5a13edbf..f4fc1f84 100644 --- a/app/lib/shell/windows/window_host.dart +++ b/app/lib/shell/windows/window_host.dart @@ -1,8 +1,10 @@ import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'window_focus.dart'; import 'window_layer.dart'; import 'window_manager.dart'; +import 'window_shortcuts.dart'; /// The app's floating windows, drawn in the shell page above the /// destinations and the terminal panel: plain children of one stack, in the @@ -33,6 +35,7 @@ class _WindowHostState extends State { void initState() { super.initState(); widget.manager.addListener(_changed); + FocusManager.instance.addEarlyKeyEventHandler(_key); // The shell page is not the top route while a popup or page covers it. _focus.covered = () => mounted && !(ModalRoute.of(context)?.isCurrent ?? true); @@ -51,6 +54,7 @@ class _WindowHostState extends State { @override void dispose() { + FocusManager.instance.removeEarlyKeyEventHandler(_key); widget.manager.removeListener(_changed); _focus.dispose(); super.dispose(); @@ -81,6 +85,22 @@ class _WindowHostState extends State { setState(() {}); } + /// Alt+` and Alt+Shift+` bring the next or previous window to the front, + /// from anywhere (a composer, the terminal). With fewer than two windows + /// showing, or while a popup covers the app, the key is left alone. + KeyEventResult _key(KeyEvent event) { + if (!mounted) return KeyEventResult.ignored; + final step = windowCycleShortcut(event, HardwareKeyboard.instance); + final manager = widget.manager; + if (step == null || + (_focus.covered?.call() ?? false) || + manager.windows.where(manager.isVisible).length < 2) { + return KeyEventResult.ignored; + } + manager.cycle(step); + return KeyEventResult.handled; + } + /// The window takes the keyboard, then its content places it. void _focusWindow(String id) { _focus.focusSoon(id); diff --git a/app/lib/shell/windows/window_layer.dart b/app/lib/shell/windows/window_layer.dart index 5d7b927d..df969711 100644 --- a/app/lib/shell/windows/window_layer.dart +++ b/app/lib/shell/windows/window_layer.dart @@ -93,11 +93,46 @@ class _WindowLayerState extends State return drawnWindowFrame(entry, widget.bounds, widget.kind.minSize); } - void _move(Offset delta) => widget.manager.setFrame( - _id, - fitWindowFrame(_current.shift(delta), widget.bounds, widget.kind.minSize), - save: false, - ); + /// Where the pointer is in the host while the title is dragged. + Offset? _pointer; + + void _move(Offset delta, Offset globalPosition) { + final box = context.findRenderObject(); + _pointer = box is RenderBox ? box.globalToLocal(globalPosition) : null; + widget.manager.setFrame( + _id, + fitWindowFrame(_current.shift(delta), widget.bounds, widget.kind.minSize), + save: false, + ); + } + + /// Dropping the title at the app's left or right edge fills that half; + /// at the top edge it maximises. + void _moveEnded() { + final pointer = _pointer; + _pointer = null; + final bounds = widget.bounds; + final snap = pointer == null ? null : windowSnapFor(pointer, bounds); + if (snap == WindowSnap.maximize) { + widget.manager.setMaximized(_id, true); + return; + } + if (snap != null) { + widget.manager.setFrame( + _id, + fitWindowFrame( + windowSnapRect(snap, bounds), + bounds, + widget.kind.minSize, + ), + save: false, + ); + // A snapped half is not the size to give this kind's next window. + widget.manager.commit(); + return; + } + widget.manager.commit(_id); + } void _resize(Offset delta) { final rect = _current; @@ -156,7 +191,7 @@ class _WindowLayerState extends State actions: kind.actions(context, entry), maximized: entry.maximized, onMove: _move, - onMoveEnd: manager.commit, + onMoveEnd: _moveEnded, onMinimize: () => manager.setMinimized(_id, true), onToggleMaximized: () => manager.setMaximized(_id, !entry.maximized), @@ -200,7 +235,7 @@ class _WindowLayerState extends State bottom: widget.bounds.height - frame.bottom, child: WindowResizeCorner( onResize: _resize, - onResizeEnd: manager.commit, + onResizeEnd: () => manager.commit(_id), ), ), ], diff --git a/app/lib/shell/windows/window_manager.dart b/app/lib/shell/windows/window_manager.dart index f2fbd4a8..ff90b0ce 100644 --- a/app/lib/shell/windows/window_manager.dart +++ b/app/lib/shell/windows/window_manager.dart @@ -27,6 +27,9 @@ class WindowManager extends ChangeNotifier { final List _all = []; final Set _hiddenGroups = {}; final Map _frames = {}; + + /// The size each kind's windows were last given, for its next new window. + final Map _sizes = {}; int _revision = 0; int _nextId = 0; @@ -108,7 +111,9 @@ class WindowManager extends ChangeNotifier { frame: frame ?? _frames[windowFrameKey(kind, serverKey, contentId)] ?? - spec.defaultFrame.shift(windowCascadeStep * siblings.toDouble()), + (spec.defaultFrame.topLeft + + windowCascadeStep * siblings.toDouble()) & + (_sizes[kind] ?? spec.defaultFrame.size), data: data, ); _change(() { @@ -176,8 +181,53 @@ class WindowManager extends ChangeNotifier { void setFrame(String id, Rect frame, {bool save = true}) => _replace(id, (w) => w.copyWith(frame: frame), save: save); - /// Saves after a drag. - void commit() => unawaited(save()); + /// Saves after a drag, and remembers the size for the kind's next window. + void commit([String? id]) { + final entry = id == null ? null : byId(id); + if (entry != null && !entry.maximized) { + _sizes[entry.kind] = entry.frame.size; + } + unawaited(save()); + } + + /// Shows every window (un-minimised, every group shown). + void showAll() { + if (windows.every(isVisible)) return; + _change(() { + for (var i = 0; i < _all.length; i++) { + final w = _all[i]; + if (!_here(w)) continue; + _hiddenGroups.remove(w.groupId); + if (w.minimized) _all[i] = w.copyWith(minimized: false); + } + }); + } + + /// Minimises every window. + void minimizeAll() { + if (!windows.any(isVisible)) return; + _change(() { + for (var i = 0; i < _all.length; i++) { + final w = _all[i]; + if (_here(w) && !w.minimized) _all[i] = w.copyWith(minimized: true); + } + }); + } + + /// Closes every window; each keeps its frame for its content. + void closeAll() { + final closing = windows; + if (closing.isEmpty) return; + _change(() { + for (final entry in closing) { + _all.remove(entry); + _remember(entry); + } + }); + for (final entry in closing) { + kinds[entry.kind]?.closed(entry); + } + } void setMinimized(String id, bool minimized) => _replace(id, (w) => w.copyWith(minimized: minimized)); @@ -274,6 +324,9 @@ class WindowManager extends ChangeNotifier { _frames ..clear() ..addAll(record.frames); + _sizes + ..clear() + ..addAll(record.sizes); notifyListeners(); } @@ -281,6 +334,7 @@ class WindowManager extends ChangeNotifier { windows: List.unmodifiable(_all), hiddenGroups: Set.unmodifiable(_hiddenGroups), frames: Map.unmodifiable(_frames), + sizes: Map.unmodifiable(_sizes), ); Future save() async { diff --git a/app/lib/shell/windows/window_shortcuts.dart b/app/lib/shell/windows/window_shortcuts.dart new file mode 100644 index 00000000..ce909350 --- /dev/null +++ b/app/lib/shell/windows/window_shortcuts.dart @@ -0,0 +1,28 @@ +import 'package:flutter/foundation.dart'; +import 'package:flutter/services.dart'; + +/// The step Alt+` (1) or Alt+Shift+` (-1) moves between the app's floating +/// windows, or null for any other key. A browser keeps Ctrl+Tab and the +/// system keeps Cmd+`, so the chord is Alt (Option), which every platform +/// delivers; Option+` types a dead key on a Mac, so the key is matched by +/// its position too. Ctrl+` stays the terminal's. +int? windowCycleShortcut(KeyEvent event, HardwareKeyboard keyboard) { + if (event is KeyUpEvent || + !keyboard.isAltPressed || + keyboard.isControlPressed || + keyboard.isMetaPressed) { + return null; + } + final backquote = + event.logicalKey == LogicalKeyboardKey.backquote || + event.physicalKey == PhysicalKeyboardKey.backquote; + if (!backquote) return null; + return keyboard.isShiftPressed ? -1 : 1; +} + +/// The chord as it reads on this platform, for the Windows menu. +String windowCycleShortcutLabel([TargetPlatform? platform]) => + switch (platform ?? defaultTargetPlatform) { + TargetPlatform.macOS || TargetPlatform.iOS => 'Option+`', + _ => 'Alt+`', + }; diff --git a/app/lib/shell/windows/window_store.dart b/app/lib/shell/windows/window_store.dart index 9417e0f0..a5172251 100644 --- a/app/lib/shell/windows/window_store.dart +++ b/app/lib/shell/windows/window_store.dart @@ -20,12 +20,16 @@ class WindowsRecord { this.windows = const [], this.hiddenGroups = const {}, this.frames = const {}, + this.sizes = const {}, }); final List windows; final Set hiddenGroups; final Map frames; + /// The last size given to a window of each kind. + final Map sizes; + String encode(WindowKinds kinds) => jsonEncode({ 'version': 1, 'windows': [ @@ -47,6 +51,10 @@ class WindowsRecord { for (final MapEntry(:key, :value) in frames.entries) key: _encodeRect(value), }, + 'sizes': { + for (final MapEntry(:key, :value) in sizes.entries) + key: [value.width, value.height], + }, }); /// Null when [source] is not a record. A window of an unregistered kind, @@ -86,6 +94,16 @@ class WindowsRecord { if (_identity(g, 256)) g as String, }, frames: frames, + sizes: { + if (data['sizes'] case final Map raw) + for (final MapEntry(:key, :value) in raw.entries) + if (key is String && _identity(key, 64)) + if (value case [ + final num w, + final num h, + ] when w.isFinite && h.isFinite && w > 0 && h > 0) + key: Size(w.toDouble(), h.toDouble()), + }, ); } on Object { return null; diff --git a/app/lib/shell/windows/windows_menu_button.dart b/app/lib/shell/windows/windows_menu_button.dart new file mode 100644 index 00000000..ff8936f9 --- /dev/null +++ b/app/lib/shell/windows/windows_menu_button.dart @@ -0,0 +1,91 @@ +import 'package:flutter/material.dart'; + +import '../../ui/adaptive/app_menu_anchor.dart'; +import 'window_manager.dart'; +import 'window_shortcuts.dart'; + +/// The shell bar's Windows button, shown while the app has floating windows: +/// a menu listing each one (choosing it shows it and brings it to the +/// front), then Show all, Minimise all and Close all. +class WindowsMenuButton extends StatelessWidget { + const WindowsMenuButton({super.key, required this.manager}); + + final WindowManager manager; + + @override + Widget build(BuildContext context) => ListenableBuilder( + listenable: Listenable.merge([ + manager, + for (final kind in manager.kinds.all) ?kind.changes, + ]), + builder: (context, _) { + final windows = manager.windows + .where((w) => manager.kinds[w.kind] != null) + .toList() + .reversed + .toList(); + if (windows.isEmpty) return const SizedBox.shrink(); + final active = manager.active?.id; + return AppMenuAnchor( + menuChildren: [ + for (final entry in windows) + MenuItemButton( + key: ValueKey('windows-menu-${entry.id}'), + leadingIcon: Icon(manager.kinds[entry.kind]!.icon), + trailingIcon: entry.id == active + ? const Icon(Icons.check) + : !manager.isVisible(entry) + ? const Icon(Icons.minimize) + : null, + onPressed: () => manager.show(entry.id), + child: ConstrainedBox( + constraints: const BoxConstraints(maxWidth: 280), + child: Text( + manager.kinds[entry.kind]!.title(context, entry), + maxLines: 1, + overflow: TextOverflow.ellipsis, + semanticsLabel: + '${manager.kinds[entry.kind]!.title(context, entry)}' + '${manager.isVisible(entry) ? '' : ', minimised'}', + ), + ), + ), + const Divider(height: 8), + MenuItemButton( + leadingIcon: const Icon(Icons.open_in_full), + onPressed: windows.every(manager.isVisible) + ? null + : manager.showAll, + child: const Text('Show all'), + ), + MenuItemButton( + leadingIcon: const Icon(Icons.minimize), + onPressed: windows.any(manager.isVisible) + ? manager.minimizeAll + : null, + child: const Text('Minimise all'), + ), + MenuItemButton( + leadingIcon: const Icon(Icons.close), + onPressed: manager.closeAll, + child: const Text('Close all'), + ), + if (windows.length > 1) + Padding( + padding: const EdgeInsets.fromLTRB(12, 8, 12, 4), + child: Text( + 'Next window: ${windowCycleShortcutLabel()}', + style: Theme.of(context).textTheme.bodySmall, + ), + ), + ], + builder: (context, menu, _) => IconButton( + key: const ValueKey('shell.windows'), + tooltip: 'Windows (${windows.length})', + onPressed: () => menu.isOpen ? menu.close() : menu.open(), + icon: const Icon(Icons.web_asset_outlined), + ), + ); + }, + ); +} diff --git a/app/notes/floating-windows.md b/app/notes/floating-windows.md index 068b4cd2..f02bd8c5 100644 --- a/app/notes/floating-windows.md +++ b/app/notes/floating-windows.md @@ -84,3 +84,23 @@ float, panel height); the bottom panel is still a shell panel. over Runs: the window, its options menu, tooltips, a second tab, typing, hide and show by Alt+T, maximise, move, minimise, and the frame after a reload. + +## Window conveniences (Phase 4) + +- **Windows menu** (`windows_menu_button.dart`): a bar button on every tab + while the app has windows. It lists each window (topmost first; a tick on the + active one, a minimise mark on hidden ones); choosing one shows it and brings + it to the front. Then Show all, Minimise all and Close all. +- **Cycle chord** (`window_shortcuts.dart`): Alt+` brings the next window to the + front and Alt+Shift+` the previous one, from anywhere, when two or more + windows show and no popup covers the app. Ctrl+` stays the terminal's. +- **Snapping**: dropping a title with the pointer within 6 px of the app's left + or right edge fills that half; at the top edge it maximises. +- **Size per kind**: a new window of a kind opens at the size that kind was last + given by a move or resize (not a snapped or maximised one), placed by the + cascade. Saved with the windows (`sizes`). +- Double click on a title maximises for every kind (since Phase 1). +- Captured 2026-11-11 at 1280x800 and 1920x1080, light and dark, over Chat and + over Runs: the button and its tooltip, the menu, a hovered item, choosing a + window, cycling, Minimise all, the menu with minimised windows, Show all, and + snapping left, right and to the top. diff --git a/app/test/shell/windows/window_conveniences_test.dart b/app/test/shell/windows/window_conveniences_test.dart new file mode 100644 index 00000000..2ab6b275 --- /dev/null +++ b/app/test/shell/windows/window_conveniences_test.dart @@ -0,0 +1,272 @@ +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 read() async => value; + @override + Future write(String record) async { + value = record; + return true; + } +} + +class _Keyboard extends HardwareKeyboard { + _Keyboard(this.pressed); + final Set pressed; + @override + bool isLogicalKeyPressed(LogicalKeyboardKey key) => pressed.contains(key); +} + +const _backquote = KeyDownEvent( + physicalKey: PhysicalKeyboardKey.backquote, + logicalKey: LogicalKeyboardKey.backquote, + timeStamp: Duration.zero, +); + +Future _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 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()?.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); + }); +} diff --git a/app/test/shell/windows/window_host_test.dart b/app/test/shell/windows/window_host_test.dart index dec28079..750c9ccc 100644 --- a/app/test/shell/windows/window_host_test.dart +++ b/app/test/shell/windows/window_host_test.dart @@ -85,16 +85,16 @@ void main() { ); await tester.drag( find.byKey(const ValueKey('window-title')), - const Offset(5000, 5000), + const Offset(600, 5000), ); await tester.pumpAndSettle(); expect( windowRect(tester, 'Probe a'), - const Rect.fromLTWH(880, 640, 400, 160), + const Rect.fromLTWH(750, 640, 400, 160), ); expect( manager.byId(entry.id)!.frame, - const Rect.fromLTWH(880, 640, 400, 160), + const Rect.fromLTWH(750, 640, 400, 160), ); });