Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit ebf01dc

Browse files
Update FlutterPlatformNodeDelegate (#38615)
* Update FlutterPlatformNodeDelegate * Unit test
1 parent 398f5d3 commit ebf01dc

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

shell/platform/common/flutter_platform_node_delegate.cc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
#include <utility>
88

9+
#include "flutter/shell/platform/common/accessibility_bridge.h"
910
#include "flutter/third_party/accessibility/ax/ax_action_data.h"
11+
#include "flutter/third_party/accessibility/ax/ax_tree_manager_map.h"
1012
#include "flutter/third_party/accessibility/gfx/geometry/rect_conversions.h"
1113

1214
namespace flutter {
@@ -118,4 +120,46 @@ ui::AXPlatformNode* FlutterPlatformNodeDelegate::GetPlatformNode() const {
118120
return nullptr;
119121
}
120122

123+
gfx::NativeViewAccessible
124+
FlutterPlatformNodeDelegate::GetLowestPlatformAncestor() const {
125+
auto bridge_ptr = bridge_.lock();
126+
BASE_DCHECK(bridge_ptr);
127+
auto lowest_platform_ancestor = ax_node_->GetLowestPlatformAncestor();
128+
if (lowest_platform_ancestor) {
129+
return bridge_ptr->GetNativeAccessibleFromId(
130+
ax_node_->GetLowestPlatformAncestor()->id());
131+
}
132+
return nullptr;
133+
}
134+
135+
ui::AXNodePosition::AXPositionInstance
136+
FlutterPlatformNodeDelegate::CreateTextPositionAt(int offset) const {
137+
return ui::AXNodePosition::CreatePosition(*ax_node_, offset);
138+
}
139+
140+
ui::AXPlatformNode* FlutterPlatformNodeDelegate::GetFromNodeID(
141+
int32_t node_id) {
142+
ui::AXTreeManager* tree_manager =
143+
ui::AXTreeManagerMap::GetInstance().GetManager(
144+
ax_node_->tree()->GetAXTreeID());
145+
AccessibilityBridge* platform_manager =
146+
static_cast<AccessibilityBridge*>(tree_manager);
147+
return platform_manager->GetPlatformNodeFromTree(node_id);
148+
}
149+
150+
ui::AXPlatformNode* FlutterPlatformNodeDelegate::GetFromTreeIDAndNodeID(
151+
const ui::AXTreeID& tree_id,
152+
int32_t node_id) {
153+
ui::AXTreeManager* tree_manager =
154+
ui::AXTreeManagerMap::GetInstance().GetManager(tree_id);
155+
AccessibilityBridge* platform_manager =
156+
static_cast<AccessibilityBridge*>(tree_manager);
157+
return platform_manager->GetPlatformNodeFromTree(node_id);
158+
}
159+
160+
const ui::AXTree::Selection FlutterPlatformNodeDelegate::GetUnignoredSelection()
161+
const {
162+
return ax_node_->GetUnignoredSelection();
163+
}
164+
121165
} // namespace flutter

shell/platform/common/flutter_platform_node_delegate.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ class FlutterPlatformNodeDelegate : public ui::AXPlatformNodeDelegateBase {
125125
const ui::AXClippingBehavior clipping_behavior,
126126
ui::AXOffscreenResult* offscreen_result) const override;
127127

128+
// |ui::AXPlatformNodeDelegateBase|
129+
gfx::NativeViewAccessible GetLowestPlatformAncestor() const override;
130+
131+
// |ui::AXPlatformNodeDelegateBase|
132+
ui::AXNodePosition::AXPositionInstance CreateTextPositionAt(
133+
int offset) const override;
134+
128135
//------------------------------------------------------------------------------
129136
/// @brief Called only once, immediately after construction. The
130137
/// constructor doesn't take any arguments because in the Windows
@@ -147,6 +154,17 @@ class FlutterPlatformNodeDelegate : public ui::AXPlatformNodeDelegateBase {
147154
// Get the platform node represented by this delegate.
148155
virtual ui::AXPlatformNode* GetPlatformNode() const;
149156

157+
// |ui::AXPlatformNodeDelegateBase|
158+
virtual ui::AXPlatformNode* GetFromNodeID(int32_t id) override;
159+
160+
// |ui::AXPlatformNodeDelegateBase|
161+
virtual ui::AXPlatformNode* GetFromTreeIDAndNodeID(
162+
const ui::AXTreeID& tree_id,
163+
int32_t node_id) override;
164+
165+
// |ui::AXPlatformNodeDelegateBase|
166+
virtual const ui::AXTree::Selection GetUnignoredSelection() const override;
167+
150168
private:
151169
ui::AXNode* ax_node_;
152170
std::weak_ptr<OwnerBridge> bridge_;

shell/platform/common/flutter_platform_node_delegate_unittests.cc

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,5 +246,63 @@ TEST(FlutterPlatformNodeDelegateTest, canUseOwnerBridge) {
246246
EXPECT_EQ(result, false);
247247
}
248248

249+
TEST(FlutterPlatformNodeDelegateTest, selfIsLowestPlatformAncestor) {
250+
std::shared_ptr<TestAccessibilityBridge> bridge =
251+
std::make_shared<TestAccessibilityBridge>();
252+
FlutterSemanticsNode root;
253+
root.id = 0;
254+
root.label = "root";
255+
root.hint = "";
256+
root.value = "";
257+
root.increased_value = "";
258+
root.decreased_value = "";
259+
root.tooltip = "";
260+
root.child_count = 0;
261+
root.children_in_traversal_order = nullptr;
262+
root.custom_accessibility_actions_count = 0;
263+
bridge->AddFlutterSemanticsNodeUpdate(&root);
264+
265+
bridge->CommitUpdates();
266+
auto root_node = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
267+
auto lowest_platform_ancestor = root_node->GetLowestPlatformAncestor();
268+
EXPECT_EQ(root_node->GetNativeViewAccessible(), lowest_platform_ancestor);
269+
}
270+
271+
TEST(FlutterPlatformNodeDelegateTest, canGetFromNodeID) {
272+
std::shared_ptr<TestAccessibilityBridge> bridge =
273+
std::make_shared<TestAccessibilityBridge>();
274+
FlutterSemanticsNode root;
275+
root.id = 0;
276+
root.label = "root";
277+
root.hint = "";
278+
root.value = "";
279+
root.increased_value = "";
280+
root.decreased_value = "";
281+
root.tooltip = "";
282+
root.child_count = 1;
283+
int32_t children[] = {1};
284+
root.children_in_traversal_order = children;
285+
root.custom_accessibility_actions_count = 0;
286+
bridge->AddFlutterSemanticsNodeUpdate(&root);
287+
288+
FlutterSemanticsNode child1;
289+
child1.id = 1;
290+
child1.label = "child 1";
291+
child1.hint = "";
292+
child1.value = "";
293+
child1.increased_value = "";
294+
child1.decreased_value = "";
295+
child1.tooltip = "";
296+
child1.child_count = 0;
297+
child1.custom_accessibility_actions_count = 0;
298+
bridge->AddFlutterSemanticsNodeUpdate(&child1);
299+
300+
bridge->CommitUpdates();
301+
auto root_node = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
302+
auto child1_node = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
303+
auto node_by_id = root_node->GetFromNodeID(1);
304+
EXPECT_EQ(child1_node->GetPlatformNode(), node_by_id);
305+
}
306+
249307
} // namespace testing
250308
} // namespace flutter

0 commit comments

Comments
 (0)