From abc838f307c83cc930fbe2c12abac3231ef6ab43 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 4 Oct 2022 19:49:01 +0200 Subject: [PATCH] handle collisions at resizing to the top --- .../launcher/views/MyAppWidgetResizeFrame.kt | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetResizeFrame.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetResizeFrame.kt index 80c144e..656732e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetResizeFrame.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetResizeFrame.kt @@ -146,15 +146,31 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In cellsRect.left = closestCellX cellChanged() } + frameRect.left = wantedLeft } DRAGGING_TOP -> { val newHeight = frameRect.bottom - event.rawY.toInt() - if (newHeight >= minHeight) { - frameRect.top = event.rawY.toInt() + val wantedTop = if (newHeight >= minHeight) { + event.rawY.toInt() } else { - frameRect.top = frameRect.bottom - minHeight + frameRect.bottom - minHeight } + + val closestCellY = roundToClosestMultiplyOfNumber(wantedTop - sideMargins.top, cellHeight) / cellHeight + var areAllCellsFree = true + for (xCell in cellsRect.left until cellsRect.right) { + if (occupiedCells.contains(Pair(xCell, closestCellY))) { + areAllCellsFree = false + } + } + + if (areAllCellsFree && cellsRect.top != closestCellY) { + cellsRect.top = closestCellY + cellChanged() + } + + frameRect.top = wantedTop } DRAGGING_RIGHT -> { val newWidth = event.rawX.toInt() - frameRect.left @@ -176,6 +192,7 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In cellsRect.right = closestCellX cellChanged() } + frameRect.right = wantedRight } DRAGGING_BOTTOM -> { @@ -217,7 +234,23 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In frameRect.left = cellsRect.left * cellWidth + sideMargins.left } } - DRAGGING_TOP -> frameRect.top = roundToClosestMultiplyOfNumber(frameRect.top - sideMargins.top, cellHeight) + sideMargins.top + DRAGGING_TOP -> { + val wantedTop = roundToClosestMultiplyOfNumber(frameRect.top - sideMargins.top, cellHeight) + val wantedTopCellY = wantedTop / cellHeight + var areAllCellsFree = true + for (xCell in cellsRect.left until cellsRect.right) { + if (occupiedCells.contains(Pair(xCell, wantedTopCellY))) { + areAllCellsFree = false + } + } + + if (areAllCellsFree) { + frameRect.top = wantedTop + sideMargins.top + cellsRect.top = wantedTopCellY + } else { + frameRect.top = cellsRect.top * cellHeight + sideMargins.top + } + } DRAGGING_RIGHT -> { val wantedRight = roundToClosestMultiplyOfNumber(frameRect.right - sideMargins.left, cellWidth) val wantedRightCellX = wantedRight / cellWidth - 1