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

Commit afc72ee

Browse files
authored
Wires up accessibility bridge in mac embedding (#23870)
* Wires up accessibility bridge in mac embedding * addressing comment * addressing comments
1 parent ce0ec55 commit afc72ee

17 files changed

+1032
-9
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,8 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterPlug
10981098
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterPluginRegistrarMacOS.h
10991099
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterViewController.h
11001100
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Info.plist
1101+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacDelegate.h
1102+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacDelegate.mm
11011103
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterAppDelegate.mm
11021104
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStore.h
11031105
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStore.mm
@@ -1132,6 +1134,9 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMouse
11321134
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.h
11331135
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.mm
11341136
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRendererTest.mm
1137+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformNodeDelegateMac.h
1138+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformNodeDelegateMac.mm
1139+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformNodeDelegateMacTest.mm
11351140
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.h
11361141
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderingBackend.h
11371142
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderingBackend.mm

shell/platform/common/accessibility_bridge.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ const ui::AXTreeData& AccessibilityBridge::GetAXTreeData() const {
104104
return tree_.data();
105105
}
106106

107+
const std::vector<ui::AXEventGenerator::TargetedEvent>
108+
AccessibilityBridge::GetPendingEvents() {
109+
std::vector<ui::AXEventGenerator::TargetedEvent> result(
110+
event_generator_.begin(), event_generator_.end());
111+
return result;
112+
}
113+
107114
void AccessibilityBridge::OnNodeWillBeDeleted(ui::AXTree* tree,
108115
ui::AXNode* node) {}
109116

shell/platform/common/accessibility_bridge.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ class AccessibilityBridge
8686
/// @param[in] action The generated flutter semantics action.
8787
/// @param[in] data Additional data associated with the
8888
/// action.
89-
virtual void DispatchAccessibilityAction(AccessibilityNodeId target,
90-
FlutterSemanticsAction action,
91-
std::vector<uint8_t> data) = 0;
89+
virtual void DispatchAccessibilityAction(
90+
AccessibilityNodeId target,
91+
FlutterSemanticsAction action,
92+
const std::vector<uint8_t>& data) = 0;
9293

9394
//---------------------------------------------------------------------------
9495
/// @brief Creates a platform specific FlutterPlatformNodeDelegate.
@@ -154,6 +155,14 @@ class AccessibilityBridge
154155
/// has the keyboard focus or the text selection range.
155156
const ui::AXTreeData& GetAXTreeData() const;
156157

158+
//------------------------------------------------------------------------------
159+
/// @brief Gets all pending accessibility events generated during
160+
/// semantics updates. This is useful when deciding how to handle
161+
/// events in AccessibilityBridgeDelegate::OnAccessibilityEvent in
162+
/// case one may decide to handle an event differently based on
163+
/// all pending events.
164+
const std::vector<ui::AXEventGenerator::TargetedEvent> GetPendingEvents();
165+
157166
private:
158167
// See FlutterSemanticsNode in embedder.h
159168
typedef struct {

shell/platform/common/flutter_platform_node_delegate.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class FlutterPlatformNodeDelegate : public ui::AXPlatformNodeDelegateBase {
3737
/// delegate.
3838
class OwnerBridge {
3939
public:
40-
~OwnerBridge() = default;
40+
virtual ~OwnerBridge() = default;
4141

4242
protected:
4343
friend class FlutterPlatformNodeDelegate;
@@ -96,7 +96,7 @@ class FlutterPlatformNodeDelegate : public ui::AXPlatformNodeDelegateBase {
9696
FlutterPlatformNodeDelegate();
9797

9898
// |ui::AXPlatformNodeDelegateBase|
99-
~FlutterPlatformNodeDelegate() override;
99+
virtual ~FlutterPlatformNodeDelegate() override;
100100

101101
// |ui::AXPlatformNodeDelegateBase|
102102
const ui::AXNodeData& GetData() const override;

shell/platform/common/test_accessibility_bridge.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void TestAccessibilityBridgeDelegate::OnAccessibilityEvent(
1919
void TestAccessibilityBridgeDelegate::DispatchAccessibilityAction(
2020
AccessibilityNodeId target,
2121
FlutterSemanticsAction action,
22-
std::vector<uint8_t> data) {
22+
const std::vector<uint8_t>& data) {
2323
performed_actions.push_back(action);
2424
}
2525

shell/platform/common/test_accessibility_bridge.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class TestAccessibilityBridgeDelegate
1818
ui::AXEventGenerator::TargetedEvent targeted_event) override;
1919
void DispatchAccessibilityAction(AccessibilityNodeId target,
2020
FlutterSemanticsAction action,
21-
std::vector<uint8_t> data) override;
21+
const std::vector<uint8_t>& data) override;
2222
std::unique_ptr<FlutterPlatformNodeDelegate>
2323
CreateFlutterPlatformNodeDelegate();
2424

shell/platform/darwin/macos/BUILD.gn

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ source_set("flutter_framework_source") {
5252
visibility = [ ":*" ]
5353

5454
sources = [
55+
"framework/Source/AccessibilityBridgeMacDelegate.h",
56+
"framework/Source/AccessibilityBridgeMacDelegate.mm",
5557
"framework/Source/FlutterAppDelegate.mm",
5658
"framework/Source/FlutterBackingStore.h",
5759
"framework/Source/FlutterBackingStore.mm",
@@ -81,6 +83,8 @@ source_set("flutter_framework_source") {
8183
"framework/Source/FlutterMouseCursorPlugin.mm",
8284
"framework/Source/FlutterOpenGLRenderer.h",
8385
"framework/Source/FlutterOpenGLRenderer.mm",
86+
"framework/Source/FlutterPlatformNodeDelegateMac.h",
87+
"framework/Source/FlutterPlatformNodeDelegateMac.mm",
8488
"framework/Source/FlutterRenderer.h",
8589
"framework/Source/FlutterRenderingBackend.h",
8690
"framework/Source/FlutterRenderingBackend.mm",
@@ -108,6 +112,7 @@ source_set("flutter_framework_source") {
108112
":macos_gpu_configuration",
109113
"//flutter/flow:flow",
110114
"//flutter/fml",
115+
"//flutter/shell/platform/common:common_cpp_accessibility",
111116
"//flutter/shell/platform/common:common_cpp_input",
112117
"//flutter/shell/platform/common:common_cpp_switches",
113118
"//flutter/shell/platform/darwin/common:framework_shared",
@@ -159,6 +164,7 @@ executable("flutter_desktop_darwin_unittests") {
159164
"framework/Source/FlutterMetalRendererTest.mm",
160165
"framework/Source/FlutterMetalSurfaceManagerTest.mm",
161166
"framework/Source/FlutterOpenGLRendererTest.mm",
167+
"framework/Source/FlutterPlatformNodeDelegateMacTest.mm",
162168
"framework/Source/FlutterViewControllerTest.mm",
163169
"framework/Source/FlutterViewControllerTestUtils.h",
164170
"framework/Source/FlutterViewControllerTestUtils.mm",
@@ -171,6 +177,7 @@ executable("flutter_desktop_darwin_unittests") {
171177
deps = [
172178
":flutter_desktop_darwin_fixtures",
173179
":flutter_framework_source",
180+
"//flutter/shell/platform/common:common_cpp_accessibility",
174181
"//flutter/shell/platform/darwin/common:framework_shared",
175182
"//flutter/shell/platform/darwin/graphics",
176183
"//flutter/shell/platform/embedder:embedder_as_internal_library",
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
5+
#ifndef FLUTTER_SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_ACCESSIBILITYBRIDGEMACDELEGATE_H_
6+
#define FLUTTER_SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_ACCESSIBILITYBRIDGEMACDELEGATE_H_
7+
8+
#import <Cocoa/Cocoa.h>
9+
10+
#include "flutter/shell/platform/common/accessibility_bridge.h"
11+
12+
@class FlutterEngine;
13+
14+
namespace flutter {
15+
16+
//------------------------------------------------------------------------------
17+
/// The macOS implementation of AccessibilityBridge::AccessibilityBridgeDelegate.
18+
/// This delegate is used to create AccessibilityBridge in the macOS embedding.
19+
class AccessibilityBridgeMacDelegate : public AccessibilityBridge::AccessibilityBridgeDelegate {
20+
public:
21+
//---------------------------------------------------------------------------
22+
/// @brief Creates an AccessibilityBridgeMacDelegate.
23+
/// @param[in] flutterEngine The weak reference to the FlutterEngine.
24+
explicit AccessibilityBridgeMacDelegate(__weak FlutterEngine* flutter_engine);
25+
virtual ~AccessibilityBridgeMacDelegate() = default;
26+
27+
// |AccessibilityBridge::AccessibilityBridgeDelegate|
28+
void OnAccessibilityEvent(ui::AXEventGenerator::TargetedEvent targeted_event) override;
29+
30+
// |AccessibilityBridge::AccessibilityBridgeDelegate|
31+
void DispatchAccessibilityAction(AccessibilityNodeId target,
32+
FlutterSemanticsAction action,
33+
const std::vector<uint8_t>& data) override;
34+
35+
// |AccessibilityBridge::AccessibilityBridgeDelegate|
36+
std::unique_ptr<FlutterPlatformNodeDelegate> CreateFlutterPlatformNodeDelegate() override;
37+
38+
private:
39+
/// A wrapper structure to wraps macOS native accessibility events.
40+
struct NSAccessibilityEvent {
41+
NSAccessibilityNotificationName name;
42+
gfx::NativeViewAccessible target;
43+
NSDictionary* user_info;
44+
};
45+
46+
//---------------------------------------------------------------------------
47+
/// @brief Posts the given event against the given node to the macOS
48+
/// accessibility notification system.
49+
/// @param[in] native_node The event target, must not be nil.
50+
/// @param[in] mac_notification The event name, must not be nil.
51+
void DispatchMacOSNotification(gfx::NativeViewAccessible native_node,
52+
NSAccessibilityNotificationName mac_notification);
53+
54+
//---------------------------------------------------------------------------
55+
/// @brief Posts the given event against the given node with the
56+
/// additional attributes to the macOS accessibility notification
57+
/// system.
58+
/// @param[in] native_node The event target, must not be nil.
59+
/// @param[in] mac_notification The event name, must not be nil.
60+
/// @param[in] user_info The additional attributes, must not be nil.
61+
void DispatchMacOSNotificationWithUserInfo(gfx::NativeViewAccessible native_node,
62+
NSAccessibilityNotificationName mac_notification,
63+
NSDictionary* user_info);
64+
65+
//---------------------------------------------------------------------------
66+
/// @brief Whether the given event is in current pending events.
67+
/// @param[in] event_type The event you would like to look up.
68+
bool HasPendingEvent(ui::AXEventGenerator::Event event) const;
69+
70+
//---------------------------------------------------------------------------
71+
/// @brief Converts the give ui::AXEventGenerator::Event into
72+
/// macOS native accessibility event[s]
73+
/// @param[in] event_type The original event type.
74+
/// @param[in] ax_node The original event target.
75+
std::vector<NSAccessibilityEvent> MacOSEventsFromAXEvent(ui::AXEventGenerator::Event event_type,
76+
const ui::AXNode& ax_node) const;
77+
78+
__weak FlutterEngine* flutter_engine_;
79+
};
80+
81+
} // namespace flutter
82+
83+
#endif // FLUTTER_SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_ACCESSIBILITYBRIDGEMACDELEGATE_H_

0 commit comments

Comments
 (0)