Keep composer focus-intent tracking local to chat screens

This commit is contained in:
zjs81 committed 2026-11-20 23:09:48 -07:00
1 parent 267936bb91
commit aa621526ec
6 files changed
+181 -152

No files matched your search

-52
View File
@@ -1,52 +0,0 @@
import 'dart:async';
import 'package:flutter/widgets.dart';
/// Tells the user focusing the chat composer apart from Flutter handing focus
/// back after an app switch or after a sheet, dialog or menu closes.
///
/// On desktop and web the focus manager takes focus off the composer when the
/// window goes inactive and puts it back on resume. That is not the user
/// starting to type, so the chat must not jump to the newest message and lose
/// the reading position. Mobile keeps focus across an app switch. On every
/// platform a sheet, dialog or menu opened over the chat takes focus and the
/// composer gets it back when that closes, which is not a tap either.
class ComposerFocusIntent with WidgetsBindingObserver {
ComposerFocusIntent() {
WidgetsBinding.instance.addObserver(this);
}
bool _lostWhileAway = false;
bool _lostToRoute = false;
/// Call from the composer focus node's listener. Returns true when focus
/// was just gained by a tap or by app code, false for a loss of focus or a
/// focus restored on resume or when a route on top closes.
///
/// [coveredByRoute] is whether the chat's route is no longer the current
/// one (a sheet, dialog or menu is open on top).
bool isUserFocusGain(bool hasFocus, {bool coveredByRoute = false}) {
if (!hasFocus) {
final state = WidgetsBinding.instance.lifecycleState;
_lostWhileAway = state != null && state != AppLifecycleState.resumed;
_lostToRoute = coveredByRoute;
return false;
}
final restored = _lostWhileAway || _lostToRoute;
_lostWhileAway = false;
_lostToRoute = false;
return !restored;
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state != AppLifecycleState.resumed) return;
// The focus manager observes the lifecycle from app start, so its restore
// is already queued and runs first. Clearing afterwards keeps a focus
// that was not restored (the user clicked elsewhere) from swallowing the
// next real tap on the composer.
scheduleMicrotask(() => _lostWhileAway = false);
}
void dispose() => WidgetsBinding.instance.removeObserver(this);
}
+26 -9
View File
@@ -16,7 +16,6 @@ import '../storage/community_store.dart';
import '../utils/platform_info.dart';
import '../helpers/chat_jump.dart';
import '../helpers/chat_scroll_controller.dart';
import '../helpers/composer_focus_intent.dart';
import '../connector/meshcore_protocol.dart';
import '../helpers/cyr2lat.dart';
import '../helpers/gif_helper.dart';
@@ -100,13 +99,15 @@ class ChannelChatScreen extends StatefulWidget {
State<ChannelChatScreen> createState() => _ChannelChatScreenState();
}
class _ChannelChatScreenState extends State<ChannelChatScreen> {
class _ChannelChatScreenState extends State<ChannelChatScreen>
with WidgetsBindingObserver {
final _holidayWatcher = HolidayTriggerWatcher();
final _messageAreaKey = GlobalKey();
final TextEditingController _textController = TextEditingController();
final ChatScrollController _scrollController = ChatScrollController();
final FocusNode _textFieldFocusNode = FocusNode();
final _composerFocusIntent = ComposerFocusIntent();
bool _focusLostWhileAway = false;
bool _focusLostToRoute = false;
ChannelMessage? _replyingToMessage;
bool _useReplyRegion = false;
final CommunityStore _communityStore = CommunityStore();
@@ -182,6 +183,7 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_textFieldFocusNode.addListener(_onTextFieldFocusChange);
_scrollController.onScrollNearTop = _loadOlderMessages;
_scrollController.showJumpToBottom.addListener(_clearDividerAtBottom);
@@ -272,13 +274,28 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
}
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state != AppLifecycleState.resumed) return;
// The focus manager observes the lifecycle from app start, so its restore
// is already queued and runs first. Clearing afterwards keeps a focus
// that was not restored (the user clicked elsewhere) from swallowing the
// next real tap on the composer.
scheduleMicrotask(() => _focusLostWhileAway = false);
}
void _onTextFieldFocusChange() {
if (!mounted) return;
final userFocus = _composerFocusIntent.isUserFocusGain(
_textFieldFocusNode.hasFocus,
coveredByRoute: !(ModalRoute.of(context)?.isCurrent ?? true),
);
if (userFocus) _scrollController.handleKeyboardOpen();
if (!_textFieldFocusNode.hasFocus) {
final state = WidgetsBinding.instance.lifecycleState;
_focusLostWhileAway = state != null && state != AppLifecycleState.resumed;
_focusLostToRoute = !(ModalRoute.of(context)?.isCurrent ?? true);
return;
}
final restored = _focusLostWhileAway || _focusLostToRoute;
_focusLostWhileAway = false;
_focusLostToRoute = false;
if (!restored) _scrollController.handleKeyboardOpen();
}
Future<void> _loadOlderMessages() async {
@@ -359,12 +376,12 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_route?.secondaryAnimation?.removeStatusListener(_onCoveringRouteStatus);
_watchedImageStore?.removeListener(_onImageStoreChanged);
_unregisterActive();
_scrollController.showJumpToBottom.removeListener(_clearDividerAtBottom);
_textFieldFocusNode.removeListener(_onTextFieldFocusChange);
_composerFocusIntent.dispose();
_textFieldFocusNode.dispose();
_textController.dispose();
_searchController.dispose();
+25 -9
View File
@@ -22,7 +22,6 @@ import '../widgets/message_status_icon.dart';
import '../widgets/empty_state.dart';
import '../helpers/chat_scroll_anchor.dart';
import '../helpers/chat_scroll_controller.dart';
import '../helpers/composer_focus_intent.dart';
import '../helpers/gif_helper.dart';
import '../helpers/ipfs_link_attach_flow.dart';
import '../helpers/contact_share_message.dart';
@@ -92,7 +91,7 @@ class ChatScreen extends StatefulWidget {
State<ChatScreen> createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
class _ChatScreenState extends State<ChatScreen> with WidgetsBindingObserver {
final _holidayWatcher = HolidayTriggerWatcher();
final _messageAreaKey = GlobalKey();
final _textController = TextEditingController();
@@ -100,7 +99,8 @@ class _ChatScreenState extends State<ChatScreen> {
final _scrollAnchor = ChatScrollAnchor();
({String id, bool bookmarksOnly})? _newestRow;
final _textFieldFocusNode = FocusNode();
final _composerFocusIntent = ComposerFocusIntent();
bool _focusLostWhileAway = false;
bool _focusLostToRoute = false;
final GlobalKey _unreadScrollKey = GlobalKey();
bool _isLoadingOlder = false;
bool _showBookmarkedOnly = false;
@@ -156,6 +156,7 @@ class _ChatScreenState extends State<ChatScreen> {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_textFieldFocusNode.addListener(_onTextFieldFocusChange);
_scrollController.onScrollNearTop = _loadOlderMessages;
_scrollController.showJumpToBottom.addListener(_clearDividerAtBottom);
@@ -223,13 +224,28 @@ class _ChatScreenState extends State<ChatScreen> {
}
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state != AppLifecycleState.resumed) return;
// The focus manager observes the lifecycle from app start, so its restore
// is already queued and runs first. Clearing afterwards keeps a focus
// that was not restored (the user clicked elsewhere) from swallowing the
// next real tap on the composer.
scheduleMicrotask(() => _focusLostWhileAway = false);
}
void _onTextFieldFocusChange() {
if (!mounted) return;
final userFocus = _composerFocusIntent.isUserFocusGain(
_textFieldFocusNode.hasFocus,
coveredByRoute: !(ModalRoute.of(context)?.isCurrent ?? true),
);
if (userFocus) _scrollController.handleKeyboardOpen();
if (!_textFieldFocusNode.hasFocus) {
final state = WidgetsBinding.instance.lifecycleState;
_focusLostWhileAway = state != null && state != AppLifecycleState.resumed;
_focusLostToRoute = !(ModalRoute.of(context)?.isCurrent ?? true);
return;
}
final restored = _focusLostWhileAway || _focusLostToRoute;
_focusLostWhileAway = false;
_focusLostToRoute = false;
if (!restored) _scrollController.handleKeyboardOpen();
}
Future<void> _loadOlderMessages() async {
@@ -246,11 +262,11 @@ class _ChatScreenState extends State<ChatScreen> {
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_route?.secondaryAnimation?.removeStatusListener(_onCoveringRouteStatus);
_unregisterActive();
_scrollController.showJumpToBottom.removeListener(_clearDividerAtBottom);
_textFieldFocusNode.removeListener(_onTextFieldFocusChange);
_composerFocusIntent.dispose();
_textFieldFocusNode.dispose();
_textController.dispose();
_searchController.dispose();