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,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)
|
||||
|
||||
Reference in New Issue
Block a user