2 * Copyright (C) 2014 Canonical, Ltd.
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.
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.
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/>.
16 * Authors: Michael Zanetti <michael.zanetti@canonical.com>
20 import Ubuntu.Components 1.1
26 anchors.margins: -resizeHandleWidth
28 // The target item managed by this. Must be a parent or a sibling
29 // The area will anchor to it and manage move and resize events
30 property Item target: null
31 property string windowId: ""
32 property int resizeHandleWidth: 0
33 property int minWidth: 0
34 property int minHeight: 0
38 readonly property int windowWidth: root.width - root.resizeHandleWidth * 2
39 readonly property int windowHeight: root.height - resizeHandleWidth * 2
41 property var startPoint
43 property bool resizeTop: false
44 property bool resizeBottom: false
45 property bool resizeLeft: false
46 property bool resizeRight: false
50 Component.onCompleted: {
51 var windowState = WindowStateStorage.getGeometry(root.windowId, Qt.rect(target.x, target.y, target.width, target.height))
52 if (windowState !== undefined) {
53 target.x = windowState.x
54 target.y = windowState.y
55 target.width = windowState.width
56 target.height = windowState.height
61 priv.startPoint = Qt.point(mouse.x, mouse.y);
62 priv.resizeTop = mouseY < root.resizeHandleWidth;
63 priv.resizeBottom = mouseY > (root.height - root.resizeHandleWidth);
64 priv.resizeLeft = mouseX < root.resizeHandleWidth;
65 priv.resizeRight = mouseX > (root.width - root.resizeHandleWidth);
69 var currentPoint = Qt.point(mouse.x, mouse.y);
70 var mouseDiff = Qt.point(currentPoint.x - priv.startPoint.x, currentPoint.y - priv.startPoint.y);
71 var moveDiff = Qt.point(0, 0);
72 var sizeDiff = Qt.point(0, 0);
73 var maxSizeDiff = Qt.point(root.minWidth - root.target.width, root.minHeight - root.target.height)
75 if (priv.resizeTop || priv.resizeBottom || priv.resizeLeft || priv.resizeRight) {
77 sizeDiff.y = Math.max(maxSizeDiff.y, -currentPoint.y + priv.startPoint.y)
78 moveDiff.y = -sizeDiff.y
80 if (priv.resizeBottom) {
81 sizeDiff.y = Math.max(maxSizeDiff.y, currentPoint.y - priv.startPoint.y)
82 priv.startPoint.y += sizeDiff.y
84 if (priv.resizeLeft) {
85 sizeDiff.x = Math.max(maxSizeDiff.x, -currentPoint.x + priv.startPoint.x)
86 moveDiff.x = -sizeDiff.x
88 if (priv.resizeRight) {
89 sizeDiff.x = Math.max(maxSizeDiff.x, currentPoint.x - priv.startPoint.x)
90 priv.startPoint.x += sizeDiff.x
93 target.x += moveDiff.x;
94 target.y += moveDiff.y;
95 target.width += sizeDiff.x;
96 target.height += sizeDiff.y;
98 target.x += mouseDiff.x;
99 target.y += mouseDiff.y;
104 Component.onDestruction: {
105 WindowStateStorage.saveGeometry(root.windowId, Qt.rect(target.x, target.y, target.width, target.height))