add some preparation for icon dragging

This commit is contained in:
tibbi 2022-09-21 22:34:49 +02:00
parent 4667cc447c
commit f86caa6c89
2 changed files with 16 additions and 5 deletions

View File

@ -151,7 +151,7 @@ class MainActivity : SimpleActivity(), FlingListener {
mTouchDownY = -1 mTouchDownY = -1
mIgnoreMoveEvents = false mIgnoreMoveEvents = false
mLongPressedIcon = null mLongPressedIcon = null
home_screen_grid.itemDraggingStopped() home_screen_grid.itemDraggingStopped(getGridTouchedX(event.x), getGridTouchedY(event.y))
if (!mIgnoreUpEvent) { if (!mIgnoreUpEvent) {
if (all_apps_fragment.y < mScreenHeight * 0.7) { if (all_apps_fragment.y < mScreenHeight * 0.7) {
showFragment(all_apps_fragment) showFragment(all_apps_fragment)
@ -231,7 +231,7 @@ class MainActivity : SimpleActivity(), FlingListener {
mIgnoreMoveEvents = true mIgnoreMoveEvents = true
main_holder.performHapticFeedback() main_holder.performHapticFeedback()
val clickedGridItem = home_screen_grid.isClickingGridItem(x - home_screen_grid.marginLeft, y - home_screen_grid.marginTop) val clickedGridItem = home_screen_grid.isClickingGridItem(getGridTouchedX(x), getGridTouchedY(y))
if (clickedGridItem != null) { if (clickedGridItem != null) {
mLongPressedIcon = clickedGridItem mLongPressedIcon = clickedGridItem
showHomeIconMenu(x, y, clickedGridItem.packageName) showHomeIconMenu(x, y, clickedGridItem.packageName)
@ -243,13 +243,17 @@ class MainActivity : SimpleActivity(), FlingListener {
fun homeScreenClicked(x: Float, y: Float) { fun homeScreenClicked(x: Float, y: Float) {
if (x >= home_screen_grid.left && x <= home_screen_grid.right && y >= home_screen_grid.top && y <= home_screen_grid.bottom) { if (x >= home_screen_grid.left && x <= home_screen_grid.right && y >= home_screen_grid.top && y <= home_screen_grid.bottom) {
val clickedGridItem = home_screen_grid.isClickingGridItem(x - home_screen_grid.marginLeft, y - home_screen_grid.marginTop) val clickedGridItem = home_screen_grid.isClickingGridItem(getGridTouchedX(x), getGridTouchedY(y))
if (clickedGridItem != null) { if (clickedGridItem != null) {
launchApp(clickedGridItem.packageName) launchApp(clickedGridItem.packageName)
} }
} }
} }
private fun getGridTouchedX(x: Float) = Math.min(Math.max(x.toInt() - home_screen_grid.marginLeft, 0), home_screen_grid.width).toInt()
private fun getGridTouchedY(y: Float) = Math.min(Math.max(y.toInt() - home_screen_grid.marginTop, 0), home_screen_grid.height).toInt()
private fun showHomeIconMenu(x: Float, y: Float, clickedPackageName: String) { private fun showHomeIconMenu(x: Float, y: Float, clickedPackageName: String) {
home_screen_popup_menu_anchor.x = x home_screen_popup_menu_anchor.x = x
home_screen_popup_menu_anchor.y = y - resources.getDimension(R.dimen.long_press_anchor_offset_y) home_screen_popup_menu_anchor.y = y - resources.getDimension(R.dimen.long_press_anchor_offset_y)

View File

@ -37,6 +37,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
private var gridItems = ArrayList<HomeScreenGridItem>() private var gridItems = ArrayList<HomeScreenGridItem>()
private var gridItemDrawables = HashMap<String, Drawable>() private var gridItemDrawables = HashMap<String, Drawable>()
private var gridCenters = ArrayList<Pair<Int, Int>>()
init { init {
textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply { textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply {
@ -68,7 +69,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
invalidate() invalidate()
} }
fun itemDraggingStopped() { fun itemDraggingStopped(x: Int, y: Int) {
draggedItem = null draggedItem = null
invalidate() invalidate()
} }
@ -87,6 +88,12 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
for (i in 0 until ROW_COUNT) { for (i in 0 until ROW_COUNT) {
rowYCoords.add(i, i * rowHeight) rowYCoords.add(i, i * rowHeight)
} }
rowXCoords.forEach { x ->
rowYCoords.forEach { y ->
gridCenters.add(Pair(x + rowWidth / 2, y + rowHeight / 2))
}
}
} }
gridItems.forEach { item -> gridItems.forEach { item ->
@ -123,7 +130,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
} }
} }
fun isClickingGridItem(x: Float, y: Float): HomeScreenGridItem? { fun isClickingGridItem(x: Int, y: Int): HomeScreenGridItem? {
for (gridItem in gridItems) { for (gridItem in gridItems) {
if (x >= gridItem.left * rowWidth && x <= gridItem.right * rowWidth && y >= gridItem.top * rowHeight && y <= gridItem.bottom * rowHeight) { if (x >= gridItem.left * rowWidth && x <= gridItem.right * rowWidth && y >= gridItem.top * rowHeight && y <= gridItem.bottom * rowHeight) {
return gridItem return gridItem