on xenial help sign in
Fix resizing windows when making it smaller than minSize and then larger again.

Approved by: Daniel d'Andrada, PS Jenkins bot

commitd6b7ae59915ee31be824b80bd96adf69f5112435 authorMichael Zanetti <michael.zanetti@canonical.com> dateWed 11 Feb 2015, 17:12 (12 years ago) parentf8ff2cffe457e3
2 files changed+41−23
qml/Stages/WindowMoveResizeArea.qml+16−23 tests/qmltests/Stages/tst_WindowMoveResizeArea.qml+25
qml/Stages/WindowMoveResizeArea.qml+16−23
@@ -39,8 +39,6 @@ MouseArea {
3939 readonly property int windowHeight: root.height - resizeHandleWidth * 2
4040
4141 property var startPoint
42− property int startWidth
43− property int startHeight
4442
4543 property bool resizeTop: false
4644 property bool resizeBottom: false
@@ -60,9 +58,7 @@ MouseArea {
6058 }
6159
6260 onPressed: {
63− priv.startPoint = mapToItem(null, Qt.point(mouse.x, mouse.y)).x;
64− priv.startWidth = root.width;
65− priv.startHeight = root.height;
61+ priv.startPoint = Qt.point(mouse.x, mouse.y);
6662 priv.resizeTop = mouseY < root.resizeHandleWidth;
6763 priv.resizeBottom = mouseY > (root.height - root.resizeHandleWidth);
6864 priv.resizeLeft = mouseX < root.resizeHandleWidth;
@@ -70,32 +66,28 @@ MouseArea {
7066 }
7167
7268 onPositionChanged: {
73− var currentPoint = mapToItem(null, Qt.point(mouse.x, mouse.y)).x;
69+ var currentPoint = Qt.point(mouse.x, mouse.y);
7470 var mouseDiff = Qt.point(currentPoint.x - priv.startPoint.x, currentPoint.y - priv.startPoint.y);
7571 var moveDiff = Qt.point(0, 0);
7672 var sizeDiff = Qt.point(0, 0);
73+ var maxSizeDiff = Qt.point(root.minWidth - root.target.width, root.minHeight - root.target.height)
74+
7775 if (priv.resizeTop || priv.resizeBottom || priv.resizeLeft || priv.resizeRight) {
7876 if (priv.resizeTop) {
79− moveDiff.y += mouseDiff.y;
80− sizeDiff.y += -mouseDiff.y;
81− } else if (priv.resizeBottom) {
82− sizeDiff.y += mouse.y - priv.startPoint.y;
83− priv.startPoint.y = mouse.y
77+ sizeDiff.y = Math.max(maxSizeDiff.y, -currentPoint.y + priv.startPoint.y)
78+ moveDiff.y = -sizeDiff.y
8479 }
85− if (priv.resizeLeft) {
86− moveDiff.x += mouseDiff.x;
87− sizeDiff.x += -mouseDiff.x;
88− } else if (priv.resizeRight) {
89− sizeDiff.x += mouse.x - priv.startPoint.x;
90− priv.startPoint.x = mouse.x
80+ if (priv.resizeBottom) {
81+ sizeDiff.y = Math.max(maxSizeDiff.y, currentPoint.y - priv.startPoint.y)
82+ priv.startPoint.y += sizeDiff.y
9183 }
92− if (priv.windowWidth + sizeDiff.x < root.minWidth) {
93− sizeDiff.x = root.minWidth - priv.windowWidth;
94− moveDiff.x = moveDiff.x == 0 ? 0 : -sizeDiff.x;
84+ if (priv.resizeLeft) {
85+ sizeDiff.x = Math.max(maxSizeDiff.x, -currentPoint.x + priv.startPoint.x)
86+ moveDiff.x = -sizeDiff.x
9587 }
96− if (priv.windowHeight + sizeDiff.y < root.minHeight) {
97− sizeDiff.y = root.minHeight - priv.windowHeight;
98− moveDiff.y = moveDiff.y == 0 ? 0 : -sizeDiff.y;
88+ if (priv.resizeRight) {
89+ sizeDiff.x = Math.max(maxSizeDiff.x, currentPoint.x - priv.startPoint.x)
90+ priv.startPoint.x += sizeDiff.x
9991 }
10092
10193 target.x += moveDiff.x;
@@ -106,6 +98,7 @@ MouseArea {
10698 target.x += mouseDiff.x;
10799 target.y += mouseDiff.y;
108100 }
101+
109102 }
110103
111104 Component.onDestruction: {
tests/qmltests/Stages/tst_WindowMoveResizeArea.qml+25
@@ -208,5 +208,30 @@ Item {
208208 tryCompare(fakeWindow, "width", Math.max(initialWindowWidth + resizeDelta, fakeWindow.minWidth));
209209 tryCompare(fakeWindow, "height", Math.max(initialWindowHeight + resizeDelta, fakeWindow.minHeight));
210210 }
211+
212+ // This tests if dragging smaller than minSize and then larger again, will keep the edge sticking
213+ // to the mouse, instead of immediately making the window grow again when switching direction
214+ function test_resizeSmallerAndLarger_data() {
215+ return [
216+ { tag: "topLeft", startX: -1, startY: -1, dx: units.gu(15), dy: units.gu(15) },
217+ { tag: "bottomRight", startX: fakeWindow.width + 1, startY: fakeWindow.height + 1, dx: -units.gu(15), dy: -units.gu(15) }
218+ ]
219+ }
220+
221+ function test_resizeSmallerAndLarger(data) {
222+ var initialWindowX = fakeWindow.x;
223+ var initialWindowY = fakeWindow.y;
224+ var initialWindowWidth = fakeWindow.width
225+ var initialWindowHeight = fakeWindow.height
226+
227+ var startDragX = initialWindowX + data.startX
228+ var startDragY = initialWindowY + data.startY
229+ mouseFlick(root, startDragX, startDragY, startDragX + data.dx, startDragY + data.dy, true, false, units.gu(.05), 10);
230+ tryCompare(fakeWindow, "width", Math.max(initialWindowWidth - Math.abs(data.dx), fakeWindow.minWidth));
231+ tryCompare(fakeWindow, "height", Math.max(initialWindowHeight - Math.abs(data.dy), fakeWindow.minHeight));
232+ mouseFlick(root, startDragX + data.dx, startDragY + data.dy, startDragX, startDragY, false, true, units.gu(.05), 10);
233+ tryCompare(fakeWindow, "width", initialWindowWidth);
234+ tryCompare(fakeWindow, "height", initialWindowHeight);
235+ }
211236 }
212237}