on 8a968c8 help sign in
unity8/qml/Stages/WindowMoveResizeArea.qml
98 lines 3.6 kB history raw
1/*
2 * Copyright (C) 2014 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors: Michael Zanetti <michael.zanetti@canonical.com>
17 */
19import QtQuick 2.3
20import Ubuntu.Components 1.1
22MouseArea {
23 id: root
24 anchors.fill: target
25 anchors.margins: -resizeHandleWidth
27 // The target item managed by this. Must be a parent or a sibling
28 // The area will anchor to it and manage move and resize events
29 property var target: null
30 property int resizeHandleWidth: 0
31 property int minWidth: 0
32 property int minHeight: 0
34 QtObject {
35 id: priv
36 readonly property int windowWidth: root.width - root.resizeHandleWidth * 2
37 readonly property int windowHeight: root.height - resizeHandleWidth * 2
39 property var startPoint
40 property int startWidth
41 property int startHeight
43 property bool resizeTop: false
44 property bool resizeBottom: false
45 property bool resizeLeft: false
46 property bool resizeRight: false
48 }
50 onPressed: {
51 priv.startPoint = mapToItem(null, Qt.point(mouse.x, mouse.y)).x;
52 priv.startWidth = root.width;
53 priv.startHeight = root.height;
54 priv.resizeTop = mouseY < root.resizeHandleWidth;
55 priv.resizeBottom = mouseY > (root.height - root.resizeHandleWidth);
56 priv.resizeLeft = mouseX < root.resizeHandleWidth;
57 priv.resizeRight = mouseX > (root.width - root.resizeHandleWidth);
58 }
60 onPositionChanged: {
61 var currentPoint = mapToItem(null, Qt.point(mouse.x, mouse.y)).x;
62 var mouseDiff = Qt.point(currentPoint.x - priv.startPoint.x, currentPoint.y - priv.startPoint.y);
63 var moveDiff = Qt.point(0, 0);
64 var sizeDiff = Qt.point(0, 0);
65 if (priv.resizeTop || priv.resizeBottom || priv.resizeLeft || priv.resizeRight) {
66 if (priv.resizeTop) {
67 moveDiff.y += mouseDiff.y;
68 sizeDiff.y += -mouseDiff.y;
69 } else if (priv.resizeBottom) {
70 sizeDiff.y += mouse.y - priv.startPoint.y;
71 priv.startPoint.y = mouse.y
72 }
73 if (priv.resizeLeft) {
74 moveDiff.x += mouseDiff.x;
75 sizeDiff.x += -mouseDiff.x;
76 } else if (priv.resizeRight) {
77 sizeDiff.x += mouse.x - priv.startPoint.x;
78 priv.startPoint.x = mouse.x
79 }
80 if (priv.windowWidth + sizeDiff.x < root.minWidth) {
81 sizeDiff.x = root.minWidth - priv.windowWidth;
82 moveDiff.x = moveDiff.x == 0 ? 0 : -sizeDiff.x;
83 }
84 if (priv.windowHeight + sizeDiff.y < root.minHeight) {
85 sizeDiff.y = root.minHeight - priv.windowHeight;
86 moveDiff.y = moveDiff.y == 0 ? 0 : -sizeDiff.y;
87 }
89 target.x += moveDiff.x;
90 target.y += moveDiff.y;
91 target.width += sizeDiff.x;
92 target.height += sizeDiff.y;
93 } else {
94 target.x += mouseDiff.x;
95 target.y += mouseDiff.y;
96 }
97 }