hide the menu at long pressing an item and moving gesture

This commit is contained in:
tibbi 2022-09-22 21:22:32 +02:00
parent 5ccaf8ce43
commit 61801ebcb2
3 changed files with 32 additions and 28 deletions

View file

@ -137,6 +137,7 @@ class MainActivity : SimpleActivity(), FlingListener {
mOpenPopupMenu?.dismiss()
mOpenPopupMenu = null
home_screen_grid.itemDraggingStarted(mLongPressedIcon!!)
hideFragment(all_apps_fragment)
}
if (mTouchDownY != -1 && !mIgnoreMoveEvents) {
@ -151,6 +152,7 @@ class MainActivity : SimpleActivity(), FlingListener {
mTouchDownY = -1
mIgnoreMoveEvents = false
mLongPressedIcon = null
(all_apps_fragment as AllAppsFragment).ignoreTouches = false
home_screen_grid.itemDraggingStopped(getGridTouchedX(event.x), getGridTouchedY(event.y))
if (!mIgnoreUpEvent) {
if (all_apps_fragment.y < mScreenHeight * 0.7) {
@ -233,7 +235,7 @@ class MainActivity : SimpleActivity(), FlingListener {
main_holder.performHapticFeedback()
val clickedGridItem = home_screen_grid.isClickingGridItem(getGridTouchedX(x), getGridTouchedY(y))
if (clickedGridItem != null) {
showHomeIconMenu(x, y - resources.getDimension(R.dimen.icon_long_press_anchor_offset_y), clickedGridItem, false)
showHomeIconMenu(x, y - resources.getDimension(R.dimen.icon_long_press_anchor_offset_y), clickedGridItem)
return
}
@ -253,11 +255,7 @@ class MainActivity : SimpleActivity(), FlingListener {
private fun getGridTouchedY(y: Float) = Math.min(Math.max(y.toInt() - home_screen_grid.marginTop, 0), home_screen_grid.height).toInt()
fun showHomeIconMenu(x: Float, y: Float, gridItem: HomeScreenGridItem, isFromAllAppsFragment: Boolean) {
if (isFromAllAppsFragment) {
hideFragment(all_apps_fragment)
}
fun showHomeIconMenu(x: Float, y: Float, gridItem: HomeScreenGridItem) {
mLongPressedIcon = gridItem
home_screen_popup_menu_anchor.x = x
home_screen_popup_menu_anchor.y = y
@ -280,6 +278,27 @@ class MainActivity : SimpleActivity(), FlingListener {
}
}
private fun handleGridItemPopupMenu(anchorView: View, appPackageName: String): PopupMenu {
val contextTheme = ContextThemeWrapper(this, getPopupMenuTheme())
return PopupMenu(contextTheme, anchorView, Gravity.TOP or Gravity.END).apply {
inflate(R.menu.menu_app_icon)
setOnMenuItemClickListener { item ->
(all_apps_fragment as AllAppsFragment).ignoreTouches = false
when (item.itemId) {
R.id.app_info -> launchAppInfo(appPackageName)
R.id.uninstall -> uninstallApp(appPackageName)
}
true
}
setOnDismissListener {
(all_apps_fragment as AllAppsFragment).ignoreTouches = false
}
show()
}
}
private fun showWidgetsFragment() {
showFragment(widgets_fragment)
}

View file

@ -4,13 +4,7 @@ import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import android.view.ContextThemeWrapper
import android.view.Gravity
import android.view.View
import android.widget.PopupMenu
import com.simplemobiletools.commons.extensions.getPopupMenuTheme
import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.helpers.UNINSTALL_APP_REQUEST_CODE
fun Activity.launchApp(packageName: String) {
@ -35,18 +29,3 @@ fun Activity.uninstallApp(packageName: String) {
startActivityForResult(this, UNINSTALL_APP_REQUEST_CODE)
}
}
fun Activity.handleGridItemPopupMenu(anchorView: View, appPackageName: String): PopupMenu {
val contextTheme = ContextThemeWrapper(this, getPopupMenuTheme())
return PopupMenu(contextTheme, anchorView, Gravity.TOP or Gravity.END).apply {
inflate(R.menu.menu_app_icon)
setOnMenuItemClickListener { item ->
when (item.itemId) {
R.id.app_info -> launchAppInfo(appPackageName)
R.id.uninstall -> uninstallApp(appPackageName)
}
true
}
show()
}
}

View file

@ -21,6 +21,7 @@ import kotlinx.android.synthetic.main.all_apps_fragment.view.*
class AllAppsFragment(context: Context, attributeSet: AttributeSet) : MyFragment(context, attributeSet), AllAppsListener {
private var touchDownY = -1
var ignoreTouches = false
@SuppressLint("ClickableViewAccessibility")
override fun setupFragment(activity: MainActivity) {
@ -49,6 +50,10 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
}
override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
if (ignoreTouches) {
return true
}
var shouldIntercept = false
if (touchDownY != -1) {
shouldIntercept = touchDownY - event.y < 0 && all_apps_grid.computeVerticalScrollOffset() == 0
@ -123,6 +128,7 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
override fun onAppLauncherLongPressed(x: Float, y: Float, appLauncher: AppLauncher) {
val gridItem = HomeScreenGridItem(null, -1, -1, -1, 1, appLauncher.packageName, appLauncher.title)
activity?.showHomeIconMenu(x, y, gridItem, true)
activity?.showHomeIconMenu(x, y, gridItem)
ignoreTouches = true
}
}