Title: [270838] trunk
Revision
270838
Author
[email protected]
Date
2020-12-15 06:34:27 -0800 (Tue, 15 Dec 2020)

Log Message

CSS Scroll Snap is not in effect when the user scrolls via the keyboard
https://bugs.webkit.org/show_bug.cgi?id=198566
<rdar://problem/51488088>

Reviewed by Simon Fraser.

Add support for scroll snap for keyboard scrolling

This change adds support for the concept of directional scroll snapping,
which will never snap to a snap point that is away from the original
scroll offset in the opposite direction of the scroll direction. This
allows escaping snap points.

Source/WebCore:

Tests: tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard-scaled.html
       tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard.html
       tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard-scaled.html
       tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard.html
       tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-horizontal-with-keyboard.html
       tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-vertical-with-keyboard.html
       tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-overflow-rtl-with-keyboard.html

* page/FrameView.cpp:
(WebCore::FrameView::pageScaleFactor const): Added.
* page/FrameView.h: Added scale factor getter.
* page/scrolling/ScrollSnapOffsetsInfo.cpp:
(WebCore::closestSnapOffset): Added a new optional argument that signals
directional snapping. This changes the behavior when choosing a snap offset
that is in the opposite direction as the scroll direction.
* page/scrolling/ScrollSnapOffsetsInfo.h:
* platform/ScrollAnimator.cpp:
(WebCore::ScrollAnimator::scroll):
* page/scrolling/ScrollSnapOffsetsInfo.h:
(WebCore::ScrollAnimator::scroll): No longer use the granularity argument.
(WebCore::ScrollAnimator::scrollWithoutAnimation): Remove the unused ScrollGranularity
argument and use the new positionFromStep helper.
(WebCore::ScrollAnimator::scrollWithDirectionalSnapping): Added.
(WebCore::ScrollAnimator::positionFromStep): Added this helper.
(WebCore::ScrollAnimator::handleWheelEvent): Remove the granularity argument.
(WebCore::ScrollAnimator::pageScaleFactor const): Added this implementation
of a ScrollControllerClient method.
* platform/ScrollAnimator.h: Update method declarations.
* platform/ScrollableArea.cpp:
(WebCore::ScrollableArea::scroll): Use the new argument to the scroll method.
* platform/cocoa/ScrollController.h: Added new method.
(WebCore::ScrollableArea::pageScaleFactor const):
* platform/cocoa/ScrollController.h:
* platform/cocoa/ScrollController.mm:
(WebCore::ScrollController::adjustScrollDestinationForDirectionalSnapping): Added.
* platform/mac/ScrollAnimatorMac.h:
* platform/mac/ScrollAnimatorMac.mm:
(WebCore::ScrollAnimatorMac::scroll): Use new positionFromStep helper.

LayoutTests:

* tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard-expected.txt: Added.
* tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard-scaled-expected.txt: Added.
* tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard-scaled.html: Added.
* tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard.html: Added.
* tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard-expected.txt: Added.
* tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard-scaled-expected.txt: Added.
* tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard-scaled.html: Added.
* tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard.html: Added.
* tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-horizontal-with-keyboard-expected.txt: Added.
* tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-horizontal-with-keyboard.html: Added.
* tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-vertical-with-keyboard-expected.txt: Added.
* tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-vertical-with-keyboard.html: Added.
* tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-overflow-rtl-with-keyboard-expected.txt: Added.
* tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-overflow-rtl-with-keyboard.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (270837 => 270838)


--- trunk/LayoutTests/ChangeLog	2020-12-15 14:14:20 UTC (rev 270837)
+++ trunk/LayoutTests/ChangeLog	2020-12-15 14:34:27 UTC (rev 270838)
@@ -1,3 +1,33 @@
+2020-12-15  Martin Robinson  <[email protected]>
+
+        CSS Scroll Snap is not in effect when the user scrolls via the keyboard
+        https://bugs.webkit.org/show_bug.cgi?id=198566
+        <rdar://problem/51488088>
+
+        Reviewed by Simon Fraser.
+
+        Add support for scroll snap for keyboard scrolling
+
+        This change adds support for the concept of directional scroll snapping,
+        which will never snap to a snap point that is away from the original
+        scroll offset in the opposite direction of the scroll direction. This
+        allows escaping snap points.
+
+        * tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard-expected.txt: Added.
+        * tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard-scaled-expected.txt: Added.
+        * tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard-scaled.html: Added.
+        * tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard.html: Added.
+        * tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard-expected.txt: Added.
+        * tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard-scaled-expected.txt: Added.
+        * tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard-scaled.html: Added.
+        * tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard.html: Added.
+        * tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-horizontal-with-keyboard-expected.txt: Added.
+        * tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-horizontal-with-keyboard.html: Added.
+        * tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-vertical-with-keyboard-expected.txt: Added.
+        * tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-vertical-with-keyboard.html: Added.
+        * tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-overflow-rtl-with-keyboard-expected.txt: Added.
+        * tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-overflow-rtl-with-keyboard.html: Added.
+
 2020-12-15  Antoine Quint  <[email protected]>
 
         [Web Animations] Implied keyframes should not account for animations on siblings

Added: trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard-expected.txt (0 => 270838)


--- trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard-expected.txt	2020-12-15 14:34:27 UTC (rev 270838)
@@ -0,0 +1,6 @@
+PASS arrow key div scrolled to second div.
+PASS arrow key div scrolled back to first div.
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard-scaled-expected.txt (0 => 270838)


--- trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard-scaled-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard-scaled-expected.txt	2020-12-15 14:34:27 UTC (rev 270838)
@@ -0,0 +1,6 @@
+PASS arrow key div scrolled to second div.
+PASS arrow key div scrolled back to first div.
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard-scaled.html (0 => 270838)


--- trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard-scaled.html	                        (rev 0)
+++ trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard-scaled.html	2020-12-15 14:34:27 UTC (rev 270838)
@@ -0,0 +1,74 @@
+<!DOCTYPE HTML>
+<html>
+    <head>
+        <style>
+            html {
+                scroll-snap-type: x mandatory;
+            }
+            .horizontalGallery {
+                width: 600vw;
+                height: 100vh;
+                margin: 0;
+                padding: 0;
+            }
+            .colorBox {
+                height: 100vh;
+                width: 100vw;
+                float: left;
+                scroll-snap-align: start;
+            }
+            #item0 { background-color: red; }
+            #item1 { background-color: green; }
+            #item2 { background-color: blue; }
+            #item3 { background-color: aqua; }
+            #item4 { background-color: yellow; }
+            #item5 { background-color: fuchsia; }
+        </style>
+        <script src=""
+        <script src=""
+        <script>
+        window.jsTestIsAsync = true;
+
+        async function runTests()
+        {
+            try {
+                await UIHelper.delayFor(0);
+
+                let scale = 2.0;
+                eventSender.scalePageBy(scale, 0, 0);
+
+                let scrollPositionBeforeSnap = document.scrollingElement.scrollLeft;
+                eventSender.keyDown("rightArrow");
+                expectTrue(document.scrollingElement.scrollLeft == (window.innerWidth * scale), "arrow key div scrolled to second div.");
+
+                eventSender.keyDown("leftArrow");
+                expectTrue(document.scrollingElement.scrollLeft == scrollPositionBeforeSnap, "arrow key div scrolled back to first div.");
+            } catch (e) {
+                console.log(e);
+            } finally {
+                finishJSTest();
+            }
+        }
+
+        function onLoad()
+        {
+            if (window.eventSender) {
+                runTests();
+            } else {
+                var messageLocation = document.getElementById('item0');
+                var message = document.createElement('div');
+                message.innerHTML = "<p>To run this test manually, scroll the page horizontally with the keyboard.</p>";
+                messageLocation.appendChild(message);
+            }
+        }
+        </script>
+    </head>
+    <body _onload_="onLoad();" class="horizontalGallery">
+        <div id="item0" class="colorBox"><div id="console"></div></div>
+        <div id="item1" class="colorBox"></div>
+        <div id="item2" class="colorBox"></div>
+        <div id="item3" class="colorBox"></div>
+        <div id="item4" class="colorBox"></div>
+        <div id="item5" class="colorBox"></div>
+    </body>
+</html>

