accessibility_bridge.h 4.3 KB
Newer Older
1 2 3 4
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_ACCESSIBILITY_BRIDGE_H_
#define SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_ACCESSIBILITY_BRIDGE_H_
7 8

#include <memory>
9
#include <unordered_map>
10 11
#include <unordered_set>
#include <vector>
12

Y
Yegor 已提交
13 14
#import <UIKit/UIKit.h>

15
#include "flutter/fml/memory/weak_ptr.h"
16
#include "flutter/fml/platform/darwin/scoped_nsobject.h"
17
#include "flutter/lib/ui/semantics/semantics_node.h"
18
#include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterChannels.h"
Y
Yegor 已提交
19
#include "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h"
20
#include "flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h"
21
#include "lib/fxl/macros.h"
J
Jason Simmons 已提交
22
#include "third_party/skia/include/core/SkMatrix44.h"
23 24
#include "third_party/skia/include/core/SkRect.h"

25 26
namespace shell {
class AccessibilityBridge;
27
}  // namespace shell
28

Y
Yegor 已提交
29 30 31
/**
 * A node in the iOS semantics tree.
 */
32
@interface SemanticsObject : NSObject
33 34 35 36

/**
 * The globally unique identifier for this node.
 */
37
@property(nonatomic, readonly) int32_t uid;
38 39 40 41 42

/**
 * The parent of this node in the node tree. Will be nil for the root node and
 * during transient state changes.
 */
43
@property(nonatomic, assign) SemanticsObject* parent;
44

Y
Yegor 已提交
45 46
/**
 * The accessibility bridge that this semantics object is attached to. This
47 48
 * object may use the bridge to access contextual application information. A weak pointer is used
 * because the platform view owns the accessibility bridge.
Y
Yegor 已提交
49
 */
50
@property(nonatomic, readonly) fml::WeakPtr<shell::AccessibilityBridge> bridge;
Y
Yegor 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

/**
 * The semantics node used to produce this semantics object.
 */
@property(nonatomic, readonly) blink::SemanticsNode node;

/**
 * Updates this semantics object using data from the `node` argument.
 */
- (void)setSemanticsNode:(const blink::SemanticsNode*)node NS_REQUIRES_SUPER;

/**
 * Whether this semantics object has child semantics objects.
 */
@property(nonatomic, readonly) BOOL hasChildren;

/**
 * Direct children of this semantics object. Each child's `parent` property must
 * be equal to this object.
 */
71
@property(nonatomic, strong) NSMutableArray<SemanticsObject*>* children;
Y
Yegor 已提交
72 73 74 75 76

- (BOOL)nodeWillCauseLayoutChange:(const blink::SemanticsNode*)node;

#pragma mark - Designated initializers

77
- (instancetype)init __attribute__((unavailable("Use initWithBridge instead")));
78
- (instancetype)initWithBridge:(fml::WeakPtr<shell::AccessibilityBridge>)bridge
79
                           uid:(int32_t)uid NS_DESIGNATED_INITIALIZER;
80 81 82

@end

Y
Yegor 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96
/**
 * The default implementation of `SemanticsObject` for most accessibility elements
 * in the iOS accessibility tree.
 *
 * Use this implementation for nodes that do not need to be expressed via UIKit-specific
 * protocols (it only implements NSObject).
 *
 * See also:
 *  * TextInputSemanticsObject, which implements `UITextInput` protocol to expose
 *    editable text widgets to a11y.
 */
@interface FlutterSemanticsObject : SemanticsObject
@end

97
namespace shell {
98
class PlatformViewIOS;
99

100
class AccessibilityBridge final {
101
 public:
102 103
  AccessibilityBridge(UIView* view, PlatformViewIOS* platform_view);
  ~AccessibilityBridge();
104

Y
Yegor 已提交
105
  void UpdateSemantics(blink::SemanticsNodeUpdates nodes);
106
  void DispatchSemanticsAction(int32_t id, blink::SemanticsAction action);
Y
Yegor 已提交
107
  UIView<UITextInput>* textInputView();
108

109
  UIView* view() const { return view_; }
110

111 112
  fml::WeakPtr<AccessibilityBridge> GetWeakPtr();

113
 private:
Y
Yegor 已提交
114
  SemanticsObject* GetOrCreateObject(int32_t id, blink::SemanticsNodeUpdates& updates);
115 116
  void VisitObjectsRecursivelyAndRemove(SemanticsObject* object,
                                        NSMutableArray<NSNumber*>* doomed_uids);
117
  void ReleaseObjects(std::unordered_map<int, SemanticsObject*>& objects);
118
  void HandleEvent(NSDictionary<NSString*, id>* annotatedEvent);
119

120
  UIView* view_;
121
  PlatformViewIOS* platform_view_;
122
  fml::scoped_nsobject<NSMutableDictionary<NSNumber*, SemanticsObject*>> objects_;
123
  fml::scoped_nsprotocol<FlutterBasicMessageChannel*> accessibility_channel_;
124
  fml::WeakPtrFactory<AccessibilityBridge> weak_factory_;
125 126
  int32_t previous_route_id_;
  std::vector<int32_t> previous_routes_;
127

128
  FXL_DISALLOW_COPY_AND_ASSIGN(AccessibilityBridge);
129 130 131 132
};

}  // namespace shell

133
#endif  // SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_ACCESSIBILITY_BRIDGE_H_