Skip to content

Commit

Permalink
Identify text fields as such to a11y on Android (flutter#4299)
Browse files Browse the repository at this point in the history
* Identify text fields as such to a11y on Android

* Add focus flag
  • Loading branch information
goderbauer authored Nov 1, 2017
1 parent 6c73503 commit 88cf09f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/ui/semantics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ class SemanticsFlags {
static const int _kIsCheckedIndex = 1 << 1;
static const int _kIsSelectedIndex = 1 << 2;
static const int _kIsButtonIndex = 1 << 3;
static const int _kIsTextFieldIndex = 1 << 4;
static const int _kIsFocusedIndex = 1 << 5;

const SemanticsFlags._(this.index);

Expand Down Expand Up @@ -161,14 +163,27 @@ class SemanticsFlags {
/// a button.
static const SemanticsFlags isButton = const SemanticsFlags._(_kIsButtonIndex);

/// Whether the semantic node represents a text field.
///
/// Text fields are announced as such and allow text input via accessibility
/// affordances.
static const SemanticsFlags isTextField = const SemanticsFlags._(_kIsTextFieldIndex);

/// Whether the semantic node currently holds the user's focus.
///
/// The focused element is usually the current receiver of keyboard inputs.
static const SemanticsFlags isFocused = const SemanticsFlags._(_kIsFocusedIndex);

/// The possible semantics flags.
///
/// The map's key is the [index] of the flag and the value is the flag itself.
static final Map<int, SemanticsFlags> values = const <int, SemanticsFlags>{
_kHasCheckedStateIndex: hasCheckedState,
_kIsCheckedIndex: isChecked,
_kIsSelectedIndex: isSelected,
_kIsButtonIndex: isButton
_kIsButtonIndex: isButton,
_kIsTextFieldIndex: isTextField,
_kIsFocusedIndex: isFocused,
};

@override
Expand All @@ -182,6 +197,10 @@ class SemanticsFlags {
return 'SemanticsFlags.isSelected';
case _kIsButtonIndex:
return 'SemanticsFlags.isButton';
case _kIsTextFieldIndex:
return 'SemanticsFlags.isTextField';
case _kIsFocusedIndex:
return 'SemanticsFlags.isFocused';
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class AccessibilityBridge extends AccessibilityNodeProvider implements BasicMess
private static final int SEMANTICS_FLAG_IS_CHECKED = 1 << 1;
private static final int SEMANTICS_FLAG_IS_SELECTED = 1 << 2;
private static final int SEMANTICS_FLAG_IS_BUTTON = 1 << 3;
private static final int SEMANTICS_FLAG_IS_TEXT_FIELD = 1 << 4;
private static final int SEMANTICS_FLAG_IS_FOCUSED = 1 << 5;

AccessibilityBridge(FlutterView owner) {
assert owner != null;
Expand Down Expand Up @@ -95,9 +97,14 @@ public AccessibilityNodeInfo createAccessibilityNodeInfo(int virtualViewId) {
result.setClassName("Flutter"); // TODO(goderbauer): Set proper class names
result.setSource(mOwner, virtualViewId);
result.setFocusable(object.isFocusable());
result.setFocused((object.flags & SEMANTICS_FLAG_IS_FOCUSED) != 0);

if (mFocusedObject != null)
result.setAccessibilityFocused(mFocusedObject.id == virtualViewId);

if ((object.flags & SEMANTICS_FLAG_IS_TEXT_FIELD) != 0)
result.setClassName("android.widget.EditText");

if (object.parent != null) {
assert object.id > 0;
result.setParent(mOwner, object.parent.id);
Expand Down

0 comments on commit 88cf09f

Please sign in to comment.