Properly translate tap coordinates into grid coordinates for touch events

This commit is contained in:
Ensar Sarajčić 2023-07-16 16:52:27 +02:00
parent 934651e88d
commit 3545701f21
2 changed files with 12 additions and 2 deletions

View File

@ -427,11 +427,12 @@ class MainActivity : SimpleActivity(), FlingListener {
}, ANIMATION_DURATION)
}
fun homeScreenLongPressed(x: Float, y: Float) {
fun homeScreenLongPressed(eventX: Float, eventY: Float) {
if (isAllAppsFragmentExpanded()) {
return
}
val (x, y) = home_screen_grid.intoViewSpaceCoords(eventX, eventY)
mIgnoreMoveEvents = true
val clickedGridItem = home_screen_grid.isClickingGridItem(x.toInt(), y.toInt())
if (clickedGridItem != null) {
@ -448,8 +449,9 @@ class MainActivity : SimpleActivity(), FlingListener {
showMainLongPressMenu(x, y)
}
fun homeScreenClicked(x: Float, y: Float) {
fun homeScreenClicked(eventX: Float, eventY: Float) {
home_screen_grid.hideResizeLines()
val (x, y) = home_screen_grid.intoViewSpaceCoords(eventX, eventY)
val clickedGridItem = home_screen_grid.isClickingGridItem(x.toInt(), y.toInt())
if (clickedGridItem != null) {
if (clickedGridItem.type == ITEM_TYPE_ICON) {

View File

@ -733,6 +733,14 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
return null
}
fun intoViewSpaceCoords(screenSpaceX: Float, screenSpaceY: Float): Pair<Float, Float> {
val viewLocation = IntArray(2)
getLocationOnScreen(viewLocation)
val x = screenSpaceX - viewLocation[0]
val y = screenSpaceY - viewLocation[1]
return Pair(x, y)
}
private fun HomeScreenGridItem.outOfBounds(): Boolean {
return (left >= cellXCoords.size || right >= cellXCoords.size || top >= cellYCoords.size || bottom >= cellYCoords.size)
}