From 43ce320987e7817b85cc0421fcd9161ef5455aca Mon Sep 17 00:00:00 2001 From: Henry-Hiles Date: Fri, 4 Dec 2026 17:02:26 -0500 Subject: [PATCH] add focus stuff good for a11y, and for seeing times on mobile --- lib/widgets/renderers/event.dart | 127 ++++++++++++------ lib/widgets/renderers/message.dart | 11 +- lib/widgets/room_chat/chat_timeline.dart | 38 +++--- .../room_chat/pinned_events_drawer.dart | 2 +- lib/widgets/room_chat/search_drawer.dart | 2 +- lib/widgets/sidebar/notifications_list.dart | 10 +- 6 files changed, 121 insertions(+), 69 deletions(-) diff --git a/lib/widgets/renderers/event.dart b/lib/widgets/renderers/event.dart index 9037df9..6a75fa4 100644 --- a/lib/widgets/renderers/event.dart +++ b/lib/widgets/renderers/event.dart @@ -82,6 +82,7 @@ class const EventRenderer( final colorScheme = theme.colorScheme; final focusNode = useFocusNode(); useListenable(focusNode); + final pointerDown = useRef<(Offset, Duration)?>(null); final senderRecognizer = useTapRecognizer(); final (event, previousEvent) = ref.watch( @@ -212,6 +213,7 @@ class const EventRenderer( ? null : () => onOpenThread!(event.eventId), isGrouped: isGrouped, + showTime: focusNode.hasPrimaryFocus, maxLines: maxLines, textOnly: textOnly, ) @@ -585,51 +587,88 @@ class const EventRenderer( if (textOnly) child else ...[ - Builder( - builder: (context) => FocusableActionDetector( - focusNode: focusNode, - actions: contextMenuCallback == null - ? null - : { - ActivateIntent: CallbackAction( - onInvoke: (_) { - final renderBox = - context.findRenderObject() as RenderBox; - final topLeft = renderBox.localToGlobal( - Offset.zero, - ); - context.showAdaptiveMenu( - getEventOptions!(event).toList(), - globalPosition: topLeft, - ); - return null; - }, - ), + // Spacing between groups is outside the highlight, so it's even + Padding( + padding: .only(top: isGrouped ? 0 : 4), + child: Builder( + builder: (context) => FocusableActionDetector( + focusNode: focusNode, + actions: contextMenuCallback == null + ? null + : { + ActivateIntent: CallbackAction( + onInvoke: (_) { + final renderBox = + context.findRenderObject() as RenderBox; + final topLeft = renderBox.localToGlobal( + Offset.zero, + ); + context.showAdaptiveMenu( + getEventOptions!(event).toList(), + globalPosition: topLeft, + ); + return null; + }, + ), + }, + child: Container( + width: double.infinity, + decoration: BoxDecoration( + // Rounds every corner, as HighlightWrapper's clip only + // reaches the ones at the row's edges + borderRadius: .circular(12), + color: focusNode.hasPrimaryFocus + ? theme.colorScheme.surfaceContainerHighest + : null, + ), + // Tapping focuses the event, e.g. showing when grouped + // messages were sent. A Listener, unlike a GestureDetector, + // sees taps without taking them from links, media and the + // like. + child: Listener( + behavior: .translucent, + onPointerDown: (pointer) => pointerDown.value = ( + pointer.position, + pointer.timeStamp, + ), + onPointerUp: (pointer) { + if (pointerDown.value + case (final position, final timeStamp) + when (pointer.position - position).distance < + kTouchSlop && + pointer.timeStamp - timeStamp < + kLongPressTimeout) { + focusNode.hasPrimaryFocus + ? focusNode.unfocus() + : focusNode.requestFocus(); + } + pointerDown.value = null; }, - child: Container( - decoration: BoxDecoration( - color: focusNode.hasPrimaryFocus - ? theme.colorScheme.surfaceContainerHighest - : null, - ), - child: GestureDetector( - onSecondaryTapUp: contextMenuCallback, - onLongPressStart: contextMenuCallback, - child: SwipeToReply( - onSwipe: onSwipe, - Padding( - padding: EdgeInsets.symmetric(horizontal: 8) - .copyWith(top: isGrouped ? 0 : 8), - child: Wrap( - verticalDirection: .up, - spacing: 6, - crossAxisAlignment: .center, - children: [ - child, - if (child is! MessageRenderer && - child is! GenericEventRenderer) - Timestamp(event.timestamp), - ], + onPointerCancel: (_) => pointerDown.value = null, + child: GestureDetector( + onSecondaryTapUp: contextMenuCallback, + onLongPressStart: contextMenuCallback, + child: SwipeToReply( + onSwipe: onSwipe, + Padding( + // Even on every side, so the highlight's centered. Lists + // add more space at their edges. + padding: .symmetric( + horizontal: 4, + vertical: child is MessageRenderer ? 4 : 0, + ), + child: Wrap( + verticalDirection: .up, + spacing: 6, + crossAxisAlignment: .center, + children: [ + child, + if (child is! MessageRenderer && + child is! GenericEventRenderer) + Timestamp(event.timestamp), + ], + ), + ), ), ), ), diff --git a/lib/widgets/renderers/message.dart b/lib/widgets/renderers/message.dart index c23d093..230fe65 100644 --- a/lib/widgets/renderers/message.dart +++ b/lib/widgets/renderers/message.dart @@ -38,6 +38,10 @@ class const MessageRenderer( final VoidCallback? onTapThread, final bool textOnly = false, final bool isGrouped = false, + + /// Whether grouped messages show when they were sent, which they otherwise + /// only do on hover. + final bool showTime = false, final int? maxLines, super.key, }) extends HookConsumerWidget { @@ -173,12 +177,11 @@ class const MessageRenderer( children: [ if (!textOnly) if (isGrouped) - // Shows when the message was sent on hover, as grouped messages - // have no header + // When the message was sent, as grouped messages have no header SizedBox( width: 40, child: Visibility.maintain( - visible: hovered.value, + visible: hovered.value || showTime, child: Padding( padding: .only(top: 14), child: Tooltip( @@ -213,7 +216,7 @@ class const MessageRenderer( ], ), Card( - margin: textOnly ? .zero : .only(bottom: 4), + margin: .zero, color: textOnly ? Colors.transparent : ref.watch( diff --git a/lib/widgets/room_chat/chat_timeline.dart b/lib/widgets/room_chat/chat_timeline.dart index d6aaef7..e3697cc 100644 --- a/lib/widgets/room_chat/chat_timeline.dart +++ b/lib/widgets/room_chat/chat_timeline.dart @@ -24,24 +24,28 @@ class const ChatTimeline({ int eventRowId, int? previousEventRowId, { required Key key, - }) => HighlightWrapper( - // To work around an AWFUL SuperSliverList bug - ConstrainedBox( - constraints: const BoxConstraints(minHeight: 0.01), - child: EventRenderer( - eventRowId, - previousEventRowId: previousEventRowId, - roomId: chat.roomId, - jumpToEvent: jumpToEvent, - onOpenThread: onOpenThread, - getEventOptions: getEventOptions, - onSwipe: onSwipe == null ? null : () => onSwipe!(eventRowId), - readReceiptsChat: chat, - threadRoot: chat.threadRoot, - ), - ), + }) => Padding( key: key, - isHighlighted: scroll.highlightedEvent == eventRowId, + // Spacing at the edges, outside events' focus highlight + padding: .symmetric(horizontal: 4), + child: HighlightWrapper( + // To work around an AWFUL SuperSliverList bug + ConstrainedBox( + constraints: const BoxConstraints(minHeight: 0.01), + child: EventRenderer( + eventRowId, + previousEventRowId: previousEventRowId, + roomId: chat.roomId, + jumpToEvent: jumpToEvent, + onOpenThread: onOpenThread, + getEventOptions: getEventOptions, + onSwipe: onSwipe == null ? null : () => onSwipe!(eventRowId), + readReceiptsChat: chat, + threadRoot: chat.threadRoot, + ), + ), + isHighlighted: scroll.highlightedEvent == eventRowId, + ), ); @override diff --git a/lib/widgets/room_chat/pinned_events_drawer.dart b/lib/widgets/room_chat/pinned_events_drawer.dart index 1e0ab6a..a52a783 100644 --- a/lib/widgets/room_chat/pinned_events_drawer.dart +++ b/lib/widgets/room_chat/pinned_events_drawer.dart @@ -66,7 +66,7 @@ final class const PinnedEventsDrawer( jumpToEvent(rowId); }, child: Padding( - padding: .symmetric(vertical: 4), + padding: .symmetric(vertical: 4, horizontal: 4), child: IgnorePointer( child: EventRenderer( rowId, diff --git a/lib/widgets/room_chat/search_drawer.dart b/lib/widgets/room_chat/search_drawer.dart index 2905789..5621b8e 100644 --- a/lib/widgets/room_chat/search_drawer.dart +++ b/lib/widgets/room_chat/search_drawer.dart @@ -121,7 +121,7 @@ final class const SearchDrawer( jumpToEvent(rowId); }, child: Padding( - padding: .symmetric(vertical: 4), + padding: .symmetric(vertical: 4, horizontal: 4), child: IgnorePointer( child: EventRenderer( rowId, diff --git a/lib/widgets/sidebar/notifications_list.dart b/lib/widgets/sidebar/notifications_list.dart index f52f944..5c34f50 100644 --- a/lib/widgets/sidebar/notifications_list.dart +++ b/lib/widgets/sidebar/notifications_list.dart @@ -170,8 +170,14 @@ class const NotificationsList({super.key}) await ref.focusRoom(roomId, rowId); }, - child: IgnorePointer( - child: EventRenderer(rowId, roomId: roomId), + child: Padding( + padding: .symmetric(horizontal: 4), + child: IgnorePointer( + child: EventRenderer( + rowId, + roomId: roomId, + ), + ), ), ), isHighlighted: isHighlighted,