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 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),
],
),
),
),
),
),

View file

@ -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(

View file

@ -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

View file

@ -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,

View file

@ -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,

View file

@ -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,