746e23788d
Prevent passive open, restored, or unfocused chats from clearing notifications. Track qualifying focused interactions and cover passive and active seen-watermark behavior with tests and notes.
129 lines
4.2 KiB
Dart
129 lines
4.2 KiB
Dart
import 'package:flutter/rendering.dart' show ScrollDirection;
|
|
import 'package:flutter/services.dart'
|
|
show HardwareKeyboard, KeyDownEvent, KeyEvent;
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
import '../../../core/servers/server_context.dart';
|
|
import '../../settings/server_connections.dart';
|
|
|
|
/// Reports the user's own actions in one open chat, so the chat can be read.
|
|
///
|
|
/// Counted: a pointer press anywhere in the pane (click or tap, which also
|
|
/// makes it the active pane), a user-driven scroll of its transcript, a key
|
|
/// press while focus is inside it (typing, Tab), focus arriving in it from a
|
|
/// key press outside it (Tab into the pane), and a notification tap that
|
|
/// opened it. Showing the pane, rebuilding it, receiving content, or focus
|
|
/// moved in code with no key press does not report anything. [onInteract]
|
|
/// decides whether the window is focused.
|
|
class ChatSeenInteraction extends StatefulWidget {
|
|
const ChatSeenInteraction({
|
|
super.key,
|
|
required this.sessionId,
|
|
required this.onInteract,
|
|
required this.child,
|
|
});
|
|
|
|
final String sessionId;
|
|
final VoidCallback onInteract;
|
|
final Widget child;
|
|
|
|
@override
|
|
State<ChatSeenInteraction> createState() => _ChatSeenInteractionState();
|
|
}
|
|
|
|
class _ChatSeenInteractionState extends State<ChatSeenInteraction> {
|
|
ServerConnections? _connections;
|
|
|
|
/// True from a key-down until the next frame. Focus changes are applied in a
|
|
/// microtask queued by the key dispatch, which runs before that frame, so a
|
|
/// focus change caused by the key sees the flag. A focus change with no key
|
|
/// press (restoration, code-driven focus) never does.
|
|
bool _keyDownInProgress = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
HardwareKeyboard.instance.addHandler(_onKeyEvent);
|
|
}
|
|
|
|
bool _onKeyEvent(KeyEvent event) {
|
|
if (event is KeyDownEvent && !_keyDownInProgress) {
|
|
_keyDownInProgress = true;
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
_keyDownInProgress = false;
|
|
});
|
|
WidgetsBinding.instance.scheduleFrame();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void _onFocusChange(bool hasFocus) {
|
|
if (hasFocus && _keyDownInProgress) widget.onInteract();
|
|
}
|
|
|
|
@override
|
|
void didChangeDependencies() {
|
|
super.didChangeDependencies();
|
|
final connections = ServerContext.maybeOf(context)?.notifier;
|
|
if (identical(connections, _connections)) return;
|
|
_connections?.removeListener(_takeNotificationOpen);
|
|
_connections = connections;
|
|
connections?.addListener(_takeNotificationOpen);
|
|
_takeNotificationOpen();
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(covariant ChatSeenInteraction oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (oldWidget.sessionId != widget.sessionId) _takeNotificationOpen();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
HardwareKeyboard.instance.removeHandler(_onKeyEvent);
|
|
_connections?.removeListener(_takeNotificationOpen);
|
|
super.dispose();
|
|
}
|
|
|
|
/// A notification tap that named this chat is the user opening it. It is
|
|
/// reported after the frame, so the list has taken the chat as active first.
|
|
void _takeNotificationOpen() {
|
|
final connections = _connections;
|
|
if (connections == null ||
|
|
!connections.takeChatOpenIntent(widget.sessionId)) {
|
|
return;
|
|
}
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (mounted) widget.onInteract();
|
|
});
|
|
}
|
|
|
|
static bool _userScrolled(ScrollNotification notification) =>
|
|
(notification is UserScrollNotification &&
|
|
notification.direction != ScrollDirection.idle) ||
|
|
(notification is ScrollStartNotification &&
|
|
notification.dragDetails != null);
|
|
|
|
@override
|
|
Widget build(BuildContext context) => Listener(
|
|
behavior: HitTestBehavior.translucent,
|
|
onPointerDown: (_) => widget.onInteract(),
|
|
child: NotificationListener<ScrollNotification>(
|
|
onNotification: (notification) {
|
|
if (_userScrolled(notification)) widget.onInteract();
|
|
return false;
|
|
},
|
|
child: Focus(
|
|
canRequestFocus: false,
|
|
skipTraversal: true,
|
|
onFocusChange: _onFocusChange,
|
|
onKeyEvent: (node, event) {
|
|
if (event is KeyDownEvent) widget.onInteract();
|
|
return KeyEventResult.ignored;
|
|
},
|
|
child: widget.child,
|
|
),
|
|
),
|
|
);
|
|
}
|