~lukas-kde/miral/shellchrome-windowinfo

« back to all changes in this revision

Viewing changes to miral-qt/demos/qml-demo-shell/WindowBufferSized.qml

  • Committer: Larry Price
  • Date: 2016-09-13 16:19:29 UTC
  • mto: (330.4.1 miral)
  • mto: This revision was merged to the branch mainline in revision 352.
  • Revision ID: larry.price@canonical.com-20160913161929-vs9ka1capmljq1es
Removing miral-qt from release branch, updating copyright file, and adding GPL3 license to root dir

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import QtQuick 2.0
2
 
import Unity.Application 0.1
3
 
 
4
 
Rectangle {
5
 
    id: root
6
 
    color: "red"
7
 
 
8
 
    property alias surface: surfaceItem.surface
9
 
    property bool touchMode: false
10
 
 
11
 
    width: surfaceItem.width + (borderThickness*2)
12
 
    height: surfaceItem.height + titleBar.height + (borderThickness*2)
13
 
 
14
 
    signal cloneRequested()
15
 
 
16
 
    onTouchModeChanged: {
17
 
        if (touchMode) {
18
 
            x -= borderThicknessTouch - borderThicknessMouse;
19
 
            width += 2*(borderThicknessTouch - borderThicknessMouse);
20
 
            y -= borderThicknessTouch - borderThicknessMouse;
21
 
            height += 2*(borderThicknessTouch - borderThicknessMouse);
22
 
        } else {
23
 
            x += borderThicknessTouch - borderThicknessMouse;
24
 
            width -= 2*(borderThicknessTouch - borderThicknessMouse);
25
 
            y += borderThicknessTouch - borderThicknessMouse;
26
 
            height -= 2*(borderThicknessTouch - borderThicknessMouse);
27
 
        }
28
 
    }
29
 
 
30
 
    readonly property real minWidth: 100
31
 
    readonly property real minHeight: 100
32
 
 
33
 
    property real borderThickness: touchMode ? borderThicknessTouch : borderThicknessMouse
34
 
    readonly property real borderThicknessMouse: 10
35
 
    readonly property real borderThicknessTouch: 40
36
 
 
37
 
    states: [
38
 
        State {
39
 
            name: "closed"
40
 
            when: (surface && !surface.live) || titleBar.closeRequested
41
 
        }
42
 
    ]
43
 
    transitions: [
44
 
        Transition {
45
 
            from: ""; to: "closed"
46
 
            SequentialAnimation {
47
 
                PropertyAnimation {
48
 
                    target: root
49
 
                    property: "scale"
50
 
                    easing.type: Easing.InBack
51
 
                    duration: 400
52
 
                    from: 1.0
53
 
                    to: 0.0
54
 
                }
55
 
                ScriptAction { script: { root.destroy(); } }
56
 
            }
57
 
        }
58
 
    ]
59
 
 
60
 
 
61
 
    MouseArea {
62
 
        id: resizeArea
63
 
 
64
 
        anchors.fill: parent
65
 
 
66
 
        property real startX
67
 
        property real startY
68
 
        property real startWidth
69
 
        property real startHeight
70
 
        property bool leftBorder
71
 
        property bool rightBorder
72
 
        property bool topBorder
73
 
        property bool bottomBorder
74
 
        property bool dragging
75
 
        onPressedChanged: {
76
 
            if (pressed) {
77
 
                var pos = mapToItem(root.parent, mouseX, mouseY);
78
 
                startX = pos.x;
79
 
                startY = pos.y;
80
 
                startWidth = surfaceItem.width;
81
 
                startHeight = surfaceItem.height;
82
 
                leftBorder = mouseX > 0 && mouseX < root.borderThickness;
83
 
                rightBorder = mouseX > (root.width - root.borderThickness) && mouseX < root.width;
84
 
                topBorder = mouseY > 0 && mouseY < root.borderThickness;
85
 
                bottomBorder = mouseY > (root.height - root.borderThickness) && mouseY < root.height;
86
 
                dragging = true;
87
 
            } else {
88
 
                dragging = false;
89
 
            }
90
 
        }
91
 
 
92
 
        onMouseXChanged: {
93
 
            if (!pressed || !dragging) {
94
 
                return;
95
 
            }
96
 
 
97
 
            var pos = mapToItem(root.parent, mouseX, mouseY);
98
 
 
99
 
            var deltaX = pos.x - startX;
100
 
            if (leftBorder) {
101
 
                if (startWidth - deltaX >= root.minWidth) {
102
 
                    surfaceItem.surfaceWidth = startWidth - deltaX;
103
 
                } else {
104
 
                    surfaceItem.surfaceWidth = root.minWidth;
105
 
                }
106
 
            } else if (rightBorder) {
107
 
                if (startWidth + deltaX >= root.minWidth) {
108
 
                    surfaceItem.surfaceWidth = startWidth + deltaX;
109
 
                } else {
110
 
                    surfaceItem.surfaceWidth = root.minWidth;
111
 
                }
112
 
            }
113
 
        }
114
 
 
115
 
        onMouseYChanged: {
116
 
            if (!pressed || !dragging) {
117
 
                return;
118
 
            }
119
 
 
120
 
            var pos = mapToItem(root.parent, mouseX, mouseY);
121
 
 
122
 
            var deltaY = pos.y - startY;
123
 
            if (topBorder) {
124
 
                if (startHeight - deltaY >= root.minHeight) {
125
 
                    surfaceItem.surfaceHeight = startHeight - deltaY;
126
 
                } else {
127
 
                    surfaceItem.surfaceHeight = root.minHeight;
128
 
                }
129
 
            } else if (bottomBorder) {
130
 
                if (startHeight + deltaY >= root.minHeight) {
131
 
                    surfaceItem.surfaceHeight = startHeight + deltaY;
132
 
                } else {
133
 
                    surfaceItem.surfaceHeight = root.minHeight;
134
 
                }
135
 
            }
136
 
        }
137
 
    }
138
 
 
139
 
    TitleBar {
140
 
        id: titleBar
141
 
        anchors.left: parent.left
142
 
        anchors.leftMargin: root.borderThickness
143
 
        anchors.right: parent.right
144
 
        anchors.rightMargin: root.borderThickness
145
 
        anchors.top: parent.top
146
 
        anchors.topMargin: root.borderThickness
147
 
 
148
 
        target: root
149
 
        cloned: root.cloned
150
 
        onCloneRequested: { root.cloneRequested(); }
151
 
    }
152
 
 
153
 
    MirSurfaceItem {
154
 
        id: surfaceItem
155
 
 
156
 
        width: surface ? surface.size.width : 50
157
 
        height: surface ? surface.size.height : 50
158
 
 
159
 
        onWidthChanged: {
160
 
            if (resizeArea.dragging && resizeArea.leftBorder) {
161
 
                root.x = resizeArea.startX + resizeArea.startWidth - surfaceItem.width;
162
 
            }
163
 
        }
164
 
 
165
 
        onHeightChanged: {
166
 
            if (resizeArea.dragging && resizeArea.topBorder) {
167
 
                root.y = resizeArea.startY + resizeArea.startHeight - surfaceItem.height;
168
 
            }
169
 
        }
170
 
 
171
 
        anchors.top: titleBar.bottom
172
 
        anchors.left: parent.left
173
 
        anchors.leftMargin: root.borderThickness
174
 
 
175
 
        consumesInput: true
176
 
    }
177
 
}
178