add focus stuff

good for a11y, and for seeing times on mobile
This commit is contained in:
Henry Hiles 2026-12-04 17:02:26 -05:00
commit 43ce320987
Signed by: Henry-Hiles
SSH key fingerprint: SHA256:VKQUdS31Q90KvX7EkKMHMBpUspcmItAh86a+v7PGiIs
6 changed files with 120 additions and 68 deletions

View file

@ -82,6 +82,7 @@ class const EventRenderer(
final colorScheme = theme.colorScheme; final colorScheme = theme.colorScheme;
final focusNode = useFocusNode(); final focusNode = useFocusNode();
useListenable(focusNode); useListenable(focusNode);
final pointerDown = useRef<(Offset, Duration)?>(null);
final senderRecognizer = useTapRecognizer(); final senderRecognizer = useTapRecognizer();
final (event, previousEvent) = ref.watch( final (event, previousEvent) = ref.watch(
@ -212,6 +213,7 @@ class const EventRenderer(
? null ? null
: () => onOpenThread!(event.eventId), : () => onOpenThread!(event.eventId),
isGrouped: isGrouped, isGrouped: isGrouped,
showTime: focusNode.hasPrimaryFocus,
maxLines: maxLines, maxLines: maxLines,
textOnly: textOnly, textOnly: textOnly,
) )
@ -585,7 +587,10 @@ class const EventRenderer(
if (textOnly) if (textOnly)
child child
else ...[ else ...[
Builder( // Spacing between groups is outside the highlight, so it's even
Padding(
padding: .only(top: isGrouped ? 0 : 4),
child: Builder(
builder: (context) => FocusableActionDetector( builder: (context) => FocusableActionDetector(
focusNode: focusNode, focusNode: focusNode,
actions: contextMenuCallback == null actions: contextMenuCallback == null
@ -607,19 +612,51 @@ class const EventRenderer(
), ),
}, },
child: Container( child: Container(
width: double.infinity,
decoration: BoxDecoration( decoration: BoxDecoration(
// Rounds every corner, as HighlightWrapper's clip only
// reaches the ones at the row's edges
borderRadius: .circular(12),
color: focusNode.hasPrimaryFocus color: focusNode.hasPrimaryFocus
? theme.colorScheme.surfaceContainerHighest ? theme.colorScheme.surfaceContainerHighest
: null, : 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;
},
onPointerCancel: (_) => pointerDown.value = null,
child: GestureDetector( child: GestureDetector(
onSecondaryTapUp: contextMenuCallback, onSecondaryTapUp: contextMenuCallback,
onLongPressStart: contextMenuCallback, onLongPressStart: contextMenuCallback,
child: SwipeToReply( child: SwipeToReply(
onSwipe: onSwipe, onSwipe: onSwipe,
Padding( Padding(
padding: EdgeInsets.symmetric(horizontal: 8) // Even on every side, so the highlight's centered. Lists
.copyWith(top: isGrouped ? 0 : 8), // add more space at their edges.
padding: .symmetric(
horizontal: 4,
vertical: child is MessageRenderer ? 4 : 0,
),
child: Wrap( child: Wrap(
verticalDirection: .up, verticalDirection: .up,
spacing: 6, spacing: 6,
@ -637,6 +674,8 @@ class const EventRenderer(
), ),
), ),
), ),
),
),
...[ ...[
if (child is! MessageRenderer) ReactionRow(event), if (child is! MessageRenderer) ReactionRow(event),

View file

@ -38,6 +38,10 @@ class const MessageRenderer(
final VoidCallback? onTapThread, final VoidCallback? onTapThread,
final bool textOnly = false, final bool textOnly = false,
final bool isGrouped = 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, final int? maxLines,
super.key, super.key,
}) extends HookConsumerWidget { }) extends HookConsumerWidget {
@ -173,12 +177,11 @@ class const MessageRenderer(
children: [ children: [
if (!textOnly) if (!textOnly)
if (isGrouped) if (isGrouped)
// Shows when the message was sent on hover, as grouped messages // When the message was sent, as grouped messages have no header
// have no header
SizedBox( SizedBox(
width: 40, width: 40,
child: Visibility.maintain( child: Visibility.maintain(
visible: hovered.value, visible: hovered.value || showTime,
child: Padding( child: Padding(
padding: .only(top: 14), padding: .only(top: 14),
child: Tooltip( child: Tooltip(
@ -213,7 +216,7 @@ class const MessageRenderer(
], ],
), ),
Card( Card(
margin: textOnly ? .zero : .only(bottom: 4), margin: .zero,
color: textOnly color: textOnly
? Colors.transparent ? Colors.transparent
: ref.watch( : ref.watch(

View file

@ -24,7 +24,11 @@ class const ChatTimeline({
int eventRowId, int eventRowId,
int? previousEventRowId, { int? previousEventRowId, {
required Key key, required Key key,
}) => HighlightWrapper( }) => Padding(
key: key,
// Spacing at the edges, outside events' focus highlight
padding: .symmetric(horizontal: 4),
child: HighlightWrapper(
// To work around an AWFUL SuperSliverList bug // To work around an AWFUL SuperSliverList bug
ConstrainedBox( ConstrainedBox(
constraints: const BoxConstraints(minHeight: 0.01), constraints: const BoxConstraints(minHeight: 0.01),
@ -40,8 +44,8 @@ class const ChatTimeline({
threadRoot: chat.threadRoot, threadRoot: chat.threadRoot,
), ),
), ),
key: key,
isHighlighted: scroll.highlightedEvent == eventRowId, isHighlighted: scroll.highlightedEvent == eventRowId,
),
); );
@override @override

View file

@ -66,7 +66,7 @@ final class const PinnedEventsDrawer(
jumpToEvent(rowId); jumpToEvent(rowId);
}, },
child: Padding( child: Padding(
padding: .symmetric(vertical: 4), padding: .symmetric(vertical: 4, horizontal: 4),
child: IgnorePointer( child: IgnorePointer(
child: EventRenderer( child: EventRenderer(
rowId, rowId,

View file

@ -121,7 +121,7 @@ final class const SearchDrawer(
jumpToEvent(rowId); jumpToEvent(rowId);
}, },
child: Padding( child: Padding(
padding: .symmetric(vertical: 4), padding: .symmetric(vertical: 4, horizontal: 4),
child: IgnorePointer( child: IgnorePointer(
child: EventRenderer( child: EventRenderer(
rowId, rowId,

View file

@ -170,8 +170,14 @@ class const NotificationsList({super.key})
await ref.focusRoom(roomId, rowId); await ref.focusRoom(roomId, rowId);
}, },
child: Padding(
padding: .symmetric(horizontal: 4),
child: IgnorePointer( child: IgnorePointer(
child: EventRenderer(rowId, roomId: roomId), child: EventRenderer(
rowId,
roomId: roomId,
),
),
), ),
), ),
isHighlighted: isHighlighted, isHighlighted: isHighlighted,