File: ResizeArea.qml

package info (click to toggle)
qtmir 0.8.0~git20230223.bd21224-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,568 kB
  • sloc: cpp: 22,847; python: 304; xml: 271; ansic: 87; makefile: 20; sh: 12
file content (128 lines) | stat: -rw-r--r-- 3,644 bytes parent folder | download | duplicates (5)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import QtQuick 2.4
import QtMir.Application 0.1

MouseArea {
    id: root

    // to be set from outside
    property Item target
    property real borderThickness

    property bool leftBorder: false
    property bool rightBorder: false
    property bool topBorder: false
    property bool bottomBorder: false

    property bool dragging: false
    property real startX
    property real startY
    property real startWidth
    property real startHeight

    hoverEnabled: true

    property string cursorName: {
        if (containsMouse || pressed) {
            if (leftBorder && !topBorder && !bottomBorder) {
                return "left_side";
            } else if (rightBorder && !topBorder && !bottomBorder) {
                return "right_side";
            } else if (topBorder && !leftBorder && !rightBorder) {
                return "top_side";
            } else if (bottomBorder && !leftBorder && !rightBorder) {
                return "bottom_side";
            } else if (leftBorder && topBorder) {
                return "top_left_corner";
            } else if (leftBorder && bottomBorder) {
                return "bottom_left_corner";
            } else if (rightBorder && topBorder) {
                return "top_right_corner";
            } else if (rightBorder && bottomBorder) {
                return "bottom_right_corner";
            } else {
                return "";
            }
        } else {
            return "";
        }
    }
    onCursorNameChanged: {
        Mir.cursorName = cursorName;
    }

    function updateBorders() {
        leftBorder = mouseX <= borderThickness;
        rightBorder = mouseX >= width - borderThickness;
        topBorder = mouseY <= borderThickness;
        bottomBorder = mouseY >= height - borderThickness;
    }

    onPressedChanged: {
        if (pressed) {
            var pos = mapToItem(target.parent, mouseX, mouseY);
            startX = pos.x;
            startY = pos.y;
            startWidth = target.width;
            startHeight = target.height;
            dragging = true;
        } else {
            dragging = false;
            if (containsMouse) {
                updateBorders();
            }
        }
    }

    onEntered: {
        if (!pressed) {
            updateBorders();
        }
    }

    onPositionChanged: {
        if (!pressed) {
            updateBorders();
        }

        if (!dragging) {
            return;
        }

        var pos = mapToItem(target.parent, mouse.x, mouse.y);

        if (leftBorder) {
            if (startX + startWidth - pos.x > target.minWidth) {
                target.x = pos.x;
                target.width = startX + startWidth - target.x;
                startX = target.x;
                startWidth = target.width;
            }

        } else if (rightBorder) {
            var deltaX = pos.x - startX;
            if (startWidth + deltaX >= target.minWidth) {
                target.width = startWidth + deltaX;
            } else {
                target.width = target.minWidth;
            }
        }

        if (topBorder) {
            if (startY + startHeight - pos.y > target.minHeight) {
                target.y = pos.y;
                target.height = startY + startHeight - target.y;
                startY = target.y;
                startHeight = target.height;
            }

        } else if (bottomBorder) {
            var deltaY = pos.y - startY;
            if (startHeight + deltaY >= target.minHeight) {
                target.height = startHeight + deltaY;
            } else {
                target.height = target.minHeight;
            }
        }
    }
}