Fix the a11y scrolling issue (flutter/engine#35899)

* Fix the a11y scrolling issue

* Add scroll overflow dom element

* Add tests for scrollTop/scrollLeft

* Documentation and code clean-ups

* call recomputePositionAndSize() after neutralizing the scroll position

* Remove unnecessary imports
This commit is contained in:
nbayati
2022-09-19 21:15:34 -07:00
committed by GitHub
parent da84861479
commit 826b0d0232
2 changed files with 62 additions and 18 deletions

View File

@@ -2,13 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:ui/src/engine.dart';
import 'package:ui/ui.dart' as ui;
import '../dom.dart';
import '../platform_dispatcher.dart';
import '../safe_browser_api.dart';
import 'semantics.dart';
/// Implements vertical and horizontal scrolling functionality for semantics
/// objects.
///
@@ -28,11 +24,31 @@ import 'semantics.dart';
/// viewport "scrollTop" may take positive values.
class Scrollable extends RoleManager {
Scrollable(SemanticsObject semanticsObject)
: super(Role.scrollable, semanticsObject);
: super(Role.scrollable, semanticsObject) {
_scrollOverflowElement.style
..position = 'absolute'
..transformOrigin = '0 0 0'
// Ignore pointer events since this is a dummy element.
..pointerEvents = 'none';
semanticsObject.element.append(_scrollOverflowElement);
}
/// Disables browser-driven scrolling in the presence of pointer events.
GestureModeCallback? _gestureModeListener;
/// DOM element used as a workaround for: https://github.com/flutter/flutter/issues/104036
///
/// When the assistive technology gets to the last element of the scrollable
/// list, the browser thinks the scrollable area doesn't have any more content,
/// so it overrides the value of "scrollTop"/"scrollLeft" with zero. As a result,
/// the user can't scroll back up/left.
///
/// As a workaround, we add this DOM element and set its size to
/// [canonicalNeutralScrollPosition] so the browser believes
/// that the scrollable area still has some more content, and doesn't override
/// scrollTop/scrollLetf with zero.
final DomElement _scrollOverflowElement = createDomElement('flt-semantics-scroll-overflow');
/// Listens to HTML "scroll" gestures detected by the browser.
///
/// This gesture is converted to [ui.SemanticsAction.scrollUp] or
@@ -79,6 +95,11 @@ class Scrollable extends RoleManager {
@override
void update() {
semanticsObject.owner.addOneTimePostUpdateCallback(() {
_neutralizeDomScrollPosition();
semanticsObject.recomputePositionAndSize();
});
if (_scrollListener == null) {
// We need to set touch-action:none explicitly here, despite the fact
// that we already have it on the <body> tag because overflow:scroll
@@ -92,13 +113,6 @@ class Scrollable extends RoleManager {
semanticsObject.element.style.touchAction = 'none';
_gestureModeDidChange();
// We neutralize the scroll position after all children have been
// updated. Otherwise the browser does not yet have the sizes of the
// child nodes and resets the scrollTop value back to zero.
semanticsObject.owner.addOneTimePostUpdateCallback(() {
_neutralizeDomScrollPosition();
});
// Memoize the tear-off because Dart does not guarantee that two
// tear-offs of a method on the same instance will produce the same
// object.
@@ -137,9 +151,22 @@ class Scrollable extends RoleManager {
void _neutralizeDomScrollPosition() {
// This value is arbitrary.
const int canonicalNeutralScrollPosition = 10;
final DomElement element = semanticsObject.element;
final ui.Rect? rect = semanticsObject.rect;
if (rect == null) {
printWarning('Warning! the rect attribute of semanticsObject is null');
return;
}
if (semanticsObject.isVerticalScrollContainer) {
// Place the _scrollOverflowElement at the end of the content and
// make sure that when we neutralize the scrolling position,
// it doesn't scroll into the visible area.
final int verticalOffset = rect.height.ceil() + canonicalNeutralScrollPosition;
_scrollOverflowElement.style
..transform = 'translate(0px,${verticalOffset}px)'
..width = '${rect.width.round()}px'
..height = '${canonicalNeutralScrollPosition}px';
element.scrollTop = canonicalNeutralScrollPosition;
// Read back because the effective value depends on the amount of content.
_effectiveNeutralScrollPosition = element.scrollTop;
@@ -148,6 +175,15 @@ class Scrollable extends RoleManager {
_effectiveNeutralScrollPosition.toDouble()
..horizontalContainerAdjustment = 0.0;
} else {
// Place the _scrollOverflowElement at the end of the content and
// make sure that when we neutralize the scrolling position,
// it doesn't scroll into the visible area.
final int horizontalOffset = rect.width.ceil() + canonicalNeutralScrollPosition;
_scrollOverflowElement.style
..transform = 'translate(${horizontalOffset}px,0px)'
..width = '${canonicalNeutralScrollPosition}px'
..height = '${rect.height.round()}px';
element.scrollLeft = canonicalNeutralScrollPosition;
// Read back because the effective value depends on the amount of content.
_effectiveNeutralScrollPosition = element.scrollLeft;

View File

@@ -832,8 +832,11 @@ void _testVerticalScrolling() {
semantics().updateSemantics(builder.build());
expectSemanticsTree('''
<sem style="$rootSemanticStyle; touch-action: none; overflow-y: scroll">
<flt-semantics-scroll-overflow></flt-semantics-scroll-overflow>
</sem>''');
final DomElement? scrollable = findScrollable();
expect(scrollable!.scrollTop, isPositive);
semantics().semanticsEnabled = false;
});
@@ -861,6 +864,7 @@ void _testVerticalScrolling() {
semantics().updateSemantics(builder.build());
expectSemanticsTree('''
<sem style="$rootSemanticStyle; touch-action: none; overflow-y: scroll">
<flt-semantics-scroll-overflow></flt-semantics-scroll-overflow>
<sem-c>
<sem></sem>
</sem-c>
@@ -870,8 +874,8 @@ void _testVerticalScrolling() {
expect(scrollable, isNotNull);
// When there's less content than the available size the neutral scrollTop
// is 0.
expect(scrollable!.scrollTop, 0);
// is still a positive number.
expect(scrollable!.scrollTop, isPositive);
semantics().semanticsEnabled = false;
});
@@ -925,6 +929,7 @@ void _testVerticalScrolling() {
semantics().updateSemantics(builder.build());
expectSemanticsTree('''
<sem style="$rootSemanticStyle; touch-action: none; overflow-y: scroll">
<flt-semantics-scroll-overflow></flt-semantics-scroll-overflow>
<sem-c>
<sem style="z-index: 3"></sem>
<sem style="z-index: 2"></sem>
@@ -982,6 +987,7 @@ void _testHorizontalScrolling() {
semantics().updateSemantics(builder.build());
expectSemanticsTree('''
<sem style="$rootSemanticStyle; touch-action: none; overflow-x: scroll">
<flt-semantics-scroll-overflow></flt-semantics-scroll-overflow>
</sem>''');
semantics().semanticsEnabled = false;
@@ -1011,6 +1017,7 @@ void _testHorizontalScrolling() {
semantics().updateSemantics(builder.build());
expectSemanticsTree('''
<sem style="$rootSemanticStyle; touch-action: none; overflow-x: scroll">
<flt-semantics-scroll-overflow></flt-semantics-scroll-overflow>
<sem-c>
<sem></sem>
</sem-c>
@@ -1020,8 +1027,8 @@ void _testHorizontalScrolling() {
expect(scrollable, isNotNull);
// When there's less content than the available size the neutral
// scrollLeft is 0.
expect(scrollable!.scrollLeft, 0);
// scrollLeft is still a positive number.
expect(scrollable!.scrollLeft, isPositive);
semantics().semanticsEnabled = false;
});
@@ -1056,6 +1063,7 @@ void _testHorizontalScrolling() {
semantics().updateSemantics(builder.build());
expectSemanticsTree('''
<sem style="$rootSemanticStyle; touch-action: none; overflow-x: scroll">
<flt-semantics-scroll-overflow></flt-semantics-scroll-overflow>
<sem-c>
<sem style="z-index: 3"></sem>
<sem style="z-index: 2"></sem>