add some preparations for adding widgets

This commit is contained in:
tibbi 2022-09-26 12:38:10 +02:00
parent f93272aa97
commit 29d776d7ae
1 changed files with 38 additions and 20 deletions

View File

@ -19,6 +19,8 @@ import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.extensions.getDrawableForPackageName import com.simplemobiletools.launcher.extensions.getDrawableForPackageName
import com.simplemobiletools.launcher.extensions.homeScreenGridItemsDB import com.simplemobiletools.launcher.extensions.homeScreenGridItemsDB
import com.simplemobiletools.launcher.helpers.COLUMN_COUNT import com.simplemobiletools.launcher.helpers.COLUMN_COUNT
import com.simplemobiletools.launcher.helpers.ITEM_TYPE_ICON
import com.simplemobiletools.launcher.helpers.ITEM_TYPE_WIDGET
import com.simplemobiletools.launcher.helpers.ROW_COUNT import com.simplemobiletools.launcher.helpers.ROW_COUNT
import com.simplemobiletools.launcher.models.HomeScreenGridItem import com.simplemobiletools.launcher.models.HomeScreenGridItem
@ -111,6 +113,14 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
return return
} }
if (draggedItem!!.type == ITEM_TYPE_ICON) {
addAppIcon(x, y)
} else if (draggedItem!!.type == ITEM_TYPE_WIDGET) {
addWidget(x, y)
}
}
private fun addAppIcon(x: Int, y: Int) {
val center = gridCenters.minBy { Math.abs(it.first - x + sideMargins.left) + Math.abs(it.second - y + sideMargins.top) } val center = gridCenters.minBy { Math.abs(it.first - x + sideMargins.left) + Math.abs(it.second - y + sideMargins.top) }
var redrawIcons = false var redrawIcons = false
@ -171,6 +181,12 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
} }
} }
private fun addWidget(x: Int, y: Int) {
draggedItem = null
draggedItemCurrentCoords = Pair(-1, -1)
invalidate()
}
// convert stuff like 102x192 to grid cells like 0x1 // convert stuff like 102x192 to grid cells like 0x1
private fun getClosestGridCells(center: Pair<Int, Int>): Pair<Int, Int>? { private fun getClosestGridCells(center: Pair<Int, Int>): Pair<Int, Int>? {
rowXCoords.forEachIndexed { xIndex, xCell -> rowXCoords.forEachIndexed { xIndex, xCell ->
@ -184,9 +200,9 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
return null return null
} }
fun getFakeWidth() = width - sideMargins.left - sideMargins.right private fun getFakeWidth() = width - sideMargins.left - sideMargins.right
fun getFakeHeight() = height - sideMargins.top - sideMargins.bottom private fun getFakeHeight() = height - sideMargins.top - sideMargins.bottom
@SuppressLint("DrawAllocation") @SuppressLint("DrawAllocation")
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
@ -244,6 +260,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
} }
if (draggedItem != null && draggedItemCurrentCoords.first != -1 && draggedItemCurrentCoords.second != -1) { if (draggedItem != null && draggedItemCurrentCoords.first != -1 && draggedItemCurrentCoords.second != -1) {
if (draggedItem!!.type == ITEM_TYPE_ICON) {
// draw a circle under the current cell // draw a circle under the current cell
val center = val center =
gridCenters.minBy { Math.abs(it.first - draggedItemCurrentCoords.first + sideMargins.left) + Math.abs(it.second - draggedItemCurrentCoords.second + sideMargins.top) } gridCenters.minBy { Math.abs(it.first - draggedItemCurrentCoords.first + sideMargins.left) + Math.abs(it.second - draggedItemCurrentCoords.second + sideMargins.top) }
@ -266,6 +283,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
draggedItem!!.drawable!!.draw(canvas) draggedItem!!.drawable!!.draw(canvas)
} }
} }
}
// get the clickable area around the icon, it includes text too // get the clickable area around the icon, it includes text too
private fun getClickableRect(item: HomeScreenGridItem): Rect { private fun getClickableRect(item: HomeScreenGridItem): Rect {