Added: trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard.html (0 => 270838)


--- trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard.html	                        (rev 0)
+++ trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard.html	2020-12-15 14:34:27 UTC (rev 270838)
@@ -0,0 +1,71 @@
+<!DOCTYPE HTML>
+<html>
+    <head>
+        <style>
+            html {
+                scroll-snap-type: x mandatory;
+            }
+            .horizontalGallery {
+                width: 600vw;
+                height: 100vh;
+                margin: 0;
+                padding: 0;
+            }
+            .colorBox {
+                height: 100vh;
+                width: 100vw;
+                float: left;
+                scroll-snap-align: start;
+            }
+            #item0 { background-color: red; }
+            #item1 { background-color: green; }
+            #item2 { background-color: blue; }
+            #item3 { background-color: aqua; }
+            #item4 { background-color: yellow; }
+            #item5 { background-color: fuchsia; }
+        </style>
+        <script src=""
+        <script src=""
+        <script>
+        window.jsTestIsAsync = true;
+
+        async function runTests()
+        {
+            try {
+                await UIHelper.delayFor(0);
+
+                let scrollPositionBeforeSnap = document.scrollingElement.scrollLeft;
+                eventSender.keyDown("rightArrow");
+                expectTrue(document.scrollingElement.scrollLeft == window.innerWidth, "arrow key div scrolled to second div.");
+
+                eventSender.keyDown("leftArrow");
+                expectTrue(document.scrollingElement.scrollLeft == scrollPositionBeforeSnap, "arrow key div scrolled back to first div.");
+            } catch (e) {
+                console.log(e);
+            } finally {
+                finishJSTest();
+            }
+        }
+
+        function onLoad()
+        {
+            if (window.eventSender) {
+                runTests();
+            } else {
+                var messageLocation = document.getElementById('item0');
+                var message = document.createElement('div');
+                message.innerHTML = "<p>To run this test manually, scroll the page horizontally with the keyboard.</p>";
+                messageLocation.appendChild(message);
+            }
+        }
+        </script>
+    </head>
+    <body _onload_="onLoad();" class="horizontalGallery">
+        <div id="item0" class="colorBox"><div id="console"></div></div>
+        <div id="item1" class="colorBox"></div>
+        <div id="item2" class="colorBox"></div>
+        <div id="item3" class="colorBox"></div>
+        <div id="item4" class="colorBox"></div>
+        <div id="item5" class="colorBox"></div>
+    </body>
+</html>

Added: trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard-expected.txt (0 => 270838)


--- trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard-expected.txt	2020-12-15 14:34:27 UTC (rev 270838)
@@ -0,0 +1,9 @@
+PASS arrow key scrolled to second div.
+PASS arrow key scrolled to third div.
+PASS arrow key div scrolled back to first div.
+PASS page down scrolled to second div.
+PASS page up div scrolled back to first div.
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard-scaled-expected.txt (0 => 270838)


--- trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard-scaled-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard-scaled-expected.txt	2020-12-15 14:34:27 UTC (rev 270838)
@@ -0,0 +1,9 @@
+PASS arrow key scrolled to second div.
+PASS arrow key scrolled to third div.
+PASS arrow key div scrolled back to first div.
+PASS page down scrolled to second div.
+PASS page up div scrolled back to first div.
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard-scaled.html (0 => 270838)


--- trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard-scaled.html	                        (rev 0)
+++ trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard-scaled.html	2020-12-15 14:34:27 UTC (rev 270838)
@@ -0,0 +1,93 @@
+<!DOCTYPE HTML>
+<html>
+    <head>
+        <style>
+            html {
+                scroll-snap-type: y mandatory;
+            }
+            .verticalGallery {
+                width: 100vw;
+                height: 600vh;
+                margin: 0;
+                padding: 0;
+            }
+            .colorBox {
+                height: 100vh;
+                width: 100vw;
+                float: left;
+                scroll-snap-align: start;
+            }
+            #item0 { background-color: red; }
+            #item1 { background-color: green; }
+            #item2 { background-color: blue; }
+            #item3 { background-color: aqua; }
+            #item4 { background-color: yellow; }
+            #item5 { background-color: fuchsia; }
+        </style>
+        <script src=""
+        <script src=""
+        <script>
+        window.jsTestIsAsync = true;
+
+        async function runTests()
+        {
+            try {
+                await UIHelper.delayFor(0);
+
+                let scale = 2.0;
+                eventSender.scalePageBy(scale, 0, 0);
+
+                let windowHeight = window.innerHeight * scale;
+                let initialScrollPosition = document.scrollingElement.scrollTop;
+                let results = [];
+
+                eventSender.keyDown("downArrow");
+                results.push([document.scrollingElement.scrollTop == windowHeight, "arrow key scrolled to second div."]);
+
+                eventSender.keyDown("downArrow");
+                results.push([document.scrollingElement.scrollTop == (windowHeight * 2), "arrow key scrolled to third div."]);
+
+                eventSender.keyDown("upArrow");
+                eventSender.keyDown("upArrow");
+                results.push([document.scrollingElement.scrollTop == 0, "arrow key div scrolled back to first div."]);
+
+                eventSender.keyDown("pageDown");
+                results.push([document.scrollingElement.scrollTop == windowHeight, "page down scrolled to second div."]);
+
+                eventSender.keyDown("pageUp");
+                results.push([document.scrollingElement.scrollTop == 0, "page up div scrolled back to first div."]);
+
+                // We avoid modifying the DOM until the test is over, because sometimes
+                // DOM modifications can reset the main frame scroll position.
+                results.forEach((result) => {
+                    expectTrue(result[0], result[1]);
+                });
+            } catch (e) {
+                console.log(e);
+            } finally {
+                finishJSTest();
+            }
+        }
+
+        function onLoad()
+        {
+            if (window.eventSender) {
+                runTests();
+            } else {
+                var messageLocation = document.getElementById('item0');
+                var message = document.createElement('div');
+                message.innerHTML = "<p>To run this test manually, scroll the page vertically with the keyboard.</p>";
+                messageLocation.appendChild(message);
+            }
+        }
+        </script>
+    </head>
+    <body _onload_="onLoad();" class="verticalGallery">
+        <div id="item0" class="colorBox"><div id="console"></div></div>
+        <div id="item1" class="colorBox"></div>
+        <div id="item2" class="colorBox"></div>
+        <div id="item3" class="colorBox"></div>
+        <div id="item4" class="colorBox"></div>
+        <div id="item5" class="colorBox"></div>
+    </body>
+</html>

Added: trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard.html (0 => 270838)


--- trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard.html	                        (rev 0)
+++ trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard.html	2020-12-15 14:34:27 UTC (rev 270838)
@@ -0,0 +1,88 @@
+<!DOCTYPE HTML>
+<html>
+    <head>
+        <style>
+            html {
+                scroll-snap-type: y mandatory;
+            }
+            .verticalGallery {
+                width: 100vw;
+                height: 600vh;
+                margin: 0;
+                padding: 0;
+            }
+            .colorBox {
+                height: 100vh;
+                width: 100vw;
+                float: left;
+                scroll-snap-align: start;
+            }
+            #item0 { background-color: red; }
+            #item1 { background-color: green; }
+            #item2 { background-color: blue; }
+            #item3 { background-color: aqua; }
+            #item4 { background-color: yellow; }
+            #item5 { background-color: fuchsia; }
+        </style>
+        <script src=""
+        <script src=""
+        <script>
+        window.jsTestIsAsync = true;
+
+        async function runTests()
+        {
+            try {
+                await UIHelper.delayFor(0);
+
+                let scrollPositionBeforeSnap = document.scrollingElement.scrollTop;
+                let results = [];
+                eventSender.keyDown("downArrow");
+                results.push([document.scrollingElement.scrollTop == window.innerHeight, "arrow key scrolled to second div."]);
+
+                eventSender.keyDown("downArrow");
+                results.push([document.scrollingElement.scrollTop == (window.innerHeight * 2), "arrow key scrolled to third div."]);
+
+                eventSender.keyDown("upArrow");
+                eventSender.keyDown("upArrow");
+                results.push([document.scrollingElement.scrollTop == scrollPositionBeforeSnap, "arrow key div scrolled back to first div."]);
+
+                eventSender.keyDown("pageDown");
+                results.push([document.scrollingElement.scrollTop == window.innerHeight, "page down scrolled to second div."]);
+
+                eventSender.keyDown("pageUp");
+                results.push([document.scrollingElement.scrollTop == scrollPositionBeforeSnap, "page up div scrolled back to first div."]);
+
+                // We avoid modifying the DOM until the test is over, because sometimes
+                // DOM modifications can reset the main frame scroll position.
+                results.forEach((result) => {
+                    expectTrue(result[0], result[1]);
+                });
+            } catch (e) {
+                console.log(e);
+            } finally {
+                finishJSTest();
+            }
+        }
+
+        function onLoad()
+        {
+            if (window.eventSender) {
+                runTests();
+            } else {
+                var messageLocation = document.getElementById('item0');
+                var message = document.createElement('div');
+                message.innerHTML = "<p>To run this test manually, scroll the page vertically with the keyboard.</p>";
+                messageLocation.appendChild(message);
+            }
+        }
+        </script>
+    </head>
+    <body _onload_="onLoad();" class="verticalGallery">
+        <div id="item0" class="colorBox"><div id="console"></div></div>
+        <div id="item1" class="colorBox"></div>
+        <div id="item2" class="colorBox"></div>
+        <div id="item3" class="colorBox"></div>
+        <div id="item4" class="colorBox"></div>
+        <div id="item5" class="colorBox"></div>
+    </body>
+</html>

Added: trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-horizontal-with-keyboard-expected.txt (0 => 270838)


--- trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-horizontal-with-keyboard-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-horizontal-with-keyboard-expected.txt	2020-12-15 14:34:27 UTC (rev 270838)
@@ -0,0 +1,6 @@
+PASS arrow keys didn't scroll to second div.
+PASS left arrow snapped back.
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-horizontal-with-keyboard.html (0 => 270838)


--- trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-horizontal-with-keyboard.html	                        (rev 0)
+++ trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-horizontal-with-keyboard.html	2020-12-15 14:34:27 UTC (rev 270838)
@@ -0,0 +1,84 @@
+<!DOCTYPE HTML>
+<html>
+    <head>
+        <style>
+            html {
+                scroll-snap-type: x proximity;
+            }
+            .horizontalGallery {
+                width: 600vw;
+                height: 100vh;
+                margin: 0;
+                padding: 0;
+            }
+            .colorBox {
+                height: 100vh;
+                width: 100vw;
+                float: left;
+                scroll-snap-align: start;
+            }
+            #item0 { background-color: red; }
+            #item1 { background-color: green; }
+            #item2 { background-color: blue; }
+            #item3 { background-color: aqua; }
+            #item4 { background-color: yellow; }
+            #item5 { background-color: fuchsia; }
+        </style>
+        <script src=""
+        <script src=""
+        <script>
+        window.jsTestIsAsync = true;
+
+        async function runTests()
+        {
+            try {
+                await UIHelper.delayFor(0);
+
+                let scrollPositionBeforeSnap = document.scrollingElement.scrollLeft;
+                eventSender.keyDown("rightArrow");
+                eventSender.keyDown("rightArrow");
+                eventSender.keyDown("rightArrow");
+                var betweenFirstScrollPositionAndSecond =
+                    document.scrollingElement.scrollLeft > scrollPositionBeforeSnap
+                    && document.scrollingElement.scrollLeft < window.innerWidth;
+                expectTrue(betweenFirstScrollPositionAndSecond, "arrow keys didn't scroll to second div.");
+
+                // We reset the scrolling position because changing the DOM when we are not on
+                // a snap point can reset our scroll position.
+                document.scrollingElement.scrollLeft = scrollPositionBeforeSnap;
+                eventSender.keyDown("rightArrow");
+                eventSender.keyDown("rightArrow");
+                eventSender.keyDown("rightArrow");
+                eventSender.keyDown("leftArrow");
+
+                // Reversing directions should snap back.
+                expectTrue(document.scrollingElement.scrollLeft == scrollPositionBeforeSnap, "left arrow snapped back.");
+            } catch (e) {
+                console.log(e);
+            } finally {
+                finishJSTest();
+            }
+        }
+
+        function onLoad()
+        {
+            if (window.eventSender) {
+                runTests();
+            } else {
+                var messageLocation = document.getElementById('item0');
+                var message = document.createElement('div');
+                message.innerHTML = "<p>To run this test manually, scroll the page horizontally with the keyboard.</p>";
+                messageLocation.appendChild(message);
+            }
+        }
+        </script>
+    </head>
+    <body _onload_="onLoad();" class="horizontalGallery">
+        <div id="item0" class="colorBox"><div id="console"></div></div>
+        <div id="item1" class="colorBox"></div>
+        <div id="item2" class="colorBox"></div>
+        <div id="item3" class="colorBox"></div>
+        <div id="item4" class="colorBox"></div>
+        <div id="item5" class="colorBox"></div>
+    </body>
+</html>

Added: trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-vertical-with-keyboard-expected.txt (0 => 270838)


--- trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-vertical-with-keyboard-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-vertical-with-keyboard-expected.txt	2020-12-15 14:34:27 UTC (rev 270838)
@@ -0,0 +1,8 @@
+PASS down arrow key did not scroll to second snap point
+PASS page down scrolled to the first snap point
+PASS down arrow key scrolled away from second div.
+PASS up arrow scrolled back to second div
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-vertical-with-keyboard.html (0 => 270838)


--- trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-vertical-with-keyboard.html	                        (rev 0)
+++ trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-vertical-with-keyboard.html	2020-12-15 14:34:27 UTC (rev 270838)
@@ -0,0 +1,95 @@
+<!DOCTYPE HTML>
+<html>
+    <head>
+        <style>
+            html {
+                scroll-snap-type: y proximity;
+            }
+            .verticalGallery {
+                width: 100vw;
+                height: 600vh;
+                margin: 0;
+                padding: 0;
+            }
+            .colorBox {
+                height: 100vh;
+                width: 100vw;
+                float: left;
+                scroll-snap-align: start;
+            }
+            #item0 { background-color: red; }
+            #item1 { background-color: green; }
+            #item2 { background-color: blue; }
+            #item3 { background-color: aqua; }
+            #item4 { background-color: yellow; }
+            #item5 { background-color: fuchsia; }
+        </style>
+        <script src=""
+        <script src=""
+        <script>
+        window.jsTestIsAsync = true;
+
+        async function runTests()
+        {
+            try {
+                await UIHelper.delayFor(0);
+
+                let scrollPositionBeforeSnap = document.scrollingElement.scrollTop;
+                let results = [];
+                await UIHelper.keyDown("downArrow");
+                await UIHelper.keyDown("downArrow");
+                var betweenFirstScrollPositionAndSecond =
+                    document.scrollingElement.scrollTop > scrollPositionBeforeSnap
+                    && document.scrollingElement.scrollTop < window.innerHeight;
+                results.push([betweenFirstScrollPositionAndSecond, "down arrow key did not scroll to second snap point"]);
+
+                await UIHelper.keyDown("pageDown");
+                results.push([document.scrollingElement.scrollTop == window.innerHeight, "page down scrolled to the first snap point"]);
+
+                // Directional scrolling should be able to escape a snap point.
+                await UIHelper.keyDown("downArrow");
+                await UIHelper.keyDown("downArrow");
+                await UIHelper.keyDown("downArrow");
+                var betweenSecondScrollPositionAndThird =
+                    document.scrollingElement.scrollTop > window.innerHeight
+                    && document.scrollingElement.scrollTop < (window.innerHeight * 2);
+                results.push([betweenSecondScrollPositionAndThird, "down arrow key scrolled away from second div."]);
+
+                // Reversing directions should snap back though.
+                await UIHelper.keyDown("upArrow");
+                results.push([document.scrollingElement.scrollTop == window.innerHeight, "up arrow scrolled back to second div"]);
+
+                // We avoid modifying the DOM until the test is over, because sometimes
+                // DOM modifications can reset the main frame scroll position.
+                results.forEach((result) => {
+                    expectTrue(result[0], result[1]);
+                });
+            } catch (e) {
+                console.log(e);
+            } finally {
+                finishJSTest();
+            }
+        }
+
+        function onLoad()
+        {
+            if (window.eventSender) {
+                runTests();
+            } else {
+                var messageLocation = document.getElementById('item0');
+                var message = document.createElement('div');
+                message.innerHTML = "<p>To run this test manually, scroll the page vertically with the keyboard.</p>";
+                messageLocation.appendChild(message);
+            }
+        }
+        </script>
+    </head>
+    <body _onload_="onLoad();" class="verticalGallery">
+        <div id="item0" class="colorBox"><div id="console"></div></div>
+        <div id="item1" class="colorBox"></div>
+        <div id="item2" class="colorBox"></div>
+        <div id="item3" class="colorBox"></div>
+        <div id="item4" class="colorBox"></div>
+        <div id="item5" class="colorBox"></div>
+    </body>
+</html>

Added: trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-overflow-rtl-with-keyboard-expected.txt (0 => 270838)


--- trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-overflow-rtl-with-keyboard-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-overflow-rtl-with-keyboard-expected.txt	2020-12-15 14:34:27 UTC (rev 270838)
@@ -0,0 +1,11 @@
+PASS scroll origin is 0
+PASS arrow keys allow escaping first snap position
+PASS right arrow key snapped back to first snap position
+PASS left arrow keys allow escaping second snap position
+PASS right arrow key snapped back to second snap position
+PASS right arrow keys allow escaping second snap position
+PASS left arrow key snapped back to second snap position
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-overflow-rtl-with-keyboard.html (0 => 270838)


--- trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-overflow-rtl-with-keyboard.html	                        (rev 0)
+++ trunk/LayoutTests/tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-overflow-rtl-with-keyboard.html	2020-12-15 14:34:27 UTC (rev 270838)
@@ -0,0 +1,114 @@
+<!DOCTYPE HTML>
+<html>
+    <head>
+        <style>
+            body {
+                margin: 0;
+                overflow: hidden;
+            }
+
+            #container {
+                width: 300px;
+                height: 300px;
+                position: absolute;
+                top: 0;
+                left: 0;
+                overflow-x: auto;
+                overflow-y: hidden;
+                scroll-snap-type: x proximity;
+                direction: rtl;
+            }
+
+            .drawer {
+                width: 900px;
+                height: 100%;
+            }
+
+            .area {
+                height: 300px;
+                width: 300px;
+                float: left;
+                scroll-snap-align: start;
+            }
+
+            .rtl {
+                direction: rtl;
+            }
+            .ltr {
+                direction: ltr;
+            }
+        </style>
+        <script src=""
+        <script src=""
+        <script>
+        window.jsTestIsAsync = true;
+
+        async function runTest()
+        {
+            try {
+                await UIHelper.delayFor(0);
+
+                eventSender.mouseMoveTo(150, 150);
+                eventSender.mouseDown();
+                eventSender.mouseUp();
+
+                let targetElement = document.getElementById("container");
+                expectTrue(0 == targetElement.scrollLeft, "scroll origin is 0");
+
+                eventSender.keyDown("leftArrow");
+                eventSender.keyDown("leftArrow");
+
+                let scrollPositionBetweenFirstAndSecondSnapPoint = targetElement.scrollLeft < 0 && targetElement.scrollLeft  > -300;
+                expectTrue(scrollPositionBetweenFirstAndSecondSnapPoint, "arrow keys allow escaping first snap position");
+
+                eventSender.keyDown("rightArrow");
+                expectTrue(targetElement.scrollLeft == 0, "right arrow key snapped back to first snap position");
+
+                // Move to the second scroll snap position.
+                targetElement.scrollLeft = -300;
+                await UIHelper.delayFor(0);
+
+                eventSender.keyDown("leftArrow");
+                eventSender.keyDown("leftArrow");
+                let scrollPositionBetweenSecondAndThirdSnapPoint = targetElement.scrollLeft < -300 && targetElement.scrollLeft  > -600;
+                expectTrue(scrollPositionBetweenSecondAndThirdSnapPoint, "left arrow keys allow escaping second snap position");
+
+                eventSender.keyDown("rightArrow");
+                expectTrue(targetElement.scrollLeft == -300, "right arrow key snapped back to second snap position");
+
+                eventSender.keyDown("rightArrow");
+                eventSender.keyDown("rightArrow");
+                scrollPositionBetweenFirstAndSecondSnapPoint = targetElement.scrollLeft < 0 && targetElement.scrollLeft  > -300;
+                expectTrue(scrollPositionBetweenFirstAndSecondSnapPoint, "right arrow keys allow escaping second snap position");
+
+                eventSender.keyDown("leftArrow");
+                expectTrue(targetElement.scrollLeft == -300, "left arrow key snapped back to second snap position");
+            } catch (e) {
+                console.log(e);
+            } finally {
+                finishJSTest();
+            }
+        }
+
+        function run() {
+            if (!window.eventSender) {
+                write("To manually test, verify that scrolling near one of the boundaries between the colored boxes");
+                write("snaps to the edge of the nearest colored box, but scrolling somewhere near the middle of two");
+                write("boxes does not cause the scroll offset to snap.");
+                return;
+            }
+            runTest();
+        }
+        </script>
+    </head>
+    <body _onload_=run()>
+        <div id="container">
+            <div class="drawer">
+                <div class="area rtl" style="background-color: red;"></div>
+                <div class="area ltr" style="background-color: blue;"></div>
+                <div class="area rtl" style="background-color: aqua;"></div>
+            </div>
+        </div>
+        <div id="output"></div>
+    </body>
+</html>

Modified: trunk/Source/WebCore/ChangeLog (270837 => 270838)


