diff --git a/engine/src/flutter/lib/ui/semantics/semantics_node.h b/engine/src/flutter/lib/ui/semantics/semantics_node.h index 1d798d66e3..b75d94eb5a 100644 --- a/engine/src/flutter/lib/ui/semantics/semantics_node.h +++ b/engine/src/flutter/lib/ui/semantics/semantics_node.h @@ -44,12 +44,17 @@ enum class SemanticsAction : int32_t { kSetText = 1 << 21, }; -const int kScrollableSemanticsActions = - static_cast(SemanticsAction::kScrollLeft) | - static_cast(SemanticsAction::kScrollRight) | +const int kVerticalScrollSemanticsActions = static_cast(SemanticsAction::kScrollUp) | static_cast(SemanticsAction::kScrollDown); +const int kHorizontalScrollSemanticsActions = + static_cast(SemanticsAction::kScrollLeft) | + static_cast(SemanticsAction::kScrollRight); + +const int kScrollableSemanticsActions = + kVerticalScrollSemanticsActions | kHorizontalScrollSemanticsActions; + /// C/C++ representation of `SemanticsFlags` defined in /// `lib/ui/semantics.dart`. ///\warning This must match the `SemanticsFlags` enum in 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 index 15a77c3efe..9093f49355 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h @@ -31,7 +31,7 @@ constexpr int32_t kRootNodeId = 0; * 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; +@property(nonatomic, assign) SemanticsObject* parent; /** * The accessibility bridge that this semantics object is attached to. This @@ -94,6 +94,14 @@ constexpr int32_t kRootNodeId = 0; - (BOOL)onCustomAccessibilityAction:(FlutterCustomAccessibilityAction*)action; +/** + * Called after accessibility bridge finishes a semantics update. + * + * Subclasses can override this method if they contain states that can only be + * updated once every node in the accessibility tree has finished updating. + */ +- (void)accessibilityBridgeDidFinishUpdate; + #pragma mark - Designated initializers - (instancetype)init __attribute__((unavailable("Use initWithBridge instead"))); @@ -159,6 +167,18 @@ constexpr int32_t kRootNodeId = 0; @end +/// The semantics object for scrollable. This class creates an UIScrollView to interact with the +/// iOS. +@interface FlutterScrollableSemanticsObject : UIScrollView + +- (instancetype)init NS_UNAVAILABLE; +- (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE; +- (instancetype)initWithCoder:(NSCoder*)coder NS_UNAVAILABLE; +- (instancetype)initWithSemanticsObject:(SemanticsObject*)semanticsObject NS_DESIGNATED_INITIALIZER; +- (void)accessibilityBridgeDidFinishUpdate; + +@end + /** * Represents a semantics object that has children and hence has to be presented to the OS as a * UIAccessibilityContainer. 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 index a0748e37c8..2a99b8e752 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm @@ -34,6 +34,58 @@ flutter::SemanticsAction GetSemanticsActionForScrollDirection( return flutter::SemanticsAction::kScrollUp; } +SkM44 GetGlobalTransform(SemanticsObject* reference) { + SkM44 globalTransform = [reference node].transform; + for (SemanticsObject* parent = [reference parent]; parent; parent = parent.parent) { + globalTransform = parent.node.transform * globalTransform; + } + return globalTransform; +} + +SkPoint ApplyTransform(SkPoint& point, const SkM44& transform) { + SkV4 vector = transform.map(point.x(), point.y(), 0, 1); + return SkPoint::Make(vector.x / vector.w, vector.y / vector.w); +} + +CGPoint ConvertPointToGlobal(SemanticsObject* reference, CGPoint local_point) { + SkM44 globalTransform = GetGlobalTransform(reference); + SkPoint point = SkPoint::Make(local_point.x, local_point.y); + point = ApplyTransform(point, globalTransform); + // `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 = [[[reference bridge]->view() window] screen].scale; + auto result = CGPointMake(point.x() / scale, point.y() / scale); + return [[reference bridge]->view() convertPoint:result toView:nil]; +} + +CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { + SkM44 globalTransform = GetGlobalTransform(reference); + + SkPoint quad[4] = { + SkPoint::Make(local_rect.origin.x, local_rect.origin.y), // top left + SkPoint::Make(local_rect.origin.x + local_rect.size.width, local_rect.origin.y), // top right + SkPoint::Make(local_rect.origin.x + local_rect.size.width, + local_rect.origin.y + local_rect.size.height), // bottom right + SkPoint::Make(local_rect.origin.x, + local_rect.origin.y + local_rect.size.height) // bottom left + }; + for (auto& point : quad) { + point = ApplyTransform(point, globalTransform); + } + SkRect rect; + NSCAssert(rect.setBoundsCheck(quad, 4), @"Transformed points can't form a 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 = [[[reference bridge]->view() window] screen].scale; + auto result = + CGRectMake(rect.x() / scale, rect.y() / scale, rect.width() / scale, rect.height() / scale); + return UIAccessibilityConvertFrameToScreenCoordinates(result, [reference bridge]->view()); +} + } // namespace @implementation FlutterSwitchSemanticsObject { @@ -88,6 +140,152 @@ flutter::SemanticsAction GetSemanticsActionForScrollDirection( @end // FlutterSwitchSemanticsObject +@interface FlutterScrollableSemanticsObject () +@property(nonatomic, strong) SemanticsObject* semanticsObject; +@end + +@implementation FlutterScrollableSemanticsObject { + fml::scoped_nsobject _container; +} + +- (instancetype)initWithSemanticsObject:(SemanticsObject*)semanticsObject { + self = [super initWithFrame:CGRectZero]; + if (self) { + _semanticsObject = [semanticsObject retain]; + [semanticsObject.bridge->view() addSubview:self]; + } + return self; +} + +- (void)dealloc { + _container.get().semanticsObject = nil; + [_semanticsObject release]; + [self removeFromSuperview]; + [super dealloc]; +} + +- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event { + return nil; +} + +- (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]; +} + +- (void)accessibilityBridgeDidFinishUpdate { + // In order to make iOS think this UIScrollView is scrollable, the following + // requirements must be true. + // 1. contentSize must be bigger than the frame size. + // 2. The scrollable isAccessibilityElement must return YES + // + // Once the requirements are met, the iOS uses contentOffset to determine + // what scroll actions are available. e.g. If the view scrolls vertically and + // contentOffset is 0.0, only the scroll down action is available. + [self setFrame:[_semanticsObject accessibilityFrame]]; + [self setContentSize:[self contentSizeInternal]]; + [self setContentOffset:[self contentOffsetInternal] animated:NO]; + if (self.contentSize.width > self.frame.size.width || + self.contentSize.height > self.frame.size.height) { + self.isAccessibilityElement = YES; + } else { + self.isAccessibilityElement = NO; + ; + } +} + +- (void)setChildren:(NSArray*)children { + [_semanticsObject setChildren:children]; + // The children's parent is pointing to _semanticsObject, need to manually + // set it this object. + for (SemanticsObject* child in _semanticsObject.children) { + child.parent = (SemanticsObject*)self; + } +} + +- (id)accessibilityContainer { + if (_container == nil) { + _container.reset([[SemanticsObjectContainer alloc] + initWithSemanticsObject:(SemanticsObject*)self + bridge:[_semanticsObject bridge]]); + } + return _container.get(); +} + +// private methods + +- (CGSize)contentSizeInternal { + CGRect result; + const SkRect& rect = _semanticsObject.node.rect; + if (_semanticsObject.node.actions & flutter::kVerticalScrollSemanticsActions) { + result = CGRectMake(rect.x(), rect.y(), rect.width(), + rect.height() + _semanticsObject.node.scrollExtentMax); + } else if (_semanticsObject.node.actions & flutter::kHorizontalScrollSemanticsActions) { + result = CGRectMake(rect.x(), rect.y(), rect.width() + _semanticsObject.node.scrollExtentMax, + rect.height()); + } else { + result = CGRectMake(rect.x(), rect.y(), rect.width(), rect.height()); + } + return ConvertRectToGlobal(_semanticsObject, result).size; +} + +- (CGPoint)contentOffsetInternal { + CGPoint result; + CGPoint origin = self.frame.origin; + const SkRect& rect = _semanticsObject.node.rect; + if (_semanticsObject.node.actions & flutter::kVerticalScrollSemanticsActions) { + result = ConvertPointToGlobal( + _semanticsObject, CGPointMake(rect.x(), rect.y() + _semanticsObject.node.scrollPosition)); + } else if (_semanticsObject.node.actions & flutter::kHorizontalScrollSemanticsActions) { + result = ConvertPointToGlobal( + _semanticsObject, CGPointMake(rect.x() + _semanticsObject.node.scrollPosition, rect.y())); + } else { + result = origin; + } + return CGPointMake(result.x - origin.x, result.y - origin.y); +} + +// The following methods are explicitly forwarded to the wrapped SemanticsObject because the +// forwarding logic above doesn't apply to them since they are also implemented in the +// UIScrollView class, the base class. + +- (BOOL)accessibilityActivate { + return [_semanticsObject accessibilityActivate]; +} + +- (void)accessibilityIncrement { + [_semanticsObject accessibilityIncrement]; +} + +- (void)accessibilityDecrement { + [_semanticsObject accessibilityDecrement]; +} + +- (BOOL)accessibilityScroll:(UIAccessibilityScrollDirection)direction { + return [_semanticsObject accessibilityScroll:direction]; +} + +- (BOOL)accessibilityPerformEscape { + return [_semanticsObject accessibilityPerformEscape]; +} + +- (void)accessibilityElementDidBecomeFocused { + [_semanticsObject accessibilityElementDidBecomeFocused]; +} + +- (void)accessibilityElementDidLoseFocus { + [_semanticsObject accessibilityElementDidLoseFocus]; +} +@end // FlutterScrollableSemanticsObject + @implementation FlutterCustomAccessibilityAction { } @end @@ -174,6 +372,9 @@ flutter::SemanticsAction GetSemanticsActionForScrollDirection( _node = *node; } +- (void)accessibilityBridgeDidFinishUpdate { /* Do nothing by default */ +} + /** * Whether calling `setSemanticsNode:` with `node` would cause a layout change. */ @@ -398,27 +599,9 @@ flutter::SemanticsAction GetSemanticsActionForScrollDirection( } - (CGRect)globalRect { - SkM44 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) { - SkV4 vector = globalTransform.map(point.x(), point.y(), 0, 1); - point.set(vector.x / vector.w, vector.y / vector.w); - } - 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()); + const SkRect& rect = [self node].rect; + CGRect localRect = CGRectMake(rect.x(), rect.y(), rect.width(), rect.height()); + return ConvertRectToGlobal(self, localRect); } #pragma mark - UIAccessibilityElement protocol diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm index 1dcf8371d6..6272357a29 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm @@ -10,6 +10,8 @@ FLUTTER_ASSERT_ARC +const CGRect kScreenSize = CGRectMake(0, 0, 600, 800); + namespace flutter { namespace { @@ -24,7 +26,11 @@ class SemanticsActionObservation { class MockAccessibilityBridge : public AccessibilityBridgeIos { public: - MockAccessibilityBridge() : observations({}) { view_ = [[UIView alloc] init]; } + MockAccessibilityBridge() : observations({}) { + view_ = [[UIView alloc] initWithFrame:kScreenSize]; + window_ = [[UIWindow alloc] initWithFrame:kScreenSize]; + [window_ addSubview:view_]; + } UIView* view() const override { return view_; } UIView* textInputView() override { return nil; } void DispatchSemanticsAction(int32_t id, SemanticsAction action) override { @@ -46,6 +52,7 @@ class MockAccessibilityBridge : public AccessibilityBridgeIos { private: UIView* view_; + UIWindow* window_; }; } // namespace } // namespace flutter @@ -160,6 +167,105 @@ class MockAccessibilityBridge : public AccessibilityBridgeIos { XCTAssertEqual([object accessibilityTraits], UIAccessibilityTraitButton); } +- (void)testVerticalFlutterScrollableSemanticsObject { + fml::WeakPtrFactory factory( + new flutter::MockAccessibilityBridge()); + fml::WeakPtr bridge = factory.GetWeakPtr(); + + float transformScale = 0.5f; + float screenScale = [[bridge->view() window] screen].scale; + float effectivelyScale = transformScale / screenScale; + float x = 10; + float y = 10; + float w = 100; + float h = 200; + float scrollExtentMax = 500.0; + float scrollPosition = 150.0; + + flutter::SemanticsNode node; + node.flags = static_cast(flutter::SemanticsFlags::kHasImplicitScrolling); + node.actions = flutter::kVerticalScrollSemanticsActions; + node.rect = SkRect::MakeXYWH(x, y, w, h); + node.scrollExtentMax = scrollExtentMax; + node.scrollPosition = scrollPosition; + node.transform = { + transformScale, 0, 0, 0, 0, transformScale, 0, 0, 0, 0, transformScale, 0, 0, 0, 0, 1.0}; + FlutterSemanticsObject* delegate = [[FlutterSemanticsObject alloc] initWithBridge:bridge uid:0]; + FlutterScrollableSemanticsObject* scrollable = + [[FlutterScrollableSemanticsObject alloc] initWithSemanticsObject:delegate]; + SemanticsObject* scrollable_object = static_cast(scrollable); + [scrollable_object setSemanticsNode:&node]; + [scrollable_object accessibilityBridgeDidFinishUpdate]; + XCTAssertTrue( + CGRectEqualToRect(scrollable.frame, CGRectMake(x * effectivelyScale, y * effectivelyScale, + w * effectivelyScale, h * effectivelyScale))); + XCTAssertTrue(CGSizeEqualToSize( + scrollable.contentSize, + CGSizeMake(w * effectivelyScale, (h + scrollExtentMax) * effectivelyScale))); + XCTAssertTrue(CGPointEqualToPoint(scrollable.contentOffset, + CGPointMake(0, scrollPosition * effectivelyScale))); +} + +- (void)testHorizontalFlutterScrollableSemanticsObject { + fml::WeakPtrFactory factory( + new flutter::MockAccessibilityBridge()); + fml::WeakPtr bridge = factory.GetWeakPtr(); + + float transformScale = 0.5f; + float screenScale = [[bridge->view() window] screen].scale; + float effectivelyScale = transformScale / screenScale; + float x = 10; + float y = 10; + float w = 100; + float h = 200; + float scrollExtentMax = 500.0; + float scrollPosition = 150.0; + + flutter::SemanticsNode node; + node.flags = static_cast(flutter::SemanticsFlags::kHasImplicitScrolling); + node.actions = flutter::kHorizontalScrollSemanticsActions; + node.rect = SkRect::MakeXYWH(x, y, w, h); + node.scrollExtentMax = scrollExtentMax; + node.scrollPosition = scrollPosition; + node.transform = { + transformScale, 0, 0, 0, 0, transformScale, 0, 0, 0, 0, transformScale, 0, 0, 0, 0, 1.0}; + FlutterSemanticsObject* delegate = [[FlutterSemanticsObject alloc] initWithBridge:bridge uid:0]; + FlutterScrollableSemanticsObject* scrollable = + [[FlutterScrollableSemanticsObject alloc] initWithSemanticsObject:delegate]; + SemanticsObject* scrollable_object = static_cast(scrollable); + [scrollable_object setSemanticsNode:&node]; + [scrollable_object accessibilityBridgeDidFinishUpdate]; + XCTAssertTrue( + CGRectEqualToRect(scrollable.frame, CGRectMake(x * effectivelyScale, y * effectivelyScale, + w * effectivelyScale, h * effectivelyScale))); + XCTAssertTrue(CGSizeEqualToSize( + scrollable.contentSize, + CGSizeMake((w + scrollExtentMax) * effectivelyScale, h * effectivelyScale))); + XCTAssertTrue(CGPointEqualToPoint(scrollable.contentOffset, + CGPointMake(scrollPosition * effectivelyScale, 0))); +} + +- (void)testFlutterScrollableSemanticsObjectIsNotHittestable { + fml::WeakPtrFactory factory( + new flutter::MockAccessibilityBridge()); + fml::WeakPtr bridge = factory.GetWeakPtr(); + + flutter::SemanticsNode node; + node.flags = static_cast(flutter::SemanticsFlags::kHasImplicitScrolling); + node.actions = flutter::kHorizontalScrollSemanticsActions; + node.rect = SkRect::MakeXYWH(0, 0, 100, 200); + node.scrollExtentMax = 100.0; + node.scrollPosition = 0.0; + + FlutterSemanticsObject* delegate = [[FlutterSemanticsObject alloc] initWithBridge:bridge uid:0]; + FlutterScrollableSemanticsObject* scrollable = + [[FlutterScrollableSemanticsObject alloc] initWithSemanticsObject:delegate]; + SemanticsObject* scrollable_object = static_cast(scrollable); + [scrollable_object setSemanticsNode:&node]; + [scrollable_object accessibilityBridgeDidFinishUpdate]; + XCTAssertEqual([scrollable hitTest:CGPointMake(10, 10) withEvent:nil], nil); +} + - (void)testSemanticsObjectBuildsAttributedString { fml::WeakPtrFactory factory( new flutter::MockAccessibilityBridge()); 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 6b0c0f5cb1..073d1d2239 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 @@ -200,11 +200,16 @@ void AccessibilityBridge::UpdateSemantics(flutter::SemanticsNodeUpdates nodes, view_controller_.view.accessibilityElements = nil; } - NSMutableArray* doomed_uids = [NSMutableArray arrayWithArray:[objects_.get() allKeys]]; - if (root) + NSMutableArray* doomed_uids = [NSMutableArray arrayWithArray:[objects_ allKeys]]; + if (root) { VisitObjectsRecursivelyAndRemove(root, doomed_uids); + } [objects_ removeObjectsForKeys:doomed_uids]; + for (SemanticsObject* object in [objects_ allValues]) { + [object accessibilityBridgeDidFinishUpdate]; + } + if (!ios_delegate_->IsFlutterViewControllerPresentingModalViewController(view_controller_)) { layoutChanged = layoutChanged || [doomed_uids count] > 0; @@ -258,6 +263,11 @@ static SemanticsObject* CreateObject(const flutter::SemanticsNode& node, } else if (node.HasFlag(flutter::SemanticsFlags::kHasToggledState) || node.HasFlag(flutter::SemanticsFlags::kHasCheckedState)) { return [[[FlutterSwitchSemanticsObject alloc] initWithBridge:weak_ptr uid:node.id] autorelease]; + } else if (node.HasFlag(flutter::SemanticsFlags::kHasImplicitScrolling)) { + SemanticsObject* delegateObject = + [[[FlutterSemanticsObject alloc] initWithBridge:weak_ptr uid:node.id] autorelease]; + return (SemanticsObject*)[[[FlutterScrollableSemanticsObject alloc] + initWithSemanticsObject:delegateObject] autorelease]; } else { return [[[FlutterSemanticsObject alloc] initWithBridge:weak_ptr uid:node.id] autorelease]; }