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

Commit 2f90bda

Browse files
authored
Merge AccessibilityBridge and AccessibilityBridgeDelegate (#36597)
* Impl * FlutterPlatformNodeDelegateMac * Format * Rename file * Windows: Compile * format * Fix tests * Fix doc * More doc * More comments * Format * Update names * Format * Compile * Change to unique * Revert as shared * Doc fixes * Make windows bridge weak * Fix win compile * Format * move weak
1 parent 0a2d451 commit 2f90bda

26 files changed

+436
-431
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,9 +2580,9 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterPlug
25802580
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterPluginRegistrarMacOS.h
25812581
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterViewController.h
25822582
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Info.plist
2583-
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacDelegate.h
2584-
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacDelegate.mm
2585-
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacDelegateTest.mm
2583+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMac.h
2584+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMac.mm
2585+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/AccessibilityBridgeMacTest.mm
25862586
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterAppDelegate.mm
25872587
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStore.h
25882588
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterBackingStore.mm
@@ -3079,9 +3079,9 @@ FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_view.h
30793079
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/flutter_linux.h
30803080
FILE: ../../../flutter/shell/platform/windows/accessibility_alert.cc
30813081
FILE: ../../../flutter/shell/platform/windows/accessibility_alert.h
3082-
FILE: ../../../flutter/shell/platform/windows/accessibility_bridge_delegate_windows.cc
3083-
FILE: ../../../flutter/shell/platform/windows/accessibility_bridge_delegate_windows.h
3084-
FILE: ../../../flutter/shell/platform/windows/accessibility_bridge_delegate_windows_unittests.cc
3082+
FILE: ../../../flutter/shell/platform/windows/accessibility_bridge_windows.cc
3083+
FILE: ../../../flutter/shell/platform/windows/accessibility_bridge_windows.h
3084+
FILE: ../../../flutter/shell/platform/windows/accessibility_bridge_windows_unittests.cc
30853085
FILE: ../../../flutter/shell/platform/windows/accessibility_root_node.cc
30863086
FILE: ../../../flutter/shell/platform/windows/accessibility_root_node.h
30873087
FILE: ../../../flutter/shell/platform/windows/angle_surface_manager.cc

shell/platform/common/accessibility_bridge.cc

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ constexpr int kHasScrollingAction =
1919
FlutterSemanticsAction::kFlutterSemanticsActionScrollDown;
2020

2121
// AccessibilityBridge
22-
AccessibilityBridge::AccessibilityBridge(
23-
std::unique_ptr<AccessibilityBridgeDelegate> delegate)
24-
: delegate_(std::move(delegate)) {
22+
AccessibilityBridge::AccessibilityBridge() {
2523
event_generator_.SetTree(&tree_);
2624
tree_.AddObserver(static_cast<ui::AXTreeObserver*>(this));
2725
}
@@ -107,7 +105,7 @@ void AccessibilityBridge::CommitUpdates() {
107105
continue;
108106
}
109107

110-
delegate_->OnAccessibilityEvent(targeted_event);
108+
OnAccessibilityEvent(targeted_event);
111109
}
112110
event_generator_.ClearEvents();
113111
}
@@ -128,20 +126,16 @@ const ui::AXTreeData& AccessibilityBridge::GetAXTreeData() const {
128126
}
129127

