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
+16 -5
View File
@@ -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() {
+3
View File
@@ -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());
}
+8 -2
View File
@@ -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!),