diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 724af2afdc..08d65c11b2 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -874,8 +874,11 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterView.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.m FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_ios.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/accessibility_text_entry.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/accessibility_text_entry.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/platform_message_response_darwin.h diff --git a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn index 7af51b5aa2..c98e2b31bf 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn +++ b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn @@ -73,6 +73,8 @@ shared_library("create_flutter_framework_dylib") { "framework/Source/FlutterView.mm", "framework/Source/FlutterViewController.mm", "framework/Source/FlutterViewController_Internal.h", + "framework/Source/SemanticsObject.h", + "framework/Source/SemanticsObject.mm", "framework/Source/accessibility_bridge.h", "framework/Source/accessibility_bridge.mm", "framework/Source/accessibility_text_entry.h", diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h new file mode 100644 index 0000000000..575b978ab2 --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h @@ -0,0 +1,156 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_SEMANTICS_OBJECT_H_ +#define SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_SEMANTICS_OBJECT_H_ + +#import + +#include "flutter/fml/macros.h" +#include "flutter/fml/memory/weak_ptr.h" +#include "flutter/lib/ui/semantics/semantics_node.h" +#include "flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_ios.h" + +constexpr int32_t kRootNodeId = 0; + +@class FlutterPlatformViewSemanticsContainer; + +/** + * A node in the iOS semantics tree. + */ +@interface SemanticsObject : UIAccessibilityElement + +/** + * The globally unique identifier for this node. + */ +@property(nonatomic, readonly) int32_t uid; + +/** + * The parent of this node in the node tree. Will be nil for the root node and + * during transient state changes. + */ +@property(nonatomic, readonly) SemanticsObject* parent; + +/** + * The accessibility bridge that this semantics object is attached to. This + * object may use the bridge to access contextual application information. A weak + * pointer is used because the platform view owns the accessibility bridge. + * If you are referencing this property from an iOS callback, be sure to + * use `isAccessibilityBridgeActive` to protect against the case where this + * node may be orphaned. + */ +@property(nonatomic, readonly) fml::WeakPtr bridge; + +/** + * Due to the fact that VoiceOver may hold onto SemanticObjects even after it shuts down, + * there can be situations where the AccessibilityBridge is shutdown, but the SemanticObject + * will still be alive. If VoiceOver is turned on again, it may try to access this orphaned + * SemanticObject. Methods that are called from the accessiblity framework should use + * this to guard against this case by just returning early if its bridge has been shutdown. + * + * See https://github.com/flutter/flutter/issues/43795 for more information. + */ +- (BOOL)isAccessibilityBridgeAlive; + +/** + * The semantics node used to produce this semantics object. + */ +@property(nonatomic, readonly) flutter::SemanticsNode node; + +/** + * Updates this semantics object using data from the `node` argument. + */ +- (void)setSemanticsNode:(const flutter::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. + */ +@property(nonatomic, strong) NSArray* children; + +/** + * Used if this SemanticsObject is for a platform view. + */ +@property(strong, nonatomic) FlutterPlatformViewSemanticsContainer* platformViewSemanticsContainer; + +- (void)replaceChildAtIndex:(NSInteger)index withChild:(SemanticsObject*)child; + +- (BOOL)nodeWillCauseLayoutChange:(const flutter::SemanticsNode*)node; + +#pragma mark - Designated initializers + +- (instancetype)init __attribute__((unavailable("Use initWithBridge instead"))); +- (instancetype)initWithBridge:(fml::WeakPtr)bridge + uid:(int32_t)uid NS_DESIGNATED_INITIALIZER; + +- (BOOL)nodeWillCauseScroll:(const flutter::SemanticsNode*)node; +- (void)collectRoutes:(NSMutableArray*)edges; +- (NSString*)routeName; + +@end + +/** + * An implementation of UIAccessibilityCustomAction which also contains the + * Flutter uid. + */ +@interface FlutterCustomAccessibilityAction : UIAccessibilityCustomAction + +/** + * The uid of the action defined by the flutter application. + */ +@property(nonatomic) int32_t uid; + +@end + +/** + * 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 + +/** + * Designated to act as an accessibility container of a platform view. + * + * This object does not take any accessibility actions on its own, nor has any accessibility + * label/value/trait/hint... on its own. The accessibility data will be handled by the platform + * view. + * + * See also: + * * `SemanticsObject` for the other type of semantics objects. + * * `FlutterSemanticsObject` for default implementation of `SemanticsObject`. + */ +@interface FlutterPlatformViewSemanticsContainer : UIAccessibilityElement + +/** + * The position inside an accessibility container. + */ +@property(nonatomic) NSInteger index; + +- (instancetype)init __attribute__((unavailable("Use initWithAccessibilityContainer: instead"))); + +- (instancetype)initWithSemanticsObject:(SemanticsObject*)object; + +@end + +/// A proxy class for SemanticsObject and UISwitch. For most Accessibility and +/// SemanticsObject methods it delegates to the semantics object, otherwise it +/// sends messages to the UISwitch. +@interface FlutterSwitchSemanticsObject : UISwitch +- (instancetype)initWithSemanticsObject:(SemanticsObject*)semanticsObject; +@end + +#endif // SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_SEMANTICS_OBJECT_H_ diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm new file mode 100644 index 0000000000..19deaf223e --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm @@ -0,0 +1,706 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h" + +#include "flutter/fml/platform/darwin/scoped_nsobject.h" +#include "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h" + +namespace { + +flutter::SemanticsAction GetSemanticsActionForScrollDirection( + UIAccessibilityScrollDirection direction) { + // To describe the vertical scroll direction, UIAccessibilityScrollDirection uses the + // direction the scroll bar moves in and SemanticsAction uses the direction the finger + // moves in. However, the horizontal scroll direction matches the SemanticsAction direction. + // That is way the following maps vertical opposite of the SemanticsAction, but the horizontal + // maps directly. + switch (direction) { + case UIAccessibilityScrollDirectionRight: + case UIAccessibilityScrollDirectionPrevious: // TODO(abarth): Support RTL using + // _node.textDirection. + return flutter::SemanticsAction::kScrollRight; + case UIAccessibilityScrollDirectionLeft: + case UIAccessibilityScrollDirectionNext: // TODO(abarth): Support RTL using + // _node.textDirection. + return flutter::SemanticsAction::kScrollLeft; + case UIAccessibilityScrollDirectionUp: + return flutter::SemanticsAction::kScrollDown; + case UIAccessibilityScrollDirectionDown: + return flutter::SemanticsAction::kScrollUp; + } + FML_DCHECK(false); // Unreachable + return flutter::SemanticsAction::kScrollUp; +} + +} // namespace + +@implementation FlutterSwitchSemanticsObject { + SemanticsObject* _semanticsObject; +} + +- (instancetype)initWithSemanticsObject:(SemanticsObject*)semanticsObject { + self = [super init]; + if (self) { + _semanticsObject = [semanticsObject retain]; + } + return self; +} + +- (void)dealloc { + [_semanticsObject release]; + [super dealloc]; +} + +- (NSMethodSignature*)methodSignatureForSelector:(SEL)sel { + NSMethodSignature* result = [super methodSignatureForSelector:sel]; + if (!result) { + result = [_semanticsObject methodSignatureForSelector:sel]; + } + return result; +} + +- (void)forwardInvocation:(NSInvocation*)anInvocation { + [anInvocation setTarget:_semanticsObject]; + [anInvocation invoke]; +} + +- (CGRect)accessibilityFrame { + return [_semanticsObject accessibilityFrame]; +} + +- (id)accessibilityContainer { + return [_semanticsObject accessibilityContainer]; +} + +- (NSString*)accessibilityLabel { + return [_semanticsObject accessibilityLabel]; +} + +- (NSString*)accessibilityHint { + return [_semanticsObject accessibilityHint]; +} + +- (NSString*)accessibilityValue { + if ([_semanticsObject node].HasFlag(flutter::SemanticsFlags::kIsToggled) || + [_semanticsObject node].HasFlag(flutter::SemanticsFlags::kIsChecked)) { + self.on = YES; + } else { + self.on = NO; + } + + if (![_semanticsObject isAccessibilityBridgeAlive]) { + return nil; + } else { + return [super accessibilityValue]; + } +} + +@end // FlutterSwitchSemanticsObject + +@implementation FlutterCustomAccessibilityAction { +} +@end + +/** + * Represents a semantics object that has children and hence has to be presented to the OS as a + * UIAccessibilityContainer. + * + * The SemanticsObject class cannot implement the UIAccessibilityContainer protocol because an + * object that returns YES for isAccessibilityElement cannot also implement + * UIAccessibilityContainer. + * + * With the help of SemanticsObjectContainer, the hierarchy of semantic objects received from + * the framework, such as: + * + * SemanticsObject1 + * SemanticsObject2 + * SemanticsObject3 + * SemanticsObject4 + * + * is translated into the following hierarchy, which is understood by iOS: + * + * SemanticsObjectContainer1 + * SemanticsObject1 + * SemanticsObjectContainer2 + * SemanticsObject2 + * SemanticsObject3 + * SemanticsObject4 + * + * From Flutter's view of the world (the first tree seen above), we construct iOS's view of the + * world (second tree) as follows: We replace each SemanticsObjects that has children with a + * SemanticsObjectContainer, which has the original SemanticsObject and its children as children. + * + * SemanticsObjects have semantic information attached to them which is interpreted by + * VoiceOver (they return YES for isAccessibilityElement). The SemanticsObjectContainers are just + * there for structure and they don't provide any semantic information to VoiceOver (they return + * NO for isAccessibilityElement). + */ +@interface SemanticsObjectContainer : UIAccessibilityElement +- (instancetype)init __attribute__((unavailable("Use initWithSemanticsObject instead"))); +- (instancetype)initWithSemanticsObject:(SemanticsObject*)semanticsObject + bridge:(fml::WeakPtr)bridge + NS_DESIGNATED_INITIALIZER; + +@property(nonatomic, weak) SemanticsObject* semanticsObject; + +@end + +@interface SemanticsObject () +/** Should only be called in conjunction with setting child/parent relationship. */ +- (void)privateSetParent:(SemanticsObject*)parent; +@end + +@implementation SemanticsObject { + fml::scoped_nsobject _container; + NSMutableArray* _children; +} + +#pragma mark - Override base class designated initializers + +// Method declared as unavailable in the interface +- (instancetype)init { + [self release]; + [super doesNotRecognizeSelector:_cmd]; + return nil; +} + +#pragma mark - Designated initializers + +- (instancetype)initWithBridge:(fml::WeakPtr)bridge + uid:(int32_t)uid { + FML_DCHECK(bridge) << "bridge must be set"; + FML_DCHECK(uid >= kRootNodeId); + // Initialize with the UIView as the container. + // The UIView will not necessarily be accessibility parent for this object. + // The bridge informs the OS of the actual structure via + // `accessibilityContainer` and `accessibilityElementAtIndex`. + self = [super initWithAccessibilityContainer:bridge->view()]; + + if (self) { + _bridge = bridge; + _uid = uid; + _children = [[NSMutableArray alloc] init]; + } + + return self; +} + +- (void)dealloc { + for (SemanticsObject* child in _children) { + [child privateSetParent:nil]; + } + [_children removeAllObjects]; + [_children release]; + _parent = nil; + _container.get().semanticsObject = nil; + [_platformViewSemanticsContainer release]; + [super dealloc]; +} + +#pragma mark - Semantic object methods + +- (BOOL)isAccessibilityBridgeAlive { + return [self bridge].get() != nil; +} + +- (void)setSemanticsNode:(const flutter::SemanticsNode*)node { + _node = *node; +} + +/** + * Whether calling `setSemanticsNode:` with `node` would cause a layout change. + */ +- (BOOL)nodeWillCauseLayoutChange:(const flutter::SemanticsNode*)node { + return [self node].rect != node->rect || [self node].transform != node->transform; +} + +/** + * Whether calling `setSemanticsNode:` with `node` would cause a scroll event. + */ +- (BOOL)nodeWillCauseScroll:(const flutter::SemanticsNode*)node { + return !isnan([self node].scrollPosition) && !isnan(node->scrollPosition) && + [self node].scrollPosition != node->scrollPosition; +} + +- (BOOL)hasChildren { + if (_node.IsPlatformViewNode()) { + return YES; + } + return [self.children count] != 0; +} + +- (void)privateSetParent:(SemanticsObject*)parent { + _parent = parent; +} + +- (void)setChildren:(NSArray*)children { + for (SemanticsObject* child in _children) { + [child privateSetParent:nil]; + } + [_children release]; + _children = [[NSMutableArray alloc] initWithArray:children]; + for (SemanticsObject* child in _children) { + [child privateSetParent:self]; + } +} + +- (void)replaceChildAtIndex:(NSInteger)index withChild:(SemanticsObject*)child { + SemanticsObject* oldChild = _children[index]; + [oldChild privateSetParent:nil]; + [child privateSetParent:self]; + [_children replaceObjectAtIndex:index withObject:child]; +} + +#pragma mark - UIAccessibility overrides + +- (BOOL)isAccessibilityElement { + if (![self isAccessibilityBridgeAlive]) + return false; + + // Note: hit detection will only apply to elements that report + // -isAccessibilityElement of YES. The framework will continue scanning the + // entire element tree looking for such a hit. + + // We enforce in the framework that no other useful semantics are merged with these nodes. + if ([self node].HasFlag(flutter::SemanticsFlags::kScopesRoute)) + return false; + + // If the only flag(s) set are scrolling related AND + // The only flags set are not kIsHidden OR + // The node doesn't have a label, value, or hint OR + // The only actions set are scrolling related actions. + // + // The kIsHidden flag set with any other flag just means this node is now + // hidden but still is a valid target for a11y focus in the tree, e.g. a list + // item that is currently off screen but the a11y navigation needs to know + // about. + return (([self node].flags & ~flutter::kScrollableSemanticsFlags) != 0 && + [self node].flags != static_cast(flutter::SemanticsFlags::kIsHidden)) || + ![self node].label.empty() || ![self node].value.empty() || ![self node].hint.empty() || + ([self node].actions & ~flutter::kScrollableSemanticsActions) != 0; +} + +- (void)collectRoutes:(NSMutableArray*)edges { + if ([self node].HasFlag(flutter::SemanticsFlags::kScopesRoute)) + [edges addObject:self]; + if ([self hasChildren]) { + for (SemanticsObject* child in self.children) { + [child collectRoutes:edges]; + } + } +} + +- (BOOL)onCustomAccessibilityAction:(FlutterCustomAccessibilityAction*)action { + if (![self node].HasAction(flutter::SemanticsAction::kCustomAction)) + return NO; + int32_t action_id = action.uid; + std::vector args; + args.push_back(3); // type=int32. + args.push_back(action_id); + args.push_back(action_id >> 8); + args.push_back(action_id >> 16); + args.push_back(action_id >> 24); + [self bridge]->DispatchSemanticsAction([self uid], flutter::SemanticsAction::kCustomAction, + std::move(args)); + return YES; +} + +- (NSString*)routeName { + // Returns the first non-null and non-empty semantic label of a child + // with an NamesRoute flag. Otherwise returns nil. + if ([self node].HasFlag(flutter::SemanticsFlags::kNamesRoute)) { + NSString* newName = [self accessibilityLabel]; + if (newName != nil && [newName length] > 0) { + return newName; + } + } + if ([self hasChildren]) { + for (SemanticsObject* child in self.children) { + NSString* newName = [child routeName]; + if (newName != nil && [newName length] > 0) { + return newName; + } + } + } + return nil; +} + +- (NSString*)accessibilityLabel { + if (![self isAccessibilityBridgeAlive]) + return nil; + + if ([self node].label.empty()) + return nil; + return @([self node].label.data()); +} + +- (NSString*)accessibilityHint { + if (![self isAccessibilityBridgeAlive]) + return nil; + + if ([self node].hint.empty()) + return nil; + return @([self node].hint.data()); +} + +- (NSString*)accessibilityValue { + if (![self isAccessibilityBridgeAlive]) + return nil; + + if (![self node].value.empty()) { + return @([self node].value.data()); + } + + // FlutterSwitchSemanticsObject should supercede these conditionals. + if ([self node].HasFlag(flutter::SemanticsFlags::kHasToggledState) || + [self node].HasFlag(flutter::SemanticsFlags::kHasCheckedState)) { + if ([self node].HasFlag(flutter::SemanticsFlags::kIsToggled) || + [self node].HasFlag(flutter::SemanticsFlags::kIsChecked)) { + return @"1"; + } else { + return @"0"; + } + } + + return nil; +} + +- (CGRect)accessibilityFrame { + if (![self isAccessibilityBridgeAlive]) + return CGRectMake(0, 0, 0, 0); + + if ([self node].HasFlag(flutter::SemanticsFlags::kIsHidden)) { + return [super accessibilityFrame]; + } + return [self globalRect]; +} + +- (CGRect)globalRect { + SkMatrix44 globalTransform = [self node].transform; + for (SemanticsObject* parent = [self parent]; parent; parent = parent.parent) { + globalTransform = parent.node.transform * globalTransform; + } + + SkPoint quad[4]; + [self node].rect.toQuad(quad); + for (auto& point : quad) { + SkScalar vector[4] = {point.x(), point.y(), 0, 1}; + globalTransform.mapScalars(vector); + point.set(vector[0] / vector[3], vector[1] / vector[3]); + } + SkRect rect; + rect.setBounds(quad, 4); + + // `rect` is in the physical pixel coordinate system. iOS expects the accessibility frame in + // the logical pixel coordinate system. Therefore, we divide by the `scale` (pixel ratio) to + // convert. + CGFloat scale = [[[self bridge]->view() window] screen].scale; + auto result = + CGRectMake(rect.x() / scale, rect.y() / scale, rect.width() / scale, rect.height() / scale); + return UIAccessibilityConvertFrameToScreenCoordinates(result, [self bridge]->view()); +} + +#pragma mark - UIAccessibilityElement protocol + +- (id)accessibilityContainer { + if ([self hasChildren] || [self uid] == kRootNodeId) { + if (_container == nil) + _container.reset([[SemanticsObjectContainer alloc] initWithSemanticsObject:self + bridge:[self bridge]]); + return _container.get(); + } + if ([self parent] == nil) { + // This can happen when we have released the accessibility tree but iOS is + // still holding onto our objects. iOS can take some time before it + // realizes that the tree has changed. + return nil; + } + return [[self parent] accessibilityContainer]; +} + +#pragma mark - UIAccessibilityAction overrides + +- (BOOL)accessibilityActivate { + if (![self isAccessibilityBridgeAlive]) + return NO; + if (![self node].HasAction(flutter::SemanticsAction::kTap)) + return NO; + [self bridge]->DispatchSemanticsAction([self uid], flutter::SemanticsAction::kTap); + return YES; +} + +- (void)accessibilityIncrement { + if (![self isAccessibilityBridgeAlive]) + return; + if ([self node].HasAction(flutter::SemanticsAction::kIncrease)) { + [self node].value = [self node].increasedValue; + [self bridge]->DispatchSemanticsAction([self uid], flutter::SemanticsAction::kIncrease); + } +} + +- (void)accessibilityDecrement { + if (![self isAccessibilityBridgeAlive]) + return; + if ([self node].HasAction(flutter::SemanticsAction::kDecrease)) { + [self node].value = [self node].decreasedValue; + [self bridge]->DispatchSemanticsAction([self uid], flutter::SemanticsAction::kDecrease); + } +} + +- (BOOL)accessibilityScroll:(UIAccessibilityScrollDirection)direction { + if (![self isAccessibilityBridgeAlive]) + return NO; + flutter::SemanticsAction action = GetSemanticsActionForScrollDirection(direction); + if (![self node].HasAction(action)) + return NO; + [self bridge]->DispatchSemanticsAction([self uid], action); + return YES; +} + +- (BOOL)accessibilityPerformEscape { + if (![self isAccessibilityBridgeAlive]) + return NO; + if (![self node].HasAction(flutter::SemanticsAction::kDismiss)) + return NO; + [self bridge]->DispatchSemanticsAction([self uid], flutter::SemanticsAction::kDismiss); + return YES; +} + +#pragma mark UIAccessibilityFocus overrides + +- (void)accessibilityElementDidBecomeFocused { + if (![self isAccessibilityBridgeAlive]) + return; + if ([self node].HasFlag(flutter::SemanticsFlags::kIsHidden)) { + [self bridge]->DispatchSemanticsAction([self uid], flutter::SemanticsAction::kShowOnScreen); + } + if ([self node].HasAction(flutter::SemanticsAction::kDidGainAccessibilityFocus)) { + [self bridge]->DispatchSemanticsAction([self uid], + flutter::SemanticsAction::kDidGainAccessibilityFocus); + } +} + +- (void)accessibilityElementDidLoseFocus { + if (![self isAccessibilityBridgeAlive]) + return; + if ([self node].HasAction(flutter::SemanticsAction::kDidLoseAccessibilityFocus)) { + [self bridge]->DispatchSemanticsAction([self uid], + flutter::SemanticsAction::kDidLoseAccessibilityFocus); + } +} + +@end + +@implementation FlutterSemanticsObject { +} + +#pragma mark - Override base class designated initializers + +// Method declared as unavailable in the interface +- (instancetype)init { + [self release]; + [super doesNotRecognizeSelector:_cmd]; + return nil; +} + +#pragma mark - Designated initializers + +- (instancetype)initWithBridge:(fml::WeakPtr)bridge + uid:(int32_t)uid { + self = [super initWithBridge:bridge uid:uid]; + return self; +} + +#pragma mark - UIAccessibility overrides + +- (UIAccessibilityTraits)accessibilityTraits { + UIAccessibilityTraits traits = UIAccessibilityTraitNone; + if ([self node].HasAction(flutter::SemanticsAction::kIncrease) || + [self node].HasAction(flutter::SemanticsAction::kDecrease)) { + traits |= UIAccessibilityTraitAdjustable; + } + // FlutterSwitchSemanticsObject should supercede these conditionals. + if ([self node].HasFlag(flutter::SemanticsFlags::kHasToggledState) || + [self node].HasFlag(flutter::SemanticsFlags::kHasCheckedState)) { + traits |= UIAccessibilityTraitButton; + } + if ([self node].HasFlag(flutter::SemanticsFlags::kIsSelected)) { + traits |= UIAccessibilityTraitSelected; + } + if ([self node].HasFlag(flutter::SemanticsFlags::kIsButton)) { + traits |= UIAccessibilityTraitButton; + } + if ([self node].HasFlag(flutter::SemanticsFlags::kHasEnabledState) && + ![self node].HasFlag(flutter::SemanticsFlags::kIsEnabled)) { + traits |= UIAccessibilityTraitNotEnabled; + } + if ([self node].HasFlag(flutter::SemanticsFlags::kIsHeader)) { + traits |= UIAccessibilityTraitHeader; + } + if ([self node].HasFlag(flutter::SemanticsFlags::kIsImage)) { + traits |= UIAccessibilityTraitImage; + } + if ([self node].HasFlag(flutter::SemanticsFlags::kIsLiveRegion)) { + traits |= UIAccessibilityTraitUpdatesFrequently; + } + if ([self node].HasFlag(flutter::SemanticsFlags::kIsLink)) { + traits |= UIAccessibilityTraitLink; + } + return traits; +} + +@end + +@implementation FlutterPlatformViewSemanticsContainer { + SemanticsObject* _semanticsObject; + UIView* _platformView; +} + +// Method declared as unavailable in the interface +- (instancetype)init { + [self release]; + [super doesNotRecognizeSelector:_cmd]; + return nil; +} + +- (instancetype)initWithSemanticsObject:(SemanticsObject*)object { + FML_CHECK(object); + // Initialize with the UIView as the container. + // The UIView will not necessarily be accessibility parent for this object. + // The bridge informs the OS of the actual structure via + // `accessibilityContainer` and `accessibilityElementAtIndex`. + if (self = [super initWithAccessibilityContainer:object.bridge->view()]) { + _semanticsObject = object; + flutter::FlutterPlatformViewsController* controller = + object.bridge->GetPlatformViewsController(); + if (controller) { + _platformView = [controller->GetPlatformViewByID(object.node.platformViewId) view]; + } + self.accessibilityElements = @[ _semanticsObject, _platformView ]; + } + return self; +} + +- (CGRect)accessibilityFrame { + return _semanticsObject.accessibilityFrame; +} + +- (BOOL)isAccessibilityElement { + return NO; +} + +- (id)accessibilityContainer { + return [_semanticsObject accessibilityContainer]; +} + +- (BOOL)accessibilityScroll:(UIAccessibilityScrollDirection)direction { + return [_platformView accessibilityScroll:direction]; +} + +@end + +@implementation SemanticsObjectContainer { + SemanticsObject* _semanticsObject; + fml::WeakPtr _bridge; +} + +#pragma mark - initializers + +// Method declared as unavailable in the interface +- (instancetype)init { + [self release]; + [super doesNotRecognizeSelector:_cmd]; + return nil; +} + +- (instancetype)initWithSemanticsObject:(SemanticsObject*)semanticsObject + bridge:(fml::WeakPtr)bridge { + FML_DCHECK(semanticsObject) << "semanticsObject must be set"; + // Initialize with the UIView as the container. + // The UIView will not necessarily be accessibility parent for this object. + // The bridge informs the OS of the actual structure via + // `accessibilityContainer` and `accessibilityElementAtIndex`. + self = [super initWithAccessibilityContainer:bridge->view()]; + + if (self) { + _semanticsObject = semanticsObject; + _bridge = bridge; + } + + return self; +} + +#pragma mark - UIAccessibilityContainer overrides + +- (NSInteger)accessibilityElementCount { + NSInteger count = [[_semanticsObject children] count] + 1; + return count; +} + +- (nullable id)accessibilityElementAtIndex:(NSInteger)index { + if (index < 0 || index >= [self accessibilityElementCount]) + return nil; + if (index == 0) { + return _semanticsObject; + } + + SemanticsObject* child = [_semanticsObject children][index - 1]; + + // Swap the original `SemanticsObject` to a `PlatformViewSemanticsContainer` + if (child.node.IsPlatformViewNode()) { + child.platformViewSemanticsContainer.index = index; + return child.platformViewSemanticsContainer; + } + + if ([child hasChildren]) + return [child accessibilityContainer]; + return child; +} + +- (NSInteger)indexOfAccessibilityElement:(id)element { + if (element == _semanticsObject) + return 0; + + // FlutterPlatformViewSemanticsContainer is always the last element of its parent. + if ([element isKindOfClass:[FlutterPlatformViewSemanticsContainer class]]) { + return ((FlutterPlatformViewSemanticsContainer*)element).index; + } + + NSArray* children = [_semanticsObject children]; + for (size_t i = 0; i < [children count]; i++) { + SemanticsObject* child = children[i]; + if ((![child hasChildren] && child == element) || + ([child hasChildren] && [child accessibilityContainer] == element)) + return i + 1; + } + return NSNotFound; +} + +#pragma mark - UIAccessibilityElement protocol + +- (BOOL)isAccessibilityElement { + return NO; +} + +- (CGRect)accessibilityFrame { + return [_semanticsObject accessibilityFrame]; +} + +- (id)accessibilityContainer { + if (!_bridge) { + return nil; + } + return ([_semanticsObject uid] == kRootNodeId) + ? _bridge->view() + : [[_semanticsObject parent] accessibilityContainer]; +} + +#pragma mark - UIAccessibilityAction overrides + +- (BOOL)accessibilityScroll:(UIAccessibilityScrollDirection)direction { + return [_semanticsObject accessibilityScroll:direction]; +} + +@end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.h index 772c84eb5e..6b7d61fe1f 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.h @@ -20,145 +20,15 @@ #include "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h" #include "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h" #include "flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h" +#include "flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h" +#include "flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_ios.h" #include "third_party/skia/include/core/SkMatrix44.h" #include "third_party/skia/include/core/SkRect.h" -namespace flutter { -class AccessibilityBridge; -} // namespace flutter - -@class FlutterPlatformViewSemanticsContainer; - -/** - * A node in the iOS semantics tree. - */ -@interface SemanticsObject : UIAccessibilityElement - -/** - * The globally unique identifier for this node. - */ -@property(nonatomic, readonly) int32_t uid; - -/** - * The parent of this node in the node tree. Will be nil for the root node and - * during transient state changes. - */ -@property(nonatomic, readonly) SemanticsObject* parent; - -/** - * The accessibility bridge that this semantics object is attached to. This - * object may use the bridge to access contextual application information. A weak - * pointer is used because the platform view owns the accessibility bridge. - * If you are referencing this property from an iOS callback, be sure to - * use `isAccessibilityBridgeActive` to protect against the case where this - * node may be orphaned. - */ -@property(nonatomic, readonly) fml::WeakPtr bridge; - -/** - * Due to the fact that VoiceOver may hold onto SemanticObjects even after it shuts down, - * there can be situations where the AccessibilityBridge is shutdown, but the SemanticObject - * will still be alive. If VoiceOver is turned on again, it may try to access this orphaned - * SemanticObject. Methods that are called from the accessiblity framework should use - * this to guard against this case by just returning early if its bridge has been shutdown. - * - * See https://github.com/flutter/flutter/issues/43795 for more information. - */ -- (BOOL)isAccessibilityBridgeAlive; - -/** - * The semantics node used to produce this semantics object. - */ -@property(nonatomic, readonly) flutter::SemanticsNode node; - -/** - * Updates this semantics object using data from the `node` argument. - */ -- (void)setSemanticsNode:(const flutter::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. - */ -@property(nonatomic, strong) NSArray* children; - -/** - * Used if this SemanticsObject is for a platform view. - */ -@property(strong, nonatomic) FlutterPlatformViewSemanticsContainer* platformViewSemanticsContainer; - -- (void)replaceChildAtIndex:(NSInteger)index withChild:(SemanticsObject*)child; - -- (BOOL)nodeWillCauseLayoutChange:(const flutter::SemanticsNode*)node; - -#pragma mark - Designated initializers - -- (instancetype)init __attribute__((unavailable("Use initWithBridge instead"))); -- (instancetype)initWithBridge:(fml::WeakPtr)bridge - uid:(int32_t)uid NS_DESIGNATED_INITIALIZER; - -@end - -/** - * An implementation of UIAccessibilityCustomAction which also contains the - * Flutter uid. - */ -@interface FlutterCustomAccessibilityAction : UIAccessibilityCustomAction - -/** - * The uid of the action defined by the flutter application. - */ -@property(nonatomic) int32_t uid; - -@end - -/** - * 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 - -/** - * Designated to act as an accessibility container of a platform view. - * - * This object does not take any accessibility actions on its own, nor has any accessibility - * label/value/trait/hint... on its own. The accessibility data will be handled by the platform - * view. - * - * See also: - * * `SemanticsObject` for the other type of semantics objects. - * * `FlutterSemanticsObject` for default implementation of `SemanticsObject`. - */ -@interface FlutterPlatformViewSemanticsContainer : UIAccessibilityElement - -/** - * The position inside an accessibility container. - */ -@property(nonatomic) NSInteger index; - -- (instancetype)init __attribute__((unavailable("Use initWithAccessibilityContainer: instead"))); - -- (instancetype)initWithSemanticsObject:(SemanticsObject*)object; - -@end - namespace flutter { class PlatformViewIOS; -class AccessibilityBridge final { +class AccessibilityBridge final : public AccessibilityBridgeIos { public: AccessibilityBridge(UIView* view, PlatformViewIOS* platform_view, @@ -167,18 +37,18 @@ class AccessibilityBridge final { void UpdateSemantics(flutter::SemanticsNodeUpdates nodes, flutter::CustomAccessibilityActionUpdates actions); - void DispatchSemanticsAction(int32_t id, flutter::SemanticsAction action); + void DispatchSemanticsAction(int32_t id, flutter::SemanticsAction action) override; void DispatchSemanticsAction(int32_t id, flutter::SemanticsAction action, - std::vector args); + std::vector args) override; - UIView* textInputView(); + UIView* textInputView() override; - UIView* view() const { return view_; } + UIView* view() const override { return view_; } fml::WeakPtr GetWeakPtr(); - FlutterPlatformViewsController* GetPlatformViewsController() const { + FlutterPlatformViewsController* GetPlatformViewsController() const override { return platform_views_controller_; }; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm index 46224c1ea4..2957489fb4 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm @@ -5,723 +5,10 @@ #include "flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.h" #include "flutter/shell/platform/darwin/ios/framework/Source/accessibility_text_entry.h" -#include -#include - -#import - -#include "flutter/fml/logging.h" -#include "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h" #include "flutter/shell/platform/darwin/ios/platform_view_ios.h" FLUTTER_ASSERT_NOT_ARC -namespace { - -constexpr int32_t kRootNodeId = 0; - -flutter::SemanticsAction GetSemanticsActionForScrollDirection( - UIAccessibilityScrollDirection direction) { - // To describe the vertical scroll direction, UIAccessibilityScrollDirection uses the - // direction the scroll bar moves in and SemanticsAction uses the direction the finger - // moves in. However, the horizontal scroll direction matches the SemanticsAction direction. - // That is way the following maps vertical opposite of the SemanticsAction, but the horizontal - // maps directly. - switch (direction) { - case UIAccessibilityScrollDirectionRight: - case UIAccessibilityScrollDirectionPrevious: // TODO(abarth): Support RTL using - // _node.textDirection. - return flutter::SemanticsAction::kScrollRight; - case UIAccessibilityScrollDirectionLeft: - case UIAccessibilityScrollDirectionNext: // TODO(abarth): Support RTL using - // _node.textDirection. - return flutter::SemanticsAction::kScrollLeft; - case UIAccessibilityScrollDirectionUp: - return flutter::SemanticsAction::kScrollDown; - case UIAccessibilityScrollDirectionDown: - return flutter::SemanticsAction::kScrollUp; - } - FML_DCHECK(false); // Unreachable - return flutter::SemanticsAction::kScrollUp; -} - -} // namespace - -/// A proxy class for SemanticsObject and UISwitch. For most Accessibility and -/// SemanticsObject methods it delegates to the semantics object, otherwise it -/// sends messages to the UISwitch. -@interface FlutterSwitchSemanticsObject : UISwitch -@end - -@implementation FlutterSwitchSemanticsObject { - SemanticsObject* _semanticsObject; -} - -- (instancetype)initWithSemanticsObject:(SemanticsObject*)semanticsObject { - self = [super init]; - if (self) { - _semanticsObject = [semanticsObject retain]; - } - return self; -} - -- (void)dealloc { - [_semanticsObject release]; - [super dealloc]; -} - -- (NSMethodSignature*)methodSignatureForSelector:(SEL)sel { - NSMethodSignature* result = [super methodSignatureForSelector:sel]; - if (!result) { - result = [_semanticsObject methodSignatureForSelector:sel]; - } - return result; -} - -- (void)forwardInvocation:(NSInvocation*)anInvocation { - [anInvocation setTarget:_semanticsObject]; - [anInvocation invoke]; -} - -- (CGRect)accessibilityFrame { - return [_semanticsObject accessibilityFrame]; -} - -- (id)accessibilityContainer { - return [_semanticsObject accessibilityContainer]; -} - -- (NSString*)accessibilityLabel { - return [_semanticsObject accessibilityLabel]; -} - -- (NSString*)accessibilityHint { - return [_semanticsObject accessibilityHint]; -} - -- (NSString*)accessibilityValue { - if ([_semanticsObject node].HasFlag(flutter::SemanticsFlags::kIsToggled) || - [_semanticsObject node].HasFlag(flutter::SemanticsFlags::kIsChecked)) { - self.on = YES; - } else { - self.on = NO; - } - - if (![_semanticsObject isAccessibilityBridgeAlive]) { - return nil; - } else { - return [super accessibilityValue]; - } -} - -@end // FlutterSwitchSemanticsObject - -@implementation FlutterCustomAccessibilityAction { -} -@end - -/** - * Represents a semantics object that has children and hence has to be presented to the OS as a - * UIAccessibilityContainer. - * - * The SemanticsObject class cannot implement the UIAccessibilityContainer protocol because an - * object that returns YES for isAccessibilityElement cannot also implement - * UIAccessibilityContainer. - * - * With the help of SemanticsObjectContainer, the hierarchy of semantic objects received from - * the framework, such as: - * - * SemanticsObject1 - * SemanticsObject2 - * SemanticsObject3 - * SemanticsObject4 - * - * is translated into the following hierarchy, which is understood by iOS: - * - * SemanticsObjectContainer1 - * SemanticsObject1 - * SemanticsObjectContainer2 - * SemanticsObject2 - * SemanticsObject3 - * SemanticsObject4 - * - * From Flutter's view of the world (the first tree seen above), we construct iOS's view of the - * world (second tree) as follows: We replace each SemanticsObjects that has children with a - * SemanticsObjectContainer, which has the original SemanticsObject and its children as children. - * - * SemanticsObjects have semantic information attached to them which is interpreted by - * VoiceOver (they return YES for isAccessibilityElement). The SemanticsObjectContainers are just - * there for structure and they don't provide any semantic information to VoiceOver (they return - * NO for isAccessibilityElement). - */ -@interface SemanticsObjectContainer : UIAccessibilityElement -- (instancetype)init __attribute__((unavailable("Use initWithSemanticsObject instead"))); -- (instancetype)initWithSemanticsObject:(SemanticsObject*)semanticsObject - bridge:(fml::WeakPtr)bridge - NS_DESIGNATED_INITIALIZER; - -@property(nonatomic, weak) SemanticsObject* semanticsObject; - -@end - -@interface SemanticsObject () -/** Should only be called in conjunction with setting child/parent relationship. */ -- (void)privateSetParent:(SemanticsObject*)parent; -@end - -@implementation SemanticsObject { - fml::scoped_nsobject _container; - NSMutableArray* _children; -} - -#pragma mark - Override base class designated initializers - -// Method declared as unavailable in the interface -- (instancetype)init { - [self release]; - [super doesNotRecognizeSelector:_cmd]; - return nil; -} - -#pragma mark - Designated initializers - -- (instancetype)initWithBridge:(fml::WeakPtr)bridge uid:(int32_t)uid { - FML_DCHECK(bridge) << "bridge must be set"; - FML_DCHECK(uid >= kRootNodeId); - // Initialize with the UIView as the container. - // The UIView will not necessarily be accessibility parent for this object. - // The bridge informs the OS of the actual structure via - // `accessibilityContainer` and `accessibilityElementAtIndex`. - self = [super initWithAccessibilityContainer:bridge->view()]; - - if (self) { - _bridge = bridge; - _uid = uid; - _children = [[NSMutableArray alloc] init]; - } - - return self; -} - -- (void)dealloc { - for (SemanticsObject* child in _children) { - [child privateSetParent:nil]; - } - [_children removeAllObjects]; - [_children release]; - _parent = nil; - _container.get().semanticsObject = nil; - [_platformViewSemanticsContainer release]; - [super dealloc]; -} - -#pragma mark - Semantic object methods - -- (BOOL)isAccessibilityBridgeAlive { - return [self bridge].get() != nil; -} - -- (void)setSemanticsNode:(const flutter::SemanticsNode*)node { - _node = *node; -} - -/** - * Whether calling `setSemanticsNode:` with `node` would cause a layout change. - */ -- (BOOL)nodeWillCauseLayoutChange:(const flutter::SemanticsNode*)node { - return [self node].rect != node->rect || [self node].transform != node->transform; -} - -/** - * Whether calling `setSemanticsNode:` with `node` would cause a scroll event. - */ -- (BOOL)nodeWillCauseScroll:(const flutter::SemanticsNode*)node { - return !isnan([self node].scrollPosition) && !isnan(node->scrollPosition) && - [self node].scrollPosition != node->scrollPosition; -} - -- (BOOL)hasChildren { - if (_node.IsPlatformViewNode()) { - return YES; - } - return [self.children count] != 0; -} - -- (void)privateSetParent:(SemanticsObject*)parent { - _parent = parent; -} - -- (void)setChildren:(NSArray*)children { - for (SemanticsObject* child in _children) { - [child privateSetParent:nil]; - } - [_children release]; - _children = [[NSMutableArray alloc] initWithArray:children]; - for (SemanticsObject* child in _children) { - [child privateSetParent:self]; - } -} - -- (void)replaceChildAtIndex:(NSInteger)index withChild:(SemanticsObject*)child { - SemanticsObject* oldChild = _children[index]; - [oldChild privateSetParent:nil]; - [child privateSetParent:self]; - [_children replaceObjectAtIndex:index withObject:child]; -} - -#pragma mark - UIAccessibility overrides - -- (BOOL)isAccessibilityElement { - if (![self isAccessibilityBridgeAlive]) - return false; - - // Note: hit detection will only apply to elements that report - // -isAccessibilityElement of YES. The framework will continue scanning the - // entire element tree looking for such a hit. - - // We enforce in the framework that no other useful semantics are merged with these nodes. - if ([self node].HasFlag(flutter::SemanticsFlags::kScopesRoute)) - return false; - - // If the only flag(s) set are scrolling related AND - // The only flags set are not kIsHidden OR - // The node doesn't have a label, value, or hint OR - // The only actions set are scrolling related actions. - // - // The kIsHidden flag set with any other flag just means this node is now - // hidden but still is a valid target for a11y focus in the tree, e.g. a list - // item that is currently off screen but the a11y navigation needs to know - // about. - return (([self node].flags & ~flutter::kScrollableSemanticsFlags) != 0 && - [self node].flags != static_cast(flutter::SemanticsFlags::kIsHidden)) || - ![self node].label.empty() || ![self node].value.empty() || ![self node].hint.empty() || - ([self node].actions & ~flutter::kScrollableSemanticsActions) != 0; -} - -- (void)collectRoutes:(NSMutableArray*)edges { - if ([self node].HasFlag(flutter::SemanticsFlags::kScopesRoute)) - [edges addObject:self]; - if ([self hasChildren]) { - for (SemanticsObject* child in self.children) { - [child collectRoutes:edges]; - } - } -} - -- (BOOL)onCustomAccessibilityAction:(FlutterCustomAccessibilityAction*)action { - if (![self node].HasAction(flutter::SemanticsAction::kCustomAction)) - return NO; - int32_t action_id = action.uid; - std::vector args; - args.push_back(3); // type=int32. - args.push_back(action_id); - args.push_back(action_id >> 8); - args.push_back(action_id >> 16); - args.push_back(action_id >> 24); - [self bridge]->DispatchSemanticsAction([self uid], flutter::SemanticsAction::kCustomAction, - std::move(args)); - return YES; -} - -- (NSString*)routeName { - // Returns the first non-null and non-empty semantic label of a child - // with an NamesRoute flag. Otherwise returns nil. - if ([self node].HasFlag(flutter::SemanticsFlags::kNamesRoute)) { - NSString* newName = [self accessibilityLabel]; - if (newName != nil && [newName length] > 0) { - return newName; - } - } - if ([self hasChildren]) { - for (SemanticsObject* child in self.children) { - NSString* newName = [child routeName]; - if (newName != nil && [newName length] > 0) { - return newName; - } - } - } - return nil; -} - -- (NSString*)accessibilityLabel { - if (![self isAccessibilityBridgeAlive]) - return nil; - - if ([self node].label.empty()) - return nil; - return @([self node].label.data()); -} - -- (NSString*)accessibilityHint { - if (![self isAccessibilityBridgeAlive]) - return nil; - - if ([self node].hint.empty()) - return nil; - return @([self node].hint.data()); -} - -- (NSString*)accessibilityValue { - if (![self isAccessibilityBridgeAlive]) - return nil; - - if (![self node].value.empty()) { - return @([self node].value.data()); - } - - // FlutterSwitchSemanticsObject should supercede these conditionals. - if ([self node].HasFlag(flutter::SemanticsFlags::kHasToggledState) || - [self node].HasFlag(flutter::SemanticsFlags::kHasCheckedState)) { - if ([self node].HasFlag(flutter::SemanticsFlags::kIsToggled) || - [self node].HasFlag(flutter::SemanticsFlags::kIsChecked)) { - return @"1"; - } else { - return @"0"; - } - } - - return nil; -} - -- (CGRect)accessibilityFrame { - if (![self isAccessibilityBridgeAlive]) - return CGRectMake(0, 0, 0, 0); - - if ([self node].HasFlag(flutter::SemanticsFlags::kIsHidden)) { - return [super accessibilityFrame]; - } - return [self globalRect]; -} - -- (CGRect)globalRect { - SkMatrix44 globalTransform = [self node].transform; - for (SemanticsObject* parent = [self parent]; parent; parent = parent.parent) { - globalTransform = parent.node.transform * globalTransform; - } - - SkPoint quad[4]; - [self node].rect.toQuad(quad); - for (auto& point : quad) { - SkScalar vector[4] = {point.x(), point.y(), 0, 1}; - globalTransform.mapScalars(vector); - point.set(vector[0] / vector[3], vector[1] / vector[3]); - } - SkRect rect; - rect.setBounds(quad, 4); - - // `rect` is in the physical pixel coordinate system. iOS expects the accessibility frame in - // the logical pixel coordinate system. Therefore, we divide by the `scale` (pixel ratio) to - // convert. - CGFloat scale = [[[self bridge]->view() window] screen].scale; - auto result = - CGRectMake(rect.x() / scale, rect.y() / scale, rect.width() / scale, rect.height() / scale); - return UIAccessibilityConvertFrameToScreenCoordinates(result, [self bridge]->view()); -} - -#pragma mark - UIAccessibilityElement protocol - -- (id)accessibilityContainer { - if ([self hasChildren] || [self uid] == kRootNodeId) { - if (_container == nil) - _container.reset([[SemanticsObjectContainer alloc] initWithSemanticsObject:self - bridge:[self bridge]]); - return _container.get(); - } - if ([self parent] == nil) { - // This can happen when we have released the accessibility tree but iOS is - // still holding onto our objects. iOS can take some time before it - // realizes that the tree has changed. - return nil; - } - return [[self parent] accessibilityContainer]; -} - -#pragma mark - UIAccessibilityAction overrides - -- (BOOL)accessibilityActivate { - if (![self isAccessibilityBridgeAlive]) - return NO; - if (![self node].HasAction(flutter::SemanticsAction::kTap)) - return NO; - [self bridge]->DispatchSemanticsAction([self uid], flutter::SemanticsAction::kTap); - return YES; -} - -- (void)accessibilityIncrement { - if (![self isAccessibilityBridgeAlive]) - return; - if ([self node].HasAction(flutter::SemanticsAction::kIncrease)) { - [self node].value = [self node].increasedValue; - [self bridge]->DispatchSemanticsAction([self uid], flutter::SemanticsAction::kIncrease); - } -} - -- (void)accessibilityDecrement { - if (![self isAccessibilityBridgeAlive]) - return; - if ([self node].HasAction(flutter::SemanticsAction::kDecrease)) { - [self node].value = [self node].decreasedValue; - [self bridge]->DispatchSemanticsAction([self uid], flutter::SemanticsAction::kDecrease); - } -} - -- (BOOL)accessibilityScroll:(UIAccessibilityScrollDirection)direction { - if (![self isAccessibilityBridgeAlive]) - return NO; - flutter::SemanticsAction action = GetSemanticsActionForScrollDirection(direction); - if (![self node].HasAction(action)) - return NO; - [self bridge]->DispatchSemanticsAction([self uid], action); - return YES; -} - -- (BOOL)accessibilityPerformEscape { - if (![self isAccessibilityBridgeAlive]) - return NO; - if (![self node].HasAction(flutter::SemanticsAction::kDismiss)) - return NO; - [self bridge]->DispatchSemanticsAction([self uid], flutter::SemanticsAction::kDismiss); - return YES; -} - -#pragma mark UIAccessibilityFocus overrides - -- (void)accessibilityElementDidBecomeFocused { - if (![self isAccessibilityBridgeAlive]) - return; - if ([self node].HasFlag(flutter::SemanticsFlags::kIsHidden)) { - [self bridge]->DispatchSemanticsAction([self uid], flutter::SemanticsAction::kShowOnScreen); - } - if ([self node].HasAction(flutter::SemanticsAction::kDidGainAccessibilityFocus)) { - [self bridge]->DispatchSemanticsAction([self uid], - flutter::SemanticsAction::kDidGainAccessibilityFocus); - } -} - -- (void)accessibilityElementDidLoseFocus { - if (![self isAccessibilityBridgeAlive]) - return; - if ([self node].HasAction(flutter::SemanticsAction::kDidLoseAccessibilityFocus)) { - [self bridge]->DispatchSemanticsAction([self uid], - flutter::SemanticsAction::kDidLoseAccessibilityFocus); - } -} - -@end - -@implementation FlutterSemanticsObject { -} - -#pragma mark - Override base class designated initializers - -// Method declared as unavailable in the interface -- (instancetype)init { - [self release]; - [super doesNotRecognizeSelector:_cmd]; - return nil; -} - -#pragma mark - Designated initializers - -- (instancetype)initWithBridge:(fml::WeakPtr)bridge uid:(int32_t)uid { - self = [super initWithBridge:bridge uid:uid]; - return self; -} - -#pragma mark - UIAccessibility overrides - -- (UIAccessibilityTraits)accessibilityTraits { - UIAccessibilityTraits traits = UIAccessibilityTraitNone; - if ([self node].HasAction(flutter::SemanticsAction::kIncrease) || - [self node].HasAction(flutter::SemanticsAction::kDecrease)) { - traits |= UIAccessibilityTraitAdjustable; - } - // FlutterSwitchSemanticsObject should supercede these conditionals. - if ([self node].HasFlag(flutter::SemanticsFlags::kHasToggledState) || - [self node].HasFlag(flutter::SemanticsFlags::kHasCheckedState)) { - traits |= UIAccessibilityTraitButton; - } - if ([self node].HasFlag(flutter::SemanticsFlags::kIsSelected)) { - traits |= UIAccessibilityTraitSelected; - } - if ([self node].HasFlag(flutter::SemanticsFlags::kIsButton)) { - traits |= UIAccessibilityTraitButton; - } - if ([self node].HasFlag(flutter::SemanticsFlags::kHasEnabledState) && - ![self node].HasFlag(flutter::SemanticsFlags::kIsEnabled)) { - traits |= UIAccessibilityTraitNotEnabled; - } - if ([self node].HasFlag(flutter::SemanticsFlags::kIsHeader)) { - traits |= UIAccessibilityTraitHeader; - } - if ([self node].HasFlag(flutter::SemanticsFlags::kIsImage)) { - traits |= UIAccessibilityTraitImage; - } - if ([self node].HasFlag(flutter::SemanticsFlags::kIsLiveRegion)) { - traits |= UIAccessibilityTraitUpdatesFrequently; - } - if ([self node].HasFlag(flutter::SemanticsFlags::kIsLink)) { - traits |= UIAccessibilityTraitLink; - } - return traits; -} - -@end - -@implementation FlutterPlatformViewSemanticsContainer { - SemanticsObject* _semanticsObject; - UIView* _platformView; -} - -// Method declared as unavailable in the interface -- (instancetype)init { - [self release]; - [super doesNotRecognizeSelector:_cmd]; - return nil; -} - -- (instancetype)initWithSemanticsObject:(SemanticsObject*)object { - FML_CHECK(object); - // Initialize with the UIView as the container. - // The UIView will not necessarily be accessibility parent for this object. - // The bridge informs the OS of the actual structure via - // `accessibilityContainer` and `accessibilityElementAtIndex`. - if (self = [super initWithAccessibilityContainer:object.bridge->view()]) { - _semanticsObject = object; - flutter::FlutterPlatformViewsController* controller = - object.bridge->GetPlatformViewsController(); - if (controller) { - _platformView = [controller->GetPlatformViewByID(object.node.platformViewId) view]; - } - self.accessibilityElements = @[ _semanticsObject, _platformView ]; - } - return self; -} - -- (CGRect)accessibilityFrame { - return _semanticsObject.accessibilityFrame; -} - -- (BOOL)isAccessibilityElement { - return NO; -} - -- (id)accessibilityContainer { - return [_semanticsObject accessibilityContainer]; -} - -- (BOOL)accessibilityScroll:(UIAccessibilityScrollDirection)direction { - return [_platformView accessibilityScroll:direction]; -} - -@end - -@implementation SemanticsObjectContainer { - SemanticsObject* _semanticsObject; - fml::WeakPtr _bridge; -} - -#pragma mark - initializers - -// Method declared as unavailable in the interface -- (instancetype)init { - [self release]; - [super doesNotRecognizeSelector:_cmd]; - return nil; -} - -- (instancetype)initWithSemanticsObject:(SemanticsObject*)semanticsObject - bridge:(fml::WeakPtr)bridge { - FML_DCHECK(semanticsObject) << "semanticsObject must be set"; - // Initialize with the UIView as the container. - // The UIView will not necessarily be accessibility parent for this object. - // The bridge informs the OS of the actual structure via - // `accessibilityContainer` and `accessibilityElementAtIndex`. - self = [super initWithAccessibilityContainer:bridge->view()]; - - if (self) { - _semanticsObject = semanticsObject; - _bridge = bridge; - } - - return self; -} - -#pragma mark - UIAccessibilityContainer overrides - -- (NSInteger)accessibilityElementCount { - NSInteger count = [[_semanticsObject children] count] + 1; - return count; -} - -- (nullable id)accessibilityElementAtIndex:(NSInteger)index { - if (index < 0 || index >= [self accessibilityElementCount]) - return nil; - if (index == 0) { - return _semanticsObject; - } - - SemanticsObject* child = [_semanticsObject children][index - 1]; - - // Swap the original `SemanticsObject` to a `PlatformViewSemanticsContainer` - if (child.node.IsPlatformViewNode()) { - child.platformViewSemanticsContainer.index = index; - return child.platformViewSemanticsContainer; - } - - if ([child hasChildren]) - return [child accessibilityContainer]; - return child; -} - -- (NSInteger)indexOfAccessibilityElement:(id)element { - if (element == _semanticsObject) - return 0; - - // FlutterPlatformViewSemanticsContainer is always the last element of its parent. - if ([element isKindOfClass:[FlutterPlatformViewSemanticsContainer class]]) { - return ((FlutterPlatformViewSemanticsContainer*)element).index; - } - - NSArray* children = [_semanticsObject children]; - for (size_t i = 0; i < [children count]; i++) { - SemanticsObject* child = children[i]; - if ((![child hasChildren] && child == element) || - ([child hasChildren] && [child accessibilityContainer] == element)) - return i + 1; - } - return NSNotFound; -} - -#pragma mark - UIAccessibilityElement protocol - -- (BOOL)isAccessibilityElement { - return NO; -} - -- (CGRect)accessibilityFrame { - return [_semanticsObject accessibilityFrame]; -} - -- (id)accessibilityContainer { - if (!_bridge) { - return nil; - } - return ([_semanticsObject uid] == kRootNodeId) - ? _bridge->view() - : [[_semanticsObject parent] accessibilityContainer]; -} - -#pragma mark - UIAccessibilityAction overrides - -- (BOOL)accessibilityScroll:(UIAccessibilityScrollDirection)direction { - return [_semanticsObject accessibilityScroll:direction]; -} - -@end - -#pragma mark - AccessibilityBridge impl - namespace flutter { AccessibilityBridge::AccessibilityBridge(UIView* view, diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_ios.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_ios.h new file mode 100644 index 0000000000..c2546ac7c3 --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_ios.h @@ -0,0 +1,32 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_ACCESSIBILITY_BRIDGE_IOS_H_ +#define SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_ACCESSIBILITY_BRIDGE_IOS_H_ + +#include + +#include "flutter/lib/ui/semantics/semantics_node.h" + +@class UIView; + +namespace flutter { +class FlutterPlatformViewsController; + +/// Interface that represents an accessibility bridge for iOS. +class AccessibilityBridgeIos { + public: + virtual ~AccessibilityBridgeIos() = default; + virtual UIView* view() const = 0; + virtual UIView* textInputView() = 0; + virtual void DispatchSemanticsAction(int32_t id, flutter::SemanticsAction action) = 0; + virtual void DispatchSemanticsAction(int32_t id, + flutter::SemanticsAction action, + std::vector args) = 0; + virtual FlutterPlatformViewsController* GetPlatformViewsController() const = 0; +}; + +} // namespace flutter + +#endif // SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_ACCESSIBILITY_BRIDGE_IOS_H_ diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_text_entry.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_text_entry.mm index 1c29941892..f39f19174d 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_text_entry.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_text_entry.mm @@ -177,7 +177,8 @@ static const UIAccessibilityTraits UIAccessibilityTraitUndocumentedEmptyLine = 0 FlutterInactiveTextInput* _inactive_text_input; } -- (instancetype)initWithBridge:(fml::WeakPtr)bridge uid:(int32_t)uid { +- (instancetype)initWithBridge:(fml::WeakPtr)bridge + uid:(int32_t)uid { self = [super initWithBridge:bridge uid:uid]; if (self) {