diff --git a/lib/widgets/chat_composer.dart b/lib/widgets/chat_composer.dart index 4f59efaa..1f07c2e5 100644 --- a/lib/widgets/chat_composer.dart +++ b/lib/widgets/chat_composer.dart @@ -1,3 +1,5 @@ +import 'dart:math' as math; + import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:provider/provider.dart'; @@ -132,6 +134,9 @@ class ChatComposer extends StatelessWidget { /// Marks the compose bubble (and the offline bar's bubble). static const bubbleKey = ValueKey('chat-composer-bubble'); + /// Marks the surface of the open "+" menu. + static const actionMenuKey = ValueKey('chat-composer-action-menu'); + const ChatComposer({ super.key, required this.controller, @@ -152,34 +157,25 @@ class ChatComposer extends StatelessWidget { final actions = actionsBuilder(); if (actions.isEmpty) return; final button = buttonContext.findRenderObject() as RenderBox; - final overlay = - Overlay.of(buttonContext).context.findRenderObject() as RenderBox; - final buttonOffset = button.localToGlobal(Offset.zero, ancestor: overlay); - final menuHeight = actions.length * kMinInteractiveDimension + 16; - final menuTop = (buttonOffset.dy - menuHeight).clamp( - 8.0, - overlay.size.height, - ); - final position = RelativeRect.fromLTRB( - buttonOffset.dx, - menuTop, - overlay.size.width - buttonOffset.dx - button.size.width, - overlay.size.height - buttonOffset.dy, - ); - final selected = await showMenu( - context: buttonContext, - position: position, - // Keep focus on the text field: if the menu route took focus, the - // keyboard would close, the composer would drop down and the menu would - // be left floating where the keyboard used to be. - requestFocus: false, - items: [ - for (final action in actions) - PopupMenuItem( - value: action.id, - child: MeshMenuItemRow(icon: action.icon, label: action.label), - ), - ], + final navigator = Navigator.of(buttonContext); + final overlay = navigator.overlay!.context.findRenderObject() as RenderBox; + final anchor = + button.localToGlobal(Offset.zero, ancestor: overlay) & button.size; + // The menu route never requests focus: if it took focus from the text + // field the keyboard would close, the composer would drop down and the + // menu would be left floating where the keyboard used to be. + final selected = await navigator.push( + _ComposerActionMenuRoute( + actions: actions, + anchor: anchor, + overlaySize: overlay.size, + capturedThemes: InheritedTheme.capture( + from: buttonContext, + to: navigator.context, + ), + barrierLabel: MaterialLocalizations.of(buttonContext).menuDismissLabel, + animate: !MediaQuery.disableAnimationsOf(buttonContext), + ), ); if (selected == null || !buttonContext.mounted) return; onAction(selected); @@ -585,3 +581,172 @@ class OfflineComposerBar extends StatelessWidget { ); } } + +/// The "+" menu: a popup that grows upward from the bottom-trailing corner, +/// just above the "+" button, so it opens out of the button instead of +/// sliding down toward it. +class _ComposerActionMenuRoute extends PopupRoute { + final List actions; + final Rect anchor; + final Size overlaySize; + final CapturedThemes capturedThemes; + final bool animate; + + @override + final String barrierLabel; + + _ComposerActionMenuRoute({ + required this.actions, + required this.anchor, + required this.overlaySize, + required this.capturedThemes, + required this.barrierLabel, + required this.animate, + }) : super(requestFocus: false); + + static const double _gap = 4; + static const double _margin = 8; + + @override + Color? get barrierColor => null; + + @override + bool get barrierDismissible => true; + + @override + Duration get transitionDuration => + animate ? const Duration(milliseconds: 250) : Duration.zero; + + @override + Duration get reverseTransitionDuration => + animate ? const Duration(milliseconds: 150) : Duration.zero; + + @override + Widget buildPage( + BuildContext context, + Animation animation, + Animation secondaryAnimation, + ) { + final direction = Directionality.of(context); + final padding = MediaQuery.paddingOf(context); + final end = direction == TextDirection.ltr + ? overlaySize.width - anchor.right + : anchor.left; + final maxHeight = math.max( + kMinInteractiveDimension, + anchor.top - _gap - padding.top - _margin, + ); + return capturedThemes.wrap( + Stack( + children: [ + Positioned.directional( + textDirection: direction, + end: math.max(_margin, end), + bottom: overlaySize.height - anchor.top + _gap, + child: ConstrainedBox( + constraints: BoxConstraints( + minWidth: 112, + maxWidth: math.max(112, math.min(280, overlaySize.width - 16)), + maxHeight: maxHeight, + ), + child: _ComposerActionMenu( + actions: actions, + animation: animation, + ), + ), + ), + ], + ), + ); + } +} + +class _ComposerActionMenu extends StatelessWidget { + final List actions; + final Animation animation; + + const _ComposerActionMenu({required this.actions, required this.animation}); + + @override + Widget build(BuildContext context) { + final theme = Theme.of(context); + final scheme = theme.colorScheme; + final menuTheme = PopupMenuTheme.of(context); + final items = IntrinsicWidth( + stepWidth: 56, + child: Semantics( + scopesRoute: true, + namesRoute: true, + explicitChildNodes: true, + label: MaterialLocalizations.of(context).popupMenuLabel, + child: SingleChildScrollView( + padding: const EdgeInsets.symmetric(vertical: 8), + child: DefaultTextStyle( + style: (menuTheme.textStyle ?? theme.textTheme.labelLarge!) + .copyWith(color: scheme.onSurface), + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + for (final action in actions) + InkWell( + onTap: () => Navigator.of(context).pop(action.id), + child: ConstrainedBox( + constraints: const BoxConstraints( + minHeight: kMinInteractiveDimension, + ), + child: Padding( + padding: const EdgeInsets.symmetric(horizontal: 16), + child: Align( + alignment: AlignmentDirectional.centerStart, + child: MeshMenuItemRow( + icon: action.icon, + label: action.label, + ), + ), + ), + ), + ), + ], + ), + ), + ), + ), + ); + return AnimatedBuilder( + animation: animation, + builder: (context, child) { + final t = animation.value; + final heightFactor = + (animation.status == AnimationStatus.reverse + ? Curves.easeIn + : Curves.easeOutCubic) + .transform(t); + return Opacity( + opacity: const Interval(0, 0.4).transform(t), + child: Material( + key: ChatComposer.actionMenuKey, + type: MaterialType.card, + color: menuTheme.color ?? scheme.surfaceContainerHigh, + elevation: menuTheme.elevation ?? 3, + shadowColor: menuTheme.shadowColor, + surfaceTintColor: Colors.transparent, + shape: + menuTheme.shape ?? + RoundedRectangleBorder( + borderRadius: BorderRadius.circular(MeshRadii.md), + ), + clipBehavior: Clip.antiAlias, + child: Align( + alignment: AlignmentDirectional.bottomEnd, + widthFactor: 1, + heightFactor: heightFactor, + child: child, + ), + ), + ); + }, + child: items, + ); + } +} diff --git a/test/widgets/chat_composer_test.dart b/test/widgets/chat_composer_test.dart index dd56e995..2e5fb072 100644 --- a/test/widgets/chat_composer_test.dart +++ b/test/widgets/chat_composer_test.dart @@ -65,6 +65,7 @@ Widget _app( Widget? replyPreview, bool disableAnimations = false, Brightness brightness = Brightness.light, + List? actions, }) { final composer = ChatComposer( controller: h.controller, @@ -72,13 +73,15 @@ Widget _app( maxBytes: maxBytes, onSend: () => h.sends++, sendTooltip: _l10n.chat_sendMessage, - actionsBuilder: () => [ - ChatComposerAction( - id: 'gif', - icon: Icons.gif_box, - label: _l10n.chat_sendGif, - ), - ], + actionsBuilder: () => + actions ?? + [ + ChatComposerAction( + id: 'gif', + icon: Icons.gif_box, + label: _l10n.chat_sendGif, + ), + ], onAction: h.actions.add, replyPreview: replyPreview, ); @@ -119,6 +122,7 @@ Widget _app( Finder get _plus => find.byTooltip(_l10n.chat_selectSendAction); Finder get _send => find.byTooltip(_l10n.chat_sendMessage); +Finder get _menu => find.byKey(ChatComposer.actionMenuKey); Finder get _bubble => find.byKey(ChatComposer.bubbleKey); Future _type(WidgetTester tester, String text) async { @@ -380,6 +384,133 @@ void main() { ); }); + testWidgets('+ menu opens above the button and grows upward from it', ( + tester, + ) async { + final h = await _harness(tester); + await tester.pumpWidget(_app(h)); + await tester.pumpAndSettle(); + final plusTop = tester.getTopLeft(_plus).dy; + final plusRight = tester.getTopRight(_plus).dx; + + await tester.tap(_plus); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 50)); + final early = tester.getRect(_menu); + await tester.pump(const Duration(milliseconds: 50)); + final later = tester.getRect(_menu); + expect(later.top, lessThan(early.top)); + expect(later.bottom, closeTo(early.bottom, 0.01)); + expect(later.height, greaterThan(early.height)); + + await tester.pumpAndSettle(); + final open = tester.getRect(_menu); + expect(open.bottom, closeTo(early.bottom, 0.01)); + expect(open.bottom, lessThanOrEqualTo(plusTop)); + // Trailing edges line up (the tap target is a little wider than the + // visible button). + expect(open.right, closeTo(plusRight, 3)); + expect(open.height, greaterThanOrEqualTo(kMinInteractiveDimension)); + // The card hugs a short label instead of filling the width cap. + expect(open.width, lessThan(280)); + + await tester.tapAt(const Offset(5, 5)); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 60)); + final closing = tester.getRect(_menu); + expect(closing.bottom, closeTo(open.bottom, 0.01)); + expect(closing.height, lessThan(open.height)); + await tester.pumpAndSettle(); + expect(_menu, findsNothing); + }); + + testWidgets('+ menu opens at once with animations disabled', (tester) async { + final h = await _harness(tester); + await tester.pumpWidget(_app(h, disableAnimations: true)); + await tester.pumpAndSettle(); + await tester.tap(_plus); + await tester.pump(); + await tester.pump(); + expect(find.text(_l10n.chat_sendGif), findsOneWidget); + expect( + tester.getRect(_menu).height, + greaterThanOrEqualTo(kMinInteractiveDimension), + ); + expect( + tester.getRect(_menu).bottom, + lessThanOrEqualTo(tester.getTopLeft(_plus).dy), + ); + }); + + testWidgets('+ menu keeps focus on the text field', (tester) async { + final h = await _harness(tester); + await tester.pumpWidget(_app(h)); + await tester.pumpAndSettle(); + h.focusNode.requestFocus(); + await tester.pumpAndSettle(); + expect(h.focusNode.hasFocus, isTrue); + + await tester.tap(_plus); + await tester.pumpAndSettle(); + expect(find.text(_l10n.chat_sendGif), findsOneWidget); + expect(h.focusNode.hasFocus, isTrue); + }); + + testWidgets('tapping outside the + menu closes it without an action', ( + tester, + ) async { + final h = await _harness(tester); + await tester.pumpWidget(_app(h)); + await tester.pumpAndSettle(); + await tester.tap(_plus); + await tester.pumpAndSettle(); + expect(_menu, findsOneWidget); + + await tester.tapAt(const Offset(5, 5)); + await tester.pumpAndSettle(); + expect(_menu, findsNothing); + expect(h.actions, isEmpty); + }); + + for (final scale in [1.3, 2.0]) { + testWidgets('+ menu stays above the button at 320dp, ${scale}x text', ( + tester, + ) async { + tester.view.physicalSize = const Size(320, 568); + tester.view.devicePixelRatio = 1; + tester.platformDispatcher.textScaleFactorTestValue = scale; + addTearDown(tester.view.reset); + addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue); + final h = await _harness(tester); + await tester.pumpWidget( + _app( + h, + actions: [ + for (var i = 0; i < 4; i++) + ChatComposerAction( + id: 'a$i', + icon: Icons.gif_box, + label: 'A rather long action label number $i for the menu', + ), + ], + ), + ); + await tester.pumpAndSettle(); + await tester.tap(_plus); + await tester.pumpAndSettle(); + expect(tester.takeException(), isNull); + final menu = tester.getRect(_menu); + expect(menu.bottom, lessThanOrEqualTo(tester.getTopLeft(_plus).dy)); + expect(menu.left, greaterThanOrEqualTo(0)); + expect(menu.right, lessThanOrEqualTo(320)); + expect(menu.top, greaterThanOrEqualTo(0)); + + await tester.tap(find.textContaining('number 3')); + await tester.pumpAndSettle(); + expect(h.actions, ['a3']); + }); + } + testWidgets('panel is solid without a chat background and transparent ' 'over one', (tester) async { final h = await _harness(tester);