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>
This commit is contained in:
@@ -65,6 +65,12 @@ constexpr float kScrollExtentMaxForInf = 1000;
|
||||
*/
|
||||
@property(nonatomic, strong) NSArray<SemanticsObject*>* 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<SemanticsObject*>* childrenInHitTestOrder;
|
||||
|
||||
/**
|
||||
* The UIAccessibility that represents this object.
|
||||
*
|
||||
|
||||
@@ -267,6 +267,7 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) {
|
||||
@implementation SemanticsObject {
|
||||
fml::scoped_nsobject<SemanticsObjectContainer> _container;
|
||||
NSMutableArray<SemanticsObject*>* _children;
|
||||
NSMutableArray<SemanticsObject*>* _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<SemanticsObject*>*)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) {
|
||||
|
||||
@@ -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<flutter::AccessibilityBridgeIos> factory(
|
||||
new flutter::MockAccessibilityBridge());
|
||||
fml::WeakPtr<flutter::AccessibilityBridgeIos> 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<flutter::AccessibilityBridgeIos> factory(
|
||||
new flutter::MockAccessibilityBridge());
|
||||
fml::WeakPtr<flutter::AccessibilityBridgeIos> 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<flutter::AccessibilityBridgeIos> factory(
|
||||
new flutter::MockAccessibilityBridge());
|
||||
fml::WeakPtr<flutter::AccessibilityBridgeIos> 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<flutter::AccessibilityBridgeIos> factory(
|
||||
new flutter::MockAccessibilityBridge());
|
||||
|
||||
@@ -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<FlutterCustomAccessibilityAction*>* accessibilityCustomActions =
|
||||
[[[NSMutableArray alloc] init] autorelease];
|
||||
|
||||
@@ -399,6 +399,7 @@ fml::RefPtr<fml::TaskRunner> 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<fml::TaskRunner> 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<fml::TaskRunner> 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<fml::TaskRunner> 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<fml::TaskRunner> 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;
|
||||
|
||||
Reference in New Issue
Block a user