130128
const std::vector<ui::AXEventGenerator::TargetedEvent>
131-
AccessibilityBridge::GetPendingEvents() {
129+
AccessibilityBridge::GetPendingEvents() const {
132130
std::vector<ui::AXEventGenerator::TargetedEvent> result(
133131
event_generator_.begin(), event_generator_.end());
134132
return result;
135133
}
136134

137-
void AccessibilityBridge::UpdateDelegate(
138-
std::unique_ptr<AccessibilityBridgeDelegate> delegate) {
139-
delegate_ = std::move(delegate);
140-
// Recreate FlutterPlatformNodeDelegates since they may contain stale state
141-
// from the previous AccessibilityBridgeDelegate.
135+
void AccessibilityBridge::RecreateNodeDelegates() {
142136
for (const auto& [node_id, old_platform_node_delegate] : id_wrapper_map_) {
143137
std::shared_ptr<FlutterPlatformNodeDelegate> platform_node_delegate =
144-
delegate_->CreateFlutterPlatformNodeDelegate();
138+
CreateFlutterPlatformNodeDelegate();
145139
platform_node_delegate->Init(
146140
std::static_pointer_cast<FlutterPlatformNodeDelegate::OwnerBridge>(
147141
shared_from_this()),
@@ -166,7 +160,7 @@ void AccessibilityBridge::OnRoleChanged(ui::AXTree* tree,
166160

167161
void AccessibilityBridge::OnNodeCreated(ui::AXTree* tree, ui::AXNode* node) {
168162
BASE_DCHECK(node);
169-
id_wrapper_map_[node->id()] = delegate_->CreateFlutterPlatformNodeDelegate();
163+
id_wrapper_map_[node->id()] = CreateFlutterPlatformNodeDelegate();
170164
id_wrapper_map_[node->id()]->Init(
171165
std::static_pointer_cast<FlutterPlatformNodeDelegate::OwnerBridge>(
172166
shared_from_this()),
@@ -629,7 +623,7 @@ void AccessibilityBridge::SetLastFocusedId(AccessibilityNodeId node_id) {
629623
auto last_focused_child =
630624
GetFlutterPlatformNodeDelegateFromID(last_focused_id_);
631625
if (!last_focused_child.expired()) {
632-
delegate_->DispatchAccessibilityAction(
626+
DispatchAccessibilityAction(
633627
last_focused_id_,
634628
FlutterSemanticsAction::
635629
kFlutterSemanticsActionDidLoseAccessibilityFocus,
@@ -659,11 +653,4 @@ gfx::RectF AccessibilityBridge::RelativeToGlobalBounds(const ui::AXNode* node,
659653
clip_bounds);
660654
}
661655

662-
void AccessibilityBridge::DispatchAccessibilityAction(
663-
AccessibilityNodeId target,
664-
FlutterSemanticsAction action,
665-
fml::MallocMapping data) {
666-
delegate_->DispatchAccessibilityAction(target, action, std::move(data));
667-
}
668-
669656
} // namespace flutter

shell/platform/common/accessibility_bridge.h

Lines changed: 37 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -31,85 +31,20 @@ namespace flutter {
3131
/// FlutterPlatformNodeDelegate to wrap each AXNode in order to provide
3232
/// an accessibility tree in the native format.
3333
///
34-
/// This class takes in a AccessibilityBridgeDelegate instance and is in charge
35-
/// of its lifecycle. The delegate are used to handle the accessibility events
36-
/// and actions.
34+
/// To use this class, one must subclass this class and provide their own
35+
/// implementation of FlutterPlatformNodeDelegate.
3736
///
38-
/// To use this class, you must provide your own implementation of
39-
/// FlutterPlatformNodeDelegate and AccessibilityBridgeDelegate.
37+
/// AccessibilityBridge must be created as a shared_ptr, since some methods
38+
/// acquires its weak_ptr.
4039
class AccessibilityBridge
4140
: public std::enable_shared_from_this<AccessibilityBridge>,
4241
public FlutterPlatformNodeDelegate::OwnerBridge,
4342
private ui::AXTreeObserver {
4443
public:
45-
//-----------------------------------------------------------------------------
46-
/// Delegate to handle requests from the accessibility bridge. The requests
47-
/// include sending accessibility event to native accessibility system,
48-
/// routing accessibility action to the Flutter framework, and creating
49-
/// platform specific FlutterPlatformNodeDelegate.
50-
///
51-
/// The accessibility events are generated when accessibility tree changes.
52-
/// These events must be sent to the native accessibility system through
53-
/// the native API for the system to pick up the changes
54-
/// (e.g. NSAccessibilityPostNotification in MacOS).
55-
///
56-
/// The accessibility actions are generated by the native accessibility system
57-
/// when users interacted with the assistive technologies. Those actions
58-
/// needed to be sent to the Flutter framework.
59-
///
60-
/// Each platform needs to implement the FlutterPlatformNodeDelegate and
61-
/// returns its platform specific instance of FlutterPlatformNodeDelegate
62-
/// in this delegate.
63-
class AccessibilityBridgeDelegate {
64-
public:
65-
virtual ~AccessibilityBridgeDelegate() = default;
66-
//---------------------------------------------------------------------------
67-
/// @brief Handle accessibility events generated due to accessibility
68-
/// tree changes. These events are generated in accessibility
69-
/// bridge and needed to be sent to native accessibility system.
70-
/// See ui::AXEventGenerator::Event for possible events.
71-
///
72-
/// @param[in] targeted_event The object that contains both the
73-
/// generated event and the event target.
74-
virtual void OnAccessibilityEvent(
75-
ui::AXEventGenerator::TargetedEvent targeted_event) = 0;
76-
77-
//---------------------------------------------------------------------------
78-
/// @brief Dispatch accessibility action back to the Flutter framework.
79-
/// These actions are generated in the native accessibility
80-
/// system when users interact with the assistive technologies.
81-
/// For example, a
82-
/// FlutterSemanticsAction::kFlutterSemanticsActionTap is
83-
/// fired when user click or touch the screen.
84-
///
85-
/// @param[in] target The semantics node id of the action
86-
/// target.
87-
/// @param[in] action The generated flutter semantics action.
88-
/// @param[in] data Additional data associated with the
89-
/// action.
90-
virtual void DispatchAccessibilityAction(AccessibilityNodeId target,
91-
FlutterSemanticsAction action,
92-
fml::MallocMapping data) = 0;
93-
94-
//---------------------------------------------------------------------------
95-
/// @brief Creates a platform specific FlutterPlatformNodeDelegate.
96-
/// Ownership passes to the caller. This method will be called
97-
/// by accessibility bridge whenever a new AXNode is created in
98-
/// AXTree. Each platform needs to implement this method in
99-
/// order to inject its subclass into the accessibility bridge.
100-
virtual std::shared_ptr<FlutterPlatformNodeDelegate>
101-
CreateFlutterPlatformNodeDelegate() = 0;
102-
};
103-
10444
//-----------------------------------------------------------------------------
10545
/// @brief Creates a new instance of a accessibility bridge.
106-
///
107-
/// @param[in] user_data A custom pointer to the data of your
108-
/// choice. This pointer can be retrieve later
109-
/// through GetUserData().
110-
explicit AccessibilityBridge(
111-
std::unique_ptr<AccessibilityBridgeDelegate> delegate);
112-
~AccessibilityBridge();
46+
AccessibilityBridge();
47+
virtual ~AccessibilityBridge();
11348

11449
//-----------------------------------------------------------------------------
11550
/// @brief The ID of the root node in the accessibility tree. In Flutter,
@@ -168,12 +103,39 @@ class AccessibilityBridge
168103
/// events in AccessibilityBridgeDelegate::OnAccessibilityEvent in
169104
/// case one may decide to handle an event differently based on
170105
/// all pending events.
171-
const std::vector<ui::AXEventGenerator::TargetedEvent> GetPendingEvents();
106+
const std::vector<ui::AXEventGenerator::TargetedEvent> GetPendingEvents()
107+
const;
108+
109+
protected:
110+
//---------------------------------------------------------------------------
111+
/// @brief Handle accessibility events generated due to accessibility
112+
/// tree changes. These events are needed to be sent to native
113+
/// accessibility system. See ui::AXEventGenerator::Event for
114+
/// possible events.
115+
///
116+
/// @param[in] targeted_event The object that contains both the
117+
/// generated event and the event target.
118+
virtual void OnAccessibilityEvent(
119+
ui::AXEventGenerator::TargetedEvent targeted_event) = 0;
120+
121+
//---------------------------------------------------------------------------
122+
/// @brief Creates a platform specific FlutterPlatformNodeDelegate.
123+
/// Ownership passes to the caller. This method will be called
124+
/// whenever a new AXNode is created in AXTree. Each platform
125+
/// needs to implement this method in order to inject its
126+
/// subclass into the accessibility bridge.
127+
virtual std::shared_ptr<FlutterPlatformNodeDelegate>
128+
CreateFlutterPlatformNodeDelegate() = 0;
172129

173130
//------------------------------------------------------------------------------
174-
/// @brief Update the AccessibilityBridgeDelegate stored in the
175-
/// accessibility bridge to a new one.
176-
void UpdateDelegate(std::unique_ptr<AccessibilityBridgeDelegate> delegate);
131+
/// @brief Recreate all FlutterPlatformNodeDelegates.
132+
///
133+
/// This can be useful for subclasses when updating some
134+
/// properties that are used by node delegates, such as views.
135+
/// Each node is recreated using
136+
/// CreateFlutterPlatformNodeDelegate, then initialized using
137+
/// AXNodes from their corresponding old one.
138+
void RecreateNodeDelegates();
177139

178140
private:
179141
// See FlutterSemanticsNode in embedder.h
@@ -220,7 +182,6 @@ class AccessibilityBridge
220182
std::unordered_map<int32_t, SemanticsCustomAction>
221183
pending_semantics_custom_action_updates_;
222184
AccessibilityNodeId last_focused_id_ = ui::AXNode::kInvalidAXID;
223-
std::unique_ptr<AccessibilityBridgeDelegate> delegate_;
224185

225186
void InitAXTree(const ui::AXTreeUpdate& initial_state);
226187

@@ -295,11 +256,6 @@ class AccessibilityBridge
295256
gfx::NativeViewAccessible GetNativeAccessibleFromId(
296257
AccessibilityNodeId id) override;
297258

298-
// |FlutterPlatformNodeDelegate::OwnerBridge|
299-
void DispatchAccessibilityAction(AccessibilityNodeId target,
300-
FlutterSemanticsAction action,
301-
fml::MallocMapping data) override;
302-
303259
// |FlutterPlatformNodeDelegate::OwnerBridge|
304260
gfx::RectF RelativeToGlobalBounds(const ui::AXNode* node,
305261
bool& offscreen,

0 commit comments

Comments
 (0)