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:
@@ -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<ChatEffortControl> {
|
||||
),
|
||||
);
|
||||
}
|
||||
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),
|
||||
|
||||
@@ -301,7 +301,18 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
_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<void>(
|
||||
MaterialPageRoute<void>(
|
||||
builder: (_) => Scaffold(
|
||||
appBar: AppBar(title: const Text('Files')),
|
||||
body: FilesPanel(controller: _files),
|
||||
),
|
||||
),
|
||||
)
|
||||
: cupertino
|
||||
? showCupertinoModalPopup<void>(
|
||||
context: context,
|
||||
builder: (_) => Align(
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -33,6 +33,9 @@ class _WindowHostState extends State<WindowHost> {
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
@@ -117,9 +117,12 @@ class _WindowLayerState extends State<WindowLayer>
|
||||
);
|
||||
}
|
||||
|
||||
/// 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<WindowLayer>
|
||||
],
|
||||
);
|
||||
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<WindowLayer>
|
||||
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!),
|
||||
|
||||
@@ -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<Widget> menuChildren;
|
||||
final MenuAnchorChildBuilder builder;
|
||||
final MenuController? controller;
|
||||
final MenuStyle? style;
|
||||
final Offset? alignmentOffset;
|
||||
final Widget? child;
|
||||
|
||||
@override
|
||||
State<AppMenuAnchor> createState() => _AppMenuAnchorState();
|
||||
}
|
||||
|
||||
class _AppMenuAnchorState extends State<AppMenuAnchor> {
|
||||
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,
|
||||
);
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user