Flutter iOS Embedder
accessibility_bridge.h
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 
5 #ifndef FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_ACCESSIBILITY_BRIDGE_H_
6 #define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_ACCESSIBILITY_BRIDGE_H_
7 
8 #import <UIKit/UIKit.h>
9 
10 #include <memory>
11 #include <unordered_map>
12 #include <unordered_set>
13 #include <vector>
14 
15 #include "flutter/fml/macros.h"
16 #include "flutter/fml/memory/weak_ptr.h"
17 #include "flutter/lib/ui/semantics/custom_accessibility_action.h"
18 #include "flutter/lib/ui/semantics/semantics_node.h"
25 
26 namespace flutter {
27 class PlatformViewIOS;
28 
29 /**
30  * An accessibility instance is bound to the `FlutterEngine` through
31  * `PlatformViewIOS` and can be rebound to one `FlutterViewController` at a
32  * time.
33  *
34  * It helps populate the UIView's accessibilityElements property from Flutter's
35  * semantics nodes.
36  */
38  public:
39  /** Delegate for handling iOS operations. */
40  class IosDelegate {
41  public:
42  virtual ~IosDelegate() = default;
43  /// Returns true when the FlutterViewController associated with the `view`
44  /// is presenting a modal view controller.
46  FlutterViewController* view_controller) = 0;
47  virtual void PostAccessibilityNotification(UIAccessibilityNotifications notification,
48  id argument) = 0;
49  };
50 
53  __weak FlutterPlatformViewsController* platform_views_controller,
54  std::unique_ptr<IosDelegate> ios_delegate = nullptr);
56 
57  // Rebinds the cached semantics tree from `previousView` to `viewController` without recreating
58  // the bridge. A non-nil new view controller must belong to the same engine because the platform
59  // views controller is fixed for the lifetime of the bridge.
61  void UpdateSemantics(flutter::SemanticsNodeUpdates nodes,
62  const flutter::CustomAccessibilityActionUpdates& actions);
63  // Returns true when a root semantics object is cached in the bridge.
64  bool HasSemantics() const;
65  void HandleEvent(NSDictionary<NSString*, id>* annotatedEvent);
66  void DispatchSemanticsAction(int32_t id, flutter::SemanticsAction action) override;
67  void DispatchSemanticsAction(int32_t id,
68  flutter::SemanticsAction action,
69  fml::MallocMapping args) override;
70  void AccessibilityObjectDidBecomeFocused(int32_t id) override;
71  void AccessibilityObjectDidLoseFocus(int32_t id) override;
72  NSString* GetDefaultLocale() override { return view_controller_.applicationLocale; }
73 
74  UIView<UITextInput>* textInputView() override;
75 
76  UIView* view() const override { return view_controller_.view; }
77 
78  UIView* ViewIfLoaded() const override { return view_controller_.viewIfLoaded; }
79 
81  return ViewIfLoaded() ?: accessibility_element_init_container_;
82  }
83 
84  bool isVoiceOverRunning() const override { return view_controller_.isVoiceOverRunning; }
85 
86  fml::WeakPtr<AccessibilityBridge> GetWeakPtr();
87 
89  return platform_views_controller_;
90  };
91 
92  void clearState();
93 
94  private:
95  bool AccessibilityElementsWereInstalledByBridge(NSArray* elements) const;
96  void ClearAccessibilityElementsIfOwnedByBridge(UIView* view);
97  void UpdateAccessibilityElementsForCurrentView();
98  void NotifySemanticsObjectsViewChanged();
99  SemanticsObject* GetOrCreateObject(int32_t id, flutter::SemanticsNodeUpdates& updates);
100  SemanticsObject* FindNextFocusableIfNecessary();
101  // Finds the first focusable SemanticsObject rooted at the parent. This includes the parent itself
102  // if it is a focusable SemanticsObject.
103  //
104  // If the parent is nil, this function use the root SemanticsObject as the parent.
105  SemanticsObject* FindFirstFocusable(SemanticsObject* parent);
106  void VisitObjectsRecursivelyAndRemove(SemanticsObject* object,
107  NSMutableArray<NSNumber*>* doomed_uids);
108 
109  __weak FlutterViewController* view_controller_;
110  UIView* accessibility_element_init_container_;
111  PlatformViewIOS* platform_view_;
112  __weak FlutterPlatformViewsController* platform_views_controller_;
113 
114  // If the this id is kSemanticObjectIdInvalid, it means either nothing has
115  // been focused or the focus is currently outside of the flutter application
116  // (i.e. the status bar or keyboard)
117  static constexpr int32_t kSemanticObjectIdInvalid = -1;
118  int32_t last_focused_semantics_object_id_ = kSemanticObjectIdInvalid;
119 
120  NSMutableDictionary<NSNumber*, SemanticsObject*>* objects_;
121  FlutterBasicMessageChannel* accessibility_channel_;
122  int32_t previous_route_id_ = 0;
123  std::unordered_map<int32_t, flutter::CustomAccessibilityAction> actions_;
124  std::vector<int32_t> previous_routes_;
125  std::unique_ptr<IosDelegate> ios_delegate_;
126  fml::WeakPtrFactory<AccessibilityBridge> weak_factory_; // Must be the last member.
127  FML_DISALLOW_COPY_AND_ASSIGN(AccessibilityBridge);
128 };
129 
130 } // namespace flutter
131 
132 #endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_ACCESSIBILITY_BRIDGE_H_
std::unique_ptr< flutter::PlatformViewIOS > platform_view
FlutterViewController * viewController
virtual void PostAccessibilityNotification(UIAccessibilityNotifications notification, id argument)=0
virtual bool IsFlutterViewControllerPresentingModalViewController(FlutterViewController *view_controller)=0
void UpdateSemantics(flutter::SemanticsNodeUpdates nodes, const flutter::CustomAccessibilityActionUpdates &actions)
void DispatchSemanticsAction(int32_t id, flutter::SemanticsAction action) override
void AccessibilityObjectDidLoseFocus(int32_t id) override
FlutterPlatformViewsController * GetPlatformViewsController() const override
AccessibilityBridge(FlutterViewController *view_controller, PlatformViewIOS *platform_view, __weak FlutterPlatformViewsController *platform_views_controller, std::unique_ptr< IosDelegate > ios_delegate=nullptr)
void SetViewController(FlutterViewController *viewController, FlutterView *previousView)
UIView * AccessibilityElementInitializationContainer() const override
void DispatchSemanticsAction(int32_t id, flutter::SemanticsAction action, fml::MallocMapping args) override
bool isVoiceOverRunning() const override
void HandleEvent(NSDictionary< NSString *, id > *annotatedEvent)
NSString * GetDefaultLocale() override
fml::WeakPtr< AccessibilityBridge > GetWeakPtr()
UIView< UITextInput > * textInputView() override
UIView * view() const override
UIView * ViewIfLoaded() const override
void AccessibilityObjectDidBecomeFocused(int32_t id) override
Interface that represents an accessibility bridge for iOS.