aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/WheelScroll.qml
blob: 845a8ffccd8ee74eb4151900dc01bcbd3e27108b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

import QtQuick

// Mouse wheel scrolling for a Flickable: a fixed distance per notch,
// animated, the same up and down. Qt's own wheel handling took a long
// list dozens of notches whatever maximumFlickVelocity said. Notches that arrive mid-animation add to its
// end point, so a fast spin travels further.
//
// angleDelta only. Under Wayland a mouse notch arrives as angleDelta 120
// with pixelDelta 15 and a device typed touchpad (measured), so trusting
// a non-zero pixelDelta, as Flickable does, scrolled 15px a notch.
// ponytail: a real touchpad then scrolls by step too; tell the two apart
// (e.g. pixelDelta != angleDelta / 8) if one is ever used here.
//
// Takes no buttons, so clicks reach what is under it; wheel events that
// an inner item (a slider, a nested list) accepts never reach it.
MouseArea {
    id: ws
    property Flickable flick
    // Pixels per notch.
    property real step: 150

    acceptedButtons: Qt.NoButton

    function clamp(y) {
        return Math.max(flick.originY, Math.min(y,
            flick.originY + Math.max(0, flick.contentHeight - flick.height)));
    }

    NumberAnimation {
        id: anim
        target: ws.flick
        property: "contentY"
        duration: 180
        easing.type: Easing.OutCubic
    }

    onWheel: wheel => {
        flick.cancelFlick();
        const from = anim.running ? anim.to : flick.contentY;
        anim.stop();
        anim.to = clamp(from - wheel.angleDelta.y / 120 * step);
        anim.start();
    }
}