[web] Fix keyboard not showing up when iOS input has decoration text (flutter/engine#55152)

In certain situations, semantics elements get assigned `pointer-events: none` when they aren't supposed to.

One such situation is when a text field has a [decoration error text](https://api.flutter.dev/flutter/material/InputDecoration/errorText.html). The semantics node become a container, and we always set `pointer-events: none` on container nodes.

This PR introduces an `acceptsPointerEvents` getter on `SemanticRole` and `SemanticBehavior` to control when `pointer-events` should be `all` or `none`.

Fixes https://github.com/flutter/flutter/issues/141975
This commit is contained in:
Mouad Debbar
2024-09-24 11:03:00 -04:00
committed by GitHub
parent 845eb2cf79
commit d99ff74d1a
5 changed files with 101 additions and 4 deletions

View File

@@ -28,6 +28,13 @@ class SemanticPlatformView extends SemanticRole {
preferredLabelRepresentation: LabelRepresentation.ariaLabel,
);
/// Ignores pointer events on all platform view nodes.
///
/// This is so that the platform views are not obscured by semantic elements
/// and can be reached by inspecting the web page.
@override
bool get acceptsPointerEvents => false;
@override
void update() {
super.update();

View File

@@ -439,6 +439,26 @@ abstract class SemanticRole {
/// The semantics object managed by this role.
final SemanticsObject semanticsObject;
/// Whether this role accepts pointer events.
///
/// This boolean decides whether to set the `pointer-events` CSS property to
/// `all` or to `none` on the semantics [element].
bool get acceptsPointerEvents {
final behaviors = _behaviors;
if (behaviors != null) {
for (final behavior in behaviors) {
if (behavior.acceptsPointerEvents) {
return true;
}
}
}
// Ignore pointer events on all container nodes.
if (semanticsObject.hasChildren) {
return false;
}
return true;
}
/// Semantic behaviors provided by this role, if any.
List<SemanticBehavior>? get behaviors => _behaviors;
List<SemanticBehavior>? _behaviors;
@@ -734,6 +754,12 @@ abstract class SemanticBehavior {
final SemanticRole owner;
/// Whether this role accepts pointer events.
///
/// This boolean decides whether to set the `pointer-events` CSS property to
/// `all` or to `none` on [SemanticsObject.element].
bool get acceptsPointerEvents => false;
/// Called immediately after the [semanticsObject] updates some of its fields.
///
/// A concrete implementation of this method would typically use some of the
@@ -1439,10 +1465,7 @@ class SemanticsObject {
recomputePositionAndSize();
}
// Ignore pointer events on all container nodes and all platform view nodes.
// This is so that the platform views are not obscured by semantic elements
// and can be reached by inspecting the web page.
if (!hasChildren && !isPlatformView) {
if (semanticRole!.acceptsPointerEvents) {
element.style.pointerEvents = 'all';
} else {
element.style.pointerEvents = 'none';

View File

@@ -53,6 +53,9 @@ class Tappable extends SemanticBehavior {
owner.element.addEventListener('click', _clickListener);
}
@override
bool get acceptsPointerEvents => true;
DomEventListener? _clickListener;
bool _isListening = false;

View File

@@ -213,6 +213,9 @@ class SemanticTextField extends SemanticRole {
_initializeEditableElement();
}
@override
bool get acceptsPointerEvents => true;
/// The element used for editing, e.g. `<input>`, `<textarea>`, which is
/// different from the host [element].
late final DomHTMLElement editableElement;

View File

@@ -1298,6 +1298,67 @@ void _testContainer() {
semantics().semanticsEnabled = false;
});
test('containers can be opaque if tappable', () async {
semantics()
..debugOverrideTimestampFunction(() => _testTime)
..semanticsEnabled = true;
final ui.SemanticsUpdateBuilder builder = ui.SemanticsUpdateBuilder();
updateNode(
builder,
actions: 0 | ui.SemanticsAction.tap.index,
childrenInTraversalOrder: Int32List.fromList(<int>[1, 2]),
childrenInHitTestOrder: Int32List.fromList(<int>[1, 2]),
);
updateNode(builder, id: 1);
updateNode(builder, id: 2);
owner().updateSemantics(builder.build());
expectSemanticsTree(owner(), '''
<sem>
<sem-c>
<sem style="z-index: 2"></sem>
<sem style="z-index: 1"></sem>
</sem-c>
</sem>''');
final DomElement root = owner().semanticsHost.querySelector('#flt-semantic-node-0')!;
expect(root.style.pointerEvents, 'all');
semantics().semanticsEnabled = false;
});
test('container can be opaque if it is a text field', () async {
semantics()
..debugOverrideTimestampFunction(() => _testTime)
..semanticsEnabled = true;
final ui.SemanticsUpdateBuilder builder = ui.SemanticsUpdateBuilder();
updateNode(
builder,
flags: 0 | ui.SemanticsFlag.isTextField.index,
childrenInTraversalOrder: Int32List.fromList(<int>[1, 2]),
childrenInHitTestOrder: Int32List.fromList(<int>[1, 2]),
);
updateNode(builder, id: 1);
updateNode(builder, id: 2);
owner().updateSemantics(builder.build());
expectSemanticsTree(owner(), '''
<sem>
<input>
<sem-c>
<sem style="z-index: 2"></sem>
<sem style="z-index: 1"></sem>
</sem-c>
</sem>''');
final DomElement root = owner().semanticsHost.querySelector('#flt-semantic-node-0')!;
expect(root.style.pointerEvents, 'all');
semantics().semanticsEnabled = false;
});
test('descendant nodes are removed from the node map, unless reparented', () async {
semantics()
..debugOverrideTimestampFunction(() => _testTime)