From 13c33441ef7dec14b23748789ed8441181a27880 Mon Sep 17 00:00:00 2001 From: hangyu Date: Tue, 7 Feb 2023 14:25:53 -0800 Subject: [PATCH] Override _accessibilityHitTest to fix IOS 16 semantics issues (flutter/engine#38794) * Update SemanticsObject.mm * lint * Update SemanticsObjectTest.mm test Update SemanticsObjectTest.mm Update SemanticsObjectTest.mm test test Update SemanticsObject.mm Update SemanticsObjectTest.mm Update SemanticsObjectTest.mm Update SemanticsObject.mm Update SemanticsObjectTest.mm update Update SemanticsObjectTest.mm * Update SemanticsObjectTest.mm * Update SemanticsObject.mm * Update SemanticsObject.mm * Update SemanticsObject.mm * Update SemanticsObject.mm * Update SemanticsObject.mm * Update SemanticsObject.mm * Update SemanticsObject.mm * Update SemanticsObject.mm * Update SemanticsObject.mm * Update SemanticsObject.mm * Update SemanticsObject.mm * Update SemanticsObject.mm * Update shell/platform/darwin/ios/framework/Source/SemanticsObject.mm Co-authored-by: chunhtai <47866232+chunhtai@users.noreply.github.com> * Update SemanticsObject.mm * hittestorder * Update SemanticsObject.h * Update accessibility_bridge.mm * Update SemanticsObject.mm * Update SemanticsObject.mm * Update SemanticsObject.mm * Update SemanticsObject.mm * Update SemanticsObject.mm * Update SemanticsObjectTest.mm * Update SemanticsObject.mm * Update accessibility_bridge_test.mm --------- Co-authored-by: chunhtai <47866232+chunhtai@users.noreply.github.com> --- .../ios/framework/Source/SemanticsObject.h | 6 + .../ios/framework/Source/SemanticsObject.mm | 52 +++++++ .../framework/Source/SemanticsObjectTest.mm | 131 ++++++++++++++++++ .../framework/Source/accessibility_bridge.mm | 7 + .../Source/accessibility_bridge_test.mm | 5 + 5 files changed, 201 insertions(+) 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 ce9347c7c4..f7c611dec2 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 @@ -65,6 +65,12 @@ constexpr float kScrollExtentMaxForInf = 1000; */ @property(nonatomic, strong) NSArray* children; +/** + * Direct children of this semantics object in hit test order. Each child's `parent` property + * must be equal to this object. + */ +@property(nonatomic, strong) NSArray* childrenInHitTestOrder; + /** * The UIAccessibility that represents this object. * 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 ff4ab7dc94..bee20b558a 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 @@ -267,6 +267,7 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { @implementation SemanticsObject { fml::scoped_nsobject _container; NSMutableArray* _children; + NSMutableArray* _childrenInHitTestOrder; BOOL _inDealloc; } @@ -295,6 +296,7 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { _bridge = bridge; _uid = uid; _children = [[NSMutableArray alloc] init]; + _childrenInHitTestOrder = [[NSMutableArray alloc] init]; } return self; @@ -305,7 +307,10 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { [child privateSetParent:nil]; } [_children removeAllObjects]; + [_childrenInHitTestOrder removeAllObjects]; [_children release]; + [_childrenInHitTestOrder release]; + _parent = nil; _container.get().semanticsObject = nil; _inDealloc = YES; @@ -325,6 +330,17 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { } } +- (void)setChildrenInHitTestOrder:(NSArray*)childrenInHitTestOrder { + for (SemanticsObject* child in _childrenInHitTestOrder) { + [child privateSetParent:nil]; + } + [_childrenInHitTestOrder release]; + _childrenInHitTestOrder = [[NSMutableArray alloc] initWithArray:childrenInHitTestOrder]; + for (SemanticsObject* child in _childrenInHitTestOrder) { + [child privateSetParent:self]; + } +} + - (BOOL)hasChildren { return [self.children count] != 0; } @@ -460,6 +476,10 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { return false; } + return [self isFocusable]; +} + +- (bool)isFocusable { // If the node is scrollable AND hidden OR // The node has a label, value, or hint OR // The node has non-scrolling related actions. @@ -517,6 +537,38 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { return label; } +- (bool)containsPoint:(CGPoint)point { + // The point is in global coordinates, so use the global rect here. + return CGRectContainsPoint([self globalRect], point); +} + +// Finds the first eligiable semantics object in hit test order. +- (SemanticsObject*)search:(CGPoint)point { + // Search children in hit test order. + for (SemanticsObject* child in [self childrenInHitTestOrder]) { + if ([child containsPoint:point]) { + SemanticsObject* childSearchResult = [child search:point]; + if (childSearchResult != nil) { + return childSearchResult; + } + } + } + + // Check if the current semantic object should be returned. + if ([self containsPoint:point] && [self isFocusable]) { + return self.nativeAccessibility; + } + return nil; +} + +// Overrides apple private method to fix https://github.com/flutter/flutter/issues/113377. +// For overlapping UIAccessibilityElements (e.g. a stack) in IOS, the focus goes to the smallest +// object before IOS 16, but to the top-left object in IOS 16. +// Overrides this method to focus the first eligiable semantics object in hit test order. +- (id)_accessibilityHitTest:(CGPoint)point withEvent:(UIEvent*)event { + return [self search:point]; +} + - (NSAttributedString*)accessibilityAttributedLabel { NSString* label = [self accessibilityLabel]; if (label.length == 0) { 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 bd9878772c..0446b3d96f 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 @@ -92,6 +92,11 @@ class MockAccessibilityBridgeNoWindow : public AccessibilityBridgeIos { @interface SemanticsObjectTest : XCTestCase @end +@interface SemanticsObject (Tests) + +- (id)_accessibilityHitTest:(CGPoint)point withEvent:(UIEvent*)event; +@end + @implementation SemanticsObjectTest - (void)testCreate { @@ -114,6 +119,132 @@ class MockAccessibilityBridgeNoWindow : public AccessibilityBridgeIos { XCTAssertNil(child.parent); } +- (void)testAccessibilityHitTestFocusAtLeaf { + fml::WeakPtrFactory factory( + new flutter::MockAccessibilityBridge()); + fml::WeakPtr bridge = factory.GetWeakPtr(); + SemanticsObject* object0 = [[SemanticsObject alloc] initWithBridge:bridge uid:0]; + SemanticsObject* object1 = [[SemanticsObject alloc] initWithBridge:bridge uid:1]; + SemanticsObject* object2 = [[SemanticsObject alloc] initWithBridge:bridge uid:2]; + SemanticsObject* object3 = [[SemanticsObject alloc] initWithBridge:bridge uid:3]; + object0.children = @[ object1 ]; + object0.childrenInHitTestOrder = @[ object1 ]; + object1.children = @[ object2, object3 ]; + object1.childrenInHitTestOrder = @[ object2, object3 ]; + + flutter::SemanticsNode node0; + node0.id = 0; + node0.rect = SkRect::MakeXYWH(0, 0, 200, 200); + node0.label = "0"; + [object0 setSemanticsNode:&node0]; + + flutter::SemanticsNode node1; + node1.id = 1; + node1.rect = SkRect::MakeXYWH(0, 0, 200, 200); + node1.label = "1"; + [object1 setSemanticsNode:&node1]; + + flutter::SemanticsNode node2; + node2.id = 2; + node2.rect = SkRect::MakeXYWH(0, 0, 100, 100); + node2.label = "2"; + [object2 setSemanticsNode:&node2]; + + flutter::SemanticsNode node3; + node3.id = 3; + node3.rect = SkRect::MakeXYWH(0, 0, 200, 200); + node3.label = "3"; + [object3 setSemanticsNode:&node3]; + + CGPoint point = CGPointMake(10, 10); + id hitTestResult = [object0 _accessibilityHitTest:point withEvent:nil]; + + // Focus to object2 because it's the first object in hit test order + XCTAssertEqual(hitTestResult, object2); +} + +- (void)testAccessibilityHitTestNoFocusableItem { + fml::WeakPtrFactory factory( + new flutter::MockAccessibilityBridge()); + fml::WeakPtr bridge = factory.GetWeakPtr(); + SemanticsObject* object0 = [[SemanticsObject alloc] initWithBridge:bridge uid:0]; + SemanticsObject* object1 = [[SemanticsObject alloc] initWithBridge:bridge uid:1]; + SemanticsObject* object2 = [[SemanticsObject alloc] initWithBridge:bridge uid:2]; + SemanticsObject* object3 = [[SemanticsObject alloc] initWithBridge:bridge uid:3]; + object0.children = @[ object1 ]; + object0.childrenInHitTestOrder = @[ object1 ]; + object1.children = @[ object2, object3 ]; + object1.childrenInHitTestOrder = @[ object2, object3 ]; + + flutter::SemanticsNode node0; + node0.id = 0; + node0.rect = SkRect::MakeXYWH(0, 0, 200, 200); + [object0 setSemanticsNode:&node0]; + + flutter::SemanticsNode node1; + node1.id = 1; + node1.rect = SkRect::MakeXYWH(0, 0, 200, 200); + [object1 setSemanticsNode:&node1]; + + flutter::SemanticsNode node2; + node2.id = 2; + node2.rect = SkRect::MakeXYWH(0, 0, 100, 100); + [object2 setSemanticsNode:&node2]; + + flutter::SemanticsNode node3; + node3.id = 3; + node3.rect = SkRect::MakeXYWH(0, 0, 200, 200); + [object3 setSemanticsNode:&node3]; + + CGPoint point = CGPointMake(10, 10); + id hitTestResult = [object0 _accessibilityHitTest:point withEvent:nil]; + + XCTAssertNil(hitTestResult); +} + +- (void)testAccessibilityHitTestOutOfRect { + fml::WeakPtrFactory factory( + new flutter::MockAccessibilityBridge()); + fml::WeakPtr bridge = factory.GetWeakPtr(); + SemanticsObject* object0 = [[SemanticsObject alloc] initWithBridge:bridge uid:0]; + SemanticsObject* object1 = [[SemanticsObject alloc] initWithBridge:bridge uid:1]; + SemanticsObject* object2 = [[SemanticsObject alloc] initWithBridge:bridge uid:2]; + SemanticsObject* object3 = [[SemanticsObject alloc] initWithBridge:bridge uid:3]; + object0.children = @[ object1 ]; + object0.childrenInHitTestOrder = @[ object1 ]; + object1.children = @[ object2, object3 ]; + object1.childrenInHitTestOrder = @[ object2, object3 ]; + + flutter::SemanticsNode node0; + node0.id = 0; + node0.rect = SkRect::MakeXYWH(0, 0, 200, 200); + node0.label = "0"; + [object0 setSemanticsNode:&node0]; + + flutter::SemanticsNode node1; + node1.id = 1; + node1.rect = SkRect::MakeXYWH(0, 0, 200, 200); + node1.label = "1"; + [object1 setSemanticsNode:&node1]; + + flutter::SemanticsNode node2; + node2.id = 2; + node2.rect = SkRect::MakeXYWH(0, 0, 100, 100); + node2.label = "2"; + [object2 setSemanticsNode:&node2]; + + flutter::SemanticsNode node3; + node3.id = 3; + node3.rect = SkRect::MakeXYWH(0, 0, 200, 200); + node3.label = "3"; + [object3 setSemanticsNode:&node3]; + + CGPoint point = CGPointMake(300, 300); + id hitTestResult = [object0 _accessibilityHitTest:point withEvent:nil]; + + XCTAssertNil(hitTestResult); +} + - (void)testReplaceChildAtIndex { 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 66f903f7f7..a54a444225 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 @@ -107,7 +107,14 @@ void AccessibilityBridge::UpdateSemantics( SemanticsObject* child = GetOrCreateObject(node.childrenInTraversalOrder[i], nodes); [newChildren addObject:child]; } + NSMutableArray* newChildrenInHitTestOrder = + [[[NSMutableArray alloc] initWithCapacity:newChildCount] autorelease]; + for (NSUInteger i = 0; i < newChildCount; ++i) { + SemanticsObject* child = GetOrCreateObject(node.childrenInHitTestOrder[i], nodes); + [newChildrenInHitTestOrder addObject:child]; + } object.children = newChildren; + object.childrenInHitTestOrder = newChildrenInHitTestOrder; if (!node.customAccessibilityActions.empty()) { NSMutableArray* accessibilityCustomActions = [[[NSMutableArray alloc] init] autorelease]; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm index 4a8a347928..9b978150e0 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm @@ -399,6 +399,7 @@ fml::RefPtr CreateNewThread(std::string name) { node.scrollExtentMax = 100.0; node.scrollPosition = 0.0; parent.childrenInTraversalOrder.push_back(1); + parent.childrenInHitTestOrder.push_back(1); flutter::SemanticsNode child; child.id = 2; @@ -407,6 +408,7 @@ fml::RefPtr CreateNewThread(std::string name) { child.value = "value"; child.hint = "hint"; node.childrenInTraversalOrder.push_back(2); + node.childrenInHitTestOrder.push_back(2); nodes[0] = parent; nodes[1] = node; @@ -427,6 +429,7 @@ fml::RefPtr CreateNewThread(std::string name) { new_node.scrollExtentMax = 100.0; new_node.scrollPosition = 0.0; new_node.childrenInTraversalOrder.push_back(2); + new_node.childrenInHitTestOrder.push_back(2); new_nodes[1] = new_node; bridge->UpdateSemantics(/*nodes=*/new_nodes, /*actions=*/actions); @@ -489,6 +492,7 @@ fml::RefPtr CreateNewThread(std::string name) { node.scrollExtentMax = 100.0; node.scrollPosition = 0.0; parent.childrenInTraversalOrder.push_back(1); + parent.childrenInHitTestOrder.push_back(1); nodes[0] = parent; nodes[1] = node; flutter::CustomAccessibilityActionUpdates actions; @@ -556,6 +560,7 @@ fml::RefPtr CreateNewThread(std::string name) { node.scrollExtentMax = 100.0; node.scrollPosition = 0.0; parent.childrenInTraversalOrder.push_back(1); + parent.childrenInHitTestOrder.push_back(1); nodes[0] = parent; nodes[1] = node; flutter::CustomAccessibilityActionUpdates actions;