Skip to content

Truly independent web browser

2.9 KB
Raw Blame
<!DOCTYPE html>
<script src="include.js"></script>
<style>
    #scroller {
        width: 200px;
        height: 200px;
        overflow: scroll;
        scroll-snap-type: y mandatory;
        position: relative;
    }
    .area {
        position: absolute;
        width: 50%;
        height: 200px;
        scroll-snap-align: start;
    }
</style>
<div id="scroller" tabindex="0">
    <div class="area" style="top: 0"></div>
    <div class="area" style="top: 500px; scroll-snap-stop: always"></div>
    <div class="area" style="top: 800px"></div>
    <div class="area" style="top: 1500px"></div>
</div>
<script>
    promiseTest(async () => {
        const scroller = document.getElementById("scroller");

        await animationFrame();
        await animationFrame();

        // An arrow key press leaves a direction-only scroll intent behind.
        await scrollSettled(scroller, () => internals.sendKey(scroller, "Down"));
        println(`scroll offset after a down arrow key press: ${scroller.scrollLeft},${scroller.scrollTop}`);

        // Dragging the scrollbar thumb is an absolute scroll, so it snaps to the position nearest the drop point. The
        // drag ends beyond the gutter so that every update along the way clamps to the maximal scroll position. The
        // press is on the thumb, as a press beside it pages instead.
        const rect = scroller.getBoundingClientRect();
        const dragThumbToTheEnd = async () => {
            const thumbY = rect.top + 10 + (scroller.scrollTop / scroller.scrollHeight) * rect.height;
            await internals.mouseEventThroughUIProcess("mousedown", rect.right - 5, thumbY);
            await internals.mouseEventThroughUIProcess("mousemove", rect.right - 5, rect.bottom + 200, { buttons: 1 });
            await internals.mouseEventThroughUIProcess("mouseup", rect.right - 5, rect.bottom + 200);
        };
        await scrollSettled(scroller, dragThumbToTheEnd);
        println(`scroll offset after dragging the scrollbar to the end: ${scroller.scrollLeft},${scroller.scrollTop}`);

        scroller.scrollTo({ top: 0, behavior: "instant" });
        await animationFrame();
        await animationFrame();

        // A flick hands the scrolling over to momentum, which travels over the snap positions it passes.
        await scrollSettled(scroller, async () => {
            await internals.wheel(50, 50, 0, 20, true, "momentum");
            await internals.wheel(50, 50, 0, 0, true, "ended");
        });
        println(`scroll offset after the momentum gesture ends: ${scroller.scrollLeft},${scroller.scrollTop}`);

        // The next drag reports no gesture phase of its own, so the momentum rules of the gesture before it do not
        // apply to it.
        await scrollSettled(scroller, dragThumbToTheEnd);
        println(`scroll offset after dragging the scrollbar to the end again: ${scroller.scrollLeft},${scroller.scrollTop}`);
    });
</script>