Skip to content

Commit dcdad71

Browse files
[iOS] Give hidden semantics nodes in scrollables a non-empty accessibility frame
Since iOS 27, VoiceOver no longer treats elements whose accessibilityFrame is CGRectZero as candidates for the next element. Off-screen children of a scrollable are flagged isHidden and reported CGRectZero, so a one-finger swipe stopped at the last visible item instead of triggering accessibilityScrollToVisible and an implicit scroll. Hidden nodes inside a FlutterScrollableSemanticsObject now report a 1pt strip at the leading or trailing edge of the scrollable's viewport, depending on which side the node lives on. VoiceOver handles them like clipped elements, and the existing showOnScreen path scrolls them in. Fixes flutter#192774
1 parent b7315d7 commit dcdad71

2 files changed

Lines changed: 190 additions & 0 deletions

File tree

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
namespace {
1313

14+
// The thickness, in points, of the frame reported for hidden nodes inside a
15+
// scrollable. See `-[SemanticsObject hiddenNodeFrame]`.
16+
constexpr CGFloat kHiddenNodeFrameExtent = 1.0;
17+
1418
flutter::SemanticsAction GetSemanticsActionForScrollDirection(
1519
UIAccessibilityScrollDirection direction) {
1620
// To describe the vertical scroll direction, UIAccessibilityScrollDirection uses the
@@ -715,11 +719,68 @@ - (CGRect)accessibilityFrame {
715719
}
716720

717721
if (self.node.flags.isHidden) {
722+
CGRect hiddenFrame = [self hiddenNodeFrame];
723+
if (!CGRectIsEmpty(hiddenFrame)) {
724+
return hiddenFrame;
725+
}
718726
return [super accessibilityFrame];
719727
}
720728
return [self globalRect];
721729
}
722730

731+
732+
// Returns the frame reported for a hidden node that lives inside a scrollable,
733+
// or CGRectZero if the node has no scrollable ancestor.
734+
//
735+
// Hidden nodes are off-screen items kept in the tree so that VoiceOver can
736+
// swipe to them, triggering `accessibilityScrollToVisible` and thus an implicit
737+
// scroll. Since iOS 27, VoiceOver skips elements with an empty frame, so a
738+
// hidden node reports a thin strip at the edge of the scrollable's viewport
739+
// closest to where the node actually is, which VoiceOver treats like a clipped
740+
// element.
741+
//
742+
// See also: https://github.com/flutter/flutter/issues/192774
743+
- (CGRect)hiddenNodeFrame {
744+
SemanticsObject* scrollable = self.parent;
745+
while (scrollable && ![scrollable isKindOfClass:[FlutterScrollableSemanticsObject class]]) {
746+
scrollable = scrollable.parent;
747+
}
748+
if (!scrollable) {
749+
return CGRectZero;
750+
}
751+
CGRect viewport = scrollable.accessibilityFrame;
752+
if (CGRectIsEmpty(viewport)) {
753+
return CGRectZero;
754+
}
755+
756+
CGRect rect = [self globalRect];
757+
BOOL isHorizontal = (scrollable.node.actions & flutter::kVerticalScrollSemanticsActions) == 0 &&
758+
(scrollable.node.actions & flutter::kHorizontalScrollSemanticsActions) != 0;
759+
if (isHorizontal) {
760+
CGFloat minY = MAX(CGRectGetMinY(rect), CGRectGetMinY(viewport));
761+
CGFloat maxY = MIN(CGRectGetMaxY(rect), CGRectGetMaxY(viewport));
762+
if (maxY <= minY) {
763+
minY = CGRectGetMinY(viewport);
764+
maxY = CGRectGetMaxY(viewport);
765+
}
766+
CGFloat x = CGRectGetMidX(rect) < CGRectGetMidX(viewport)
767+
? CGRectGetMinX(viewport)
768+
: CGRectGetMaxX(viewport) - kHiddenNodeFrameExtent;
769+
return CGRectMake(x, minY, kHiddenNodeFrameExtent, maxY - minY);
770+
}
771+
772+
CGFloat minX = MAX(CGRectGetMinX(rect), CGRectGetMinX(viewport));
773+
CGFloat maxX = MIN(CGRectGetMaxX(rect), CGRectGetMaxX(viewport));
774+
if (maxX <= minX) {
775+
minX = CGRectGetMinX(viewport);
776+
maxX = CGRectGetMaxX(viewport);
777+
}
778+
CGFloat y = CGRectGetMidY(rect) < CGRectGetMidY(viewport)
779+
? CGRectGetMinY(viewport)
780+
: CGRectGetMaxY(viewport) - kHiddenNodeFrameExtent;
781+
return CGRectMake(minX, y, maxX - minX, kHiddenNodeFrameExtent);
782+
}
783+
723784
- (CGRect)globalRect {
724785
const SkRect& rect = self.node.rect;
725786
CGRect localRect = CGRectMake(rect.x(), rect.y(), rect.width(), rect.height());

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm‎

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,135 @@ - (void)testVerticalFlutterScrollableSemanticsObject {
438438
kFloatCompareEpsilon);
439439
}
440440

441+
442+
- (void)testHiddenNodeInVerticalScrollableHasNonEmptyFrameAtViewportEdge {
443+
fml::WeakPtrFactory<flutter::testing::MockAccessibilityBridge> factory(
444+
new flutter::testing::MockAccessibilityBridge());
445+
fml::WeakPtr<flutter::testing::MockAccessibilityBridge> bridge = factory.GetWeakPtr();
446+
CGFloat scale = ((bridge->view().window.screen ?: UIScreen.mainScreen)).scale;
447+
448+
FlutterScrollableSemanticsObject* scrollable =
449+
[[FlutterScrollableSemanticsObject alloc] initWithBridge:bridge uid:0];
450+
SemanticsObject* above = [[SemanticsObject alloc] initWithBridge:bridge uid:1];
451+
SemanticsObject* visible = [[SemanticsObject alloc] initWithBridge:bridge uid:2];
452+
SemanticsObject* below = [[SemanticsObject alloc] initWithBridge:bridge uid:3];
453+
scrollable.children = @[ above, visible, below ];
454+
455+
flutter::SemanticsNode scrollableNode;
456+
scrollableNode.id = 0;
457+
scrollableNode.flags.hasImplicitScrolling = true;
458+
scrollableNode.actions = flutter::kVerticalScrollSemanticsActions;
459+
scrollableNode.rect = SkRect::MakeXYWH(0, 100 * scale, 300 * scale, 400 * scale);
460+
[scrollable setSemanticsNode:&scrollableNode];
461+
462+
flutter::SemanticsNode aboveNode;
463+
aboveNode.id = 1;
464+
aboveNode.label = "above";
465+
aboveNode.flags.isHidden = true;
466+
aboveNode.rect = SkRect::MakeXYWH(0, 0, 300 * scale, 50 * scale);
467+
[above setSemanticsNode:&aboveNode];
468+
469+
flutter::SemanticsNode visibleNode;
470+
visibleNode.id = 2;
471+
visibleNode.label = "visible";
472+
visibleNode.rect = SkRect::MakeXYWH(0, 200 * scale, 300 * scale, 50 * scale);
473+
[visible setSemanticsNode:&visibleNode];
474+
475+
flutter::SemanticsNode belowNode;
476+
belowNode.id = 3;
477+
belowNode.label = "below";
478+
belowNode.flags.isHidden = true;
479+
belowNode.rect = SkRect::MakeXYWH(0, 550 * scale, 300 * scale, 50 * scale);
480+
[below setSemanticsNode:&belowNode];
481+
482+
CGRect viewport = scrollable.accessibilityFrame;
483+
XCTAssertFalse(CGRectIsEmpty(viewport));
484+
485+
CGRect visibleFrame = visible.accessibilityFrame;
486+
XCTAssertEqualWithAccuracy(CGRectGetMinY(visibleFrame), CGRectGetMinY(viewport) + 100,
487+
kFloatCompareEpsilon);
488+
XCTAssertEqualWithAccuracy(CGRectGetHeight(visibleFrame), 50, kFloatCompareEpsilon);
489+
490+
CGRect aboveFrame = above.accessibilityFrame;
491+
XCTAssertFalse(CGRectIsEmpty(aboveFrame));
492+
XCTAssertTrue(CGRectContainsRect(viewport, aboveFrame));
493+
XCTAssertEqualWithAccuracy(CGRectGetMinY(aboveFrame), CGRectGetMinY(viewport),
494+
kFloatCompareEpsilon);
495+
XCTAssertEqualWithAccuracy(CGRectGetWidth(aboveFrame), CGRectGetWidth(viewport),
496+
kFloatCompareEpsilon);
497+
498+
CGRect belowFrame = below.accessibilityFrame;
499+
XCTAssertFalse(CGRectIsEmpty(belowFrame));
500+
XCTAssertTrue(CGRectContainsRect(viewport, belowFrame));
501+
XCTAssertEqualWithAccuracy(CGRectGetMaxY(belowFrame), CGRectGetMaxY(viewport),
502+
kFloatCompareEpsilon);
503+
XCTAssertEqualWithAccuracy(CGRectGetWidth(belowFrame), CGRectGetWidth(viewport),
504+
kFloatCompareEpsilon);
505+
506+
[below accessibilityScrollToVisible];
507+
XCTAssertTrue(bridge->observations.size() == 1);
508+
XCTAssertTrue(bridge->observations[0].id == 3);
509+
XCTAssertTrue(bridge->observations[0].action == flutter::SemanticsAction::kShowOnScreen);
510+
}
511+
512+
- (void)testHiddenNodeInHorizontalScrollableHasNonEmptyFrameAtViewportEdge {
513+
fml::WeakPtrFactory<flutter::AccessibilityBridgeIos> factory(
514+
new flutter::testing::MockAccessibilityBridge());
515+
fml::WeakPtr<flutter::AccessibilityBridgeIos> bridge = factory.GetWeakPtr();
516+
CGFloat scale = ((bridge->view().window.screen ?: UIScreen.mainScreen)).scale;
517+
518+
FlutterScrollableSemanticsObject* scrollable =
519+
[[FlutterScrollableSemanticsObject alloc] initWithBridge:bridge uid:0];
520+
SemanticsObject* trailing = [[SemanticsObject alloc] initWithBridge:bridge uid:1];
521+
scrollable.children = @[ trailing ];
522+
523+
flutter::SemanticsNode scrollableNode;
524+
scrollableNode.id = 0;
525+
scrollableNode.flags.hasImplicitScrolling = true;
526+
scrollableNode.actions = flutter::kHorizontalScrollSemanticsActions;
527+
scrollableNode.rect = SkRect::MakeXYWH(0, 100 * scale, 300 * scale, 100 * scale);
528+
[scrollable setSemanticsNode:&scrollableNode];
529+
530+
flutter::SemanticsNode trailingNode;
531+
trailingNode.id = 1;
532+
trailingNode.label = "trailing";
533+
trailingNode.flags.isHidden = true;
534+
trailingNode.rect = SkRect::MakeXYWH(350 * scale, 100 * scale, 100 * scale, 100 * scale);
535+
[trailing setSemanticsNode:&trailingNode];
536+
537+
CGRect viewport = scrollable.accessibilityFrame;
538+
CGRect frame = trailing.accessibilityFrame;
539+
XCTAssertFalse(CGRectIsEmpty(frame));
540+
XCTAssertTrue(CGRectContainsRect(viewport, frame));
541+
XCTAssertEqualWithAccuracy(CGRectGetMaxX(frame), CGRectGetMaxX(viewport), kFloatCompareEpsilon);
542+
XCTAssertEqualWithAccuracy(CGRectGetHeight(frame), CGRectGetHeight(viewport),
543+
kFloatCompareEpsilon);
544+
}
545+
546+
- (void)testHiddenNodeOutsideScrollableKeepsEmptyFrame {
547+
fml::WeakPtrFactory<flutter::AccessibilityBridgeIos> factory(
548+
new flutter::testing::MockAccessibilityBridge());
549+
fml::WeakPtr<flutter::AccessibilityBridgeIos> bridge = factory.GetWeakPtr();
550+
551+
SemanticsObject* parent = [[SemanticsObject alloc] initWithBridge:bridge uid:0];
552+
SemanticsObject* child = [[SemanticsObject alloc] initWithBridge:bridge uid:1];
553+
parent.children = @[ child ];
554+
555+
flutter::SemanticsNode parentNode;
556+
parentNode.id = 0;
557+
parentNode.rect = SkRect::MakeXYWH(0, 0, 300, 400);
558+
[parent setSemanticsNode:&parentNode];
559+
560+
flutter::SemanticsNode childNode;
561+
childNode.id = 1;
562+
childNode.label = "hidden";
563+
childNode.flags.isHidden = true;
564+
childNode.rect = SkRect::MakeXYWH(0, 500, 300, 50);
565+
[child setSemanticsNode:&childNode];
566+
567+
XCTAssertTrue(CGRectIsEmpty(child.accessibilityFrame));
568+
}
569+
441570
- (void)testVerticalFlutterScrollableSemanticsObjectNoWindowDoesNotCrash {
442571
fml::WeakPtrFactory<flutter::AccessibilityBridgeIos> factory(
443572
new flutter::testing::MockAccessibilityBridgeNoWindow());

0 commit comments

Comments
 (0)