Unity 8
WindowMoveResizeArea.qml
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  */
18 
19 import QtQuick 2.3
20 import Ubuntu.Components 1.1
21 import Utils 0.1
22 
23 MouseArea {
24  id: root
25  anchors.fill: target
26  anchors.margins: -resizeHandleWidth
27 
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
35 
36  QtObject {
37  id: priv
38  readonly property int windowWidth: root.width - root.resizeHandleWidth * 2
39  readonly property int windowHeight: root.height - resizeHandleWidth * 2
40 
41  property var startPoint
42 
43  property bool resizeTop: false
44  property bool resizeBottom: false
45  property bool resizeLeft: false
46  property bool resizeRight: false
47 
48  }
49 
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
57  }
58  }
59 
60  onPressed: {
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);
66  }
67 
68  onPositionChanged: {
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)
74 
75  if (priv.resizeTop || priv.resizeBottom || priv.resizeLeft || priv.resizeRight) {
76  if (priv.resizeTop) {
77  sizeDiff.y = Math.max(maxSizeDiff.y, -currentPoint.y + priv.startPoint.y)
78  moveDiff.y = -sizeDiff.y
79  }
80  if (priv.resizeBottom) {
81  sizeDiff.y = Math.max(maxSizeDiff.y, currentPoint.y - priv.startPoint.y)
82  priv.startPoint.y += sizeDiff.y
83  }
84  if (priv.resizeLeft) {
85  sizeDiff.x = Math.max(maxSizeDiff.x, -currentPoint.x + priv.startPoint.x)
86  moveDiff.x = -sizeDiff.x
87  }
88  if (priv.resizeRight) {
89  sizeDiff.x = Math.max(maxSizeDiff.x, currentPoint.x - priv.startPoint.x)
90  priv.startPoint.x += sizeDiff.x
91  }
92 
93  target.x += moveDiff.x;
94  target.y += moveDiff.y;
95  target.width += sizeDiff.x;
96  target.height += sizeDiff.y;
97  } else {
98  target.x += mouseDiff.x;
99  target.y += mouseDiff.y;
100  }
101 
102  }
103 
104  Component.onDestruction: {
105  WindowStateStorage.saveGeometry(root.windowId, Qt.rect(target.x, target.y, target.width, target.height))
106  }
107 }