diff --git a/app/lib/features/chat/conversation/chat_effort_control.dart b/app/lib/features/chat/conversation/chat_effort_control.dart index 8ebe1e5e..ff86f1de 100644 --- a/app/lib/features/chat/conversation/chat_effort_control.dart +++ b/app/lib/features/chat/conversation/chat_effort_control.dart @@ -1,3 +1,4 @@ +import '../../../ui/adaptive/app_menu_anchor.dart'; import 'dart:async'; import 'package:flutter/cupertino.dart'; @@ -158,9 +159,7 @@ class _ChatEffortControlState extends State { ), ); } - return MenuAnchor( - // Above every floating window, and not clipped to one. - useRootOverlay: true, + return AppMenuAnchor( menuChildren: [ Padding( padding: const EdgeInsets.fromLTRB(12, 8, 12, 4), diff --git a/app/lib/features/chat/conversation/chat_thread_view.dart b/app/lib/features/chat/conversation/chat_thread_view.dart index 5aa31dfc..c6a03f99 100644 --- a/app/lib/features/chat/conversation/chat_thread_view.dart +++ b/app/lib/features/chat/conversation/chat_thread_view.dart @@ -301,7 +301,18 @@ class _ChatThreadViewState extends State { _filesSheetOpen = true; final cupertino = chatControlsStyleOf(context) == ChatControlsStyle.cupertino; - final sheet = cupertino + // In a floating window the explorer is a page of that window, so a file + // it opens stays in the window too. + final sheet = WindowScope.maybeOf(context) != null + ? Navigator.of(context).push( + MaterialPageRoute( + builder: (_) => Scaffold( + appBar: AppBar(title: const Text('Files')), + body: FilesPanel(controller: _files), + ), + ), + ) + : cupertino ? showCupertinoModalPopup( context: context, builder: (_) => Align( diff --git a/app/lib/features/pipelines/widgets/pipeline_controls.dart b/app/lib/features/pipelines/widgets/pipeline_controls.dart index deb81e3e..859b4bbc 100644 --- a/app/lib/features/pipelines/widgets/pipeline_controls.dart +++ b/app/lib/features/pipelines/widgets/pipeline_controls.dart @@ -1,3 +1,4 @@ +import '../../../ui/adaptive/app_menu_anchor.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; @@ -108,9 +109,7 @@ class PipelineSortButton extends StatelessWidget { ), ); } - return MenuAnchor( - // Above every floating window, and not clipped to one. - useRootOverlay: true, + return AppMenuAnchor( menuChildren: [ for (final option in PipelineSort.values) MenuItemButton( diff --git a/app/lib/shell/windows/window_focus.dart b/app/lib/shell/windows/window_focus.dart index 8be7d9ca..d3d55f9e 100644 --- a/app/lib/shell/windows/window_focus.dart +++ b/app/lib/shell/windows/window_focus.dart @@ -40,18 +40,29 @@ class WindowFocus { : current; } - /// Focuses [id]'s window once this frame has built it. + /// Whether something covers the windows and owns the keyboard (a sheet, + /// dialog or page over the app); set by the host. + bool Function()? covered; + + /// Focuses [id]'s window once this frame has built it, unless a popup the + /// same press opened has covered the windows meanwhile. void focusSoon(String id) { WidgetsBinding.instance ..addPostFrameCallback((_) { - final scope = _scopes[id]; - if (scope != null && scope.context != null && scope.canRequestFocus) { - scope.requestFocus(); - } + if (covered?.call() ?? false) return; + focusNow(id); }) ..ensureVisualUpdate(); } + /// Focuses [id]'s window if it is built. + void focusNow(String id) { + final scope = _scopes[id]; + if (scope != null && scope.context != null && scope.canRequestFocus) { + scope.requestFocus(); + } + } + /// Gives the keyboard back to what had it before the windows showed, if /// a window has it now. void release() { diff --git a/app/lib/shell/windows/window_host.dart b/app/lib/shell/windows/window_host.dart index 357bb895..5a13edbf 100644 --- a/app/lib/shell/windows/window_host.dart +++ b/app/lib/shell/windows/window_host.dart @@ -33,6 +33,9 @@ class _WindowHostState extends State { void initState() { super.initState(); widget.manager.addListener(_changed); + // The shell page is not the top route while a popup or page covers it. + _focus.covered = () => + mounted && !(ModalRoute.of(context)?.isCurrent ?? true); WidgetsBinding.instance.addPostFrameCallback((_) => _changed()); } diff --git a/app/lib/shell/windows/window_layer.dart b/app/lib/shell/windows/window_layer.dart index b30e78b8..5d7b927d 100644 --- a/app/lib/shell/windows/window_layer.dart +++ b/app/lib/shell/windows/window_layer.dart @@ -117,9 +117,12 @@ class _WindowLayerState extends State ); } + /// A press brings the window to the top and gives it the keyboard at + /// once, before the press does anything else (focus a field, open a + /// popup that takes the keyboard itself). void _pressed() { + if (!widget.focus.has(_id)) widget.focus.focusNow(_id); widget.manager.raise(_id); - if (!widget.focus.has(_id)) widget.focus.focusSoon(_id); } @override @@ -203,6 +206,7 @@ class _WindowLayerState extends State ], ); final center = widget.bounds.center(Offset.zero); + final current = ModalRoute.of(context)?.isCurrent ?? true; return AnimatedBuilder( animation: _reveal, builder: (context, child) { @@ -213,7 +217,9 @@ class _WindowLayerState extends State enabled: !offstage, child: SurfaceVisibility( shown: !offstage, - current: !hiding, + // Not while a page, sheet or dialog covers the app: the + // window's own route is always its navigator's current one. + current: !hiding && current, child: IgnorePointer( ignoring: hiding, child: ExcludeFocus(excluding: hiding, child: child!), diff --git a/app/lib/ui/adaptive/app_menu_anchor.dart b/app/lib/ui/adaptive/app_menu_anchor.dart new file mode 100644 index 00000000..e1aa31df --- /dev/null +++ b/app/lib/ui/adaptive/app_menu_anchor.dart @@ -0,0 +1,82 @@ +import 'package:flutter/material.dart'; + +/// The app's anchored menu: a [MenuAnchor] whose panel is in the root +/// overlay, so it opens above every floating window and is never clipped to +/// one, and which takes the keyboard while open, so Escape closes it and the +/// arrow keys move through it wherever the keyboard was. Closing gives the +/// keyboard back to what had it. +/// +/// Use it instead of [MenuAnchor] everywhere: any content can end up in a +/// window, where only the root overlay reaches past the window's edges. +class AppMenuAnchor extends StatefulWidget { + const AppMenuAnchor({ + super.key, + required this.menuChildren, + required this.builder, + this.controller, + this.style, + this.alignmentOffset, + this.child, + }); + + final List menuChildren; + final MenuAnchorChildBuilder builder; + final MenuController? controller; + final MenuStyle? style; + final Offset? alignmentOffset; + final Widget? child; + + @override + State createState() => _AppMenuAnchorState(); +} + +class _AppMenuAnchorState extends State { + final FocusNode _focus = FocusNode( + debugLabel: 'menu anchor', + skipTraversal: true, + ); + FocusNode? _previous; + + @override + void dispose() { + _focus.dispose(); + super.dispose(); + } + + void _opened() { + final current = FocusManager.instance.primaryFocus; + _previous = identical(current, _focus) ? null : current; + _focus.requestFocus(); + } + + void _closed() { + final previous = _previous; + _previous = null; + if (!_focus.hasPrimaryFocus) return; + if (previous != null && + previous.context != null && + previous.canRequestFocus) { + previous.requestFocus(); + } else { + _focus.unfocus(); + } + } + + @override + Widget build(BuildContext context) => MenuAnchor( + useRootOverlay: true, + controller: widget.controller, + style: widget.style, + alignmentOffset: widget.alignmentOffset, + childFocusNode: _focus, + onOpen: _opened, + onClose: _closed, + menuChildren: widget.menuChildren, + builder: (context, menu, child) => Focus( + focusNode: _focus, + includeSemantics: false, + child: widget.builder(context, menu, child), + ), + child: widget.child, + ); +} diff --git a/app/lib/ui/adaptive/segmented_bar_overflow.dart b/app/lib/ui/adaptive/segmented_bar_overflow.dart index 64ba8173..dffdfe12 100644 --- a/app/lib/ui/adaptive/segmented_bar_overflow.dart +++ b/app/lib/ui/adaptive/segmented_bar_overflow.dart @@ -1,3 +1,4 @@ +import 'app_menu_anchor.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; @@ -98,9 +99,7 @@ class SegmentedMoreButton extends StatelessWidget { ), ); } - return MenuAnchor( - // Above every floating window, and not clipped to one. - useRootOverlay: true, + return AppMenuAnchor( menuChildren: [ for (final entry in entries) if (entry.choices.isEmpty) diff --git a/app/test/shell/windows/window_layering_test.dart b/app/test/shell/windows/window_layering_test.dart index 55fe0163..a0371596 100644 --- a/app/test/shell/windows/window_layering_test.dart +++ b/app/test/shell/windows/window_layering_test.dart @@ -7,6 +7,8 @@ import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/shell/windows/window_manager.dart'; +import 'package:pipelines_app/ui/motion/surface_visibility.dart'; + import 'window_test_harness.dart'; /// Layering acceptance (docs/plans/floating-windows.md, Phase 1.4): every @@ -210,6 +212,31 @@ void main() { expect(find.text('Sheet item').hitTestable(), findsOneWidget); }); + testWidgets('a popup opened by the press that focuses its window keeps the ' + 'keyboard', (tester) async { + await open(tester, 0); + // The keyboard is in the app behind, as after using another tab. + FocusManager.instance.primaryFocus?.unfocus(); + await tester.pump(); + await tester.tap(find.byKey(const ValueKey('b-sheet'))); + await tester.pumpAndSettle(); + expect(find.text('Sheet item').hitTestable(), findsOneWidget); + final focus = FocusManager.instance.primaryFocus!.context!; + expect(ModalRoute.of(focus), isA>()); + await tester.sendKeyEvent(LogicalKeyboardKey.escape); + await tester.pumpAndSettle(); + expect(find.text('Sheet item'), findsNothing); + // The window content is not the current surface under a popup. + await tester.tap(find.byKey(const ValueKey('b-dialog'))); + await tester.pumpAndSettle(); + expect( + SurfaceVisibility.currentOf( + tester.element(find.byKey(const ValueKey('content-b'))), + ), + isFalse, + ); + }); + testWidgets('Escape closes the popup, not the window', (tester) async { await open(tester, 0); await tester.tap(find.byKey(const ValueKey('a-dialog'))); diff --git a/app/test/shell/windows/window_popup_rules_test.dart b/app/test/shell/windows/window_popup_rules_test.dart index 5178a005..a6dea366 100644 --- a/app/test/shell/windows/window_popup_rules_test.dart +++ b/app/test/shell/windows/window_popup_rules_test.dart @@ -3,23 +3,20 @@ import 'dart:io'; import 'package:flutter_test/flutter_test.dart'; /// Window content takes input only inside its window (see `WindowNavigator`), -/// so a `MenuAnchor` panel, which may extend past the window, must be in the -/// root overlay. Any content can end up in a window, so every `MenuAnchor` in -/// the app opts in. +/// so a menu panel, which may extend past the window, must be in the root +/// overlay. Any content can end up in a window, so the app's menus are all +/// `AppMenuAnchor`, the one place a `MenuAnchor` is built. void main() { - test('every MenuAnchor targets the root overlay', () { + test('MenuAnchor is used only through AppMenuAnchor', () { final offenders = []; for (final file in Directory('lib').listSync(recursive: true)) { if (file is! File || !file.path.endsWith('.dart')) continue; + if (file.path.endsWith('ui/adaptive/app_menu_anchor.dart')) continue; final source = file.readAsStringSync(); for (final match in RegExp(r'\bMenuAnchor\(').allMatches(source)) { - final end = (match.end + 400).clamp(0, source.length); - if (!source - .substring(match.end, end) - .contains('useRootOverlay: true')) { - final line = '\n'.allMatches(source.substring(0, match.start)).length; - offenders.add('${file.path}:${line + 1}'); - } + if (match.start > 0 && source[match.start - 1] == 'p') continue; + final line = '\n'.allMatches(source.substring(0, match.start)).length; + offenders.add('${file.path}:${line + 1}'); } } expect(offenders, isEmpty); diff --git a/app/test/ui/adaptive/app_menu_anchor_test.dart b/app/test/ui/adaptive/app_menu_anchor_test.dart new file mode 100644 index 00000000..19cc9043 --- /dev/null +++ b/app/test/ui/adaptive/app_menu_anchor_test.dart @@ -0,0 +1,61 @@ +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/ui/adaptive/app_menu_anchor.dart'; + +void main() { + testWidgets('Escape closes the menu and the keyboard returns to the field', ( + tester, + ) async { + var picked = 0; + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Column( + children: [ + const TextField(key: ValueKey('field')), + AppMenuAnchor( + menuChildren: [ + MenuItemButton( + onPressed: () => picked++, + child: const Text('First'), + ), + MenuItemButton( + onPressed: () => picked += 10, + child: const Text('Second'), + ), + ], + builder: (context, menu, _) => IconButton( + tooltip: 'More', + onPressed: () => menu.isOpen ? menu.close() : menu.open(), + icon: const Icon(Icons.more_vert), + ), + ), + ], + ), + ), + ), + ); + await tester.tap(find.byKey(const ValueKey('field'))); + await tester.pump(); + final field = FocusManager.instance.primaryFocus; + + await tester.tap(find.byTooltip('More')); + await tester.pumpAndSettle(); + expect(find.text('First'), findsOneWidget); + await tester.sendKeyEvent(LogicalKeyboardKey.escape); + await tester.pumpAndSettle(); + expect(find.text('First'), findsNothing); + expect(FocusManager.instance.primaryFocus, same(field)); + + // The arrow keys and Enter work from wherever the keyboard was. + await tester.tap(find.byTooltip('More')); + await tester.pumpAndSettle(); + await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown); + await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown); + await tester.sendKeyEvent(LogicalKeyboardKey.enter); + await tester.pumpAndSettle(); + expect(picked, 10); + expect(find.text('First'), findsNothing); + }); +}