Files
pipelines/app/lib/ui/adaptive/app_menu_anchor.dart
T
alan 16afda3634 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.
2026-11-10 21:33:07 +00:00

83 lines
2.2 KiB
Dart

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,
);
}