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

@@ -1,78 +0,0 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/helpers/composer_focus_intent.dart';
void _setLifecycle(WidgetTester tester, List<AppLifecycleState> states) {
for (final state in states) {
tester.binding.handleAppLifecycleStateChanged(state);
}
}
const _away = [
AppLifecycleState.inactive,
AppLifecycleState.hidden,
AppLifecycleState.paused,
];
const _back = [
AppLifecycleState.hidden,
AppLifecycleState.inactive,
AppLifecycleState.resumed,
];
void main() {
testWidgets('focus gained while in the foreground is user focus', (
tester,
) async {
final intent = ComposerFocusIntent();
addTearDown(intent.dispose);
expect(intent.isUserFocusGain(true), isTrue);
expect(intent.isUserFocusGain(false), isFalse);
expect(intent.isUserFocusGain(true), isTrue);
});
testWidgets('focus lost while away and given back on resume is not', (
tester,
) async {
final intent = ComposerFocusIntent();
addTearDown(intent.dispose);
expect(intent.isUserFocusGain(true), isTrue);
_setLifecycle(tester, _away.sublist(0, 1));
expect(intent.isUserFocusGain(false), isFalse);
_setLifecycle(tester, [..._away.sublist(1), ..._back]);
// The restore lands before the resume microtask clears the flag.
expect(intent.isUserFocusGain(true), isFalse);
// The next tap counts again.
expect(intent.isUserFocusGain(false), isFalse);
expect(intent.isUserFocusGain(true), isTrue);
});
testWidgets('focus not restored on resume does not swallow the next tap', (
tester,
) async {
final intent = ComposerFocusIntent();
addTearDown(intent.dispose);
_setLifecycle(tester, _away.sublist(0, 1));
expect(intent.isUserFocusGain(false), isFalse);
_setLifecycle(tester, [..._away.sublist(1), ..._back]);
await tester.pump(); // Resume microtasks run; nothing restored focus.
expect(intent.isUserFocusGain(true), isTrue);
});
testWidgets('focus given back when a sheet or dialog closes is not', (
tester,
) async {
final intent = ComposerFocusIntent();
addTearDown(intent.dispose);
expect(intent.isUserFocusGain(true), isTrue);
expect(intent.isUserFocusGain(false, coveredByRoute: true), isFalse);
expect(intent.isUserFocusGain(true), isFalse);
// Losing focus with nothing on top, then tapping, counts again.
expect(intent.isUserFocusGain(false), isFalse);
expect(intent.isUserFocusGain(true), isTrue);
});
}
@@ -85,6 +85,14 @@ void main() {
expect(find.byTooltip('Clear selection'), findsNothing);
expect(_position(tester).pixels, 600);
// Losing focus with nothing on top, then tapping the composer, still jumps
// to the newest message.
FocusManager.instance.primaryFocus?.unfocus();
await _settle(tester);
await tester.tap(find.byType(TextField).last);
await _settle(tester);
expect(_position(tester).pixels, 0);
await _tearDown(tester);
});
+122 -4
View File
@@ -9,10 +9,12 @@ import 'package:provider/single_child_widget.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:meshcore_open/connector/meshcore_connector.dart';
import 'package:meshcore_open/connector/meshcore_protocol.dart';
import 'package:meshcore_open/l10n/app_localizations.dart';
import 'package:meshcore_open/review_mode/review_frames.dart';
import 'package:meshcore_open/review_mode/review_radio.dart';
import 'package:meshcore_open/screens/channel_chat_screen.dart';
import 'package:meshcore_open/screens/chat_screen.dart';
import 'package:meshcore_open/services/app_debug_log_service.dart';
import 'package:meshcore_open/services/app_settings_service.dart';
import 'package:meshcore_open/services/ble_debug_log_service.dart';
@@ -140,6 +142,90 @@ void main() {
await _tearDown(tester);
}, variant: TargetPlatformVariant.only(TargetPlatform.linux));
testWidgets(
'desktop app switch when focus is not restored does not swallow the next tap',
(tester) async {
tester.view.physicalSize = _screen;
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.reset);
await _pumpChannelChat(tester);
await tester.tap(_composerField);
await _settle(tester);
_list(tester).jumpTo(300);
await _settle(tester);
_goBackground(tester);
await tester.pump();
expect(_composer(tester).focusNode.hasFocus, isFalse);
// User returns and interacts elsewhere (e.g. opens search) so focus is
// not restored to the composer on resume.
await tester.tap(find.byIcon(Icons.search));
await tester.pump();
_goForeground(tester);
await _settle(tester);
await tester.tap(find.byIcon(Icons.arrow_back));
await _settle(tester);
expect(_composer(tester).focusNode.hasFocus, isFalse);
expect(_list(tester).position.pixels, closeTo(300, 1));
await tester.tap(_composerField);
await _settle(tester);
expect(_composer(tester).focusNode.hasFocus, isTrue);
expect(_list(tester).position.pixels, 0);
await _tearDown(tester);
},
variant: TargetPlatformVariant.only(TargetPlatform.linux),
);
testWidgets(
'DM chat: desktop app switch and dialog close keep reading position',
(tester) async {
tester.view.physicalSize = _screen;
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.reset);
await _pumpDirectChat(tester);
await tester.tap(_composerField);
await _settle(tester);
_list(tester).jumpTo(300);
await _settle(tester);
// App switch away and back restores focus without jumping to bottom.
_goBackground(tester);
await tester.pump();
expect(_composer(tester).focusNode.hasFocus, isFalse);
_goForeground(tester);
await _settle(tester);
expect(_composer(tester).focusNode.hasFocus, isTrue);
expect(_list(tester).position.pixels, closeTo(300, 1));
// Opening and closing a dialog over the chat returns focus without jumping.
await tester.tap(find.byIcon(Icons.more_vert));
await _settle(tester);
await tester.tap(find.byIcon(Icons.info_outline));
await _settle(tester);
expect(find.byType(AlertDialog), findsOneWidget);
Navigator.of(tester.element(find.byType(AlertDialog))).pop();
await _settle(tester);
expect(_list(tester).position.pixels, closeTo(300, 1));
// Losing focus in the foreground and tapping again still jumps to bottom.
_composer(tester).focusNode.unfocus();
await _settle(tester);
await tester.tap(_composerField);
await _settle(tester);
expect(_list(tester).position.pixels, 0);
await _tearDown(tester);
},
variant: TargetPlatformVariant.only(TargetPlatform.linux),
);
}
final _composerField = find.descendant(
@@ -170,8 +256,9 @@ void _goForeground(WidgetTester tester) {
}
}
ScrollController _list(WidgetTester tester) =>
tester.widget<ListView>(find.byType(ListView).first).controller!;
ScrollController _list(WidgetTester tester) => tester
.widget<ListView>(find.byWidgetPredicate((w) => w is ListView).first)
.controller!;
double _composerBottom(WidgetTester tester) =>
tester.getBottomLeft(find.byType(ChatComposer)).dy;
@@ -191,6 +278,38 @@ Future<void> _tearDown(WidgetTester tester) async {
Future<MeshCoreConnector> _pumpChannelChat(WidgetTester tester) async {
final (connector, providers) = _shared ??= await _connect(tester);
await _pumpScreen(
tester,
providers,
ChannelChatScreen(channel: connector.channels[0]),
);
return connector;
}
Future<MeshCoreConnector> _pumpDirectChat(WidgetTester tester) async {
final (connector, providers) = _shared ??= await _connect(tester);
final contact = connector.contacts.firstWhere((c) => c.type == advTypeChat);
if (connector.getMessages(contact).length < 40) {
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
for (var i = 0; i < 40; i++) {
connector.handleFrameForTest(
ReviewFrames.contactMessage(
senderPublicKey: contact.publicKey,
text: 'Direct history line $i about the weather on the hill',
timestampSecs: now - 4000 + i,
),
);
}
}
await _pumpScreen(tester, providers, ChatScreen(contact: contact));
return connector;
}
Future<void> _pumpScreen(
WidgetTester tester,
List<SingleChildWidget> providers,
Widget screen,
) async {
await tester.pumpWidget(
MultiProvider(
providers: providers,
@@ -204,12 +323,11 @@ Future<MeshCoreConnector> _pumpChannelChat(WidgetTester tester) async {
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: AppLocalizations.supportedLocales,
home: ChannelChatScreen(channel: connector.channels[0]),
home: screen,
),
),
);
await _settle(tester);
return connector;
}
Future<(MeshCoreConnector, List<SingleChildWidget>)> _connect(