--- trunk/Source/WebCore/ChangeLog	2020-12-15 14:14:20 UTC (rev 270837)
+++ trunk/Source/WebCore/ChangeLog	2020-12-15 14:34:27 UTC (rev 270838)
@@ -1,3 +1,57 @@
+2020-12-15  Martin Robinson  <[email protected]>
+
+        CSS Scroll Snap is not in effect when the user scrolls via the keyboard
+        https://bugs.webkit.org/show_bug.cgi?id=198566
+        <rdar://problem/51488088>
+
+        Reviewed by Simon Fraser.
+
+        Add support for scroll snap for keyboard scrolling
+
+        This change adds support for the concept of directional scroll snapping,
+        which will never snap to a snap point that is away from the original
+        scroll offset in the opposite direction of the scroll direction. This
+        allows escaping snap points.
+
+        Tests: tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard-scaled.html
+               tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-horizontal-with-keyboard.html
+               tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard-scaled.html
+               tiled-drawing/scrolling/scroll-snap/scroll-snap-mandatory-mainframe-vertical-with-keyboard.html
+               tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-horizontal-with-keyboard.html
+               tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-mainframe-vertical-with-keyboard.html
+               tiled-drawing/scrolling/scroll-snap/scroll-snap-proximity-overflow-rtl-with-keyboard.html
+
+        * page/FrameView.cpp:
+        (WebCore::FrameView::pageScaleFactor const): Added.
+        * page/FrameView.h: Added scale factor getter.
+        * page/scrolling/ScrollSnapOffsetsInfo.cpp:
+        (WebCore::closestSnapOffset): Added a new optional argument that signals
+        directional snapping. This changes the behavior when choosing a snap offset
+        that is in the opposite direction as the scroll direction.
+        * page/scrolling/ScrollSnapOffsetsInfo.h:
+        * platform/ScrollAnimator.cpp:
+        (WebCore::ScrollAnimator::scroll):
+        * page/scrolling/ScrollSnapOffsetsInfo.h:
+        (WebCore::ScrollAnimator::scroll): No longer use the granularity argument.
+        (WebCore::ScrollAnimator::scrollWithoutAnimation): Remove the unused ScrollGranularity
+        argument and use the new positionFromStep helper.
+        (WebCore::ScrollAnimator::scrollWithDirectionalSnapping): Added.
+        (WebCore::ScrollAnimator::positionFromStep): Added this helper.
+        (WebCore::ScrollAnimator::handleWheelEvent): Remove the granularity argument.
+        (WebCore::ScrollAnimator::pageScaleFactor const): Added this implementation
+        of a ScrollControllerClient method.
+        * platform/ScrollAnimator.h: Update method declarations.
+        * platform/ScrollableArea.cpp:
+        (WebCore::ScrollableArea::scroll): Use the new argument to the scroll method.
+        * platform/cocoa/ScrollController.h: Added new method.
+        (WebCore::ScrollableArea::pageScaleFactor const):
+        * platform/cocoa/ScrollController.h:
+        * platform/cocoa/ScrollController.mm:
+        (WebCore::ScrollController::adjustScrollDestinationForDirectionalSnapping): Added.
+        * platform/mac/ScrollAnimatorMac.h:
+        * platform/mac/ScrollAnimatorMac.mm:
+        (WebCore::ScrollAnimatorMac::scroll): Use new positionFromStep helper.
+
 2020-12-15  Antoine Quint  <[email protected]>
 
         [Web Animations] Implied keyframes should not account for animations on siblings

Modified: trunk/Source/WebCore/page/FrameView.cpp (270837 => 270838)


--- trunk/Source/WebCore/page/FrameView.cpp	2020-12-15 14:14:20 UTC (rev 270837)
+++ trunk/Source/WebCore/page/FrameView.cpp	2020-12-15 14:34:27 UTC (rev 270838)
@@ -5532,6 +5532,11 @@
     }
 }
 
+float FrameView::pageScaleFactor() const
+{
+    return frame().frameScaleFactor();
+}
+
 } // namespace WebCore
 
 #undef PAGE_ID

Modified: trunk/Source/WebCore/page/FrameView.h (270837 => 270838)


--- trunk/Source/WebCore/page/FrameView.h	2020-12-15 14:14:20 UTC (rev 270837)
+++ trunk/Source/WebCore/page/FrameView.h	2020-12-15 14:34:27 UTC (rev 270838)
@@ -848,6 +848,9 @@
 
     void didFinishProhibitingScrollingWhenChangingContentSize() final;
 
+    // ScrollableArea.
+    float pageScaleFactor() const override;
+
     static MonotonicTime sCurrentPaintTimeStamp; // used for detecting decoded resource thrash in the cache
 
     const Ref<Frame> m_frame;

Modified: trunk/Source/WebCore/page/scrolling/ScrollSnapOffsetsInfo.cpp (270837 => 270838)


--- trunk/Source/WebCore/page/scrolling/ScrollSnapOffsetsInfo.cpp	2020-12-15 14:14:20 UTC (rev 270837)
+++ trunk/Source/WebCore/page/scrolling/ScrollSnapOffsetsInfo.cpp	2020-12-15 14:34:27 UTC (rev 270838)
@@ -100,7 +100,7 @@
 }
 
 template <typename LayoutType>
-LayoutType closestSnapOffset(const Vector<LayoutType>& snapOffsets, const Vector<ScrollOffsetRange<LayoutType>>& snapOffsetRanges, LayoutType scrollDestination, float velocity, unsigned& activeSnapIndex)
+static LayoutType closestSnapOffset(const Vector<LayoutType>& snapOffsets, const Vector<ScrollOffsetRange<LayoutType>>& snapOffsetRanges, LayoutType scrollDestination, float velocity, unsigned& activeSnapIndex, Optional<LayoutType> originalPositionForDirectionalSnapping)
 {
     ASSERT(snapOffsets.size());
     activeSnapIndex = 0;
@@ -132,32 +132,43 @@
     }
 
     // Non-zero velocity indicates a flick gesture. Even if another snap point is closer, we should choose the one in the direction of the flick gesture
-    // as long as a scroll snap offset range does not lie between the scroll destination and the targeted snap offset.
+    // as long as a scroll snap offset range does not lie between the scroll destination and the targeted snap offset. If we are doing directional
+    // snapping, we should never snap to a point that was on the other side of the original position in the opposite direction of this scroll.
+    // This allows directional scrolling to escape snap points.
     if (velocity < 0) {
-        if (lowerSnapOffsetRangeIndex != invalidSnapOffsetIndex && lowerSnapPosition < snapOffsetRanges[lowerSnapOffsetRangeIndex].end) {
+        if (lowerSnapOffsetRangeIndex == invalidSnapOffsetIndex || lowerSnapPosition >= snapOffsetRanges[lowerSnapOffsetRangeIndex].end) {
+            activeSnapIndex = lowerIndex;
+            return lowerSnapPosition;
+        }
+
+        if (!originalPositionForDirectionalSnapping.hasValue() || *originalPositionForDirectionalSnapping > upperSnapPosition) {
             activeSnapIndex = upperIndex;
             return upperSnapPosition;
         }
-        activeSnapIndex = lowerIndex;
-        return lowerSnapPosition;
+    } else {
+        if (upperSnapOffsetRangeIndex == invalidSnapOffsetIndex || snapOffsetRanges[upperSnapOffsetRangeIndex].start >= upperSnapPosition) {
+            activeSnapIndex = upperIndex;
+            return upperSnapPosition;
+        }
+
+        if (!originalPositionForDirectionalSnapping.hasValue() || *originalPositionForDirectionalSnapping < lowerSnapPosition) {
+            activeSnapIndex = lowerIndex;
+            return lowerSnapPosition;
+        }
     }
 
-    if (upperSnapOffsetRangeIndex != invalidSnapOffsetIndex && snapOffsetRanges[upperSnapOffsetRangeIndex].start < upperSnapPosition) {
-        activeSnapIndex = lowerIndex;
-        return lowerSnapPosition;
-    }
-    activeSnapIndex = upperIndex;
-    return upperSnapPosition;
+    activeSnapIndex = invalidSnapOffsetIndex;
+    return scrollDestination;
 }
 
