mirror of
https://github.com/SimpleMobileTools/Simple-Launcher.git
synced 2025-04-20 04:47:33 +02:00
handle widget dragging
This commit is contained in:
parent
d4b4edd6eb
commit
65d13c31b1
@ -21,7 +21,7 @@ interface HomeScreenGridItemsDao {
|
|||||||
fun updateAppTitle(title: String, packageName: String)
|
fun updateAppTitle(title: String, packageName: String)
|
||||||
|
|
||||||
@Query("UPDATE home_screen_grid_items SET `left` = :left, `top` = :top, `right` = :right, `bottom` = :bottom WHERE id = :id")
|
@Query("UPDATE home_screen_grid_items SET `left` = :left, `top` = :top, `right` = :right, `bottom` = :bottom WHERE id = :id")
|
||||||
fun updateAppPosition(left: Int, top: Int, right: Int, bottom: Int, id: Long)
|
fun updateItemPosition(left: Int, top: Int, right: Int, bottom: Int, id: Long)
|
||||||
|
|
||||||
@Query("DELETE FROM home_screen_grid_items WHERE id = :id")
|
@Query("DELETE FROM home_screen_grid_items WHERE id = :id")
|
||||||
fun deleteById(id: Long)
|
fun deleteById(id: Long)
|
||||||
|
@ -8,6 +8,7 @@ import android.graphics.Canvas
|
|||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
import android.graphics.Paint
|
import android.graphics.Paint
|
||||||
import android.graphics.Rect
|
import android.graphics.Rect
|
||||||
|
import android.graphics.drawable.BitmapDrawable
|
||||||
import android.text.Layout
|
import android.text.Layout
|
||||||
import android.text.StaticLayout
|
import android.text.StaticLayout
|
||||||
import android.text.TextPaint
|
import android.text.TextPaint
|
||||||
@ -82,8 +83,10 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
ensureBackgroundThread {
|
ensureBackgroundThread {
|
||||||
gridItems = context.homeScreenGridItemsDB.getAllItems() as ArrayList<HomeScreenGridItem>
|
gridItems = context.homeScreenGridItemsDB.getAllItems() as ArrayList<HomeScreenGridItem>
|
||||||
gridItems.forEach { item ->
|
gridItems.forEach { item ->
|
||||||
|
if (item.type == ITEM_TYPE_ICON) {
|
||||||
item.drawable = context.getDrawableForPackageName(item.packageName)
|
item.drawable = context.getDrawableForPackageName(item.packageName)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
redrawGrid()
|
redrawGrid()
|
||||||
}
|
}
|
||||||
@ -187,7 +190,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
bottom = yIndex + 1
|
bottom = yIndex + 1
|
||||||
|
|
||||||
ensureBackgroundThread {
|
ensureBackgroundThread {
|
||||||
context.homeScreenGridItemsDB.updateAppPosition(left, top, right, bottom, id!!)
|
context.homeScreenGridItemsDB.updateItemPosition(left, top, right, bottom, id!!)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
redrawIcons = true
|
redrawIcons = true
|
||||||
@ -268,11 +271,28 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
}
|
}
|
||||||
|
|
||||||
ensureBackgroundThread {
|
ensureBackgroundThread {
|
||||||
|
// store the new widget at creating it, else just move the existing one
|
||||||
|
if (widgetItem.id == null) {
|
||||||
val itemId = context.homeScreenGridItemsDB.insert(widgetItem)
|
val itemId = context.homeScreenGridItemsDB.insert(widgetItem)
|
||||||
widgetItem.id = itemId
|
widgetItem.id = itemId
|
||||||
post {
|
post {
|
||||||
bindWidget(widgetItem, false)
|
bindWidget(widgetItem, false)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
context.homeScreenGridItemsDB.updateItemPosition(widgetItem.left, widgetItem.top, widgetItem.right, widgetItem.bottom, widgetItem.id!!)
|
||||||
|
val widgetView = widgetViews.firstOrNull { it.tag == widgetItem.widgetId }
|
||||||
|
if (widgetView != null) {
|
||||||
|
widgetView.x = calculateWidgetX(widgetItem.left)
|
||||||
|
widgetView.y = calculateWidgetY(widgetItem.top)
|
||||||
|
}
|
||||||
|
|
||||||
|
gridItems.firstOrNull { it.id == widgetItem.id }?.apply {
|
||||||
|
left = widgetItem.left
|
||||||
|
right = widgetItem.right
|
||||||
|
top = widgetItem.top
|
||||||
|
bottom = widgetItem.bottom
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
performHapticFeedback()
|
performHapticFeedback()
|
||||||
@ -319,16 +339,30 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
widgetView.longPressListener = { x, y ->
|
widgetView.longPressListener = { x, y ->
|
||||||
val yOffset = resources.getDimension(R.dimen.home_long_press_anchor_offset_y)
|
val yOffset = resources.getDimension(R.dimen.home_long_press_anchor_offset_y)
|
||||||
(context as? MainActivity)?.showHomeIconMenu(x, widgetView.y - yOffset, item, false)
|
(context as? MainActivity)?.showHomeIconMenu(x, widgetView.y - yOffset, item, false)
|
||||||
|
|
||||||
|
val gridItem = gridItems.firstOrNull { it.widgetId == appWidgetId }
|
||||||
|
if (gridItem != null) {
|
||||||
|
widgetView.buildDrawingCache()
|
||||||
|
gridItem.drawable = BitmapDrawable(widgetView.drawingCache)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
widgetView.x = item.left * rowWidth + sideMargins.left.toFloat()
|
widgetView.x = calculateWidgetX(item.left)
|
||||||
widgetView.y = item.top * rowHeight + sideMargins.top.toFloat()
|
widgetView.y = calculateWidgetY(item.top)
|
||||||
val widgetWidth = item.widthCells * rowWidth
|
val widgetWidth = item.widthCells * rowWidth
|
||||||
val widgetHeight = item.heightCells * rowHeight
|
val widgetHeight = item.heightCells * rowHeight
|
||||||
addView(widgetView, widgetWidth, widgetHeight)
|
addView(widgetView, widgetWidth, widgetHeight)
|
||||||
widgetViews.add(widgetView)
|
widgetViews.add(widgetView)
|
||||||
|
|
||||||
|
// remove the drawable so that it gets refreshed on long press
|
||||||
|
item.drawable = null
|
||||||
|
gridItems.add(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun calculateWidgetX(leftCell: Int) = leftCell * rowWidth + sideMargins.left.toFloat()
|
||||||
|
|
||||||
|
private fun calculateWidgetY(topCell: Int) = topCell * rowHeight + sideMargins.top.toFloat()
|
||||||
|
|
||||||
// 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 ->
|
||||||
|
Loading…
x
Reference in New Issue
Block a user