on xenial help sign in
snap and properly restore the window when dragging it back

also added some comments to explain the new properties

commitf6ec03f4aff6548016671eddc3e810d74de1c53a authorLukáš Tinkl <lukas.tinkl@canonical.com> dateTue 23 Aug 2016, 20:28 (10 years ago) parentf0c271d
3 files changed+24−14
qml/Stages/DesktopStage.qml+2 qml/Stages/WindowDecoration.qml+21−14 qml/Stages/WindowResizeArea.qml+1
qml/Stages/DesktopStage.qml+2
@@ -505,6 +505,8 @@ AbstractStage {
505505 duration: UbuntuAnimation.SnapDuration
506506 }
507507
508+ // unlike requestedX/Y, this is the last known grab position before being pushed against edges/corners
509+ // when restoring, the window should return to these, not to the place where it was dropped near the edge
508510 property real restoredX
509511 property real restoredY
510512
qml/Stages/WindowDecoration.qml+21−14
@@ -17,6 +17,7 @@
1717import QtQuick 2.4
1818import Unity.Application 0.1 // For Mir singleton
1919import Ubuntu.Components 1.3
20+import Utils 0.1
2021import "../Components"
2122import "../Components/PanelState"
2223
@@ -53,6 +54,7 @@ MouseArea {
5354 onDoubleClicked: {
5455 priv.resetEdges();
5556 if (target.canBeMaximized && mouse.button == Qt.LeftButton) {
57+ priv.dragging = false; // do not interfere with a quick double click followed by a mouse move/drag
5658 root.maximizeClicked();
5759 }
5860 }
@@ -82,16 +84,9 @@ MouseArea {
8284 nearBottomRightCorner = false;
8385 }
8486
85− function saveRestoredPos(x, y) {
86− var pos = mapToItem(root.target, x, y);
87− target.restoredX = Math.round(pos.x);
88− target.restoredY = Math.round(pos.y);
89− print("!!! Saved pos", target.restoredX, target.restoredY)
90− }
91−
9287 // return the progress of mouse pointer movement from 0 to 1 within a corner square of the screen
9388 // 0 -> before the mouse enters the square
94− // 1 -> mouse is exactly in the very corner
89+ // 1 -> mouse is in the outer corner
9590 // a is the corner, b is the mouse pos
9691 function progressInCorner(ax, ay, bx, by) {
9792 // distance of two points, a and b, in pixels
@@ -107,7 +102,14 @@ MouseArea {
107102 onPressedChanged: {
108103 if (pressed && pressedButtons == Qt.LeftButton) {
109104 var pos = mapToItem(root.target, mouseX, mouseY);
110− priv.distanceX = pos.x;
105+ if (target.anyMaximized) {
106+ // keep distanceX relative to the normal window width minus the window control buttons (+spacing)
107+ // so that dragging it back doesn't make the window jump around to weird positions, away from the mouse pointer
108+ priv.distanceX = MathUtils.clampAndProject(pos.x, 0, root.target.width, buttons.width + row.spacing, root.target.resizeArea.normalWidth);
109+ } else {
110+ priv.distanceX = pos.x;
111+ }
112+
111113 priv.distanceY = pos.y;
112114 priv.dragging = true;
113115 } else {
@@ -119,11 +121,16 @@ MouseArea {
119121 onPositionChanged: {
120122 if (priv.dragging) {
121123 Mir.cursorName = "grabbing";
124+
125+ if (target.anyMaximized) { // restore from maximized when dragging away from edges/corners
126+ priv.progress = 0;
127+ target.restore(false, WindowStateStorage.WindowStateNormal);
128+ }
129+
122130 var pos = mapToItem(root.target.parent, mouseX, mouseY);
123131 // Use integer coordinate values to ensure that target is left in a pixel-aligned
124132 // position. Mouse movement could have subpixel precision, yielding a fractional
125133 // mouse position.
126−
127134 root.target.requestedX = Math.round(pos.x - priv.distanceX);
128135 root.target.requestedY = Math.round(Math.max(pos.y - priv.distanceY, PanelState.panelHeight));
129136
@@ -185,9 +192,6 @@ MouseArea {
185192 priv.progress = 0;
186193 priv.resetEdges();
187194 root.stopFakeAnimation();
188− } else if (target.anyMaximized) {
189− priv.progress = 0;
190− target.restoreFromMaximized();
191195 }
192196 }
193197 }
@@ -195,7 +199,9 @@ MouseArea {
195199 onReleased: {
196200 print("Mouse released (left/top/right)", priv.nearLeftEdge, priv.nearTopEdge, priv.nearRightEdge)
197201 if (mouse.button == Qt.LeftButton && (target.state == "normal" || target.state == "restored") && priv.progress == 0) {
198− priv.saveRestoredPos(target.x, target.y);
202+ // save the x/y to restore to
203+ target.restoredX = target.x;
204+ target.restoredY = target.y;
199205 } else if (priv.progress < 0.3) { // cancel the preview shape if under 30%
200206 priv.progress = 0;
201207 priv.resetEdges();
@@ -228,6 +234,7 @@ MouseArea {
228234 }
229235
230236 Row {
237+ id: row
231238 anchors {
232239 fill: parent
233240 leftMargin: overlayShown ? units.gu(5) : units.gu(1)
qml/Stages/WindowResizeArea.qml+1
@@ -30,6 +30,7 @@ MouseArea {
3030
3131 property var windowStateStorage: WindowStateStorage
3232 readonly property alias dragging: d.dragging
33+ readonly property alias normalWidth: priv.normalWidth
3334
3435 // The target item managed by this. Must be a parent or a sibling
3536 // The area will anchor to it and manage resize events