Files
pipelines/app/lib/ui/adaptive/app_popups.dart
T
alan 8c224fecf8 Anchored menus: bound by system insets instead of shifting by them
The anchored choice menu route wrapped its layout in a SafeArea while the
anchor rect is in overlay coordinates, so on Android (edge to edge, a
status bar inset) every top bar menu opened an inset below its control.
The insets and a half-opened hinge now only bound the panel.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 21:19:55 +00:00

217 lines
7.2 KiB
Dart

import 'package:flutter/material.dart';
import '../motion/motion.dart';
import 'anchored_menu_layout.dart';
/// The narrowest app that gets desktop popups: the width where a list shows
/// beside its open item (`kListDetailMinWidth`).
const double kDesktopPopupMinWidth = 600;
/// Whether popups here are desktop ones (a menu anchored to its button, a
/// dialog) rather than a phone's bottom sheets. It is the app's width that
/// decides, not the surface asking: content in a narrow floating window on a
/// wide app still gets menus and dialogs.
bool usesDesktopPopups(BuildContext context) {
final root = Navigator.maybeOf(context, rootNavigator: true)?.context;
return MediaQuery.sizeOf(root ?? context).width >= kDesktopPopupMinWidth;
}
/// Shows a short list of choices (a mode, a sort order, a pane to add, a
/// row's actions) built by [builder].
///
/// On a wide app it is a menu panel anchored to [anchor] (the button that
/// opened it): below it, or above when there is no room, kept inside the app.
/// When [anchor] is a region rather than a button (a pane), the panel opens
/// over its top left.
/// On a phone it is a bottom sheet. Either way it is on the root navigator,
/// so it is above every floating window. Escape, Back or a press outside
/// closes it; the content closes it with `Navigator.of(context).pop(value)`.
Future<T?> showAppChoiceMenu<T>(
BuildContext anchor, {
required WidgetBuilder builder,
double width = 320,
String? semanticLabel,
}) {
final navigator = Navigator.of(anchor, rootNavigator: true);
if (!usesDesktopPopups(anchor)) {
return showModalBottomSheet<T>(
context: anchor,
useRootNavigator: true,
showDragHandle: true,
isScrollControlled: true,
useSafeArea: true,
builder: builder,
);
}
final box = anchor.findRenderObject()! as RenderBox;
final overlay = navigator.overlay!.context.findRenderObject()! as RenderBox;
final rect = box.localToGlobal(Offset.zero, ancestor: overlay) & box.size;
return navigator.push<T>(
_AnchoredMenuRoute<T>(
anchor: rect,
width: width,
builder: builder,
label: semanticLabel ?? 'Menu',
themes: InheritedTheme.capture(from: anchor, to: navigator.context),
duration: Motion.of(anchor, Motion.quick),
),
);
}
/// A choice row shared by the app's anchored choice menus. Keeping the row
/// here makes checked state and tap affordances consistent across selectors.
class AppChoiceMenuItem extends StatelessWidget {
const AppChoiceMenuItem({
super.key,
required this.child,
required this.selected,
required this.onTap,
this.contentPadding,
});
final Widget child;
final bool selected;
final VoidCallback? onTap;
final EdgeInsetsGeometry? contentPadding;
@override
Widget build(BuildContext context) => ListTile(
enabled: onTap != null,
title: child,
contentPadding: contentPadding,
trailing: selected
? Icon(Icons.check, color: Theme.of(context).colorScheme.primary)
: null,
onTap: onTap,
);
}
/// Shows richer content (a chat's details, a file's details) built by
/// [builder]: a dialog on a wide app, a bottom sheet on a phone, on the root
/// navigator either way. The content sizes itself (a `Column` with
/// `MainAxisSize.min`); the dialog scrolls it when it is taller than the app.
Future<T?> showAppDetails<T>(
BuildContext context, {
required WidgetBuilder builder,
double maxWidth = 560,
}) {
if (!usesDesktopPopups(context)) {
return showModalBottomSheet<T>(
context: context,
useRootNavigator: true,
showDragHandle: true,
isScrollControlled: true,
useSafeArea: true,
builder: builder,
);
}
return showDialog<T>(
context: context,
useRootNavigator: true,
builder: (dialog) => Dialog(
clipBehavior: Clip.antiAlias,
child: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: maxWidth,
maxHeight: MediaQuery.sizeOf(dialog).height * 0.85,
),
// Content taller than the app scrolls inside the dialog.
child: SingleChildScrollView(
padding: const EdgeInsets.only(top: 16),
child: builder(dialog),
),
),
),
);
}
class _AnchoredMenuRoute<T> extends PopupRoute<T> {
_AnchoredMenuRoute({
required this.anchor,
required this.width,
required this.builder,
required this.label,
required this.themes,
required this.duration,
});
final Rect anchor;
final double width;
final WidgetBuilder builder;
final String label;
final CapturedThemes themes;
final Duration duration;
@override
Color? get barrierColor => null;
@override
bool get barrierDismissible => true;
@override
String? get barrierLabel => 'Dismiss';
@override
Duration get transitionDuration => duration;
@override
Widget buildPage(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) {
final colors = Theme.of(context).colorScheme;
final media = MediaQuery.of(context);
// The anchor is in the overlay's coordinates, which this page fills: the
// system insets bound the panel but must not shift it (a SafeArea here
// opened menus an inset below their anchor on Android). The panel itself
// is clear of them, so content that pads itself for a bottom sheet
// (a SafeArea) adds nothing inside it.
return CustomSingleChildLayout(
delegate: AnchoredMenuLayout(
anchor: anchor,
width: width,
insets:
media.padding + EdgeInsets.only(bottom: media.viewInsets.bottom),
avoid: DisplayFeatureSubScreen.avoidBounds(media).toList(),
),
child: MediaQuery.removePadding(
context: context,
removeLeft: true,
removeTop: true,
removeRight: true,
removeBottom: true,
child: FadeTransition(
opacity: CurvedAnimation(parent: animation, curve: Motion.curve),
child: Semantics(
scopesRoute: true,
namesRoute: true,
explicitChildNodes: true,
label: label,
child: Material(
key: const ValueKey('app-choice-menu'),
// A design's menus (Terminal's flat frame, Paper's whisper
// of shadow) shape this panel too.
elevation: Theme.of(context).popupMenuTheme.elevation ?? 3,
shadowColor: Theme.of(context).popupMenuTheme.shadowColor,
// A step above a window's own surface, so the panel reads
// as raised when it opens over one.
color:
Theme.of(context).popupMenuTheme.color ??
colors.surfaceContainerHigh,
surfaceTintColor: Colors.transparent,
shape:
Theme.of(context).popupMenuTheme.shape ??
const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(12)),
),
clipBehavior: Clip.antiAlias,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: themes.wrap(Builder(builder: builder)),
),
),
),
),
),
);
}
}