-LayoutUnit closestSnapOffset(const Vector<LayoutUnit>& snapOffsets, const Vector<ScrollOffsetRange<LayoutUnit>>& snapOffsetRanges, LayoutUnit scrollDestination, float velocity, unsigned& activeSnapIndex)
+LayoutUnit closestSnapOffset(const Vector<LayoutUnit>& snapOffsets, const Vector<ScrollOffsetRange<LayoutUnit>>& snapOffsetRanges, LayoutUnit scrollDestination, float velocity, unsigned& activeSnapIndex, Optional<LayoutUnit> originalPositionForDirectionalSnapping)
 {
-    return closestSnapOffset<LayoutUnit>(snapOffsets, snapOffsetRanges, scrollDestination, velocity, activeSnapIndex);
+    return closestSnapOffset<LayoutUnit>(snapOffsets, snapOffsetRanges, scrollDestination, velocity, activeSnapIndex, originalPositionForDirectionalSnapping);
 }
 
-float closestSnapOffset(const Vector<float>& snapOffsets, const Vector<ScrollOffsetRange<float>>& snapOffsetRanges, float scrollDestination, float velocity, unsigned& activeSnapIndex)
+float closestSnapOffset(const Vector<float>& snapOffsets, const Vector<ScrollOffsetRange<float>>& snapOffsetRanges, float scrollDestination, float velocity, unsigned& activeSnapIndex, Optional<float> originalPositionForDirectionalSnapping)
 {
-    return closestSnapOffset<float>(snapOffsets, snapOffsetRanges, scrollDestination, velocity, activeSnapIndex);
+    return closestSnapOffset<float>(snapOffsets, snapOffsetRanges, scrollDestination, velocity, activeSnapIndex, originalPositionForDirectionalSnapping);
 }
 
 enum class InsetOrOutset {

Modified: trunk/Source/WebCore/page/scrolling/ScrollSnapOffsetsInfo.h (270837 => 270838)


--- trunk/Source/WebCore/page/scrolling/ScrollSnapOffsetsInfo.h	2020-12-15 14:14:20 UTC (rev 270837)
+++ trunk/Source/WebCore/page/scrolling/ScrollSnapOffsetsInfo.h	2020-12-15 14:34:27 UTC (rev 270838)
@@ -29,6 +29,7 @@
 
 #include "LayoutUnit.h"
 #include "StyleScrollSnapPoints.h"
+#include <wtf/Optional.h>
 #include <wtf/Vector.h>
 
 namespace WebCore {
@@ -65,8 +66,8 @@
 // the scrolling container's border box.
 void updateSnapOffsetsForScrollableArea(ScrollableArea&, const RenderBox& scrollingElementBox, const RenderStyle& scrollingElementStyle, LayoutRect viewportRectInBorderBoxCoordinates);
 
-WEBCORE_EXPORT LayoutUnit closestSnapOffset(const Vector<LayoutUnit>& snapOffsets, const Vector<ScrollOffsetRange<LayoutUnit>>& snapOffsetRanges, LayoutUnit scrollDestination, float velocity, unsigned& activeSnapIndex);
-WEBCORE_EXPORT float closestSnapOffset(const Vector<float>& snapOffsets, const Vector<ScrollOffsetRange<float>>& snapOffsetRanges, float scrollDestination, float velocity, unsigned& activeSnapIndex);
+WEBCORE_EXPORT LayoutUnit closestSnapOffset(const Vector<LayoutUnit>& snapOffsets, const Vector<ScrollOffsetRange<LayoutUnit>>& snapOffsetRanges, LayoutUnit scrollDestination, float velocity, unsigned& activeSnapIndex, Optional<LayoutUnit> originalPositionForDirectionalSnapping = WTF::nullopt);
+WEBCORE_EXPORT float closestSnapOffset(const Vector<float>& snapOffsets, const Vector<ScrollOffsetRange<float>>& snapOffsetRanges, float scrollDestination, float velocity, unsigned& activeSnapIndex, Optional<float> originalPositionForDirectionalSnapping = WTF::nullopt);
 
 }; // namespace WebCore
 

Modified: trunk/Source/WebCore/platform/ScrollAnimator.cpp (270837 => 270838)


--- trunk/Source/WebCore/platform/ScrollAnimator.cpp	2020-12-15 14:14:20 UTC (rev 270837)
+++ trunk/Source/WebCore/platform/ScrollAnimator.cpp	2020-12-15 14:34:27 UTC (rev 270838)
@@ -80,21 +80,37 @@
 #endif
 }
 
-bool ScrollAnimator::scroll(ScrollbarOrientation orientation, ScrollGranularity granularity, float step, float multiplier)
+bool ScrollAnimator::scroll(ScrollbarOrientation orientation, ScrollGranularity granularity, float step, float multiplier, ScrollBehavior behavior)
 {
-    return scrollWithoutAnimation(orientation, granularity, step, multiplier);
+#if ENABLE(CSS_SCROLL_SNAP)
+    if (behavior != ScrollBehavior::DoDirectionalSnapping)
+        return scrollWithoutAnimation(orientation, step, multiplier);
+
+    FloatSize scrollOrigin = toFloatSize(m_scrollableArea.scrollOrigin());
+    FloatPoint newPosition = positionFromStep(orientation, step, multiplier);
+    auto newOffset = ScrollableArea::scrollOffsetFromPosition(newPosition, scrollOrigin);
+    auto currentOffset = ScrollableArea::scrollOffsetFromPosition(this->currentPosition(), scrollOrigin);
+    if (orientation == HorizontalScrollbar) {
+        newOffset.setX(m_scrollController.adjustScrollDestinationForDirectionalSnapping(ScrollEventAxis::Horizontal, newOffset.x(), multiplier, currentOffset.x()));
+        newPosition = ScrollableArea::scrollPositionFromOffset(newOffset, scrollOrigin);
+        return scroll(HorizontalScrollbar, granularity, newPosition.x() - this->currentPosition().x(), 1.0);
+    }
+
+    newOffset.setY(m_scrollController.adjustScrollDestinationForDirectionalSnapping(ScrollEventAxis::Vertical, newPosition.y(), multiplier, currentOffset.y()));
+    newPosition = ScrollableArea::scrollPositionFromOffset(newOffset, scrollOrigin);
+    return scroll(VerticalScrollbar, granularity, newPosition.y() - this->currentPosition().y(), 1.0);
+#else
+    UNUSED_PARAM(granularity);
+    UNUSED_PARAM(behavior);
+    return scrollWithoutAnimation(orientation, step, multiplier);
+#endif
 }
 
-bool ScrollAnimator::scrollWithoutAnimation(ScrollbarOrientation orientation, ScrollGranularity, float step, float multiplier)
+bool ScrollAnimator::scrollWithoutAnimation(ScrollbarOrientation orientation, float step, float multiplier)
 {
     FloatPoint currentPosition = this->currentPosition();
-    FloatSize delta;
-    if (orientation == HorizontalScrollbar)
-        delta.setWidth(step * multiplier);
-    else
-        delta.setHeight(step * multiplier);
-
-    FloatPoint newPosition = FloatPoint(currentPosition + delta).constrainedBetween(m_scrollableArea.minimumScrollPosition(), m_scrollableArea.maximumScrollPosition());
+    FloatPoint newPosition = positionFromStep(orientation, step, multiplier);
+    newPosition = newPosition.constrainedBetween(m_scrollableArea.minimumScrollPosition(), m_scrollableArea.maximumScrollPosition());
     if (currentPosition == newPosition)
         return false;
 
@@ -120,6 +136,16 @@
     updateActiveScrollSnapIndexForOffset();
 }
 
+FloatPoint ScrollAnimator::positionFromStep(ScrollbarOrientation orientation, float step, float multiplier)
+{
+    FloatSize delta;
+    if (orientation == HorizontalScrollbar)
+        delta.setWidth(step * multiplier);
+    else
+        delta.setHeight(step * multiplier);
+    return this->currentPosition() + delta;
+}
+
 #if ENABLE(CSS_SCROLL_SNAP)
 #if PLATFORM(MAC)
 bool ScrollAnimator::processWheelEventForScrollSnap(const PlatformWheelEvent& wheelEvent)
