From 3c85d89dad6f2098aac743da2db0f7b63db28362 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 1 Oct 2022 21:55:44 +0200 Subject: [PATCH] adding some long press related improvements --- .../launcher/activities/MainActivity.kt | 33 ++++++++++++++++--- .../launcher/adapters/LaunchersAdapter.kt | 8 +++-- .../launcher/fragments/AllAppsFragment.kt | 2 +- .../launcher/models/HomeScreenGridItem.kt | 2 +- .../launcher/views/HomeScreenGrid.kt | 25 ++++++++++---- app/src/main/res/values/dimens.xml | 2 +- 6 files changed, 54 insertions(+), 18 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 824a86d..c4fcd93 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt @@ -269,10 +269,11 @@ class MainActivity : SimpleActivity(), FlingListener { } mIgnoreMoveEvents = true - main_holder.performHapticFeedback() val clickedGridItem = home_screen_grid.isClickingGridItem(x.toInt(), y.toInt()) if (clickedGridItem != null) { - showHomeIconMenu(x, y - resources.getDimension(R.dimen.icon_long_press_anchor_offset_y), clickedGridItem, false) + val yOffset = resources.getDimension(R.dimen.long_press_anchor_button_offset_y) + val anchorY = home_screen_grid.sideMargins.top + (clickedGridItem.top * home_screen_grid.rowHeight.toFloat()) - yOffset + showHomeIconMenu(x, anchorY, clickedGridItem, false) return } @@ -288,9 +289,18 @@ class MainActivity : SimpleActivity(), FlingListener { fun showHomeIconMenu(x: Float, y: Float, gridItem: HomeScreenGridItem, isOnAllAppsFragment: Boolean) { mLongPressedIcon = gridItem + val anchorY = if (isOnAllAppsFragment || gridItem.type == ITEM_TYPE_WIDGET) { + y + } else { + home_screen_grid.sideMargins.top + (gridItem.top * home_screen_grid.rowHeight.toFloat()) + } + home_screen_popup_menu_anchor.x = x - home_screen_popup_menu_anchor.y = y - mOpenPopupMenu = handleGridItemPopupMenu(home_screen_popup_menu_anchor, gridItem, isOnAllAppsFragment) + home_screen_popup_menu_anchor.y = anchorY + + if (mOpenPopupMenu == null) { + mOpenPopupMenu = handleGridItemPopupMenu(home_screen_popup_menu_anchor, gridItem, isOnAllAppsFragment) + } } fun widgetLongPressedOnList(gridItem: HomeScreenGridItem) { @@ -301,7 +311,7 @@ class MainActivity : SimpleActivity(), FlingListener { private fun showMainLongPressMenu(x: Float, y: Float) { home_screen_popup_menu_anchor.x = x - home_screen_popup_menu_anchor.y = y - resources.getDimension(R.dimen.home_long_press_anchor_offset_y) + home_screen_popup_menu_anchor.y = y - resources.getDimension(R.dimen.long_press_anchor_button_offset_y) val contextTheme = ContextThemeWrapper(this, getPopupMenuTheme()) PopupMenu(contextTheme, home_screen_popup_menu_anchor, Gravity.TOP or Gravity.END).apply { inflate(R.menu.menu_home_screen) @@ -316,8 +326,21 @@ class MainActivity : SimpleActivity(), FlingListener { } private fun handleGridItemPopupMenu(anchorView: View, gridItem: HomeScreenGridItem, isOnAllAppsFragment: Boolean): PopupMenu { + var visibleMenuButtons = 3 + if (gridItem.type != ITEM_TYPE_ICON) { + visibleMenuButtons -= 2 + } + + if (isOnAllAppsFragment) { + visibleMenuButtons -= 1 + } + + val yOffset = resources.getDimension(R.dimen.long_press_anchor_button_offset_y) * (visibleMenuButtons - 1) + anchorView.y -= yOffset + val contextTheme = ContextThemeWrapper(this, getPopupMenuTheme()) return PopupMenu(contextTheme, anchorView, Gravity.TOP or Gravity.END).apply { + setForceShowIcon(true) inflate(R.menu.menu_app_icon) menu.findItem(R.id.app_info).isVisible = gridItem.type == ITEM_TYPE_ICON menu.findItem(R.id.uninstall).isVisible = gridItem.type == ITEM_TYPE_ICON diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/adapters/LaunchersAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/adapters/LaunchersAdapter.kt index ba6fac9..8cfc617 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/adapters/LaunchersAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/adapters/LaunchersAdapter.kt @@ -9,7 +9,10 @@ import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions import com.bumptech.glide.request.transition.DrawableCrossFadeFactory import com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller -import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor +import com.simplemobiletools.commons.extensions.getProperTextColor +import com.simplemobiletools.commons.extensions.portrait +import com.simplemobiletools.commons.extensions.realScreenSize import com.simplemobiletools.launcher.R import com.simplemobiletools.launcher.activities.SimpleActivity import com.simplemobiletools.launcher.interfaces.AllAppsListener @@ -19,7 +22,6 @@ import kotlinx.android.synthetic.main.item_launcher_label.view.* class LaunchersAdapter( val activity: SimpleActivity, var launchers: ArrayList, - val fastScroller: RecyclerViewFastScroller, val allAppsListener: AllAppsListener, val itemClick: (Any) -> Unit ) : RecyclerView.Adapter(), RecyclerViewFastScroller.OnPopupTextUpdate { @@ -85,7 +87,7 @@ class LaunchersAdapter( setOnClickListener { itemClick(launcher) } setOnLongClickListener { view -> - allAppsListener.onAppLauncherLongPressed(view.x, view.y, launcher) + allAppsListener.onAppLauncherLongPressed(view.x + width / 2, view.y, launcher) true } } diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt index cb61eb3..738b24a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt @@ -89,7 +89,7 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : MyFragment val currAdapter = all_apps_grid.adapter if (currAdapter == null) { - LaunchersAdapter(activity!!, launchers, all_apps_fastscroller, this) { + LaunchersAdapter(activity!!, launchers, this) { activity?.launchApp((it as AppLauncher).packageName) }.apply { all_apps_grid.adapter = this diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/models/HomeScreenGridItem.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/models/HomeScreenGridItem.kt index 0411504..971661d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/models/HomeScreenGridItem.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/models/HomeScreenGridItem.kt @@ -4,7 +4,7 @@ import android.graphics.drawable.Drawable import androidx.room.* import com.simplemobiletools.launcher.helpers.ITEM_TYPE_ICON -// grid coords are from 0-5 by default. Icons occupy 1 slot only, widgets can be bigger +// grid cells are from 0-5 by default. Icons occupy 1 slot only, widgets can be bigger @Entity(tableName = "home_screen_grid_items", indices = [(Index(value = ["id"], unique = true))]) data class HomeScreenGridItem( @PrimaryKey(autoGenerate = true) var id: Long?, 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 0c076f0..b17b06d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt @@ -35,12 +35,12 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel // let's use a 6x5 grid for now with 1 special row at the bottom, prefilled with default apps private var rowXCoords = ArrayList(COLUMN_COUNT) private var rowYCoords = ArrayList(ROW_COUNT) - private var rowWidth = 0 - private var rowHeight = 0 + var rowWidth = 0 + var rowHeight = 0 private var iconSize = 0 // apply fake margins at the home screen. Real ones would cause the icons be cut at dragging at screen sides - private var sideMargins = Rect() + var sideMargins = Rect() private var gridItems = ArrayList() private var gridCenters = ArrayList>() @@ -351,7 +351,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel widgetView.tag = appWidgetId widgetView.setAppWidget(appWidgetId, appWidgetProviderInfo) widgetView.longPressListener = { x, y -> - val yOffset = resources.getDimension(R.dimen.home_long_press_anchor_offset_y) + val yOffset = resources.getDimension(R.dimen.long_press_anchor_button_offset_y) (context as? MainActivity)?.showHomeIconMenu(x, widgetView.y - yOffset, item, false) } @@ -549,9 +549,20 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel fun isClickingGridItem(x: Int, y: Int): HomeScreenGridItem? { for (gridItem in gridItems) { - val rect = getClickableRect(gridItem) - if (x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom) { - return gridItem + if (gridItem.type == ITEM_TYPE_ICON) { + val rect = getClickableRect(gridItem) + if (x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom) { + return gridItem + } + } else if (gridItem.type == ITEM_TYPE_WIDGET) { + val left = calculateWidgetX(gridItem.left) + val top = calculateWidgetY(gridItem.top) + val right = left + gridItem.widthCells * rowWidth + val bottom = top + gridItem.heightCells * rowHeight + + if (x >= left && x <= right && y >= top && y <= bottom) { + return gridItem + } } } diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index 814ebb6..d33a44e 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -1,7 +1,7 @@ 55dp 150dp - 40dp + 50dp 140dp 10dp