Flutter macOS Embedder
FlutterPlatformNodeDelegateMac.mm
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 
11 
13 #include "flutter/third_party/accessibility/ax/ax_action_data.h"
14 #include "flutter/third_party/accessibility/ax/ax_node_position.h"
15 #include "flutter/third_party/accessibility/ax/platform/ax_platform_node.h"
16 #include "flutter/third_party/accessibility/ax/platform/ax_platform_node_base.h"
17 #include "flutter/third_party/accessibility/base/string_utils.h"
18 #include "flutter/third_party/accessibility/gfx/geometry/rect_conversions.h"
19 #include "flutter/third_party/accessibility/gfx/mac/coordinate_conversion.h"
20 
21 namespace flutter { // namespace
22 
24  std::weak_ptr<AccessibilityBridge> bridge,
25  __weak FlutterViewController* view_controller)
26  : bridge_(std::move(bridge)), view_controller_(view_controller) {}
27 
28 void FlutterPlatformNodeDelegateMac::Init(std::weak_ptr<OwnerBridge> bridge, ui::AXNode* node) {
30  if (GetData().IsTextField() && !GetData().IsReadOnlyOrDisabled()) {
31  ax_platform_node_ = new FlutterTextPlatformNode(this, view_controller_);
32  } else {
33  ax_platform_node_ = ui::AXPlatformNode::Create(this);
34  }
35  NSCAssert(ax_platform_node_, @"Failed to create platform node.");
36 }
37 
38 void FlutterPlatformNodeDelegateMac::NodeDataChanged(const ui::AXNodeData& old_node_data,
39  const ui::AXNodeData& new_node_data) {
40  bool old_is_editable_textfield =
41  old_node_data.IsTextField() && !old_node_data.IsReadOnlyOrDisabled();
42  bool new_is_editable_textfield =
43  new_node_data.IsTextField() && !new_node_data.IsReadOnlyOrDisabled();
44  if (old_is_editable_textfield && !new_is_editable_textfield) {
45  ax_platform_node_->Destroy();
46  ax_platform_node_ = ui::AXPlatformNode::Create(this);
47  } else if (!old_is_editable_textfield && new_is_editable_textfield) {
48  ax_platform_node_->Destroy();
49  ax_platform_node_ = new FlutterTextPlatformNode(this, view_controller_);
50  }
51 }
52 
53 const ui::AXNodeData& FlutterPlatformNodeDelegateMac::GetData() const {
54  const ui::AXNodeData& data = FlutterPlatformNodeDelegate::GetData();
55  if (data.IsTextField() && data.IsReadOnlyOrDisabled()) {
56  cached_data_ = data;
57  cached_data_.role = ax::mojom::Role::kStaticText;
58  return cached_data_;
59  }
60  return data;
61 }
62 
64  // Destroy() also calls delete on itself.
65  ax_platform_node_->Destroy();
66 }
67 
69  NSCAssert(ax_platform_node_, @"Platform node does not exist.");
70  return ax_platform_node_->GetNativeViewAccessible();
71 }
72 
73 gfx::NativeViewAccessible FlutterPlatformNodeDelegateMac::GetParent() {
74  gfx::NativeViewAccessible parent = FlutterPlatformNodeDelegate::GetParent();
75  if (!parent) {
76  NSCAssert(view_controller_.viewLoaded, @"Flutter view must be loaded");
77  return view_controller_.flutterView;
78  }
79  return parent;
80 }
81 
83  const ui::AXCoordinateSystem coordinate_system,
84  const ui::AXClippingBehavior clipping_behavior,
85  ui::AXOffscreenResult* offscreen_result) const {
86  gfx::Rect local_bounds = FlutterPlatformNodeDelegate::GetBoundsRect(
87  coordinate_system, clipping_behavior, offscreen_result);
88  gfx::RectF local_bounds_f(local_bounds);
89  gfx::RectF screen_bounds = ConvertBoundsFromLocalToScreen(local_bounds_f);
90  return gfx::ToEnclosingRect(ConvertBoundsFromScreenToGlobal(screen_bounds));
91 }
92 
93 gfx::NativeViewAccessible FlutterPlatformNodeDelegateMac::GetNSWindow() {
94  FlutterAppDelegate* appDelegate = (FlutterAppDelegate*)[NSApp delegate];
95  return appDelegate.mainFlutterWindow;
96 }
97 
99  if (GetAXNode()->IsIgnored()) {
100  return "";
101  }
102 
103  std::string text = GetData().GetStringAttribute(ax::mojom::StringAttribute::kName);
104  if (!text.empty()) {
105  return text;
106  };
107  auto bridge_ptr = bridge_.lock();
108  NSCAssert(bridge_ptr, @"Accessibility bridge in flutter engine must not be null.");
109  for (int32_t child : GetData().child_ids) {
110  auto delegate_child = bridge_ptr->GetFlutterPlatformNodeDelegateFromID(child).lock();
111  if (!delegate_child) {
112  continue;
113  }
114  text += std::static_pointer_cast<FlutterPlatformNodeDelegateMac>(delegate_child)
115  ->GetLiveRegionText();
116  }
117  return text;
118 }
119 
120 gfx::RectF FlutterPlatformNodeDelegateMac::ConvertBoundsFromLocalToScreen(
121  const gfx::RectF& local_bounds) const {
122  // Converts to NSRect to use NSView rect conversion.
123  NSRect ns_local_bounds =
124  NSMakeRect(local_bounds.x(), local_bounds.y(), local_bounds.width(), local_bounds.height());
125  // The macOS XY coordinates start at bottom-left and increase toward top-right,
126  // which is different from the Flutter's XY coordinates that start at top-left
127  // increasing to bottom-right. Therefore, this method needs to flip the y coordinate when
128  // it converts the bounds from flutter coordinates to macOS coordinates.
129  ns_local_bounds.origin.y = -ns_local_bounds.origin.y - ns_local_bounds.size.height;
130 
131  NSCAssert(view_controller_.viewLoaded, @"Flutter view must be loaded.");
132  NSRect ns_view_bounds = [view_controller_.flutterView convertRectFromBacking:ns_local_bounds];
133  NSRect ns_window_bounds = [view_controller_.flutterView convertRect:ns_view_bounds toView:nil];
134  NSRect ns_screen_bounds =
135  [[view_controller_.flutterView window] convertRectToScreen:ns_window_bounds];
136  gfx::RectF screen_bounds(ns_screen_bounds.origin.x, ns_screen_bounds.origin.y,
137  ns_screen_bounds.size.width, ns_screen_bounds.size.height);
138  return screen_bounds;
139 }
140 
141 gfx::RectF FlutterPlatformNodeDelegateMac::ConvertBoundsFromScreenToGlobal(
142  const gfx::RectF& screen_bounds) const {
143  // The VoiceOver seems to only accept bounds that are relative to primary screen.
144  // Thus, this method uses [[NSScreen screens] firstObject] instead of [NSScreen mainScreen].
145  NSScreen* screen = [[NSScreen screens] firstObject];
146  NSRect primary_screen_bounds = [screen frame];
147  // The screen is flipped against y axis.
148  float flipped_y = primary_screen_bounds.size.height - screen_bounds.y() - screen_bounds.height();
149  return {screen_bounds.x(), flipped_y, screen_bounds.width(), screen_bounds.height()};
150 }
151 
152 } // namespace flutter
gfx::NativeViewAccessible GetParent() override
gfx::Rect GetBoundsRect(const ui::AXCoordinateSystem coordinate_system, const ui::AXClippingBehavior clipping_behavior, ui::AXOffscreenResult *offscreen_result) const override
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 ...
ui::AXNode * GetAXNode() const
Gets the underlying ax node for this platform node delegate.
const ui::AXNodeData & GetData() const override
const ui::AXNodeData & GetData() const override
gfx::NativeViewAccessible GetNativeViewAccessible() override
void NodeDataChanged(const ui::AXNodeData &old_node_data, const ui::AXNodeData &new_node_data) override
gfx::NativeViewAccessible GetParent() override
gfx::NativeViewAccessible GetNSWindow() override
FlutterPlatformNodeDelegateMac(std::weak_ptr< AccessibilityBridge > bridge, __weak FlutterViewController *view_controller)
gfx::Rect GetBoundsRect(const ui::AXCoordinateSystem coordinate_system, const ui::AXClippingBehavior clipping_behavior, ui::AXOffscreenResult *offscreen_result) const override
void Init(std::weak_ptr< OwnerBridge > bridge, ui::AXNode *node) override
Called only once, immediately after construction. The constructor doesn't take any arguments because ...
std::string GetLiveRegionText() const
Gets the live region text of this node in UTF-8 format. This is useful to determine the changes in be...
The ax platform node for a text field.
IBOutlet NSWindow * mainFlutterWindow