@@ -182,7 +208,7 @@
                     deltaY = -deltaY;
             }
             if (e.hasPreciseScrollingDeltas())
-                scrollWithoutAnimation(VerticalScrollbar, granularity, verticalScrollbar->pixelStep(), -deltaY);
+                scrollWithoutAnimation(VerticalScrollbar, verticalScrollbar->pixelStep(), -deltaY);
             else
                 scroll(VerticalScrollbar, granularity, verticalScrollbar->pixelStep(), -deltaY);
         }
@@ -195,7 +221,7 @@
                     deltaX = -deltaX;
             }
             if (e.hasPreciseScrollingDeltas())
-                scrollWithoutAnimation(HorizontalScrollbar, granularity, horizontalScrollbar->pixelStep(), -deltaX);
+                scrollWithoutAnimation(HorizontalScrollbar, horizontalScrollbar->pixelStep(), -deltaX);
             else
                 scroll(HorizontalScrollbar, granularity, horizontalScrollbar->pixelStep(), -deltaX);
         }
@@ -271,6 +297,10 @@
     return m_scrollableArea.visibleSize();
 }
 
+float ScrollAnimator::pageScaleFactor() const
+{
+    return m_scrollableArea.pageScaleFactor();
+}
 #endif
 
 #if ENABLE(CSS_SCROLL_SNAP) || ENABLE(RUBBER_BANDING)

Modified: trunk/Source/WebCore/platform/ScrollAnimator.h (270837 => 270838)


--- trunk/Source/WebCore/platform/ScrollAnimator.h	2020-12-15 14:14:20 UTC (rev 270837)
+++ trunk/Source/WebCore/platform/ScrollAnimator.h	2020-12-15 14:34:27 UTC (rev 270838)
@@ -67,12 +67,17 @@
     explicit ScrollAnimator(ScrollableArea&);
     virtual ~ScrollAnimator();
 
+    enum ScrollBehavior {
+        Default,
+        DoDirectionalSnapping,
+    };
+
     // Computes a scroll destination for the given parameters.  Returns false if
     // already at the destination.  Otherwise, starts scrolling towards the
     // destination and returns true.  Scrolling may be immediate or animated.
     // The base class implementation always scrolls immediately, never animates.
-    virtual bool scroll(ScrollbarOrientation, ScrollGranularity, float step, float multiplier);
-    bool scrollWithoutAnimation(ScrollbarOrientation, ScrollGranularity, float step, float multiplier);
+    virtual bool scroll(ScrollbarOrientation, ScrollGranularity, float step, float multiplier, ScrollBehavior = ScrollBehavior::Default);
+    bool scrollWithoutAnimation(ScrollbarOrientation, float step, float multiplier);
 
     void scrollToOffset(const FloatPoint&);
     virtual void scrollToOffsetWithoutAnimation(const FloatPoint&, ScrollClamping = ScrollClamping::Clamped);
@@ -133,11 +138,6 @@
 
     void setWheelEventTestMonitor(RefPtr<WheelEventTestMonitor>&& testMonitor) { m_wheelEventTestMonitor = testMonitor; }
 
-#if (ENABLE(CSS_SCROLL_SNAP) || ENABLE(RUBBER_BANDING)) && PLATFORM(MAC)
-    void deferWheelEventTestCompletionForReason(WheelEventTestMonitor::ScrollableAreaIdentifier, WheelEventTestMonitor::DeferReason) const override;
-    void removeWheelEventTestCompletionDeferralForReason(WheelEventTestMonitor::ScrollableAreaIdentifier, WheelEventTestMonitor::DeferReason) const override;
-#endif
-
 #if ENABLE(CSS_SCROLL_SNAP) || ENABLE(RUBBER_BANDING)
     std::unique_ptr<ScrollControllerTimer> createTimer(Function<void()>&&) final;
 #endif
@@ -147,17 +147,27 @@
     bool processWheelEventForScrollSnap(const PlatformWheelEvent&);
 #endif
     void updateScrollSnapState();
+    bool activeScrollSnapIndexDidChange() const;
+    unsigned activeScrollSnapIndexForAxis(ScrollEventAxis) const;
+#endif
+
+    // ScrollControllerClient.
+#if ENABLE(CSS_SCROLL_SNAP)
     FloatPoint scrollOffset() const override;
     void immediateScrollOnAxis(ScrollEventAxis, float delta) override;
-    bool activeScrollSnapIndexDidChange() const;
-    unsigned activeScrollSnapIndexForAxis(ScrollEventAxis) const;
+    float pageScaleFactor() const override;
     LayoutSize scrollExtent() const override;
     FloatSize viewportSize() const override;
 #endif
+#if (ENABLE(CSS_SCROLL_SNAP) || ENABLE(RUBBER_BANDING)) && PLATFORM(MAC)
+    void deferWheelEventTestCompletionForReason(WheelEventTestMonitor::ScrollableAreaIdentifier, WheelEventTestMonitor::DeferReason) const override;
+    void removeWheelEventTestCompletionDeferralForReason(WheelEventTestMonitor::ScrollableAreaIdentifier, WheelEventTestMonitor::DeferReason) const override;
+#endif
 
 protected:
     virtual void notifyPositionChanged(const FloatSize& delta);
     void updateActiveScrollSnapIndexForOffset();
+    FloatPoint positionFromStep(ScrollbarOrientation, float step, float multiplier);
 
     ScrollableArea& m_scrollableArea;
     RefPtr<WheelEventTestMonitor> m_wheelEventTestMonitor;

Modified: trunk/Source/WebCore/platform/ScrollableArea.cpp (270837 => 270838)


--- trunk/Source/WebCore/platform/ScrollableArea.cpp	2020-12-15 14:14:20 UTC (rev 270837)
+++ trunk/Source/WebCore/platform/ScrollableArea.cpp	2020-12-15 14:34:27 UTC (rev 270838)
@@ -140,7 +140,7 @@
         multiplier = -multiplier;
 
     step = adjustScrollStepForFixedContent(step, orientation, granularity);
-    return scrollAnimator().scroll(orientation, granularity, step, multiplier);
+    return scrollAnimator().scroll(orientation, granularity, step, multiplier, ScrollAnimator::ScrollBehavior::DoDirectionalSnapping);
 }
 
 void ScrollableArea::scrollToOffsetWithAnimation(const FloatPoint& offset, ScrollClamping)

Modified: trunk/Source/WebCore/platform/ScrollableArea.h (270837 => 270838)


--- trunk/Source/WebCore/platform/ScrollableArea.h	2020-12-15 14:14:20 UTC (rev 270837)
+++ trunk/Source/WebCore/platform/ScrollableArea.h	2020-12-15 14:34:27 UTC (rev 270838)
@@ -355,6 +355,11 @@
     
     virtual String debugDescription() const = 0;
 
+    virtual float pageScaleFactor() const
+    {
+        return 1.0f;
+    }
+
 protected:
     WEBCORE_EXPORT ScrollableArea();
     WEBCORE_EXPORT virtual ~ScrollableArea();

Modified: trunk/Source/WebCore/platform/cocoa/ScrollController.h (270837 => 270838)


--- trunk/Source/WebCore/platform/cocoa/ScrollController.h	2020-12-15 14:14:20 UTC (rev 270837)
+++ trunk/Source/WebCore/platform/cocoa/ScrollController.h	2020-12-15 14:34:27 UTC (rev 270838)
@@ -108,10 +108,7 @@
     virtual void willStartScrollSnapAnimation() { }
     virtual void didStopScrollSnapAnimation() { }
 
