From 826b0d02329b05cb71c1cd3a1d12722248d191fd Mon Sep 17 00:00:00 2001 From: nbayati <99771966+nbayati@users.noreply.github.com> Date: Mon, 19 Sep 2022 21:15:34 -0700 Subject: [PATCH] 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 --- .../lib/src/engine/semantics/scrollable.dart | 64 +++++++++++++++---- .../test/engine/semantics/semantics_test.dart | 16 +++-- 2 files changed, 62 insertions(+), 18 deletions(-) diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/scrollable.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/scrollable.dart index 1776a8a206..839a9c7bd5 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/scrollable.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/scrollable.dart @@ -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 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; diff --git a/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_test.dart b/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_test.dart index 84051cc5dd..e7ce97a688 100644 --- a/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_test.dart +++ b/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_test.dart @@ -832,8 +832,11 @@ void _testVerticalScrolling() { semantics().updateSemantics(builder.build()); expectSemanticsTree(''' + '''); + final DomElement? scrollable = findScrollable(); + expect(scrollable!.scrollTop, isPositive); semantics().semanticsEnabled = false; }); @@ -861,6 +864,7 @@ void _testVerticalScrolling() { semantics().updateSemantics(builder.build()); expectSemanticsTree(''' + @@ -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(''' + @@ -982,6 +987,7 @@ void _testHorizontalScrolling() { semantics().updateSemantics(builder.build()); expectSemanticsTree(''' + '''); semantics().semanticsEnabled = false; @@ -1011,6 +1017,7 @@ void _testHorizontalScrolling() { semantics().updateSemantics(builder.build()); expectSemanticsTree(''' + @@ -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(''' +