Flutter macOS Embedder
flutter_platform_node_delegate.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 #include <utility>
8 
10 #include "flutter/third_party/accessibility/ax/ax_action_data.h"
11 #include "flutter/third_party/accessibility/ax/ax_tree_manager_map.h"
12 #include "flutter/third_party/accessibility/gfx/geometry/rect_conversions.h"
13 
14 namespace flutter {
15 
17 
19 
20 void FlutterPlatformNodeDelegate::Init(std::weak_ptr<OwnerBridge> bridge,
21  ui::AXNode* node) {
22  bridge_ = std::move(bridge);
23  ax_node_ = node;
24 }
25 
27  return ax_node_;
28 }
29 
31  const ui::AXActionData& data) {
32  AccessibilityNodeId target = ax_node_->id();
33  auto bridge_ptr = bridge_.lock();
34  BASE_DCHECK(bridge_ptr);
35  switch (data.action) {
36  case ax::mojom::Action::kDoDefault:
37  bridge_ptr->DispatchAccessibilityAction(
38  target, FlutterSemanticsAction::kFlutterSemanticsActionTap, {});
39  return true;
40  case ax::mojom::Action::kFocus:
41  bridge_ptr->SetLastFocusedId(target);
42  bridge_ptr->DispatchAccessibilityAction(
43  target,
44  FlutterSemanticsAction::
45  kFlutterSemanticsActionDidGainAccessibilityFocus,
46  {});
47  return true;
48  case ax::mojom::Action::kScrollToMakeVisible:
49  bridge_ptr->DispatchAccessibilityAction(
50  target, FlutterSemanticsAction::kFlutterSemanticsActionShowOnScreen,
51  {});
52  return true;
53  // TODO(chunhtai): support more actions.
54  default:
55  return false;
56  }
57  return false;
58 }
59 
60 const ui::AXNodeData& FlutterPlatformNodeDelegate::GetData() const {
61  return ax_node_->data();
62 }
63 
64 gfx::NativeViewAccessible FlutterPlatformNodeDelegate::GetParent() {
65  if (!ax_node_->parent()) {
66  return nullptr;
67  }
68  auto bridge_ptr = bridge_.lock();
69  BASE_DCHECK(bridge_ptr);
70  return bridge_ptr->GetNativeAccessibleFromId(ax_node_->parent()->id());
71 }
72 
73 gfx::NativeViewAccessible FlutterPlatformNodeDelegate::GetFocus() {
74  auto bridge_ptr = bridge_.lock();
75  BASE_DCHECK(bridge_ptr);
76  AccessibilityNodeId last_focused = bridge_ptr->GetLastFocusedId();
77  if (last_focused == ui::AXNode::kInvalidAXID) {
78  return nullptr;
79  }
80  return bridge_ptr->GetNativeAccessibleFromId(last_focused);
81 }
82 
84  return static_cast<int>(ax_node_->GetUnignoredChildCount());
85 }
86 
87 gfx::NativeViewAccessible FlutterPlatformNodeDelegate::ChildAtIndex(int index) {
88  auto bridge_ptr = bridge_.lock();
89  BASE_DCHECK(bridge_ptr);
90  AccessibilityNodeId child = ax_node_->GetUnignoredChildAtIndex(index)->id();
91  return bridge_ptr->GetNativeAccessibleFromId(child);
92 }
93 
95  const ui::AXCoordinateSystem coordinate_system,
96  const ui::AXClippingBehavior clipping_behavior,
97  ui::AXOffscreenResult* offscreen_result) const {
98  auto bridge_ptr = bridge_.lock();
99  BASE_DCHECK(bridge_ptr);
100  // TODO(chunhtai): We need to apply screen dpr in here.
101  // https://github.com/flutter/flutter/issues/74283
102  const bool clip_bounds =
103  clipping_behavior == ui::AXClippingBehavior::kClipped;
104  bool offscreen = false;
105  gfx::RectF bounds =
106  bridge_ptr->RelativeToGlobalBounds(ax_node_, offscreen, clip_bounds);
107  if (offscreen_result != nullptr) {
108  *offscreen_result = offscreen ? ui::AXOffscreenResult::kOffscreen
109  : ui::AXOffscreenResult::kOnscreen;
110  }
111  return gfx::ToEnclosingRect(bounds);
112 }
113 
114 std::weak_ptr<FlutterPlatformNodeDelegate::OwnerBridge>
116  return bridge_;
117 }
118 
119 ui::AXPlatformNode* FlutterPlatformNodeDelegate::GetPlatformNode() const {
120  return nullptr;
121 }
122 
123 gfx::NativeViewAccessible
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
137  return ui::AXNodePosition::CreatePosition(*ax_node_, offset);
138 }
139 
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 
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 
161  const {
162  return ax_node_->GetUnignoredSelection();
163 }
164 
165 } // namespace flutter
flutter::FlutterPlatformNodeDelegate::GetFromTreeIDAndNodeID
virtual ui::AXPlatformNode * GetFromTreeIDAndNodeID(const ui::AXTreeID &tree_id, int32_t node_id) override
Definition: flutter_platform_node_delegate.cc:150
flutter::FlutterPlatformNodeDelegate::ChildAtIndex
gfx::NativeViewAccessible ChildAtIndex(int index) override
Definition: flutter_platform_node_delegate.cc:87
flutter::FlutterPlatformNodeDelegate::Init
virtual void Init(std::weak_ptr< OwnerBridge > bridge, ui::AXNode *node)
Called only once, immediately after construction. The constructor doesn't take any arguments because ...
Definition: flutter_platform_node_delegate.cc:20
flutter::AccessibilityBridge::GetPlatformNodeFromTree
ui::AXPlatformNode * GetPlatformNodeFromTree(const ui::AXNode::AXID node_id) const override
Definition: accessibility_bridge.cc:717
flutter::FlutterPlatformNodeDelegate::GetOwnerBridge
std::weak_ptr< OwnerBridge > GetOwnerBridge() const
Gets the owner of this platform node delegate. This is useful when you want to get the information ab...
Definition: flutter_platform_node_delegate.cc:115
flutter::FlutterPlatformNodeDelegate::GetFromNodeID
virtual ui::AXPlatformNode * GetFromNodeID(int32_t id) override
Definition: flutter_platform_node_delegate.cc:140
flutter::FlutterPlatformNodeDelegate::CreateTextPositionAt
ui::AXNodePosition::AXPositionInstance CreateTextPositionAt(int offset) const override
Definition: flutter_platform_node_delegate.cc:136
flutter::FlutterPlatformNodeDelegate::GetUnignoredSelection
virtual const ui::AXTree::Selection GetUnignoredSelection() const override
Definition: flutter_platform_node_delegate.cc:160
flutter::FlutterPlatformNodeDelegate::GetPlatformNode
virtual ui::AXPlatformNode * GetPlatformNode() const
Definition: flutter_platform_node_delegate.cc:119
flutter::FlutterPlatformNodeDelegate::GetData
const ui::AXNodeData & GetData() const override
Definition: flutter_platform_node_delegate.cc:60
flutter::FlutterPlatformNodeDelegate::GetBoundsRect
gfx::Rect GetBoundsRect(const ui::AXCoordinateSystem coordinate_system, const ui::AXClippingBehavior clipping_behavior, ui::AXOffscreenResult *offscreen_result) const override
Definition: flutter_platform_node_delegate.cc:94
flutter::AccessibilityBridge
Definition: accessibility_bridge.h:40
flutter::FlutterPlatformNodeDelegate::~FlutterPlatformNodeDelegate
virtual ~FlutterPlatformNodeDelegate() override
flutter::AccessibilityNodeId
ui::AXNode::AXID AccessibilityNodeId
Definition: flutter_platform_node_delegate.h:15
flutter
Definition: AccessibilityBridgeMac.h:16
flutter_platform_node_delegate.h
flutter::FlutterPlatformNodeDelegate::GetAXNode
ui::AXNode * GetAXNode() const
Gets the underlying ax node for this platform node delegate.
Definition: flutter_platform_node_delegate.cc:26
flutter::FlutterPlatformNodeDelegate::GetChildCount
int GetChildCount() const override
Definition: flutter_platform_node_delegate.cc:83
flutter::FlutterPlatformNodeDelegate::GetParent
gfx::NativeViewAccessible GetParent() override
Definition: flutter_platform_node_delegate.cc:64
flutter::FlutterPlatformNodeDelegate::GetFocus
gfx::NativeViewAccessible GetFocus() override
Definition: flutter_platform_node_delegate.cc:73
flutter::FlutterPlatformNodeDelegate::AccessibilityPerformAction
bool AccessibilityPerformAction(const ui::AXActionData &data) override
Definition: flutter_platform_node_delegate.cc:30
accessibility_bridge.h
flutter::FlutterPlatformNodeDelegate::FlutterPlatformNodeDelegate
FlutterPlatformNodeDelegate()
flutter::FlutterPlatformNodeDelegate::GetLowestPlatformAncestor
gfx::NativeViewAccessible GetLowestPlatformAncestor() const override
Definition: flutter_platform_node_delegate.cc:124