Files
pipelines/app/lib/ui/adaptive/anchored_menu_layout.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

127 lines
4.0 KiB
Dart

import 'dart:math' as math;
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
/// Places an anchored menu panel below its anchor, aligned to the anchor's
/// leading edge, or above it when it fits better there, and always inside
/// the app's safe area.
///
/// [anchor] and the layout share one coordinate space: the overlay the menu
/// route fills. The system insets ([insets]: a status bar, a cutout, a
/// gesture bar) are therefore only bounds the panel stays inside, never an
/// offset. Wrapping the layout in a `SafeArea` shifted the panel down by the
/// top inset while the anchor stayed put, which on an unfolded foldable
/// opened a top bar's menus far below the bar. A hinge that splits the
/// screen ([avoid]) confines the panel to the anchor's side.
class AnchoredMenuLayout extends SingleChildLayoutDelegate {
AnchoredMenuLayout({
required this.anchor,
required this.width,
this.insets = EdgeInsets.zero,
this.avoid = const [],
});
final Rect anchor;
final double width;
final EdgeInsets insets;
final List<Rect> avoid;
static const double margin = 8;
static const double gap = 4;
/// Less room than this on both sides means the anchor is not a button but
/// a region (a pane's bar, a whole pane): the panel then opens over it,
/// from its top.
static const double minRoom = 200;
/// Where the panel may go in an overlay of [size].
Rect bounds(Size size) {
var screen = Offset.zero & size;
if (avoid.isNotEmpty) {
Rect? closest;
var best = double.infinity;
for (final candidate in DisplayFeatureSubScreen.subScreensInBounds(
screen,
avoid,
)) {
final distance = _distance(anchor.center, candidate);
if (distance < best) {
best = distance;
closest = candidate;
}
}
screen = closest ?? screen;
}
final inner = insets.deflateRect(screen).deflate(margin);
// An area too small for the insets and margins collapses, never inverts.
return Rect.fromLTRB(
inner.left,
inner.top,
math.max(inner.left, inner.right),
math.max(inner.top, inner.bottom),
);
}
static double _distance(Offset point, Rect rect) {
final dx = math.max(
0.0,
math.max(rect.left - point.dx, point.dx - rect.right),
);
final dy = math.max(
0.0,
math.max(rect.top - point.dy, point.dy - rect.bottom),
);
return math.sqrt(dx * dx + dy * dy);
}
double _below(Rect area) => area.bottom - anchor.bottom - gap;
double _above(Rect area) => anchor.top - gap - area.top;
bool _over(Rect area) => _below(area) < minRoom && _above(area) < minRoom;
@override
BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
final area = bounds(constraints.biggest);
final below = _below(area);
final above = _above(area);
final double room;
if (_over(area)) {
room = area.bottom - anchor.top.clamp(area.top, area.bottom);
} else {
room = below >= minRoom || below >= above ? below : above;
}
final panelWidth = width.clamp(0.0, area.width);
return BoxConstraints(
minWidth: panelWidth,
maxWidth: panelWidth,
maxHeight: room.clamp(0.0, area.height),
);
}
@override
Offset getPositionForChild(Size size, Size childSize) {
final area = bounds(size);
final double top;
if (_over(area)) {
top = anchor.top + gap;
} else if (childSize.height <= _below(area)) {
top = anchor.bottom + gap;
} else {
top = anchor.top - gap - childSize.height;
}
final maxTop = math.max(area.top, area.bottom - childSize.height);
final maxLeft = math.max(area.left, area.right - childSize.width);
return Offset(
anchor.left.clamp(area.left, maxLeft),
top.clamp(area.top, maxTop),
);
}
@override
bool shouldRelayout(AnchoredMenuLayout oldDelegate) =>
oldDelegate.anchor != anchor ||
oldDelegate.width != width ||
oldDelegate.insets != insets ||
!listEquals(oldDelegate.avoid, avoid);
}