From f86caa6c8943a718d6da35e660ce85d3d1dc68ec Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 21 Sep 2022 22:34:49 +0200 Subject: [PATCH] add some preparation for icon dragging --- .../launcher/activities/MainActivity.kt | 10 +++++++--- .../launcher/views/HomeScreenGrid.kt | 11 +++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt index c646c27..109e9f5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt @@ -151,7 +151,7 @@ class MainActivity : SimpleActivity(), FlingListener { mTouchDownY = -1 mIgnoreMoveEvents = false mLongPressedIcon = null - home_screen_grid.itemDraggingStopped() + home_screen_grid.itemDraggingStopped(getGridTouchedX(event.x), getGridTouchedY(event.y)) if (!mIgnoreUpEvent) { if (all_apps_fragment.y < mScreenHeight * 0.7) { showFragment(all_apps_fragment) @@ -231,7 +231,7 @@ class MainActivity : SimpleActivity(), FlingListener { mIgnoreMoveEvents = true 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) { mLongPressedIcon = clickedGridItem showHomeIconMenu(x, y, clickedGridItem.packageName) @@ -243,13 +243,17 @@ class MainActivity : SimpleActivity(), FlingListener { 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) { - 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) { 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) { home_screen_popup_menu_anchor.x = x home_screen_popup_menu_anchor.y = y - resources.getDimension(R.dimen.long_press_anchor_offset_y) 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 d3662c3..482123f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt @@ -37,6 +37,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie private var gridItems = ArrayList() private var gridItemDrawables = HashMap() + private var gridCenters = ArrayList>() init { textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply { @@ -68,7 +69,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie invalidate() } - fun itemDraggingStopped() { + fun itemDraggingStopped(x: Int, y: Int) { draggedItem = null invalidate() } @@ -87,6 +88,12 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie for (i in 0 until ROW_COUNT) { rowYCoords.add(i, i * rowHeight) } + + rowXCoords.forEach { x -> + rowYCoords.forEach { y -> + gridCenters.add(Pair(x + rowWidth / 2, y + rowHeight / 2)) + } + } } 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) { if (x >= gridItem.left * rowWidth && x <= gridItem.right * rowWidth && y >= gridItem.top * rowHeight && y <= gridItem.bottom * rowHeight) { return gridItem