~dandrader/qtmir/mousePointer

« back to all changes in this revision

Viewing changes to demos/qml-demo-shell/ResizeArea.qml

  • Committer: Daniel d'Andrada
  • Date: 2015-09-22 20:22:20 UTC
  • Revision ID: daniel.dandrada@canonical.com-20150922202220-xt122anm8giiej73
Shell draws its own cursor using the new Cursor QML element

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import QtQuick 2.4
 
2
import Unity.Application 0.1
 
3
 
 
4
MouseArea {
 
5
    id: root
 
6
 
 
7
    // to be set from outside
 
8
    property Item target
 
9
    property real borderThickness
 
10
 
 
11
    property bool leftBorder: false
 
12
    property bool rightBorder: false
 
13
    property bool topBorder: false
 
14
    property bool bottomBorder: false
 
15
 
 
16
    property bool dragging: false
 
17
    property real startX
 
18
    property real startY
 
19
    property real startWidth
 
20
    property real startHeight
 
21
 
 
22
    hoverEnabled: true
 
23
 
 
24
    property string cursorName: {
 
25
        if (containsMouse || pressed) {
 
26
            if (leftBorder && !topBorder && !bottomBorder) {
 
27
                return "left_side";
 
28
            } else if (rightBorder && !topBorder && !bottomBorder) {
 
29
                return "right_side";
 
30
            } else if (topBorder && !leftBorder && !rightBorder) {
 
31
                return "top_side";
 
32
            } else if (bottomBorder && !leftBorder && !rightBorder) {
 
33
                return "bottom_side";
 
34
            } else if (leftBorder && topBorder) {
 
35
                return "top_left_corner";
 
36
            } else if (leftBorder && bottomBorder) {
 
37
                return "bottom_left_corner";
 
38
            } else if (rightBorder && topBorder) {
 
39
                return "top_right_corner";
 
40
            } else if (rightBorder && bottomBorder) {
 
41
                return "bottom_right_corner";
 
42
            } else {
 
43
                return "";
 
44
            }
 
45
        } else {
 
46
            return "";
 
47
        }
 
48
    }
 
49
    onCursorNameChanged: {
 
50
        Mir.cursorName = cursorName;
 
51
    }
 
52
 
 
53
    function updateBorders() {
 
54
        leftBorder = mouseX <= borderThickness;
 
55
        rightBorder = mouseX >= width - borderThickness;
 
56
        topBorder = mouseY <= borderThickness;
 
57
        bottomBorder = mouseY >= height - borderThickness;
 
58
    }
 
59
 
 
60
    onPressedChanged: {
 
61
        if (pressed) {
 
62
            var pos = mapToItem(target.parent, mouseX, mouseY);
 
63
            startX = pos.x;
 
64
            startY = pos.y;
 
65
            startWidth = target.width;
 
66
            startHeight = target.height;
 
67
            dragging = true;
 
68
        } else {
 
69
            dragging = false;
 
70
            if (containsMouse) {
 
71
                updateBorders();
 
72
            }
 
73
        }
 
74
    }
 
75
 
 
76
    onEntered: {
 
77
        if (!pressed) {
 
78
            updateBorders();
 
79
        }
 
80
    }
 
81
 
 
82
    onPositionChanged: {
 
83
        if (!pressed) {
 
84
            updateBorders();
 
85
        }
 
86
 
 
87
        if (!dragging) {
 
88
            return;
 
89
        }
 
90
 
 
91
        var pos = mapToItem(target.parent, mouse.x, mouse.y);
 
92
 
 
93
        if (leftBorder) {
 
94
            if (startX + startWidth - pos.x > target.minWidth) {
 
95
                target.x = pos.x;
 
96
                target.width = startX + startWidth - target.x;
 
97
                startX = target.x;
 
98
                startWidth = target.width;
 
99
            }
 
100
 
 
101
        } else if (rightBorder) {
 
102
            var deltaX = pos.x - startX;
 
103
            if (startWidth + deltaX >= target.minWidth) {
 
104
                target.width = startWidth + deltaX;
 
105
            } else {
 
106
                target.width = target.minWidth;
 
107
            }
 
108
        }
 
109
 
 
110
        if (topBorder) {
 
111
            if (startY + startHeight - pos.y > target.minHeight) {
 
112
                target.y = pos.y;
 
113
                target.height = startY + startHeight - target.y;
 
114
                startY = target.y;
 
115
                startHeight = target.height;
 
116
            }
 
117
 
 
118
        } else if (bottomBorder) {
 
119
            var deltaY = pos.y - startY;
 
120
            if (startHeight + deltaY >= target.minHeight) {
 
121
                target.height = startHeight + deltaY;
 
122
            } else {
 
123
                target.height = target.minHeight;
 
124
            }
 
125
        }
 
126
    }
 
127
}
 
128