Skip to content

Commit 5c1670f

Browse files
committed
[web] End the autofill fill window when any field of the form gets focus
A password manager's menu taking the focus from the active field opens the fill window, which holds the fields still while the manager fills. The window ended only when that same field, the window or the page got the focus back. A manager that focuses another field of the form instead left the window open, so placement updates stayed disabled and the fields stayed at stale coordinates, for example after a resize. Every field of the form now ends the fill window when it gets the focus. A placement that arrives while the window is open is now held by the form and applied when the window ends, instead of being dropped until the next layout change. It moves the fields but not the focus, since the manager may have just focused another field. A form that goes dormant drops the held placement, as its connection is closing.
1 parent dd6e4da commit 5c1670f

2 files changed

Lines changed: 125 additions & 6 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_editing.dart‎

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -589,18 +589,39 @@ class EngineAutofillForm {
589589
/// Whether the fields are held still for a fill. See [beginFillWindow].
590590
bool get fillWindowActive => _fillWindowActive;
591591

592+
/// The focused field's latest placement, received while the fill window held
593+
/// the fields still. See [holdPlacement].
594+
EditableTextGeometry? _heldPlacement;
595+
592596
/// Holds the fields still while focus is away from the form, as when a
593597
/// password manager's menu takes it to fill the form.
594598
///
595-
/// Ended by [endFillWindow] when focus returns to the field or the page, or
596-
/// when the form goes dormant: an event boundary, not a timeout.
599+
/// Ended by [endFillWindow] when any field of the form or the page gets the
600+
/// focus, or when the form goes dormant: an event boundary, not a timeout.
597601
void beginFillWindow() {
598602
_fillWindowActive = true;
599603
}
600604

605+
/// Defers [geometry] for the focused field until the fill window ends, so a
606+
/// resize or scroll during a fill is applied late instead of lost.
607+
void holdPlacement(EditableTextGeometry geometry) {
608+
_heldPlacement = geometry;
609+
}
610+
601611
/// Ends the window opened by [beginFillWindow], so placement resumes.
612+
///
613+
/// A placement held during the window is applied now, to the focused field
614+
/// and the fields clustered under it. It moves them but not the focus: the
615+
/// manager may have just focused another field to fill it.
602616
void endFillWindow() {
603617
_fillWindowActive = false;
618+
final EditableTextGeometry? held = _heldPlacement;
619+
_heldPlacement = null;
620+
final DomHTMLElement? focused = elements[focusedElementId];
621+
if (held != null && focused != null) {
622+
held.applyToDomElement(focused);
623+
clusterNonFocusedFieldsAt(focused);
624+
}
604625
}
605626

606627
/// Positions the non-focused proxies right next to the focused field.
@@ -685,7 +706,9 @@ class EngineAutofillForm {
685706

686707
// Read before the window is ended below.
687708
final bool fillInProgress = _fillWindowActive;
688-
// The form is deactivated; end any open fill window so it never stays frozen.
709+
// The form is deactivated; end any open fill window so it never stays
710+
// frozen. Its connection is closing, so a held placement is dropped.
711+
_heldPlacement = null;
689712
endFillWindow();
690713
_isDormant = true;
691714
dormantForms[formIdentifier] = this;
@@ -979,6 +1002,7 @@ class EngineAutofillForm {
9791002
/// each time the form wakes. The focused field is observed only after its
9801003
/// connection has closed (see the guard in the listener); while it is live its
9811004
/// edits and autofills are delivered by [DefaultTextEditingStrategy.handleChange].
1005+
/// Every field also ends the fill window when it gets the focus.
9821006
void attachPersistentFormListeners() {
9831007
_ensureAutofillStyleInjected();
9841008
_cancelFormSubscriptions();
@@ -1015,6 +1039,13 @@ class EngineAutofillForm {
10151039
),
10161040
);
10171041
}
1042+
// A password manager does not always hand the focus back to the field it
1043+
// was invoked from; it can focus another field of the form to fill it.
1044+
// Any field gaining the focus ends the fill window, so placement does not
1045+
// stay frozen at stale coordinates. See [beginFillWindow].
1046+
_formSubscriptions.add(
1047+
DomSubscription(element, 'focus', createDomEventListener((DomEvent _) => endFillWindow())),
1048+
);
10181049
}
10191050
}
10201051

@@ -2258,9 +2289,11 @@ abstract class DefaultTextEditingStrategy
22582289
// Hold the field where it is during a fill (see
22592290
// EngineAutofillForm.beginFillWindow). Re-applying the transform as the
22602291
// browser scrolls the field under the opening menu churns the DOM under
2261-
// the manager's overlay, which can break its fill. The stored geometry is
2262-
// applied by the next placement once the window ends.
2263-
if (inputConfiguration.autofillGroup?.fillWindowActive ?? false) {
2292+
// the manager's overlay, which can break its fill. The form applies the
2293+
// held placement when the window ends.
2294+
final EngineAutofillForm? group = inputConfiguration.autofillGroup;
2295+
if (group != null && group.fillWindowActive) {
2296+
group.holdPlacement(textGeometry);
22642297
return;
22652298
}
22662299
// On updates, we shouldn't go through the entire placeElement() flow if

‎engine/src/flutter/lib/web_ui/test/engine/text_editing_test.dart‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3952,6 +3952,92 @@ Future<void> testMain() async {
39523952
expectFormParked(form.group);
39533953
});
39543954

