Skip to content

Commit

Permalink
Fix some resizing issues
Browse files Browse the repository at this point in the history
Signed-off-by: swurl <[email protected]>
  • Loading branch information
crueter committed Mar 7, 2025
1 parent 336bf6d commit e71fcd6
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 28 deletions.
2 changes: 0 additions & 2 deletions qml/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ ApplicationWindow {
height = platformHelper.screenHeight() - 234
width = platformHelper.screenWidth()

// TODO: test on windows & verify geometry
// check if it works with scaling as well
x = 0
y = 0
}
Expand Down
31 changes: 20 additions & 11 deletions qml/items/Tab.qml
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,22 @@ Rectangle {

property bool currentOpValid: false

function validResize(width, height, row, column, rowSpan, colSpan) {
let size = getSize(width, height)
function validResize(width, height, x, y, row, column, rowSpan, colSpan) {
let rect = getRect(x, y, width, height)

let newRowSpan = size.height
let newColSpan = size.width
let newRow = rect.y
let newColumn = rect.x

let newRowSpan = rect.height
let newColSpan = rect.width

let ignore = Qt.rect(column, row, colSpan, rowSpan)

let valid = !twm.cellOccupied(row, column, newRowSpan,
let valid = !twm.cellOccupied(newRow, newColumn, newRowSpan,
newColSpan, ignore)

validRect.x = column * colWidth()
validRect.y = row * rowWidth()
validRect.x = newColumn * colWidth()
validRect.y = newRow * rowWidth()
validRect.width = newColSpan * colWidth()
validRect.height = newRowSpan * rowWidth()

Expand Down Expand Up @@ -200,17 +203,23 @@ Rectangle {
return Qt.point(newCol, newRow)
}

function getSize(width, height) {
let newRows = Math.round(height / rowWidth())
let newCols = Math.round(width / colWidth())
function getRect(x, y, width, height) {
let point = getPoint(x, y, false)

// Hacky fix for weird margins issues
let bottomRight = getPoint(x + (width - 16),
y + (height - 16), false)

let newRows = Math.ceil(bottomRight.y - point.y + 1)
let newCols = Math.ceil(bottomRight.x - point.x + 1)

if (newRows < 1)
newRows = 1

if (newCols < 1)
newCols = 1

return Qt.size(newCols, newRows)
return Qt.rect(point.x, point.y, newCols, newRows)
}

function colWidth() {
Expand Down
77 changes: 62 additions & 15 deletions qml/widgets/BaseWidget.qml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Rectangle {
signal moved(real x, real y)

id: widget

width: 100
height: 100
z: 3
Expand All @@ -18,7 +19,6 @@ Rectangle {
}

radius: 12 * Constants.scalar

property int item_titleFontSize: 16

property alias dragArea: dragArea
Expand All @@ -38,7 +38,7 @@ Rectangle {

function checkResize() {
if (resizeActive) {
grid.validResize(width, height, row, column, rowSpan, colSpan)
grid.validResize(width, height, x, y, row, column, rowSpan, colSpan)
}
}

Expand All @@ -49,13 +49,13 @@ Rectangle {
}

function startDrag() {
originalPoint = Qt.point(widget.x, widget.y)
originalRect = Qt.rect(widget.x, widget.y, widget.width, widget.height)
dragArea.drag.target = widget
widget.z = 4
}

function startResize() {
originalSize = Qt.size(widget.width, widget.height)
originalRect = Qt.rect(widget.x, widget.y, widget.width, widget.height)
widget.resizeActive = true
widget.z = 4
}
Expand All @@ -64,6 +64,54 @@ Rectangle {
return grid.getPoint(x, y, false)
}

// Drag/Resize animations
ParallelAnimation {
id: resizeBackAnim
SmoothedAnimation {
id: resizeBackAnimX
target: widget
property: "x"
duration: 250
}
SmoothedAnimation {
id: resizeBackAnimY
target: widget
property: "y"
duration: 250
}
SmoothedAnimation {
id: resizeBackAnimWidth
target: widget
property: "width"
duration: 250
}
SmoothedAnimation {
id: resizeBackAnimHeight
target: widget
property: "height"
duration: 250
}

onFinished: {
width = widget.originalRect.width
height = widget.originalRect.height
x = widget.originalRect.x
y = widget.originalRect.y
}
}

function animateBacksize() {
resizeBackAnimX.from = widget.x
resizeBackAnimX.to = originalRect.x
resizeBackAnimY.from = widget.y
resizeBackAnimY.to = originalRect.y
resizeBackAnimWidth.from = widget.width
resizeBackAnimWidth.to = originalRect.width
resizeBackAnimHeight.from = widget.height
resizeBackAnimHeight.to = originalRect.height
resizeBackAnim.start()
}

onXChanged: checkDrag()
onYChanged: checkDrag()

Expand All @@ -75,8 +123,7 @@ Rectangle {
property int mrowSpan
property int mcolumnSpan

property point originalPoint: Qt.point(0, 0)
property size originalSize: Qt.size(0, 0)
property rect originalRect: Qt.rect(0, 0, 0, 0)

property bool resizeActive: false
property bool dragForced: false
Expand Down Expand Up @@ -188,9 +235,7 @@ Rectangle {
mrow = newPoint.y
mcolumn = newPoint.x
} else {
// TODO: animate again
widget.x = originalPoint.x
widget.y = originalPoint.y
animateBacksize()
}
grid.resetValid()

Expand Down Expand Up @@ -222,11 +267,13 @@ Rectangle {
if (mouse.button === Qt.LeftButton) {
if (grid.validResize(widget.width,
widget.height,
row, column,
rowSpan,
widget.x,
widget.y, row,
column, rowSpan,
colSpan)) {

let newSize = grid.getSize(
let newSize = grid.getRect(
widget.x, widget.y,
widget.width, widget.height)

// Force update it to ensure the grid properly lays it out
Expand All @@ -237,10 +284,10 @@ Rectangle {

mrowSpan = newSize.height
mcolumnSpan = newSize.width
mrow = newSize.y
mcolumn = newSize.x
} else {
// TODO: animate again
widget.width = originalSize.width
widget.height = originalSize.height
animateBacksize()
}

resizeActive = false
Expand Down

0 comments on commit e71fcd6

Please sign in to comment.