diff --git a/mobile/lib/features/channels/message_content.dart b/mobile/lib/features/channels/message_content.dart index 13be35a808..ac5a29d2aa 100644 --- a/mobile/lib/features/channels/message_content.dart +++ b/mobile/lib/features/channels/message_content.dart @@ -1051,6 +1051,8 @@ class _MediaPreviewFallback extends StatelessWidget { } } +const _codeCopyButtonSize = 28.0; + class _MessageCodeBlock extends HookWidget { final String name; final String code; @@ -1099,6 +1101,14 @@ class _MessageCodeBlock extends HookWidget { : [TextSpan(text: shownCode)], [code, name, isDark, ink], ); + // Opaque, so the copy button can share it and hide code scrolled beneath. + // An inked block sits on the outgoing fill, so its tint is blended there. + final surface = ink == null + ? CodeStyle.background(context.colors) + : Color.alphaBlend( + chatCodeTint(ink, 0.14), + ChatColors.of(context).outgoing, + ); return Container( // In a bubble, the vertical margin brings the bubble's 8px vertical // padding up to its 16px side padding, so the block is inset evenly and @@ -1107,9 +1117,7 @@ class _MessageCodeBlock extends HookWidget { ? const EdgeInsets.symmetric(vertical: Grid.xs - Grid.xxs) : const EdgeInsets.only(top: Grid.half), decoration: BoxDecoration( - color: ink == null - ? CodeStyle.background(context.colors) - : chatCodeTint(ink, 0.14), + color: surface, borderRadius: BorderRadius.circular( bubble ? Radii.container - Grid.xs : 12, ), @@ -1120,50 +1128,55 @@ class _MessageCodeBlock extends HookWidget { ), child: Stack( children: [ - // Even 12px insets; the right side also reserves the copy button. - Padding( - padding: const EdgeInsets.fromLTRB( - Grid.twelve, - Grid.twelve, - Grid.twelve + 28, - Grid.twelve, - ), - child: Column( - crossAxisAlignment: CrossAxisAlignment.stretch, - mainAxisSize: MainAxisSize.min, - children: [ - if (name.isNotEmpty) - Padding( - padding: const EdgeInsets.only(bottom: Grid.half), - child: Text( - name, - style: context.textTheme.labelSmall?.copyWith( - color: ink ?? context.colors.onSurfaceVariant, - ), + // As on desktop, the code scrolls under the copy button rather than + // beside it, and its end padding keeps every line end reachable. + Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + mainAxisSize: MainAxisSize.min, + children: [ + if (name.isNotEmpty) + Padding( + padding: const EdgeInsets.fromLTRB( + Grid.twelve, + Grid.twelve, + Grid.twelve + _codeCopyButtonSize, + Grid.half, + ), + child: Text( + name, + style: context.textTheme.labelSmall?.copyWith( + color: ink ?? context.colors.onSurfaceVariant, ), ), - SingleChildScrollView( - scrollDirection: Axis.horizontal, - child: RichText( - softWrap: false, - text: TextSpan(style: codeBaseStyle, children: codeSpans), - ), ), - ], - ), + SingleChildScrollView( + scrollDirection: Axis.horizontal, + padding: EdgeInsets.fromLTRB( + Grid.twelve, + name.isEmpty ? Grid.twelve : 0, + Grid.twelve + _codeCopyButtonSize, + Grid.twelve, + ), + child: RichText( + softWrap: false, + text: TextSpan(style: codeBaseStyle, children: codeSpans), + ), + ), + ], ), Positioned( top: Grid.half + Grid.quarter, right: Grid.half + Grid.quarter, child: SizedBox( - width: 28, - height: 28, + width: _codeCopyButtonSize, + height: _codeCopyButtonSize, child: IconButton( onPressed: handleCopy, padding: EdgeInsets.zero, visualDensity: VisualDensity.compact, style: IconButton.styleFrom( tapTargetSize: MaterialTapTargetSize.shrinkWrap, + backgroundColor: surface, ), icon: Icon( isCopied.value ? BuzzIcons.check : BuzzIcons.copy, diff --git a/mobile/test/features/channels/message_content_test.dart b/mobile/test/features/channels/message_content_test.dart index 080312cd78..1808af0dc4 100644 --- a/mobile/test/features/channels/message_content_test.dart +++ b/mobile/test/features/channels/message_content_test.dart @@ -1755,6 +1755,18 @@ void main() { expect(_findRich('void main() {}'), findsWidgets); expect(find.text('dart'), findsOneWidget); + + // The copy button floats over the code, as on desktop, instead of + // reserving a column: the scroller spans the block inside its border. + final scroller = find.byType(SingleChildScrollView).last; + final block = find + .ancestor(of: scroller, matching: find.byType(Container)) + .first; + expect(tester.getSize(scroller).width, tester.getSize(block).width - 2); + expect( + tester.getRect(find.byIcon(BuzzIcons.copy)).right, + lessThan(tester.getRect(scroller).right), + ); }); });