From 9fbf63623689ff6807401630a32195c5749fc49a Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 26 Sep 2022 16:08:35 +0200 Subject: [PATCH] draw a shadow under widget while being dragged --- .../launcher/adapters/WidgetsAdapter.kt | 1 - .../launcher/views/HomeScreenGrid.kt | 34 +++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/adapters/WidgetsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/adapters/WidgetsAdapter.kt index 3cfffd5..020aa96 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/adapters/WidgetsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/adapters/WidgetsAdapter.kt @@ -59,7 +59,6 @@ class WidgetsAdapter( view.apply { widget_app_title.text = section.appTitle widget_app_title.setTextColor(textColor) - widget_app_icon.setImageDrawable(section.appIcon) } } 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 f00d37a..01eb148 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt @@ -29,6 +29,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie private var iconMargin = context.resources.getDimension(R.dimen.icon_side_margin).toInt() private var labelSideMargin = context.resources.getDimension(R.dimen.small_margin).toInt() + private var roundedCornerRadius = context.resources.getDimension(R.dimen.activity_margin) private var textPaint: TextPaint private var dragShadowCirclePaint: Paint private var draggedItem: HomeScreenGridItem? = null @@ -262,8 +263,10 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie if (draggedItem != null && draggedItemCurrentCoords.first != -1 && draggedItemCurrentCoords.second != -1) { if (draggedItem!!.type == ITEM_TYPE_ICON) { // draw a circle under the current cell - val center = - gridCenters.minBy { Math.abs(it.first - draggedItemCurrentCoords.first + sideMargins.left) + Math.abs(it.second - draggedItemCurrentCoords.second + sideMargins.top) } + val center = gridCenters.minBy { + Math.abs(it.first - draggedItemCurrentCoords.first + sideMargins.left) + Math.abs(it.second - draggedItemCurrentCoords.second + sideMargins.top) + } + val gridCells = getClosestGridCells(center) if (gridCells != null) { val shadowX = rowXCoords[gridCells.first] + iconMargin.toFloat() + iconSize / 2 + sideMargins.left @@ -281,6 +284,33 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie val drawableY = (draggedItemCurrentCoords.second - iconSize / 1.2f).toInt() draggedItem!!.drawable!!.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize) draggedItem!!.drawable!!.draw(canvas) + } else { + val center = gridCenters.minBy { + Math.abs(it.first - draggedItemCurrentCoords.first + sideMargins.left) + Math.abs(it.second - draggedItemCurrentCoords.second + sideMargins.top) + } + + val gridCells = getClosestGridCells(center) + if (gridCells != null) { + val rect = Rect(gridCells.first, gridCells.second, gridCells.first + draggedItem!!.widthCells, gridCells.second + draggedItem!!.heightCells) + if (rect.right > COLUMN_COUNT) { + val diff = rect.right - COLUMN_COUNT + rect.right -= diff + rect.left -= diff + } + + // do not allow placing widgets at the bottom row, that is for pinned default apps + if (rect.bottom >= ROW_COUNT) { + val diff = rect.bottom - ROW_COUNT + 1 + rect.bottom -= diff + rect.top -= diff + } + + val leftSide = rect.left * rowWidth + sideMargins.left + iconMargin.toFloat() + val topSide = rect.top * rowHeight + sideMargins.top + iconMargin.toFloat() + val rightSide = leftSide + draggedItem!!.widthCells * rowWidth - sideMargins.right - iconMargin.toFloat() + val bottomSide = topSide + draggedItem!!.heightCells * rowHeight - sideMargins.top + canvas.drawRoundRect(leftSide, topSide, rightSide, bottomSide, roundedCornerRadius, roundedCornerRadius, dragShadowCirclePaint) + } } } }