Close menus with Escape and keep a window's popups focused

AppMenuAnchor is the app's one MenuAnchor: root overlay, takes the keyboard
while open (Escape and arrows work wherever focus was) and gives it back.
A window no longer takes the keyboard back from a popup its own press
opened. In a window, Files opens as a page of that window and Chat details
is in the More menu.
This commit is contained in:
2026-11-10 21:33:07 +00:00
parent fdc730422d
commit 16afda3634
11 changed files with 223 additions and 28 deletions
@@ -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<PopupRoute<void>>());
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')));
@@ -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 = <String>[];
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);
@@ -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);
});
}