From dcfd92f566c680447d597269392649079f6cb4ce Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Fri, 19 May 2023 15:02:05 -0700 Subject: [PATCH] Overrides accessibilityScrollToVisible (flutter/engine#42047) fixes https://github.com/flutter/flutter/issues/61624 Observed behavior is that this method is called when an item is swipe-to-focusd in VoiceOver. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../ios/framework/Source/SemanticsObject.mm | 32 ++++++++++++--- .../framework/Source/SemanticsObjectTest.mm | 39 ++++++++++++++++++- 2 files changed, 65 insertions(+), 6 deletions(-) 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 68f1beb7c3..6e2f9c5a20 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 @@ -460,6 +460,10 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { return attributedString; } +- (void)showOnScreen { + [self bridge]->DispatchSemanticsAction([self uid], flutter::SemanticsAction::kShowOnScreen); +} + #pragma mark - UIAccessibility overrides - (BOOL)isAccessibilityElement { @@ -561,14 +565,32 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { 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. +// iOS uses this method to determine the hittest results when users touch +// explore in VoiceOver. +// +// 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]; } +// iOS calls this method when this item is swipe-to-focusd in VoiceOver. +- (BOOL)accessibilityScrollToVisible { + [self showOnScreen]; + return YES; +} + +// iOS calls this method when this item is swipe-to-focusd in VoiceOver. +- (BOOL)accessibilityScrollToVisibleWithChild:(id)child { + if ([child isKindOfClass:[SemanticsObject class]]) { + [child showOnScreen]; + return YES; + } + return NO; +} + - (NSAttributedString*)accessibilityAttributedLabel { NSString* label = [self accessibilityLabel]; if (label.length == 0) { @@ -750,7 +772,7 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { [self bridge]->AccessibilityObjectDidBecomeFocused([self uid]); if ([self node].HasFlag(flutter::SemanticsFlags::kIsHidden) || [self node].HasFlag(flutter::SemanticsFlags::kIsHeader)) { - [self bridge]->DispatchSemanticsAction([self uid], flutter::SemanticsAction::kShowOnScreen); + [self showOnScreen]; } if ([self node].HasAction(flutter::SemanticsAction::kDidGainAccessibilityFocus)) { [self bridge]->DispatchSemanticsAction([self uid], 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 cc2e29a126..48ebd00635 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 @@ -93,7 +93,8 @@ class MockAccessibilityBridgeNoWindow : public AccessibilityBridgeIos { @end @interface SemanticsObject (Tests) - +- (BOOL)accessibilityScrollToVisible; +- (BOOL)accessibilityScrollToVisibleWithChild:(id)child; - (id)_accessibilityHitTest:(CGPoint)point withEvent:(UIEvent*)event; @end @@ -202,6 +203,42 @@ class MockAccessibilityBridgeNoWindow : public AccessibilityBridgeIos { XCTAssertNil(hitTestResult); } +- (void)testAccessibilityScrollToVisible { + fml::WeakPtrFactory factory( + new flutter::MockAccessibilityBridge()); + fml::WeakPtr bridge = factory.GetWeakPtr(); + SemanticsObject* object3 = [[SemanticsObject alloc] initWithBridge:bridge uid:3]; + + flutter::SemanticsNode node3; + node3.id = 3; + node3.rect = SkRect::MakeXYWH(0, 0, 200, 200); + [object3 setSemanticsNode:&node3]; + + [object3 accessibilityScrollToVisible]; + + XCTAssertTrue(bridge->observations.size() == 1); + XCTAssertTrue(bridge->observations[0].id == 3); + XCTAssertTrue(bridge->observations[0].action == flutter::SemanticsAction::kShowOnScreen); +} + +- (void)testAccessibilityScrollToVisibleWithChild { + fml::WeakPtrFactory factory( + new flutter::MockAccessibilityBridge()); + fml::WeakPtr bridge = factory.GetWeakPtr(); + SemanticsObject* object3 = [[SemanticsObject alloc] initWithBridge:bridge uid:3]; + + flutter::SemanticsNode node3; + node3.id = 3; + node3.rect = SkRect::MakeXYWH(0, 0, 200, 200); + [object3 setSemanticsNode:&node3]; + + [object3 accessibilityScrollToVisibleWithChild:object3]; + + XCTAssertTrue(bridge->observations.size() == 1); + XCTAssertTrue(bridge->observations[0].id == 3); + XCTAssertTrue(bridge->observations[0].action == flutter::SemanticsAction::kShowOnScreen); +} + - (void)testAccessibilityHitTestOutOfRect { fml::WeakPtrFactory factory( new flutter::MockAccessibilityBridge());