-    virtual float pageScaleFactor() const
-    {
-        return 1.0f;
-    }
+    virtual float pageScaleFactor() const = 0;
 
     virtual unsigned activeScrollOffsetIndex(ScrollEventAxis) const
     {
@@ -167,9 +164,8 @@
     void setScrollSnapIndexDidChange(bool state) { m_activeScrollSnapIndexDidChange = state; }
     unsigned activeScrollSnapIndexForAxis(ScrollEventAxis) const;
     void updateScrollSnapState(const ScrollableArea&);
-
     void updateGestureInProgressState(const PlatformWheelEvent&);
-
+    float adjustScrollDestinationForDirectionalSnapping(ScrollEventAxis, float destination, float velocity, float originalPosition);
 #if PLATFORM(MAC)
     bool processWheelEventForScrollSnap(const PlatformWheelEvent&);
 #endif

Modified: trunk/Source/WebCore/platform/cocoa/ScrollController.mm (270837 => 270838)


--- trunk/Source/WebCore/platform/cocoa/ScrollController.mm	2020-12-15 14:14:20 UTC (rev 270837)
+++ trunk/Source/WebCore/platform/cocoa/ScrollController.mm	2020-12-15 14:34:27 UTC (rev 270838)
@@ -905,6 +905,22 @@
     setActiveScrollSnapIndexForAxis(axis, activeIndex);
 }
 
+float ScrollController::adjustScrollDestinationForDirectionalSnapping(ScrollEventAxis axis, float destination, float velocity, float originalPosition)
+{
+    if (!usesScrollSnap())
+        return destination;
+
+    ScrollSnapAnimatorState& snapState = *m_scrollSnapState;
+    auto snapOffsets = snapState.snapOffsetsForAxis(axis);
+    if (!snapOffsets.size())
+        return destination;
+
+    unsigned snapIndex;
+    LayoutUnit offset = closestSnapOffset(snapState.snapOffsetsForAxis(axis), snapState.snapOffsetRangesForAxis(axis), LayoutUnit(destination / m_client.pageScaleFactor()), velocity, snapIndex, LayoutUnit(originalPosition / m_client.pageScaleFactor()));
+    return offset * m_client.pageScaleFactor();
+}
+
+
 void ScrollController::setActiveScrollSnapIndicesForOffset(ScrollOffset offset)
 {
     if (!usesScrollSnap())

Modified: trunk/Source/WebCore/platform/generic/ScrollAnimatorGeneric.cpp (270837 => 270838)


--- trunk/Source/WebCore/platform/generic/ScrollAnimatorGeneric.cpp	2020-12-15 14:14:20 UTC (rev 270837)
+++ trunk/Source/WebCore/platform/generic/ScrollAnimatorGeneric.cpp	2020-12-15 14:34:27 UTC (rev 270838)
@@ -94,11 +94,16 @@
 #endif
 
 #if ENABLE(SMOOTH_SCROLLING)
-bool ScrollAnimatorGeneric::scroll(ScrollbarOrientation orientation, ScrollGranularity granularity, float step, float multiplier)
+bool ScrollAnimatorGeneric::scroll(ScrollbarOrientation orientation, ScrollGranularity granularity, float step, float multiplier, ScrollBehavior behavior)
 {
     if (!m_scrollableArea.scrollAnimatorEnabled())
         return ScrollAnimator::scroll(orientation, granularity, step, multiplier);
 
+    // This method doesn't do directional snapping, but our base class does. It will call into
+    // ScrollAnimatorGeneric::scroll again with the snapped positions and ScrollBehavior::Default.
+    if (behavior == ScrollBehavior::DoDirectionalSnapping)
+        return ScrollAnimator::scroll(orientation, granularity, step, multiplier, behavior);
+
     ensureSmoothScrollingAnimation();
     return m_smoothAnimation->scroll(orientation, granularity, step, multiplier);
 }

Modified: trunk/Source/WebCore/platform/generic/ScrollAnimatorGeneric.h (270837 => 270838)


--- trunk/Source/WebCore/platform/generic/ScrollAnimatorGeneric.h	2020-12-15 14:14:20 UTC (rev 270837)
+++ trunk/Source/WebCore/platform/generic/ScrollAnimatorGeneric.h	2020-12-15 14:34:27 UTC (rev 270838)
@@ -45,7 +45,7 @@
 
 private:
 #if ENABLE(SMOOTH_SCROLLING)
-    bool scroll(ScrollbarOrientation, ScrollGranularity, float step, float multiplier) override;
+    bool scroll(ScrollbarOrientation, ScrollGranularity, float step, float multiplier, ScrollBehavior) override;
 #endif
     void scrollToOffsetWithoutAnimation(const FloatPoint&, ScrollClamping) override;
     void willEndLiveResize() override;

Modified: trunk/Source/WebCore/platform/mac/ScrollAnimatorMac.h (270837 => 270838)


--- trunk/Source/WebCore/platform/mac/ScrollAnimatorMac.h	2020-12-15 14:14:20 UTC (rev 270837)
+++ trunk/Source/WebCore/platform/mac/ScrollAnimatorMac.h	2020-12-15 14:34:27 UTC (rev 270838)
@@ -79,7 +79,7 @@
     Timer m_sendContentAreaScrolledTimer;
     FloatSize m_contentAreaScrolledTimerScrollDelta;
 
-    bool scroll(ScrollbarOrientation, ScrollGranularity, float step, float multiplier) override;
+    bool scroll(ScrollbarOrientation, ScrollGranularity, float step, float multiplier, ScrollBehavior) override;
     void scrollToOffsetWithoutAnimation(const FloatPoint&, ScrollClamping) override;
 
 #if ENABLE(RUBBER_BANDING)

Modified: trunk/Source/WebCore/platform/mac/ScrollAnimatorMac.mm (270837 => 270838)


--- trunk/Source/WebCore/platform/mac/ScrollAnimatorMac.mm	2020-12-15 14:14:20 UTC (rev 270837)
+++ trunk/Source/WebCore/platform/mac/ScrollAnimatorMac.mm	2020-12-15 14:34:27 UTC (rev 270838)
@@ -779,24 +779,24 @@
 }
 #endif
 
-bool ScrollAnimatorMac::scroll(ScrollbarOrientation orientation, ScrollGranularity granularity, float step, float multiplier)
+bool ScrollAnimatorMac::scroll(ScrollbarOrientation orientation, ScrollGranularity granularity, float step, float multiplier, ScrollBehavior behavior)
 {
     m_haveScrolledSincePageLoad = true;
 
     if (!scrollAnimationEnabledForSystem() || !m_scrollableArea.scrollAnimatorEnabled())
-        return ScrollAnimator::scroll(orientation, granularity, step, multiplier);
+        return ScrollAnimator::scroll(orientation, granularity, step, multiplier, behavior);
 
     if (granularity == ScrollByPixel)
-        return ScrollAnimator::scroll(orientation, granularity, step, multiplier);
+        return ScrollAnimator::scroll(orientation, granularity, step, multiplier, behavior);
 
+    // This method doesn't do directional snapping, but our base class does. It will call into
+    // ScrollAnimatorMac::scroll again with the snapped positions and ScrollBehavior::Default.
+    if (behavior == ScrollBehavior::DoDirectionalSnapping)
+        return ScrollAnimator::scroll(orientation, granularity, step, multiplier, behavior);
+
     FloatPoint currentPosition = this->currentPosition();
-    FloatSize delta;
-    if (orientation == HorizontalScrollbar)
-        delta.setWidth(step * multiplier);
-    else
-        delta.setHeight(step * multiplier);
-
-    FloatPoint newPosition = FloatPoint(currentPosition + delta).constrainedBetween(m_scrollableArea.minimumScrollPosition(), m_scrollableArea.maximumScrollPosition());
+    FloatPoint newPosition = positionFromStep(orientation, step, multiplier);
+    newPosition = newPosition.constrainedBetween(m_scrollableArea.minimumScrollPosition(), m_scrollableArea.maximumScrollPosition());
     if (currentPosition == newPosition)
         return false;
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to