diff --git a/app/lib/ui/adaptive/app_popups.dart b/app/lib/ui/adaptive/app_popups.dart index a4f8212a..b0834334 100644 --- a/app/lib/ui/adaptive/app_popups.dart +++ b/app/lib/ui/adaptive/app_popups.dart @@ -20,6 +20,8 @@ bool usesDesktopPopups(BuildContext context) { /// /// 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)`. @@ -169,38 +171,59 @@ class _AnchoredMenuLayout extends SingleChildLayoutDelegate { 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; + + double _below(Size size) => size.height - anchor.bottom - gap - margin; + double _above() => anchor.top - gap - margin; + bool _over(Size size) => _below(size) < minRoom && _above() < minRoom; + @override BoxConstraints getConstraintsForChild(BoxConstraints constraints) { final available = constraints.biggest; - final below = available.height - anchor.bottom - gap - margin; - final above = anchor.top - gap - margin; - final height = (below >= 240 || below >= above ? below : above).clamp( - 0.0, - available.height - 2 * margin, - ); + final below = _below(available); + final above = _above(); + final double room; + if (_over(available)) { + room = + available.height - + anchor.top.clamp(margin, available.height) - + margin; + } else { + room = below >= minRoom || below >= above ? below : above; + } final maxWidth = (available.width - 2 * margin).clamp(0.0, double.infinity); return BoxConstraints( minWidth: width.clamp(0.0, maxWidth), maxWidth: width.clamp(0.0, maxWidth), - maxHeight: height, + maxHeight: room.clamp(0.0, available.height - 2 * margin), ); } @override Offset getPositionForChild(Size size, Size childSize) { - final below = size.height - anchor.bottom - gap - margin; - final fitsBelow = childSize.height <= below; - final top = fitsBelow - ? anchor.bottom + gap - : (anchor.top - gap - childSize.height).clamp( - margin, - size.height - margin - childSize.height, - ); - final left = anchor.left.clamp( + final double top; + if (_over(size)) { + top = anchor.top + gap; + } else if (childSize.height <= _below(size)) { + top = anchor.bottom + gap; + } else { + top = anchor.top - gap - childSize.height; + } + final maxTop = (size.height - margin - childSize.height).clamp( margin, - (size.width - margin - childSize.width).clamp(margin, double.infinity), + double.infinity, + ); + final maxLeft = (size.width - margin - childSize.width).clamp( + margin, + double.infinity, + ); + return Offset( + anchor.left.clamp(margin, maxLeft), + top.clamp(margin, maxTop), ); - return Offset(left, top); } @override diff --git a/app/test/ui/adaptive/app_popups_test.dart b/app/test/ui/adaptive/app_popups_test.dart index 762f6e1d..45428d04 100644 --- a/app/test/ui/adaptive/app_popups_test.dart +++ b/app/test/ui/adaptive/app_popups_test.dart @@ -102,6 +102,42 @@ void main() { expect(menu.top, greaterThanOrEqualTo(8)); }); + testWidgets('wide: anchored to a whole pane, the menu opens over its top', ( + tester, + ) async { + _size(tester, const Size(1280, 800)); + await tester.pumpWidget( + MaterialApp( + home: Padding( + padding: const EdgeInsets.only(left: 400), + child: Builder( + builder: (pane) => Center( + child: TextButton( + onPressed: () => showAppChoiceMenu( + pane, + width: 420, + builder: (_) => const SizedBox( + height: 300, + child: Center(child: Text('Picker')), + ), + ), + child: const Text('Open'), + ), + ), + ), + ), + ), + ); + await tester.tap(find.text('Open')); + await tester.pumpAndSettle(); + final menu = tester.getRect(_menu); + expect(menu.size.width, 420); + expect(menu.height, greaterThanOrEqualTo(300)); + expect(menu.left, 400); + expect(menu.top, lessThan(40)); + expect(find.text('Picker').hitTestable(), findsOneWidget); + }); + testWidgets('wide: details are a dialog', (tester) async { _size(tester, const Size(1280, 800)); await tester.pumpWidget(_app());