From 6c10551ace5d50c877ecff9d629d361e3d2c06a0 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 4 Oct 2022 12:12:46 +0200 Subject: [PATCH] check some collisions at resizing widgets to the left --- .../launcher/views/HomeScreenGrid.kt | 6 +- .../launcher/views/MyAppWidgetResizeFrame.kt | 58 ++++++++++++++++--- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt index 52d577e..7ba4082 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt @@ -177,15 +177,11 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel val widgetView = widgetViews.firstOrNull { it.tag == resizedWidget!!.widgetId } resize_frame.beGone() if (widgetView != null) { - val appWidgetProviderInfo = item.providerInfo ?: appWidgetManager!!.installedProviders.firstOrNull { - it.provider.className == item.className - } ?: return - val viewX = widgetView.x.toInt() val viewY = widgetView.y.toInt() val frameRect = Rect(viewX, viewY, viewX + widgetView.width, viewY + widgetView.height) val otherGridItems = gridItems.filter { it.widgetId != item.widgetId }.toMutableList() as ArrayList - resize_frame.updateFrameCoords(frameRect, cellWidth, cellHeight, sideMargins, appWidgetProviderInfo, otherGridItems) + resize_frame.updateFrameCoords(frameRect, cellWidth, cellHeight, sideMargins, item, otherGridItems) resize_frame.beVisible() resize_frame.z = 1f // make sure the frame isnt behind the widget itself resize_frame.onClickListener = { 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 5e610aa..ab25d04 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetResizeFrame.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/MyAppWidgetResizeFrame.kt @@ -1,7 +1,7 @@ package com.simplemobiletools.launcher.views import android.annotation.SuppressLint -import android.appwidget.AppWidgetProviderInfo +import android.appwidget.AppWidgetManager import android.content.Context import android.graphics.* import android.graphics.drawable.ColorDrawable @@ -23,13 +23,14 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In private var resizeWidgetLineDotPaint: Paint private var actionDownCoords = PointF() private var actionDownMS = 0L - private var frameRect = Rect(0, 0, 0, 0) + private var frameRect = Rect(0, 0, 0, 0) // coords in pixels + private var cellsRect = Rect(0, 0, 0, 0) // cell IDs like 0, 1, 2.. private var cellWidth = 0 private var cellHeight = 0 private var minResizeWidthCells = 1 private var minResizeHeightCells = 1 private val occupiedCells = ArrayList>() - private var providerInfo: AppWidgetProviderInfo? = null + private var resizedItem: HomeScreenGridItem? = null private var sideMargins = Rect() private val lineDotRadius = context.resources.getDimension(R.dimen.resize_frame_dot_radius) private val MAX_TOUCH_LINE_DISTANCE = lineDotRadius * 5 // how close we have to be to the widgets side to drag it @@ -61,14 +62,19 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In cellWidth: Int, cellHeight: Int, sideMargins: Rect, - providerInfo: AppWidgetProviderInfo, + gridItem: HomeScreenGridItem, allGridItems: ArrayList ) { frameRect = coords + cellsRect = Rect(gridItem.left, gridItem.top, gridItem.right, gridItem.bottom) this.cellWidth = cellWidth this.cellHeight = cellHeight this.sideMargins = sideMargins - this.providerInfo = providerInfo + this.resizedItem = gridItem + val providerInfo = gridItem.providerInfo ?: AppWidgetManager.getInstance(context)!!.installedProviders.firstOrNull { + it.provider.className == gridItem.className + } ?: return + minResizeWidthCells = Math.min(COLUMN_COUNT, context.getTileCount(providerInfo.minResizeWidth)) minResizeHeightCells = Math.min(ROW_COUNT, context.getTileCount(providerInfo.minResizeHeight)) redrawFrame() @@ -91,6 +97,10 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In requestLayout() } + private fun cellChanged() { + + } + override fun onTouchEvent(event: MotionEvent?): Boolean { if (event == null) { return false @@ -118,11 +128,25 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In when (dragDirection) { DRAGGING_LEFT -> { val newWidth = frameRect.right - event.rawX.toInt() - if (newWidth >= minWidth) { - frameRect.left = event.rawX.toInt() + val wantedLeft = if (newWidth >= minWidth) { + event.rawX.toInt() } else { - frameRect.left = frameRect.right - minWidth + frameRect.right - minWidth } + + val closestCellX = roundToClosestMultiplyOfNumber(wantedLeft - sideMargins.left, cellWidth) / cellWidth + var areAllCellsFree = true + for (yCell in cellsRect.top until cellsRect.bottom) { + if (occupiedCells.contains(Pair(closestCellX, yCell))) { + areAllCellsFree = false + } + } + + if (areAllCellsFree && cellsRect.left != closestCellX) { + cellsRect.left = closestCellX + cellChanged() + } + frameRect.left = wantedLeft } DRAGGING_TOP -> { val newHeight = frameRect.bottom - event.rawY.toInt() @@ -162,7 +186,23 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In dragDirection = DRAGGING_NONE } else { when (dragDirection) { - DRAGGING_LEFT -> frameRect.left = roundToClosestMultiplyOfNumber(frameRect.left - sideMargins.left, cellWidth) + sideMargins.left + DRAGGING_LEFT -> { + val wantedLeft = roundToClosestMultiplyOfNumber(frameRect.left - sideMargins.left, cellWidth) + val wantedLeftCellX = wantedLeft / cellWidth + var areAllCellsFree = true + for (yCell in cellsRect.top until cellsRect.bottom) { + if (occupiedCells.contains(Pair(wantedLeftCellX, yCell))) { + areAllCellsFree = false + } + } + + if (areAllCellsFree) { + frameRect.left = wantedLeft + sideMargins.left + cellsRect.left = wantedLeftCellX + } else { + frameRect.left = cellsRect.left * cellWidth + } + } DRAGGING_TOP -> frameRect.top = roundToClosestMultiplyOfNumber(frameRect.top - sideMargins.top, cellHeight) + sideMargins.top DRAGGING_RIGHT -> frameRect.right = roundToClosestMultiplyOfNumber(frameRect.right - sideMargins.left, cellWidth) + sideMargins.left DRAGGING_BOTTOM -> frameRect.bottom = roundToClosestMultiplyOfNumber(frameRect.bottom - sideMargins.top, cellHeight) + sideMargins.top