Flutter iOS Embedder
SemanticsObject.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 
9 
11 
12 namespace {
13 
14 flutter::SemanticsAction GetSemanticsActionForScrollDirection(
15  UIAccessibilityScrollDirection direction) {
16  // To describe the vertical scroll direction, UIAccessibilityScrollDirection uses the
17  // direction the scroll bar moves in and SemanticsAction uses the direction the finger
18  // moves in. However, the horizontal scroll direction matches the SemanticsAction direction.
19  // That is way the following maps vertical opposite of the SemanticsAction, but the horizontal
20  // maps directly.
21  switch (direction) {
22  case UIAccessibilityScrollDirectionRight:
23  case UIAccessibilityScrollDirectionPrevious: // TODO(abarth): Support RTL using
24  // _node.textDirection.
25  return flutter::SemanticsAction::kScrollRight;
26  case UIAccessibilityScrollDirectionLeft:
27  case UIAccessibilityScrollDirectionNext: // TODO(abarth): Support RTL using
28  // _node.textDirection.
29  return flutter::SemanticsAction::kScrollLeft;
30  case UIAccessibilityScrollDirectionUp:
31  return flutter::SemanticsAction::kScrollDown;
32  case UIAccessibilityScrollDirectionDown:
33  return flutter::SemanticsAction::kScrollUp;
34  }
35  FML_DCHECK(false); // Unreachable
36  return flutter::SemanticsAction::kScrollUp;
37 }
38 
40  SkM44 globalTransform = [reference node].transform;
41  for (SemanticsObject* parent = [reference parent]; parent; parent = parent.parent) {
42  globalTransform = parent.node.transform * globalTransform;
43  }
44  return globalTransform;
45 }
46 
47 SkPoint ApplyTransform(SkPoint& point, const SkM44& transform) {
48  SkV4 vector = transform.map(point.x(), point.y(), 0, 1);
49  return SkPoint::Make(vector.x / vector.w, vector.y / vector.w);
50 }
51 
52 CGPoint ConvertPointToGlobal(SemanticsObject* reference, CGPoint local_point) {
53  UIView* containerView = [reference bridgeView];
54  if (!containerView) {
55  return CGPointZero;
56  }
57  SkM44 globalTransform = GetGlobalTransform(reference);
58  SkPoint point = SkPoint::Make(local_point.x, local_point.y);
59  point = ApplyTransform(point, globalTransform);
60  // `rect` is in the physical pixel coordinate system. iOS expects the accessibility frame in
61  // the logical pixel coordinate system. Therefore, we divide by the `scale` (pixel ratio) to
62  // convert.
63  UIScreen* screen = containerView.window.screen;
64  // Screen can be nil if the FlutterView is covered by another native view.
65  CGFloat scale = (screen ?: UIScreen.mainScreen).scale;
66  auto result = CGPointMake(point.x() / scale, point.y() / scale);
67  return [containerView convertPoint:result toView:nil];
68 }
69 
70 CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) {
71  UIView* containerView = [reference bridgeView];
72  if (!containerView) {
73  return CGRectZero;
74  }
75  SkM44 globalTransform = GetGlobalTransform(reference);
76 
77  SkPoint quad[4] = {
78  SkPoint::Make(local_rect.origin.x, local_rect.origin.y), // top left
79  SkPoint::Make(local_rect.origin.x + local_rect.size.width, local_rect.origin.y), // top right
80  SkPoint::Make(local_rect.origin.x + local_rect.size.width,
81  local_rect.origin.y + local_rect.size.height), // bottom right
82  SkPoint::Make(local_rect.origin.x,
83  local_rect.origin.y + local_rect.size.height) // bottom left
84  };
85  for (auto& point : quad) {
86  point = ApplyTransform(point, globalTransform);
87  }
88  SkRect rect;
89  NSCAssert(rect.setBoundsCheck({quad, 4}), @"Transformed points can't form a rect");
90  rect.setBounds({quad, 4});
91 
92  // `rect` is in the physical pixel coordinate system. iOS expects the accessibility frame in
93  // the logical pixel coordinate system. Therefore, we divide by the `scale` (pixel ratio) to
94  // convert.
95  UIScreen* screen = containerView.window.screen;
96  // Screen can be nil if the FlutterView is covered by another native view.
97  CGFloat scale = (screen ?: UIScreen.mainScreen).scale;
98  auto result =
99  CGRectMake(rect.x() / scale, rect.y() / scale, rect.width() / scale, rect.height() / scale);
100  return UIAccessibilityConvertFrameToScreenCoordinates(result, containerView);
101 }
102 
103 } // namespace
104 
106 @property(nonatomic, retain, readonly) UISwitch* nativeSwitch;
107 @end
108 
109 @implementation FlutterSwitchSemanticsObject
110 
111 - (instancetype)initWithBridge:(fml::WeakPtr<flutter::AccessibilityBridgeIos>)bridge
112  uid:(int32_t)uid {
113  self = [super initWithBridge:bridge uid:uid];
114  if (self) {
115  _nativeSwitch = [[UISwitch alloc] init];
116  }
117  return self;
118 }
119 
120 - (NSMethodSignature*)methodSignatureForSelector:(SEL)sel {
121  NSMethodSignature* result = [super methodSignatureForSelector:sel];
122  if (!result) {
123  result = [self.nativeSwitch methodSignatureForSelector:sel];
124  }
125  return result;
126 }
127 
128 - (void)forwardInvocation:(NSInvocation*)anInvocation {
129  anInvocation.target = self.nativeSwitch;
130  [anInvocation invoke];
131 }
132 
133 - (NSString*)accessibilityValue {
134  self.nativeSwitch.on = self.node.flags.isToggled == flutter::SemanticsTristate::kTrue ||
135  self.node.flags.isChecked == flutter::SemanticsCheckState::kTrue;
136 
137  if (!self.bridge) {
138  return nil;
139  } else {
140  return self.nativeSwitch.accessibilityValue;
141  }
142 }
143 
144 - (UIAccessibilityTraits)accessibilityTraits {
145  self.nativeSwitch.enabled = self.node.flags.isEnabled == flutter::SemanticsTristate::kTrue;
146 
147  return self.nativeSwitch.accessibilityTraits;
148 }
149 
150 @end // FlutterSwitchSemanticsObject
151 
154 - (UIView*)reattachScrollViewToCurrentBridgeView;
155 - (void)updateScrollViewForCurrentBridgeView;
156 - (void)updateScrollViewGeometry;
157 @end
158 
159 @implementation FlutterScrollableSemanticsObject
160 
161 - (instancetype)initWithBridge:(fml::WeakPtr<flutter::AccessibilityBridgeIos>)bridge
162  uid:(int32_t)uid {
163  self = [super initWithBridge:bridge uid:uid];
164  if (self) {
165  _scrollView = [[FlutterSemanticsScrollView alloc] initWithSemanticsObject:self];
166  [_scrollView setShowsHorizontalScrollIndicator:NO];
167  [_scrollView setShowsVerticalScrollIndicator:NO];
168  [_scrollView setContentInset:UIEdgeInsetsZero];
169  [_scrollView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
171  }
172  return self;
173 }
174 
175 - (void)dealloc {
176  [_scrollView removeFromSuperview];
177 }
178 
180  [self updateScrollViewForCurrentBridgeView];
181 }
182 
184  [self updateScrollViewForCurrentBridgeView];
185 }
186 
187 - (id)nativeAccessibility {
188  return _scrollView;
189 }
190 
191 // private methods
192 
193 - (UIView*)reattachScrollViewToCurrentBridgeView {
194  UIView* view = self.bridgeView;
195  if (_scrollView.superview != view) {
196  [_scrollView removeFromSuperview];
197  [view addSubview:_scrollView];
198  }
199  return view;
200 }
201 
202 - (void)updateScrollViewForCurrentBridgeView {
203  UIView* view = [self reattachScrollViewToCurrentBridgeView];
204  if (view) {
205  [self updateScrollViewGeometry];
206  }
207 }
208 
209 - (void)updateScrollViewGeometry {
210  // In order to make iOS think this UIScrollView is scrollable, the following
211  // requirements must be true.
212  // 1. contentSize must be bigger than the frame size.
213  // 2. The scrollable isAccessibilityElement must return YES
214  //
215  // Once the requirements are met, the iOS uses contentOffset to determine
216  // what scroll actions are available. e.g. If the view scrolls vertically and
217  // contentOffset is 0.0, only the scroll down action is available.
218  _scrollView.frame = self.accessibilityFrame;
219  _scrollView.contentSize = [self contentSizeInternal];
220  // See the documentation on `isDoingSystemScrolling`.
221  if (!_scrollView.isDoingSystemScrolling) {
222  [_scrollView setContentOffset:self.contentOffsetInternal animated:NO];
223  }
224 }
225 
226 - (float)scrollExtentMax {
227  if (!self.bridge) {
228  return 0.0f;
229  }
230  float scrollExtentMax = self.node.scrollExtentMax;
231  if (isnan(scrollExtentMax)) {
232  scrollExtentMax = 0.0f;
233  } else if (!isfinite(scrollExtentMax)) {
234  scrollExtentMax = kScrollExtentMaxForInf + [self scrollPosition];
235  }
236  return scrollExtentMax;
237 }
238 
239 - (float)scrollPosition {
240  if (!self.bridge) {
241  return 0.0f;
242  }
243  float scrollPosition = self.node.scrollPosition;
244  if (isnan(scrollPosition)) {
245  scrollPosition = 0.0f;
246  }
247  NSCAssert(isfinite(scrollPosition), @"The scrollPosition must not be infinity");
248  return scrollPosition;
249 }
250 
251 - (CGSize)contentSizeInternal {
252  CGRect result;
253  const SkRect& rect = self.node.rect;
254 
255  if (self.node.actions & flutter::kVerticalScrollSemanticsActions) {
256  result = CGRectMake(rect.x(), rect.y(), rect.width(), rect.height() + [self scrollExtentMax]);
257  } else if (self.node.actions & flutter::kHorizontalScrollSemanticsActions) {
258  result = CGRectMake(rect.x(), rect.y(), rect.width() + [self scrollExtentMax], rect.height());
259  } else {
260  result = CGRectMake(rect.x(), rect.y(), rect.width(), rect.height());
261  }
262  return ConvertRectToGlobal(self, result).size;
263 }
264 
265 - (CGPoint)contentOffsetInternal {
266  CGPoint result;
267  CGPoint origin = _scrollView.frame.origin;
268  const SkRect& rect = self.node.rect;
269  if (self.node.actions & flutter::kVerticalScrollSemanticsActions) {
270  result = ConvertPointToGlobal(self, CGPointMake(rect.x(), rect.y() + [self scrollPosition]));
271  } else if (self.node.actions & flutter::kHorizontalScrollSemanticsActions) {
272  result = ConvertPointToGlobal(self, CGPointMake(rect.x() + [self scrollPosition], rect.y()));
273  } else {
274  result = origin;
275  }
276  return CGPointMake(result.x - origin.x, result.y - origin.y);
277 }
278 
279 @end // FlutterScrollableSemanticsObject
280 
281 @implementation FlutterCustomAccessibilityAction {
282 }
283 @end
284 
285 @interface SemanticsObject ()
286 @property(nonatomic) SemanticsObjectContainer* container;
287 
288 /** Should only be called in conjunction with setting child/parent relationship. */
289 @property(nonatomic, weak, readwrite) SemanticsObject* parent;
290 
291 @end
292 
293 @implementation SemanticsObject {
294  fml::WeakPtr<flutter::AccessibilityBridgeIos> _weakBridge;
295  NSMutableArray<SemanticsObject*>* _children;
297 }
298 
299 #pragma mark - Designated initializers
300 
301 - (instancetype)initWithBridge:(fml::WeakPtr<flutter::AccessibilityBridgeIos>)bridge
302  uid:(int32_t)uid {
303  FML_DCHECK(bridge) << "bridge must be set";
304  FML_DCHECK(uid >= kRootNodeId);
305  flutter::AccessibilityBridgeIos* accessibility_bridge = bridge.get();
306  if (!accessibility_bridge) {
307  return nil;
308  }
309  // Initialize with the UIView as the container.
310  // The UIView will not necessarily be accessibility parent for this object.
311  // The bridge informs the OS of the actual structure via
312  // `accessibilityContainer` and `accessibilityElementAtIndex`.
313  UIView* accessibilityContainer =
314  accessibility_bridge->AccessibilityElementInitializationContainer();
315  self = [super initWithAccessibilityContainer:accessibilityContainer];
316 
317  if (self) {
318  _weakBridge = bridge;
319  _uid = uid;
320  _children = [[NSMutableArray alloc] init];
321  _childrenInHitTestOrder = [[NSArray alloc] init];
322  }
323 
324  return self;
325 }
326 
327 - (void)dealloc {
328  // Set parent and children parents to nil explicitly in dealloc.
329  // -[UIAccessibilityElement dealloc] has in the past called into -accessibilityContainer
330  // and self.children. There have also been crashes related to iOS
331  // accessing methods during dealloc, and there's a lag before the tree changes.
332  // See https://github.com/flutter/engine/pull/4602 and
333  // https://github.com/flutter/engine/pull/27786.
334  for (SemanticsObject* child in _children) {
335  if (child.parent == self) {
336  child.parent = nil;
337  }
338  }
339  [_children removeAllObjects];
340 
341  _parent = nil;
342  _inDealloc = YES;
343 }
344 
345 #pragma mark - Semantic object property accesser
346 
347 - (NSArray<SemanticsObject*>*)children {
348  return [_children copy];
349 }
350 
351 - (void)setChildren:(NSArray<SemanticsObject*>*)children {
352  for (SemanticsObject* child in _children) {
353  if (child.parent == self) {
354  child.parent = nil;
355  }
356  }
357  _children = [children mutableCopy];
358  for (SemanticsObject* child in _children) {
359  child.parent = self;
360  }
361 }
362 
363 - (void)setChildrenInHitTestOrder:(NSArray<SemanticsObject*>*)childrenInHitTestOrder {
364  _childrenInHitTestOrder = [childrenInHitTestOrder copy];
365 }
366 
367 - (BOOL)hasChildren {
368  return [self.children count] != 0;
369 }
370 
371 #pragma mark - Semantic object method
372 
373 - (flutter::AccessibilityBridgeIos*)bridge {
374  return _weakBridge.get();
375 }
376 
377 - (UIView*)bridgeView {
378  flutter::AccessibilityBridgeIos* bridge = [self bridge];
379  return bridge ? bridge->ViewIfLoaded() : nil;
380 }
381 
382 - (void)setSemanticsNode:(const flutter::SemanticsNode*)node {
383  _node = *node;
384 }
385 
386 - (void)accessibilityBridgeDidFinishUpdate { /* Do nothing by default */
387 }
388 
389 - (void)accessibilityBridgeDidChangeView { /* Do nothing by default */
390 }
391 
392 /**
393  * Whether calling `setSemanticsNode:` with `node` would cause a layout change.
394  */
395 - (BOOL)nodeWillCauseLayoutChange:(const flutter::SemanticsNode*)node {
396  return self.node.rect != node->rect || self.node.transform != node->transform;
397 }
398 
399 /**
400  * Whether calling `setSemanticsNode:` with `node` would cause a scroll event.
401  */
402 - (BOOL)nodeWillCauseScroll:(const flutter::SemanticsNode*)node {
403  return !isnan(self.node.scrollPosition) && !isnan(node->scrollPosition) &&
404  self.node.scrollPosition != node->scrollPosition;
405 }
406 
407 /**
408  * Whether calling `setSemanticsNode:` with `node` should trigger an
409  * announcement.
410  */
411 - (BOOL)nodeShouldTriggerAnnouncement:(const flutter::SemanticsNode*)node {
412  // The node dropped the live region flag, if it ever had one.
413  if (!node || !node->flags.isLiveRegion) {
414  return NO;
415  }
416 
417  // The node has gained a new live region flag, always announce.
418  if (!self.node.flags.isLiveRegion) {
419  return YES;
420  }
421 
422  // The label has updated, and the new node has a live region flag.
423  return self.node.label != node->label;
424 }
425 
426 - (void)replaceChildAtIndex:(NSInteger)index withChild:(SemanticsObject*)child {
427  SemanticsObject* oldChild = _children[index];
428  oldChild.parent = nil;
429  child.parent = self;
430  [_children replaceObjectAtIndex:index withObject:child];
431 }
432 
433 - (NSString*)routeName {
434  // Returns the first non-null and non-empty semantic label of a child
435  // with an NamesRoute flag. Otherwise returns nil.
436  if (self.node.flags.namesRoute) {
437  NSString* newName = self.accessibilityLabel;
438  if (newName != nil && [newName length] > 0) {
439  return newName;
440  }
441  }
442  if ([self hasChildren]) {
443  for (SemanticsObject* child in self.children) {
444  NSString* newName = [child routeName];
445  if (newName != nil && [newName length] > 0) {
446  return newName;
447  }
448  }
449  }
450  return nil;
451 }
452 
453 - (id)nativeAccessibility {
454  return self;
455 }
456 
457 - (NSAttributedString*)createAttributedStringFromString:(NSString*)string
458  withAttributes:
459  (const flutter::StringAttributes&)attributes {
460  NSMutableAttributedString* attributedString =
461  [[NSMutableAttributedString alloc] initWithString:string];
462  for (const auto& attribute : attributes) {
463  NSRange range = NSMakeRange(attribute->start, attribute->end - attribute->start);
464  switch (attribute->type) {
465  case flutter::StringAttributeType::kLocale: {
466  std::shared_ptr<flutter::LocaleStringAttribute> locale_attribute =
467  std::static_pointer_cast<flutter::LocaleStringAttribute>(attribute);
468  NSDictionary* attributeDict = @{
469  UIAccessibilitySpeechAttributeLanguage : @(locale_attribute->locale.data()),
470  };
471  [attributedString setAttributes:attributeDict range:range];
472  break;
473  }
474  case flutter::StringAttributeType::kSpellOut: {
475  NSDictionary* attributeDict = @{
476  UIAccessibilitySpeechAttributeSpellOut : @YES,
477  };
478  [attributedString setAttributes:attributeDict range:range];
479  break;
480  }
481  }
482  }
483  return attributedString;
484 }
485 
486 - (void)showOnScreen {
487  flutter::AccessibilityBridgeIos* bridge = self.bridge;
488  if (!bridge) {
489  return;
490  }
491  bridge->DispatchSemanticsAction(self.uid, flutter::SemanticsAction::kShowOnScreen);
492 }
493 
494 #pragma mark - UIAccessibility overrides
495 
496 - (BOOL)isAccessibilityElement {
497  if (!self.bridge) {
498  return false;
499  }
500 
501  // Note: hit detection will only apply to elements that report
502  // -isAccessibilityElement of YES. The framework will continue scanning the
503  // entire element tree looking for such a hit.
504 
505  // We enforce in the framework that no other useful semantics are merged with these nodes.
506  if (self.node.flags.scopesRoute) {
507  return false;
508  }
509 
510  return [self isFocusable];
511 }
512 
513 - (NSString*)accessibilityLanguage {
514  flutter::AccessibilityBridgeIos* bridge = self.bridge;
515  if (!bridge) {
516  return nil;
517  }
518 
519  if (!self.node.locale.empty()) {
520  return @(self.node.locale.data());
521  }
522  return bridge->GetDefaultLocale();
523 }
524 
525 - (bool)isFocusable {
526  // If the node is scrollable AND hidden OR
527  // The node has a label, value, or hint OR
528  // The node has non-scrolling related actions.
529  //
530  // The kIsHidden flag set with the scrollable flag means this node is now
531  // hidden but still is a valid target for a11y focus in the tree, e.g. a list
532  // item that is currently off screen but the a11y navigation needs to know
533  // about.
534  return (self.node.flags.hasImplicitScrolling && self.node.flags.isHidden)
535 
536  || !self.node.label.empty() || !self.node.value.empty() || !self.node.hint.empty() ||
537  (self.node.actions & ~flutter::kScrollableSemanticsActions) != 0;
538 }
539 
540 - (void)collectRoutes:(NSMutableArray<SemanticsObject*>*)edges {
541  if (self.node.flags.scopesRoute) {
542  [edges addObject:self];
543  }
544  if ([self hasChildren]) {
545  for (SemanticsObject* child in self.children) {
546  [child collectRoutes:edges];
547  }
548  }
549 }
550 
551 - (BOOL)onCustomAccessibilityAction:(FlutterCustomAccessibilityAction*)action {
552  flutter::AccessibilityBridgeIos* bridge = self.bridge;
553  if (!bridge) {
554  return NO;
555  }
556  if (!self.node.HasAction(flutter::SemanticsAction::kCustomAction)) {
557  return NO;
558  }
559  int32_t action_id = action.uid;
560  std::vector<uint8_t> args;
561  args.push_back(3); // type=int32.
562  args.push_back(action_id);
563  args.push_back(action_id >> 8);
564  args.push_back(action_id >> 16);
565  args.push_back(action_id >> 24);
566  bridge->DispatchSemanticsAction(
567  self.uid, flutter::SemanticsAction::kCustomAction,
568  fml::MallocMapping::Copy(args.data(), args.size() * sizeof(uint8_t)));
569  return YES;
570 }
571 
572 - (NSString*)accessibilityIdentifier {
573  if (!self.bridge) {
574  return nil;
575  }
576 
577  if (self.node.identifier.empty()) {
578  return nil;
579  }
580  return @(self.node.identifier.data());
581 }
582 
583 - (NSString*)accessibilityLabel {
584  if (!self.bridge) {
585  return nil;
586  }
587  NSString* label = nil;
588  if (!self.node.label.empty()) {
589  label = @(self.node.label.data());
590  }
591  if (!self.node.tooltip.empty()) {
592  label = label ? [NSString stringWithFormat:@"%@\n%@", label, @(self.node.tooltip.data())]
593  : @(self.node.tooltip.data());
594  }
595  return label;
596 }
597 
598 - (bool)containsPoint:(CGPoint)point {
599  // The point is in global coordinates, so use the global rect here.
600  return CGRectContainsPoint([self globalRect], point);
601 }
602 
603 // Finds the first eligiable semantics object in hit test order.
604 - (id)search:(CGPoint)point {
605  // Search children in hit test order.
606  for (SemanticsObject* child in [self childrenInHitTestOrder]) {
607  if ([child containsPoint:point]) {
608  id childSearchResult = [child search:point];
609  if (childSearchResult != nil) {
610  return childSearchResult;
611  }
612  }
613  }
614  // Check if the current semantic object should be returned.
615  if ([self containsPoint:point] && [self isFocusable]) {
616  return self.nativeAccessibility;
617  }
618  return nil;
619 }
620 
621 // iOS uses this method to determine the hittest results when users touch
622 // explore in VoiceOver.
623 //
624 // For overlapping UIAccessibilityElements (e.g. a stack) in IOS, the focus
625 // goes to the smallest object before IOS 16, but to the top-left object in
626 // IOS 16. Overrides this method to focus the first eligiable semantics
627 // object in hit test order.
628 - (id)_accessibilityHitTest:(CGPoint)point withEvent:(UIEvent*)event {
629  if (!self.bridge) {
630  return nil;
631  }
632  return [self search:point];
633 }
634 
635 // iOS calls this method when this item is swipe-to-focusd in VoiceOver.
636 - (BOOL)accessibilityScrollToVisible {
637  [self showOnScreen];
638  return YES;
639 }
640 
641 // iOS calls this method when this item is swipe-to-focusd in VoiceOver.
642 - (BOOL)accessibilityScrollToVisibleWithChild:(id)child {
643  if ([child isKindOfClass:[SemanticsObject class]]) {
644  [child showOnScreen];
645  return YES;
646  }
647  return NO;
648 }
649 
650 - (NSAttributedString*)accessibilityAttributedLabel {
651  NSString* label = self.accessibilityLabel;
652  if (label.length == 0) {
653  return nil;
654  }
655  return [self createAttributedStringFromString:label withAttributes:self.node.labelAttributes];
656 }
657 
658 - (NSString*)accessibilityHint {
659  if (!self.bridge) {
660  return nil;
661  }
662 
663  if (self.node.hint.empty()) {
664  return nil;
665  }
666  return @(self.node.hint.data());
667 }
668 
669 - (NSAttributedString*)accessibilityAttributedHint {
670  NSString* hint = [self accessibilityHint];
671  if (hint.length == 0) {
672  return nil;
673  }
674  return [self createAttributedStringFromString:hint withAttributes:self.node.hintAttributes];
675 }
676 
677 - (NSString*)accessibilityValue {
678  if (!self.bridge) {
679  return nil;
680  }
681 
682  if (!self.node.value.empty()) {
683  return @(self.node.value.data());
684  }
685 
686  // iOS does not announce values of native radio buttons.
687  if (self.node.flags.isInMutuallyExclusiveGroup) {
688  return nil;
689  }
690 
691  // FlutterSwitchSemanticsObject should supercede these conditionals.
692 
693  if (self.node.flags.isToggled == flutter::SemanticsTristate::kTrue ||
694  self.node.flags.isChecked == flutter::SemanticsCheckState::kTrue) {
695  return @"1";
696  } else if (self.node.flags.isToggled == flutter::SemanticsTristate::kFalse ||
697  self.node.flags.isChecked == flutter::SemanticsCheckState::kFalse) {
698  return @"0";
699  }
700 
701  return nil;
702 }
703 
704 - (NSAttributedString*)accessibilityAttributedValue {
705  NSString* value = [self accessibilityValue];
706  if (value.length == 0) {
707  return nil;
708  }
709  return [self createAttributedStringFromString:value withAttributes:self.node.valueAttributes];
710 }
711 
712 - (CGRect)accessibilityFrame {
713  if (!self.bridge) {
714  return CGRectZero;
715  }
716 
717  if (self.node.flags.isHidden) {
718  return [super accessibilityFrame];
719  }
720  return [self globalRect];
721 }
722 
723 - (CGRect)globalRect {
724  const SkRect& rect = self.node.rect;
725  CGRect localRect = CGRectMake(rect.x(), rect.y(), rect.width(), rect.height());
726  return ConvertRectToGlobal(self, localRect);
727 }
728 
729 #pragma mark - UIAccessibilityElement protocol
730 
731 - (void)setAccessibilityContainer:(id)container {
732  // Explicit noop. The containers are calculated lazily in `accessibilityContainer`.
733  // See also: https://github.com/flutter/flutter/issues/54366
734 }
735 
736 - (id)accessibilityContainer {
737  if (_inDealloc) {
738  // In iOS9, `accessibilityContainer` will be called by `[UIAccessibilityElementSuperCategory
739  // dealloc]` during `[super dealloc]`. And will crash when accessing `_children` which has
740  // called `[_children release]` in `[SemanticsObject dealloc]`.
741  // https://github.com/flutter/flutter/issues/87247
742  return nil;
743  }
744 
745  if (!self.bridge) {
746  return nil;
747  }
748 
749  if ([self hasChildren] || self.uid == kRootNodeId) {
750  if (self.container == nil) {
751  self.container = [[SemanticsObjectContainer alloc] initWithSemanticsObject:self];
752  }
753  return self.container;
754  }
755  if (self.parent == nil) {
756  // This can happen when we have released the accessibility tree but iOS is
757  // still holding onto our objects. iOS can take some time before it
758  // realizes that the tree has changed.
759  return nil;
760  }
761  return self.parent.accessibilityContainer;
762 }
763 
764 #pragma mark - UIAccessibilityAction overrides
765 
766 - (BOOL)accessibilityActivate {
767  flutter::AccessibilityBridgeIos* bridge = self.bridge;
768  if (!bridge) {
769  return NO;
770  }
771  if (!self.node.HasAction(flutter::SemanticsAction::kTap)) {
772  // Prevent sliders to receive a regular tap which will change the value.
773  //
774  // This is needed because it causes slider to select to middle if it
775  // does not have a semantics tap.
776  if (self.node.flags.isSlider) {
777  return YES;
778  }
779  return NO;
780  }
781  bridge->DispatchSemanticsAction(self.uid, flutter::SemanticsAction::kTap);
782  return YES;
783 }
784 
785 - (void)accessibilityIncrement {
786  flutter::AccessibilityBridgeIos* bridge = self.bridge;
787  if (!bridge) {
788  return;
789  }
790  if (self.node.HasAction(flutter::SemanticsAction::kIncrease)) {
791  self.node.value = self.node.increasedValue;
792  bridge->DispatchSemanticsAction(self.uid, flutter::SemanticsAction::kIncrease);
793  }
794 }
795 
796 - (void)accessibilityDecrement {
797  flutter::AccessibilityBridgeIos* bridge = self.bridge;
798  if (!bridge) {
799  return;
800  }
801  if (self.node.HasAction(flutter::SemanticsAction::kDecrease)) {
802  self.node.value = self.node.decreasedValue;
803  bridge->DispatchSemanticsAction(self.uid, flutter::SemanticsAction::kDecrease);
804  }
805 }
806 
807 - (BOOL)accessibilityScroll:(UIAccessibilityScrollDirection)direction {
808  flutter::AccessibilityBridgeIos* bridge = self.bridge;
809  if (!bridge) {
810  return NO;
811  }
812  flutter::SemanticsAction action = GetSemanticsActionForScrollDirection(direction);
813  if (!self.node.HasAction(action)) {
814  return NO;
815  }
816  bridge->DispatchSemanticsAction(self.uid, action);
817  return YES;
818 }
819 
820 - (BOOL)accessibilityPerformEscape {
821  flutter::AccessibilityBridgeIos* bridge = self.bridge;
822  if (!bridge) {
823  return NO;
824  }
825  if (!self.node.HasAction(flutter::SemanticsAction::kDismiss)) {
826  return NO;
827  }
828  bridge->DispatchSemanticsAction(self.uid, flutter::SemanticsAction::kDismiss);
829  return YES;
830 }
831 
832 #pragma mark UIAccessibilityFocus overrides
833 
834 - (void)accessibilityElementDidBecomeFocused {
835  flutter::AccessibilityBridgeIos* bridge = self.bridge;
836  if (!bridge) {
837  return;
838  }
839  bridge->AccessibilityObjectDidBecomeFocused(self.uid);
840  if (self.node.flags.isHidden || self.node.flags.isHeader) {
841  [self showOnScreen];
842  }
843  if (self.node.HasAction(flutter::SemanticsAction::kDidGainAccessibilityFocus)) {
844  bridge->DispatchSemanticsAction(self.uid, flutter::SemanticsAction::kDidGainAccessibilityFocus);
845  }
846 }
847 
848 - (void)accessibilityElementDidLoseFocus {
849  flutter::AccessibilityBridgeIos* bridge = self.bridge;
850  if (!bridge) {
851  return;
852  }
853  bridge->AccessibilityObjectDidLoseFocus(self.uid);
854  if (self.node.HasAction(flutter::SemanticsAction::kDidLoseAccessibilityFocus)) {
855  bridge->DispatchSemanticsAction(self.uid, flutter::SemanticsAction::kDidLoseAccessibilityFocus);
856  }
857 }
858 
859 - (BOOL)accessibilityRespondsToUserInteraction {
860  if (self.node.flags.isAccessibilityFocusBlocked) {
861  return false;
862  }
863 
864  // Return true only if the node contains actions other than system actions.
865  if ((self.node.actions & ~flutter::kSystemActions) != 0) {
866  return true;
867  }
868 
869  if (!self.node.customAccessibilityActions.empty()) {
870  return true;
871  }
872 
873  return false;
874 }
875 
876 @end
877 
878 @implementation FlutterSemanticsObject
879 
880 #pragma mark - Designated initializers
881 
882 - (instancetype)initWithBridge:(fml::WeakPtr<flutter::AccessibilityBridgeIos>)bridge
883  uid:(int32_t)uid {
884  self = [super initWithBridge:bridge uid:uid];
885  return self;
886 }
887 
888 #pragma mark - UIAccessibility overrides
889 
890 - (UIAccessibilityTraits)accessibilityTraits {
891  UIAccessibilityTraits traits = UIAccessibilityTraitNone;
892  if (self.node.HasAction(flutter::SemanticsAction::kIncrease) ||
893  self.node.HasAction(flutter::SemanticsAction::kDecrease)) {
894  traits |= UIAccessibilityTraitAdjustable;
895  }
896  // This should also capture radio buttons.
897  if (self.node.flags.isToggled != flutter::SemanticsTristate::kNone ||
898  self.node.flags.isChecked != flutter::SemanticsCheckState::kNone) {
899  traits |= UIAccessibilityTraitButton;
900  }
901  if (self.node.flags.isSelected == flutter::SemanticsTristate::kTrue) {
902  traits |= UIAccessibilityTraitSelected;
903  }
904  if (self.node.flags.isButton) {
905  traits |= UIAccessibilityTraitButton;
906  }
907  if (self.node.flags.isEnabled == flutter::SemanticsTristate::kFalse) {
908  traits |= UIAccessibilityTraitNotEnabled;
909  }
910  if (self.node.headingLevel > 0) {
911  // VoiceOver treats UIAccessibilityTraitHeader as heading.
912  traits |= UIAccessibilityTraitHeader;
913  }
914  if (self.node.flags.isImage) {
915  traits |= UIAccessibilityTraitImage;
916  }
917  if (self.node.flags.isLiveRegion) {
918  traits |= UIAccessibilityTraitUpdatesFrequently;
919  }
920  if (self.node.flags.isLink) {
921  traits |= UIAccessibilityTraitLink;
922  }
923  if (traits == UIAccessibilityTraitNone && ![self hasChildren] &&
924  self.accessibilityLabel.length != 0 && !self.node.flags.isTextField) {
925  traits = UIAccessibilityTraitStaticText;
926  }
927  return traits;
928 }
929 
930 @end
931 
933 @property(nonatomic, weak) UIView* platformView;
934 @end
935 
937 
938 - (instancetype)initWithBridge:(fml::WeakPtr<flutter::AccessibilityBridgeIos>)bridge
939  uid:(int32_t)uid
940  platformView:(nonnull FlutterTouchInterceptingView*)platformView {
941  if (self = [super initWithBridge:bridge uid:uid]) {
942  _platformView = platformView;
943  [platformView setFlutterAccessibilityContainer:self];
944  }
945  return self;
946 }
947 
948 - (id)nativeAccessibility {
949  return self.platformView;
950 }
951 
952 @end
953 
954 @implementation SemanticsObjectContainer {
955 }
956 
957 #pragma mark - initializers
958 
959 - (instancetype)initWithSemanticsObject:(SemanticsObject*)semanticsObject {
960  FML_DCHECK(semanticsObject) << "semanticsObject must be set";
961  flutter::AccessibilityBridgeIos* accessibility_bridge = semanticsObject.bridge;
962  if (!accessibility_bridge) {
963  return nil;
964  }
965  // Initialize with the UIView as the container.
966  // The UIView will not necessarily be accessibility parent for this object.
967  // The bridge informs the OS of the actual structure via
968  // `accessibilityContainer` and `accessibilityElementAtIndex`.
969  UIView* accessibilityContainer =
970  accessibility_bridge->AccessibilityElementInitializationContainer();
971  self = [super initWithAccessibilityContainer:accessibilityContainer];
972 
973  if (self) {
974  _semanticsObject = semanticsObject;
975  }
976 
977  return self;
978 }
979 
980 #pragma mark - UIAccessibilityContainer overrides
981 
982 - (NSInteger)accessibilityElementCount {
983  return self.semanticsObject.children.count + 1;
984 }
985 
986 - (nullable id)accessibilityElementAtIndex:(NSInteger)index {
987  if (index < 0 || index >= [self accessibilityElementCount]) {
988  return nil;
989  }
990  if (index == 0) {
991  return self.semanticsObject.nativeAccessibility;
992  }
993 
994  SemanticsObject* child = self.semanticsObject.children[index - 1];
995 
996  if ([child hasChildren]) {
997  return child.accessibilityContainer;
998  }
999  return child.nativeAccessibility;
1000 }
1001 
1002 - (NSInteger)indexOfAccessibilityElement:(id)element {
1003  if (element == self.semanticsObject.nativeAccessibility) {
1004  return 0;
1005  }
1006 
1007  NSArray<SemanticsObject*>* children = self.semanticsObject.children;
1008  for (size_t i = 0; i < [children count]; i++) {
1009  SemanticsObject* child = children[i];
1010  if ((![child hasChildren] && child.nativeAccessibility == element) ||
1011  ([child hasChildren] && [child.nativeAccessibility accessibilityContainer] == element)) {
1012  return i + 1;
1013  }
1014  }
1015  return NSNotFound;
1016 }
1017 
1018 #pragma mark - UIAccessibilityElement protocol
1019 
1020 - (BOOL)isAccessibilityElement {
1021  return NO;
1022 }
1023 
1024 - (CGRect)accessibilityFrame {
1025  // For OverlayPortals, the child element is sometimes outside the bounds of the parent
1026  // Even if it's marked accessible, VoiceControl labels will not appear if it's too
1027  // spatially distant. Set the frame to be the max screen size so all children are guaraenteed
1028  // to be contained.
1029  //
1030  // Prefer the screen that the view is on. Fallback to main screen if not available (e.g. the
1031  // Flutter view hasn't been added to any screen).
1032  UIScreen* screen = self.semanticsObject.bridgeView.window.windowScene.screen;
1033  return (screen ?: UIScreen.mainScreen).bounds;
1034 }
1035 
1036 - (id)accessibilityContainer {
1037  return ([self.semanticsObject uid] == kRootNodeId)
1038  ? self.semanticsObject.bridgeView
1039  : self.semanticsObject.parent.accessibilityContainer;
1040 }
1041 
1042 #pragma mark - UIAccessibilityAction overrides
1043 
1044 - (BOOL)accessibilityScroll:(UIAccessibilityScrollDirection)direction {
1045  return [self.semanticsObject accessibilityScroll:direction];
1046 }
1047 
1048 @end
constexpr NS_ASSUME_NONNULL_BEGIN int32_t kRootNodeId
constexpr float kScrollExtentMaxForInf
NSMutableArray< SemanticsObject * > * _children
BOOL _inDealloc
Interface that represents an accessibility bridge for iOS.
virtual void DispatchSemanticsAction(int32_t id, flutter::SemanticsAction action)=0
virtual void AccessibilityObjectDidLoseFocus(int32_t id)=0
virtual void AccessibilityObjectDidBecomeFocused(int32_t id)=0
virtual UIView * ViewIfLoaded() const =0
virtual UIView * AccessibilityElementInitializationContainer() const =0
virtual NSString * GetDefaultLocale()=0
FlutterSemanticsScrollView * scrollView
void accessibilityBridgeDidChangeView()
NSArray< SemanticsObject * > * children
SemanticsObject * parent
nullable UIView * bridgeView()
void accessibilityBridgeDidFinishUpdate()
nullable flutter::AccessibilityBridgeIos * bridge()
flutter::SemanticsNode node
CGRect ConvertRectToGlobal(SemanticsObject *reference, CGRect local_rect)
flutter::SemanticsAction GetSemanticsActionForScrollDirection(UIAccessibilityScrollDirection direction)
CGPoint ConvertPointToGlobal(SemanticsObject *reference, CGPoint local_point)
SkM44 GetGlobalTransform(SemanticsObject *reference)
SkPoint ApplyTransform(SkPoint &point, const SkM44 &transform)