diff --git a/app/lib/features/chat/conversation/chat_composer_bar.dart b/app/lib/features/chat/conversation/chat_composer_bar.dart index e75bfe02..63035ed5 100644 --- a/app/lib/features/chat/conversation/chat_composer_bar.dart +++ b/app/lib/features/chat/conversation/chat_composer_bar.dart @@ -27,6 +27,8 @@ import '../../../ui/adaptive/pop_entrance.dart'; import 'chat_reading_width.dart'; import 'composer_focus_request.dart'; import 'composer_focus_restore.dart'; +import 'composer_focus_ring.dart'; +import '../ui/sessions/chat_color_palette.dart'; /// The composer's placeholder when the chat names no working directory. const String chatComposerPlaceholder = 'Message the agent'; @@ -49,6 +51,22 @@ bool chatEnterSends([TargetPlatform? platform]) { return target != TargetPlatform.android && target != TargetPlatform.iOS; } +/// The composer's gap above the bottom of the window (or its bottom safe +/// area) on a phone, where it sits just over the keyboard or home indicator. +const double kChatComposerBottomMargin = 12; + +/// The same gap on a desktop host (native or a desktop browser), whose +/// window's bottom edge is a hard frame under a pointer: the composer keeps +/// more room from it so it does not read as stuck to the edge. +const double kChatComposerDesktopBottomMargin = 24; + +/// The composer's bottom gap on [platform] (the running host by default), +/// split between phones and desktops as [chatEnterSends] is. +double chatComposerBottomMargin([TargetPlatform? platform]) => + chatEnterSends(platform) + ? kChatComposerDesktopBottomMargin + : kChatComposerBottomMargin; + /// The single primary control's words while a turn is and is not running. const String chatSendLabel = 'Send'; const String chatStopLabel = 'Stop'; @@ -103,8 +121,13 @@ class ChatComposerBar extends StatefulWidget { this.placeholder = chatComposerPlaceholder, this.enterSends, this.images, + this.chatColor, }); + /// The chat's colour name (see `chatSessionColor`), the accent of the + /// field's focus ring; null takes the host's primary colour. + final String? chatColor; + /// The transcript's image cache, told about uploaded files so the sent turn /// shows them without a round trip. final ChatAttachmentImages? images; @@ -717,7 +740,12 @@ class _ChatComposerBarState extends State { constraints: const BoxConstraints(maxWidth: kChatReadingWidth), child: Padding( key: const Key('chat-composer'), - padding: const EdgeInsetsDirectional.fromSTEB(16, 8, 16, 12), + padding: EdgeInsetsDirectional.fromSTEB( + 16, + 8, + 16, + chatComposerBottomMargin(), + ), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, @@ -797,22 +825,39 @@ class _ChatComposerBarState extends State { ); } + static const OutlinedBorder _pillShape = RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(28)), + ); + /// The field and its controls in one frosted-glass surface floating over /// the transcript, so the composer reads as a single input rather than a - /// row of separate controls, and the conversation shows through it. - Widget _pill(BuildContext context, bool cupertino, Widget child) => GlassPill( - key: const Key('chat-composer-pill'), - cupertino: cupertino, - shape: const RoundedRectangleBorder( - borderRadius: BorderRadius.all(Radius.circular(28)), - ), - // The end controls keep the same gap from the pill's sides as from its - // bottom (and its top while the field is one line). - child: Padding( - padding: const EdgeInsets.all(GlassBarMetrics.fieldEdgeGap), - child: child, - ), - ); + /// row of separate controls, and the conversation shows through it. While + /// the field has focus the pill's edge takes the chat's accent. + Widget _pill(BuildContext context, bool cupertino, Widget child) => + ComposerFocusRing( + focus: _focus, + accent: _accent(context, cupertino), + shape: _pillShape, + child: GlassPill( + key: const Key('chat-composer-pill'), + cupertino: cupertino, + shape: _pillShape, + // The end controls keep the same gap from the pill's sides as from + // its bottom (and its top while the field is one line). + child: Padding( + padding: const EdgeInsets.all(GlassBarMetrics.fieldEdgeGap), + child: child, + ), + ), + ); + + /// The chat's accent: its own colour's shade for this host and brightness, + /// else the host's primary colour. + Color _accent(BuildContext context, bool cupertino) => + chatColorShade(context, widget.chatColor) ?? + (cupertino + ? CupertinoTheme.of(context).primaryColor + : Theme.of(context).colorScheme.primary); /// The attach control: a host button that opens the picker, disabled when the /// host has no picker or one prompt is already awaiting its reply. diff --git a/app/lib/features/chat/conversation/chat_thread_view.dart b/app/lib/features/chat/conversation/chat_thread_view.dart index 5eef36ef..9d032d43 100644 --- a/app/lib/features/chat/conversation/chat_thread_view.dart +++ b/app/lib/features/chat/conversation/chat_thread_view.dart @@ -956,6 +956,7 @@ class _ChatThreadViewState extends State { placeholder: chatComposerPlaceholderFor( '${widget.session?['directory'] ?? ''}', ), + chatColor: chatSessionColor(widget.session), backgroundTasks: ChatBackgroundTasks( key: ValueKey('chat-background-tasks-${widget.sessionId}'), gateway: widget.gateway, diff --git a/app/lib/features/chat/conversation/composer_focus_ring.dart b/app/lib/features/chat/conversation/composer_focus_ring.dart new file mode 100644 index 00000000..3454efb2 --- /dev/null +++ b/app/lib/features/chat/conversation/composer_focus_ring.dart @@ -0,0 +1,82 @@ +import 'package:flutter/widgets.dart'; + +/// The composer's focus indication: a thin ring in the chat's accent colour +/// along the pill's edge and a soft glow of the same colour outside it, so +/// a focused field shows as such without relying on its caret alone. Both +/// fade in and out with focus; at rest unfocused nothing visible is drawn. +/// +/// The accent is the chat's own colour (its highlight in the list) when it +/// has one, else the host's primary colour, never the pill's background: the +/// ring is translucent so the same tint stays subtle on a light and a dark +/// pill. +class ComposerFocusRing extends StatelessWidget { + const ComposerFocusRing({ + super.key, + required this.focus, + required this.accent, + required this.shape, + required this.child, + }); + + /// The field's focus node; the ring follows its [FocusNode.hasFocus]. + final FocusNode focus; + + /// The chat's accent, at full opacity; the ring and glow fade it. + final Color accent; + + /// The pill's shape, so the ring hugs its edge. + final OutlinedBorder shape; + final Widget child; + + /// How long the ring takes to appear or fade. + static const Duration duration = Duration(milliseconds: 160); + + /// The ring's stroke, drawn just inside the pill's edge over its hairline. + static const double ringWidth = 1.5; + + /// The accent's opacity in the ring while focused. + static const double ringOpacity = 0.6; + + /// The accent's opacity in the glow while focused. + static const double glowOpacity = 0.18; + + /// The glow's blur radius; it spreads [glowSpread] past the pill first. + static const double glowBlur = 12; + static const double glowSpread = 1; + + @override + Widget build(BuildContext context) => ListenableBuilder( + listenable: focus, + child: child, + builder: (context, child) { + final focused = focus.hasFocus; + // Both decorations keep the same shape, shadow and stroke while only + // their colours change, so the container lerps them into one fade. + return AnimatedContainer( + key: const Key('chat-composer-focus-ring'), + duration: duration, + curve: Curves.easeOutCubic, + decoration: ShapeDecoration( + shape: shape, + shadows: [ + BoxShadow( + color: accent.withValues(alpha: focused ? glowOpacity : 0), + blurRadius: glowBlur, + spreadRadius: glowSpread, + ), + ], + ), + // Over the child: the pill's fill would otherwise cover the ring. + foregroundDecoration: ShapeDecoration( + shape: shape.copyWith( + side: BorderSide( + color: accent.withValues(alpha: focused ? ringOpacity : 0), + width: ringWidth, + ), + ), + ), + child: child, + ); + }, + ); +} diff --git a/app/test/features/chat/conversation/chat_composer_bottom_margin_test.dart b/app/test/features/chat/conversation/chat_composer_bottom_margin_test.dart new file mode 100644 index 00000000..1eff3734 --- /dev/null +++ b/app/test/features/chat/conversation/chat_composer_bottom_margin_test.dart @@ -0,0 +1,68 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_composer_bar.dart'; + +import 'chat_composer_bar_fixtures.dart'; + +/// The phones: Fuchsia counts as a keyboard host (Enter sends), like desktop. +final _phones = TargetPlatformVariant({ + TargetPlatform.android, + TargetPlatform.iOS, +}); + +double _bottomMargin(WidgetTester tester) { + final padding = tester.widget( + find.byKey(const Key('chat-composer')), + ); + return (padding.padding as EdgeInsetsDirectional).bottom; +} + +void main() { + test('phones keep the close bottom margin, desktops a wider one', () { + expect( + chatComposerBottomMargin(TargetPlatform.android), + kChatComposerBottomMargin, + ); + expect( + chatComposerBottomMargin(TargetPlatform.iOS), + kChatComposerBottomMargin, + ); + for (final desktop in [ + TargetPlatform.macOS, + TargetPlatform.windows, + TargetPlatform.linux, + ]) { + expect( + chatComposerBottomMargin(desktop), + kChatComposerDesktopBottomMargin, + ); + } + expect( + kChatComposerDesktopBottomMargin, + greaterThan(kChatComposerBottomMargin), + ); + }); + + group('rendered composer', () { + late ComposerBarGateway gateway; + + setUp(() => gateway = ComposerBarGateway()..page = fixtureTurn()); + tearDown(() => gateway.close()); + + testWidgets( + 'keeps more room from a desktop window bottom', + (tester) async { + await tester.pumpWidget(composerThread(gateway)); + await tester.pumpAndSettle(); + expect(_bottomMargin(tester), kChatComposerDesktopBottomMargin); + }, + variant: TargetPlatformVariant.desktop(), + ); + + testWidgets('stays close to a phone bottom', (tester) async { + await tester.pumpWidget(composerThread(gateway)); + await tester.pumpAndSettle(); + expect(_bottomMargin(tester), kChatComposerBottomMargin); + }, variant: _phones); + }); +} diff --git a/app/test/features/chat/conversation/chat_composer_focus_ring_test.dart b/app/test/features/chat/conversation/chat_composer_focus_ring_test.dart new file mode 100644 index 00000000..cec806fd --- /dev/null +++ b/app/test/features/chat/conversation/chat_composer_focus_ring_test.dart @@ -0,0 +1,109 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/conversation/composer_focus_ring.dart'; +import 'package:pipelines_app/features/chat/ui/sessions/chat_color_palette.dart'; + +import 'chat_composer_bar_fixtures.dart'; + +/// The focus ring wrapping the composer pill: its outer glow and inner ring. +({Color glow, BorderSide side}) _ring(WidgetTester tester) { + final container = tester.widget( + find.byKey(const Key('chat-composer-focus-ring')), + ); + final glow = (container.decoration! as ShapeDecoration).shadows!.single; + final shape = (container.foregroundDecoration! as ShapeDecoration).shape; + return (glow: glow.color, side: (shape as RoundedRectangleBorder).side); +} + +Future _focusField(WidgetTester tester) async { + await tester.tap(find.byKey(const Key('chat-composer-field'))); + await tester.pumpAndSettle(); +} + +void main() { + late ComposerBarGateway gateway; + + setUp(() => gateway = ComposerBarGateway()..page = fixtureTurn()); + tearDown(() => gateway.close()); + + testWidgets('unfocused composer draws no ring or glow', (tester) async { + await tester.pumpWidget(composerThread(gateway)); + await tester.pumpAndSettle(); + + final ring = _ring(tester); + expect(ring.side.color.a, 0); + expect(ring.glow.a, 0); + expect(ring.side.width, ComposerFocusRing.ringWidth); + }); + + testWidgets('focused composer takes the primary colour as its ring', ( + tester, + ) async { + await tester.pumpWidget(composerThread(gateway)); + await tester.pumpAndSettle(); + await _focusField(tester); + + final primary = Theme.of( + tester.element(find.byKey(const Key('chat-composer-field'))), + ).colorScheme.primary; + final ring = _ring(tester); + expect( + ring.side.color, + primary.withValues(alpha: ComposerFocusRing.ringOpacity), + ); + expect(ring.glow, primary.withValues(alpha: ComposerFocusRing.glowOpacity)); + expect(ring.side.width, ComposerFocusRing.ringWidth); + }); + + testWidgets('a coloured chat lends its own shade to the ring', ( + tester, + ) async { + await tester.pumpWidget( + composerThread(gateway, session: {'id': 's', 'color': 'teal'}), + ); + await tester.pumpAndSettle(); + await _focusField(tester); + + final teal = chatColorShade( + tester.element(find.byKey(const Key('chat-composer-field'))), + 'teal', + )!; + expect( + _ring(tester).side.color, + teal.withValues(alpha: ComposerFocusRing.ringOpacity), + ); + }); + + testWidgets('the ring fades out again when focus leaves', (tester) async { + await tester.pumpWidget(composerThread(gateway)); + await tester.pumpAndSettle(); + await _focusField(tester); + expect(_ring(tester).side.color.a, greaterThan(0)); + + FocusManager.instance.primaryFocus?.unfocus(); + // One frame applies the focus change, the next rebuilds the ring towards + // its unfocused decoration, which the container then animates into. + await tester.pump(); + await tester.pump(); + expect(_ring(tester).side.color.a, 0); + await tester.pumpAndSettle(); + expect(_ring(tester).glow.a, 0); + }); + + testWidgets('Cupertino composer rings in the theme primary colour', ( + tester, + ) async { + await tester.pumpWidget(composerThread(gateway, cupertino: true)); + await tester.pumpAndSettle(); + await _focusField(tester); + + final primary = CupertinoTheme.of( + tester.element(find.byKey(const Key('chat-composer-field'))), + ).primaryColor; + expect( + _ring(tester).side.color, + primary.withValues(alpha: ComposerFocusRing.ringOpacity), + ); + }); +}