summaryrefslogtreecommitdiff
path: root/components/IMouseArea.qml
blob: 4c360ed19a7760ac2f80ff19b5926669e633c1cb (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
/// NOTE: This is not zero cost, there's performance penalty, only use this instead of mousearea if hover is strictly required to be still
/// Such as when the component may appear where the mouse is already is

import QtQuick

MouseArea {
    hoverEnabled: true

    property real gx: Number.MAX_VALUE
    property real gy: Number.MAX_VALUE

    property bool hovered: false

    signal entered_
    signal positionChanged_(var mouse)

    onPositionChanged: mouse => {
        let gpos = mapToItem(null, mouse.x, mouse.y);

        if (gx === Number.MAX_VALUE && gy === Number.MAX_VALUE) {
            gx = gpos.x;
            gy = gpos.y;
            return;
        }

        if (gpos.x === gx && gpos.y === gy)
            return;

        gx = gpos.x;
        gy = gpos.y;

        if (!hovered) {
            hovered = true;
            entered_();
        }

        positionChanged_(mouse);
    }

    onExited: {
        hovered = false;
        gx = Number.MAX_VALUE;
        gy = Number.MAX_VALUE;
    }
}