Skip to content

Commit

Permalink
Support for accessibility label and hint (flutter#4264)
Browse files Browse the repository at this point in the history
* Support for accessibility label and hint

* review comments
  • Loading branch information
goderbauer authored Oct 23, 2017
1 parent 8e79156 commit 91071f8
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 9 deletions.
12 changes: 10 additions & 2 deletions lib/ui/semantics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,10 @@ class SemanticsUpdateBuilder extends NativeFieldWrapperClass2 {
/// asynchronously, the [Window.onSemanticsAction] callback might be called
/// with an action that is no longer possible.
///
/// The `label` is a string that describes this node. Its reading direction is
/// given by `textDirection`.
/// The `label` is a string that describes this node. The `value` property
/// describes the current value of the node as a string. The `hint` string
/// describes what result an action performed on this node has. The reading
/// direction of all these strings is given by `textDirection`.
///
/// The `rect` is the region occupied by this node in its own coordinate
/// system.
Expand All @@ -228,6 +230,8 @@ class SemanticsUpdateBuilder extends NativeFieldWrapperClass2 {
int actions,
Rect rect,
String label,
String hint,
String value,
TextDirection textDirection,
Float64List transform,
Int32List children
Expand All @@ -242,6 +246,8 @@ class SemanticsUpdateBuilder extends NativeFieldWrapperClass2 {
rect.right,
rect.bottom,
label,
hint,
value,
textDirection != null ? textDirection.index + 1 : 0,
transform,
children);
Expand All @@ -255,6 +261,8 @@ class SemanticsUpdateBuilder extends NativeFieldWrapperClass2 {
double right,
double bottom,
String label,
String hint,
String value,
int textDirection,
Float64List transform,
Int32List children
Expand Down
2 changes: 2 additions & 0 deletions lib/ui/semantics/semantics_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ struct SemanticsNode {
int32_t flags = 0;
int32_t actions = 0;
std::string label;
std::string hint;
std::string value;
int32_t textDirection = 0; // 0=unknown, 1=rtl, 2=ltr

SkRect rect = SkRect::MakeEmpty();
Expand Down
4 changes: 4 additions & 0 deletions lib/ui/semantics/semantics_update_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ void SemanticsUpdateBuilder::updateNode(int id,
double right,
double bottom,
std::string label,
std::string hint,
std::string value,
int textDirection,
const tonic::Float64List& transform,
const tonic::Int32List& children) {
Expand All @@ -51,6 +53,8 @@ void SemanticsUpdateBuilder::updateNode(int id,
node.actions = actions;
node.rect = SkRect::MakeLTRB(left, top, right, bottom);
node.label = label;
node.hint = hint;
node.value = value;
node.textDirection = textDirection;
node.transform.setColMajord(transform.data());
node.children = std::vector<int32_t>(
Expand Down
2 changes: 2 additions & 0 deletions lib/ui/semantics/semantics_update_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class SemanticsUpdateBuilder
double right,
double bottom,
std::string label,
std::string hint,
std::string value,
int textDirection,
const tonic::Float64List& transform,
const tonic::Int32List& children);
Expand Down
30 changes: 24 additions & 6 deletions shell/platform/android/io/flutter/view/AccessibilityBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public AccessibilityNodeInfo createAccessibilityNodeInfo(int virtualViewId) {
result.setCheckable((object.flags & SEMANTICS_FLAG_HAS_CHECKED_STATE) != 0);
result.setChecked((object.flags & SEMANTICS_FLAG_IS_CHECKED) != 0);
result.setSelected((object.flags & SEMANTICS_FLAG_IS_SELECTED) != 0);
result.setText(object.label);
result.setText(object.getValueLabelHint());

if ((object.flags & SEMANTICS_FLAG_IS_BUTTON) != 0) {
result.setClassName("android.widget.Button");
Expand Down Expand Up @@ -434,6 +434,8 @@ private class SemanticsObject {
int flags;
int actions;
String label;
String value;
String hint;
TextDirection textDirection;

private float left;
Expand Down Expand Up @@ -468,11 +470,14 @@ void updateWith(ByteBuffer buffer, String[] strings) {
flags = buffer.getInt();
actions = buffer.getInt();

final int stringIndex = buffer.getInt();
if (stringIndex == -1)
label = null;
else
label = strings[stringIndex];
int stringIndex = buffer.getInt();
label = stringIndex == -1 ? null : strings[stringIndex];

stringIndex = buffer.getInt();
value = stringIndex == -1 ? null : strings[stringIndex];

stringIndex = buffer.getInt();
hint = stringIndex == -1 ? null : strings[stringIndex];

textDirection = TextDirection.fromInt(buffer.getInt());

Expand Down Expand Up @@ -620,5 +625,18 @@ private float min(float a, float b, float c, float d) {
private float max(float a, float b, float c, float d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}

private String getValueLabelHint() {
StringBuilder sb = new StringBuilder();
String[] array = { value, label, hint };
for (String word: array) {
if (word != null && (word = word.trim()).length() > 0) {
if (sb.length() > 0)
sb.append(", ");
sb.append(word);
}
}
return sb.length() > 0 ? sb.toString() : null;
}
}
}
14 changes: 13 additions & 1 deletion shell/platform/android/platform_view_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ bool PlatformViewAndroid::ResourceContextMakeCurrent() {

void PlatformViewAndroid::UpdateSemantics(
std::vector<blink::SemanticsNode> update) {
constexpr size_t kBytesPerNode = 26 * sizeof(int32_t);
constexpr size_t kBytesPerNode = 28 * sizeof(int32_t);
constexpr size_t kBytesPerChild = sizeof(int32_t);

JNIEnv* env = fml::jni::AttachCurrentThread();
Expand Down Expand Up @@ -464,6 +464,18 @@ void PlatformViewAndroid::UpdateSemantics(
buffer_int32[position++] = strings.size();
strings.push_back(node.label);
}
if (node.value.empty()) {
buffer_int32[position++] = -1;
} else {
buffer_int32[position++] = strings.size();
strings.push_back(node.value);
}
if (node.hint.empty()) {
buffer_int32[position++] = -1;
} else {
buffer_int32[position++] = strings.size();
strings.push_back(node.hint);
}
buffer_int32[position++] = node.textDirection;
buffer_float32[position++] = node.rect.left();
buffer_float32[position++] = node.rect.top();
Expand Down
12 changes: 12 additions & 0 deletions shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ - (NSString*)accessibilityLabel {
return @(_node.label.data());
}

- (NSString*)accessibilityHint {
if (_node.hint.empty())
return nil;
return @(_node.hint.data());
}

- (NSString*)accessibilityValue {
if (_node.value.empty())
return nil;
return @(_node.value.data());
}

- (UIAccessibilityTraits)accessibilityTraits {
UIAccessibilityTraits traits = UIAccessibilityTraitNone;
if (_node.HasAction(blink::SemanticsAction::kIncrease) ||
Expand Down

0 comments on commit 91071f8

Please sign in to comment.