3955+
// A focus event as the browser dispatches it. Unlike createDomEvent it does
3956+
// not bubble; one that did would reach the window's focus listener, which
3957+
// ends the fill window by itself and would hide what these tests check.
3958+
DomEvent focusEvent() => domDocument.createEvent('Event')..initEvent('focus', false);
3959+
3960+
// The field of a clustered form that is not the focused one.
3961+
DomHTMLElement otherField(EngineAutofillForm group, DomHTMLElement active) =>
3962+
group.elements.values.firstWhere((DomHTMLElement element) => !identical(element, active));
3963+
3964+
// Lays the focused field out 280 px to the right of where openClusteredForm
3965+
// put it, as a resize of the browser window does.
3966+
void moveField(void Function(MethodCall) send) => send(
3967+
configureSetSizeAndTransformMethodCall(
3968+
150,
3969+
50,
3970+
Matrix4.translationValues(290.0, 20.0, 30.0).storage.toList(),
3971+
),
3972+
);
3973+
3974+
test('a manager focusing another field of the form ends the fill window', () {
3975+
void send(MethodCall call) =>
3976+
textEditing!.channel.handleTextInput(codec.encodeMethodCall(call), (ByteData? _) {});
3977+
final ({EngineAutofillForm group, DomHTMLElement active}) form = openClusteredForm(send);
3978+
final EngineAutofillForm group = form.group;
3979+
final DomHTMLElement active = form.active;
3980+
final DomHTMLElement other = otherField(group, active);
3981+
3982+
// A password manager's menu taking the focus from the field it was
3983+
// invoked on opens the fill window. The manager then focuses the other
3984+
// field to fill it, instead of handing the focus back.
3985+
group.beginFillWindow();
3986+
other.dispatchEvent(focusEvent());
3987+
3988+
// The app is laid out again: both proxies follow the field instead of
3989+
// staying at stale coordinates.
3990+
final double activeLeft = active.getBoundingClientRect().left;
3991+
final double otherLeft = other.getBoundingClientRect().left;
3992+
moveField(send);
3993+
expect(active.getBoundingClientRect().left - activeLeft, 280);
3994+
expect(other.getBoundingClientRect().left - otherLeft, 280);
3995+
3996+
send(const MethodCall('TextInput.clearClient'));
3997+
});
3998+
3999+
for (final (String trigger, void Function(DomHTMLElement, DomHTMLElement) endFill)
4000+
in <(String, void Function(DomHTMLElement, DomHTMLElement))>[
4001+
(
4002+
'another field of the form gets the focus',
4003+
(_, DomHTMLElement other) => other.dispatchEvent(focusEvent()),
4004+
),
4005+
(
4006+
'the field gets the focus back',
4007+
(DomHTMLElement active, _) => active.dispatchEvent(focusEvent()),
4008+
),
4009+
(
4010+
'the window gets the focus back',
4011+
(_, _) => domWindow.dispatchEvent(createDomEvent('Event', 'focus')),
4012+
),
4013+
]) {
4014+
test('a placement held during the fill window is applied when $trigger', () {
4015+
void send(MethodCall call) =>
4016+
textEditing!.channel.handleTextInput(codec.encodeMethodCall(call), (ByteData? _) {});
4017+
final ({EngineAutofillForm group, DomHTMLElement active}) form = openClusteredForm(send);
4018+
final EngineAutofillForm group = form.group;
4019+
final DomHTMLElement active = form.active;
4020+
final DomHTMLElement other = otherField(group, active);
4021+
final double activeLeft = active.getBoundingClientRect().left;
4022+
final double otherLeft = other.getBoundingClientRect().left;
4023+
4024+
// The app is laid out again while a manager fills, as when the browser
4025+
// window is resized with its menu open: the fields hold still.
4026+
group.beginFillWindow();
4027+
moveField(send);
4028+
expect(active.getBoundingClientRect().left, activeLeft);
4029+
expect(other.getBoundingClientRect().left, otherLeft);
4030+
4031+
// Once the fill window ends, the held placement is applied rather than
4032+
// waiting for a layout change that may never come.
4033+
endFill(active, other);
4034+
expect(active.getBoundingClientRect().left - activeLeft, 280);
4035+
expect(other.getBoundingClientRect().left - otherLeft, 280);
4036+
4037+
send(const MethodCall('TextInput.clearClient'));
4038+
});
4039+
}
4040+
39554041
// A password manager fills a login form by focusing each field in turn and
39564042
// writing into it, and the field it starts from is whichever one the user
39574043
// invoked it on. Whatever it writes has to reach the framework in every

0 commit comments

Comments
 (0)