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:
@@ -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);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user