Present tooltip to screen reader on Windows (flutter/engine#36799)

* Propagate tooltip property

* Append tooltip to name

* Formatting

* Fix unit tests

* Tooltip at end

* Tooltip
This commit is contained in:
yaakovschectman
2022-10-17 16:57:11 -04:00
committed by GitHub
parent a55e414eef
commit 723551a5ee
13 changed files with 106 additions and 2 deletions

View File

@@ -283,6 +283,7 @@ void AccessibilityBridge::ConvertFlutterUpdate(const SemanticsNode& node,
SetStringListAttributesFromFlutterUpdate(node_data, node);
SetNameFromFlutterUpdate(node_data, node);
SetValueFromFlutterUpdate(node_data, node);
SetTooltipFromFlutterUpdate(node_data, node);
node_data.relative_bounds.bounds.SetRect(node.rect.left, node.rect.top,
node.rect.right - node.rect.left,
node.rect.bottom - node.rect.top);
@@ -516,6 +517,12 @@ void AccessibilityBridge::SetValueFromFlutterUpdate(ui::AXNodeData& node_data,
node_data.SetValue(node.value);
}
void AccessibilityBridge::SetTooltipFromFlutterUpdate(
ui::AXNodeData& node_data,
const SemanticsNode& node) {
node_data.SetTooltip(node.tooltip);
}
void AccessibilityBridge::SetTreeData(const SemanticsNode& node,
ui::AXTreeUpdate& tree_update) {
FlutterSemanticsFlag flags = node.flags;
@@ -582,6 +589,9 @@ AccessibilityBridge::FromFlutterSemanticsNode(
if (flutter_node->decreased_value) {
result.decreased_value = std::string(flutter_node->decreased_value);
}
if (flutter_node->tooltip) {
result.tooltip = std::string(flutter_node->tooltip);
}
result.text_direction = flutter_node->text_direction;
result.rect = flutter_node->rect;
result.transform = flutter_node->transform;

View File

@@ -195,6 +195,7 @@ class AccessibilityBridge
std::string value;
std::string increased_value;
std::string decreased_value;
std::string tooltip;
FlutterTextDirection text_direction;
FlutterRect rect;
FlutterTransformation transform;
@@ -249,6 +250,8 @@ class AccessibilityBridge
const SemanticsNode& node);
void SetValueFromFlutterUpdate(ui::AXNodeData& node_data,
const SemanticsNode& node);
void SetTooltipFromFlutterUpdate(ui::AXNodeData& node_data,
const SemanticsNode& node);
void SetTreeData(const SemanticsNode& node, ui::AXTreeUpdate& tree_update);
SemanticsNode FromFlutterSemanticsNode(
const FlutterSemanticsNode* flutter_node);

View File

@@ -32,6 +32,7 @@ FlutterSemanticsNode CreateSemanticsNode(
.child_count = children ? children->size() : 0,
.children_in_traversal_order = children ? children->data() : nullptr,
.custom_accessibility_actions_count = 0,
.tooltip = "",
};
}

View File

@@ -57,6 +57,7 @@ TEST(FlutterPlatformNodeDelegateTest, canPerfomActions) {
root.value = "";
root.increased_value = "";
root.decreased_value = "";
root.tooltip = "";
root.child_count = 0;
root.custom_accessibility_actions_count = 0;
bridge->AddFlutterSemanticsNodeUpdate(&root);
@@ -102,6 +103,7 @@ TEST(FlutterPlatformNodeDelegateTest, canGetAXNode) {
root.value = "";
root.increased_value = "";
root.decreased_value = "";
root.tooltip = "";
root.child_count = 0;
root.custom_accessibility_actions_count = 0;
bridge->AddFlutterSemanticsNodeUpdate(&root);
@@ -123,6 +125,7 @@ TEST(FlutterPlatformNodeDelegateTest, canCalculateBoundsCorrectly) {
root.value = "";
root.increased_value = "";
root.decreased_value = "";
root.tooltip = "";
root.child_count = 1;
int32_t children[] = {1};
root.children_in_traversal_order = children;
@@ -138,6 +141,7 @@ TEST(FlutterPlatformNodeDelegateTest, canCalculateBoundsCorrectly) {
child1.value = "";
child1.increased_value = "";
child1.decreased_value = "";
child1.tooltip = "";
child1.child_count = 0;
child1.custom_accessibility_actions_count = 0;
child1.rect = {0, 0, 50, 50}; // LTRB
@@ -168,6 +172,7 @@ TEST(FlutterPlatformNodeDelegateTest, canCalculateOffScreenBoundsCorrectly) {
root.value = "";
root.increased_value = "";
root.decreased_value = "";
root.tooltip = "";
root.child_count = 1;
int32_t children[] = {1};
root.children_in_traversal_order = children;
@@ -183,6 +188,7 @@ TEST(FlutterPlatformNodeDelegateTest, canCalculateOffScreenBoundsCorrectly) {
child1.value = "";
child1.increased_value = "";
child1.decreased_value = "";
child1.tooltip = "";
child1.child_count = 0;
child1.custom_accessibility_actions_count = 0;
child1.rect = {90, 90, 100, 100}; // LTRB
@@ -213,6 +219,7 @@ TEST(FlutterPlatformNodeDelegateTest, canUseOwnerBridge) {
root.value = "";
root.increased_value = "";
root.decreased_value = "";
root.tooltip = "";
root.child_count = 1;
int32_t children[] = {1};
root.children_in_traversal_order = children;
@@ -228,6 +235,7 @@ TEST(FlutterPlatformNodeDelegateTest, canUseOwnerBridge) {
child1.value = "";
child1.increased_value = "";
child1.decreased_value = "";
child1.tooltip = "";
child1.child_count = 0;
child1.custom_accessibility_actions_count = 0;
child1.rect = {0, 0, 50, 50}; // LTRB

View File

@@ -67,6 +67,7 @@ TEST(AccessibilityBridgeMacDelegateTest,
root.value = "";
root.increased_value = "";
root.decreased_value = "";
root.tooltip = "";
root.child_count = 0;
root.custom_accessibility_actions_count = 0;
bridge->AddFlutterSemanticsNodeUpdate(&root);
@@ -119,6 +120,7 @@ TEST(AccessibilityBridgeMacDelegateTest, doesNotSendAccessibilityCreateNotificat
root.value = "";
root.increased_value = "";
root.decreased_value = "";
root.tooltip = "";
root.child_count = 0;
root.custom_accessibility_actions_count = 0;
bridge->AddFlutterSemanticsNodeUpdate(&root);
@@ -172,6 +174,7 @@ TEST(AccessibilityBridgeMacDelegateTest, doesNotSendAccessibilityCreateNotificat
root.value = "";
root.increased_value = "";
root.decreased_value = "";
root.tooltip = "";
root.child_count = 0;
root.custom_accessibility_actions_count = 0;
bridge->AddFlutterSemanticsNodeUpdate(&root);

View File

@@ -145,6 +145,7 @@ TEST_F(FlutterEngineTest, CanToggleAccessibility) {
root.value = "";
root.increased_value = "";
root.decreased_value = "";
root.tooltip = "";
root.child_count = 1;
int32_t children[] = {1};
root.children_in_traversal_order = children;
@@ -162,6 +163,7 @@ TEST_F(FlutterEngineTest, CanToggleAccessibility) {
child1.value = "";
child1.increased_value = "";
child1.decreased_value = "";
child1.tooltip = "";
child1.child_count = 0;
child1.custom_accessibility_actions_count = 0;
update_node_callback(&child1, (void*)CFBridgingRetain(engine));
@@ -238,6 +240,7 @@ TEST_F(FlutterEngineTest, CanToggleAccessibilityWhenHeadless) {
root.value = "";
root.increased_value = "";
root.decreased_value = "";
root.tooltip = "";
root.child_count = 1;
int32_t children[] = {1};
root.children_in_traversal_order = children;
@@ -255,6 +258,7 @@ TEST_F(FlutterEngineTest, CanToggleAccessibilityWhenHeadless) {
child1.value = "";
child1.increased_value = "";
child1.decreased_value = "";
child1.tooltip = "";
child1.child_count = 0;
child1.custom_accessibility_actions_count = 0;
update_node_callback(&child1, (void*)CFBridgingRetain(engine));
@@ -327,6 +331,7 @@ TEST_F(FlutterEngineTest, ResetsAccessibilityBridgeWhenSetsNewViewController) {
root.value = "";
root.increased_value = "";
root.decreased_value = "";
root.tooltip = "";
root.child_count = 1;
int32_t children[] = {1};
root.children_in_traversal_order = children;
@@ -344,6 +349,7 @@ TEST_F(FlutterEngineTest, ResetsAccessibilityBridgeWhenSetsNewViewController) {
child1.value = "";
child1.increased_value = "";
child1.decreased_value = "";
child1.tooltip = "";
child1.child_count = 0;
child1.custom_accessibility_actions_count = 0;
update_node_callback(&child1, (void*)CFBridgingRetain(engine));

View File

@@ -46,6 +46,7 @@ TEST(FlutterPlatformNodeDelegateMac, Basics) {
root.value = "";
root.increased_value = "";
root.decreased_value = "";
root.tooltip = "";
root.child_count = 0;
root.custom_accessibility_actions_count = 0;
bridge->AddFlutterSemanticsNodeUpdate(&root);
@@ -82,6 +83,7 @@ TEST(FlutterPlatformNodeDelegateMac, SelectableTextHasCorrectSemantics) {
root.value = "selectable text";
root.increased_value = "";
root.decreased_value = "";
root.tooltip = "";
root.child_count = 0;
root.custom_accessibility_actions_count = 0;
bridge->AddFlutterSemanticsNodeUpdate(&root);
@@ -122,6 +124,7 @@ TEST(FlutterPlatformNodeDelegateMac, SelectableTextWithoutSelectionReturnZeroRan
root.value = "selectable text";
root.increased_value = "";
root.decreased_value = "";
root.tooltip = "";
root.child_count = 0;
root.custom_accessibility_actions_count = 0;
bridge->AddFlutterSemanticsNodeUpdate(&root);
@@ -168,6 +171,7 @@ TEST(FlutterPlatformNodeDelegateMac, CanPerformAction) {
root.value = "";
root.increased_value = "";
root.decreased_value = "";
root.tooltip = "";
root.child_count = 1;
int32_t children[] = {1};
root.children_in_traversal_order = children;
@@ -181,6 +185,7 @@ TEST(FlutterPlatformNodeDelegateMac, CanPerformAction) {
child1.value = "";
child1.increased_value = "";
child1.decreased_value = "";
child1.tooltip = "";
child1.child_count = 0;
child1.custom_accessibility_actions_count = 0;
bridge->AddFlutterSemanticsNodeUpdate(&child1);
@@ -248,6 +253,7 @@ TEST(FlutterPlatformNodeDelegateMac, TextFieldUsesFlutterTextField) {
root.value = "";
root.increased_value = "";
root.decreased_value = "";
root.tooltip = "";
root.child_count = 1;
int32_t children[] = {1};
root.children_in_traversal_order = children;
@@ -268,6 +274,7 @@ TEST(FlutterPlatformNodeDelegateMac, TextFieldUsesFlutterTextField) {
child1.value = "textfield";
child1.increased_value = "";
child1.decreased_value = "";
child1.tooltip = "";
child1.text_selection_base = -1;
child1.text_selection_extent = -1;
child1.child_count = 0;

View File

@@ -1462,6 +1462,7 @@ FlutterEngineResult FlutterEngineInitialize(size_t version,
node.customAccessibilityActions.size(),
node.customAccessibilityActions.data(),
node.platformViewId,
node.tooltip.c_str(),
};
update_semantics_node_callback(&embedder_node, user_data);
}

View File

@@ -1082,8 +1082,8 @@ typedef struct {
/// A value that `value` will have after a kFlutterSemanticsActionDecrease`
/// action has been performed.
const char* decreased_value;
/// The reading direction for `label`, `value`, `hint`, `increasedValue`, and
/// `decreasedValue`.
/// The reading direction for `label`, `value`, `hint`, `increasedValue`,
/// `decreasedValue`, and `tooltip`.
FlutterTextDirection text_direction;
/// The bounding box for this node in its coordinate system.
FlutterRect rect;
@@ -1104,6 +1104,8 @@ typedef struct {
/// Identifier of the platform view associated with this semantics node, or
/// -1 if none.
FlutterPlatformViewIdentifier platform_view_id;
/// A textual tooltip attached to the node.
const char* tooltip;
} FlutterSemanticsNode;
/// `FlutterSemanticsCustomAction` ID used as a sentinel to signal the end of a

View File

@@ -821,5 +821,48 @@ TEST(FlutterWindowsViewTest, SwitchNativeState) {
}
}
TEST(FlutterWindowsViewTest, TooltipNodeData) {
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
EngineModifier modifier(engine.get());
modifier.embedder_api().UpdateSemanticsEnabled =
[](FLUTTER_API_SYMBOL(FlutterEngine) engine, bool enabled) {
return kSuccess;
};
auto window_binding_handler =
std::make_unique<::testing::NiceMock<MockWindowBindingHandler>>();
FlutterWindowsView view(std::move(window_binding_handler));
view.SetEngine(std::move(engine));
// Enable semantics to instantiate accessibility bridge.
view.OnUpdateSemanticsEnabled(true);
auto bridge = view.GetEngine()->accessibility_bridge().lock();
ASSERT_TRUE(bridge);
FlutterSemanticsNode root{sizeof(FlutterSemanticsNode), 0};
root.id = 0;
root.label = "root";
root.hint = "";
root.value = "";
root.increased_value = "";
root.decreased_value = "";
root.tooltip = "tooltip";
root.child_count = 0;
root.custom_accessibility_actions_count = 0;
root.flags = static_cast<FlutterSemanticsFlag>(
FlutterSemanticsFlag::kFlutterSemanticsFlagIsTextField);
bridge->AddFlutterSemanticsNodeUpdate(&root);
bridge->CommitUpdates();
auto root_node = bridge
->GetFlutterPlatformNodeDelegateFromID(
AccessibilityBridge::kRootNodeId)
.lock();
std::string tooltip = root_node->GetData().GetStringAttribute(
ax::mojom::StringAttribute::kTooltip);
EXPECT_EQ(tooltip, "tooltip");
}
} // namespace testing
} // namespace flutter

View File

@@ -633,6 +633,14 @@ void AXNodeData::SetValue(const std::u16string& value) {
SetValue(base::UTF16ToUTF8(value));
}
void AXNodeData::SetTooltip(const std::string& value) {
AddStringAttribute(ax::mojom::StringAttribute::kTooltip, value);
}
void AXNodeData::SetTooltip(const std::u16string& value) {
SetTooltip(base::UTF16ToUTF8(value));
}
bool AXNodeData::HasState(ax::mojom::State state_enum) const {
return IsFlagSet(state, static_cast<uint32_t>(state_enum));
}

View File

@@ -148,6 +148,10 @@ struct AX_BASE_EXPORT AXNodeData {
void SetValue(const std::string& value);
void SetValue(const std::u16string& value);
// Adds the tooltip attribute or replaces it if already present.
void SetTooltip(const std::string& value);
void SetTooltip(const std::u16string& value);
// Returns true if the given enum bit is 1.
bool HasState(ax::mojom::State state) const;
bool HasAction(ax::mojom::Action action) const;

View File

@@ -1017,6 +1017,14 @@ IFACEMETHODIMP AXPlatformNodeWin::get_accName(VARIANT var_id, BSTR* name_bstr) {
bool has_name = target->HasStringAttribute(ax::mojom::StringAttribute::kName);
std::u16string name = target->GetNameAsString16();
// Simply appends the tooltip, if any, to the end of the MSAA name.
const std::u16string tooltip =
target->GetString16Attribute(ax::mojom::StringAttribute::kTooltip);
if (!tooltip.empty()) {
AppendTextToString(tooltip, &name);
}
auto status = GetData().GetImageAnnotationStatus();
switch (status) {
case ax::mojom::ImageAnnotationStatus::kNone: