Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a146a49

Browse files
committedDec 15, 2024·
autocomplete: Put best matches near input field.
This commit reverses the list that was originally presented to the user while showing the typeahead menu. This makes sense since on mobile its easier to click on options closer to the input box, i.e. where your fingers are currently present, instead of pressing arrow keys on a keyboard which is true on a desktop setup. Hence we place the best matching options not at the top of the typeahead menu, but instead put them at the bottom for better reachability and convenience of the user. Tests have been added to verify the emoji behavior rendering behavior. Fixes #1123. Fixes #1121.
1 parent 28b3536 commit a146a49

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
 

‎lib/widgets/autocomplete.dart

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class _AutocompleteFieldState<QueryT extends AutocompleteQuery, ResultT extends
134134
constraints: const BoxConstraints(maxHeight: 300), // TODO not hard-coded
135135
child: ListView.builder(
136136
padding: EdgeInsets.zero,
137+
reverse: true,
137138
shrinkWrap: true,
138139
itemCount: _resultsToDisplay.length,
139140
itemBuilder: _buildItem))));

‎test/widgets/autocomplete_test.dart

+95
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,101 @@ void main() {
247247
debugNetworkImageHttpClientProvider = null;
248248
});
249249

250+
testWidgets('emoji options appear in the correct rendering order and do not scroll down', (tester) async {
251+
final composeInputFinder = await setupToComposeInput(tester);
252+
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);
253+
254+
store.setServerEmojiData(
255+
ServerEmojiData(
256+
codeToNames: {
257+
'1f4a4': ['zzz', 'sleepy'], // Unicode emoji for "zzz"
258+
'1f52a': ['biohazard'],
259+
'1f92a': ['zany_face'],
260+
'1f993': ['zebra'],
261+
'0030-fe0f-20e3': ['zero'],
262+
'1f9d0': ['zombie'],
263+
},
264+
),
265+
);
266+
await store.handleEvent(
267+
RealmEmojiUpdateEvent(
268+
id: 1,
269+
realmEmoji: {
270+
'1': eg.realmEmojiItem(emojiCode: '1', emojiName: 'buzzing'),
271+
},
272+
),
273+
);
274+
275+
const zulipOptionLabel = 'zulip';
276+
const zanyFaceOptionLabel = 'zany_face';
277+
const zebraOptionLabel = 'zebra';
278+
const zzzOptionLabel = 'zzz, sleepy';
279+
const unicodeGlyph = '💤';
280+
const zombieOptionLabel = 'zombie';
281+
const zeroOptionLabel = 'zero';
282+
const buzzingOptionLabel = 'buzzing';
283+
const biohazardOptionLabel = 'biohazard';
284+
285+
// Adjust the order so the best match appears last
286+
final emojiSequence = [
287+
zulipOptionLabel,
288+
zzzOptionLabel,
289+
unicodeGlyph,
290+
zanyFaceOptionLabel,
291+
zebraOptionLabel,
292+
zeroOptionLabel,
293+
zombieOptionLabel,
294+
buzzingOptionLabel,
295+
// biohazardOptionLabel, this won't be rendered in the list initally since it is the 7th option.
296+
];
297+
298+
await tester.enterText(composeInputFinder, 'hi :');
299+
await tester.enterText(composeInputFinder, 'hi :z');
300+
await tester.pumpAndSettle();
301+
302+
final listViewFinder = find.byType(ListView);
303+
expect(listViewFinder, findsOneWidget, reason: 'ListView should be rendered');
304+
305+
final positions = emojiSequence.map((icon) {
306+
final finder = find.text(icon);
307+
expect(finder, findsOneWidget, reason: 'Each emoji option should be rendered');
308+
return tester.getTopLeft(finder).dy;
309+
}).toList();
310+
311+
for (int i = 0; i < positions.length - 1; i++) {
312+
expect(positions[i] > positions[i + 1], isTrue,
313+
reason: '${emojiSequence[i + 1]} should appear above ${emojiSequence[i]} because of reverse order');
314+
}
315+
316+
final initialScrollOffset = tester.getTopLeft(listViewFinder).dy;
317+
await tester.drag(listViewFinder, const Offset(0, -50));
318+
await tester.pumpAndSettle();
319+
final scrollOffsetAfterDragDown = tester.getTopLeft(listViewFinder).dy;
320+
321+
expect(scrollOffsetAfterDragDown, initialScrollOffset,
322+
reason: 'ListView should not scroll down because it is already at the bottom');
323+
324+
final biohazardFinder = find.text(biohazardOptionLabel);
325+
expect(biohazardFinder, findsNothing, reason: 'The biohazard emoji should not be visible before scrolling up');
326+
327+
// Scroll up
328+
await tester.drag(listViewFinder, const Offset(0, 50));
329+
await tester.pumpAndSettle();
330+
331+
expect(biohazardFinder, findsOneWidget, reason: 'The biohazard emoji should be visible after scrolling up');
332+
333+
final firstEmojiPositionAfterScrollUp = tester.getTopLeft(find.text(emojiSequence[0])).dy;
334+
// print(firstEmojiPositionAfterScrollUp);
335+
// print(positions[0]);
336+
expect(firstEmojiPositionAfterScrollUp >= positions[0], isTrue,
337+
reason: 'Scrolling up should reveal other emoji matches');
338+
339+
expect(tester.getTopLeft(find.text(biohazardOptionLabel)).runtimeType,const Offset(1,2).runtimeType, reason:"7th best result should only be visible on scrolling up");
340+
341+
debugNetworkImageHttpClientProvider = null;
342+
343+
});
344+
250345
testWidgets('text emoji means just show text', (tester) async {
251346
final composeInputFinder = await setupToComposeInput(tester);
252347
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);

0 commit comments

Comments
 (0)