From f6f81212db12688c480288765d10f9013283f70c Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 22 Aug 2022 23:09:42 +0200 Subject: [PATCH] show a popup menu at long pressing the home screen --- .../launcher/activities/MainActivity.kt | 35 +++++++++++++++++-- app/src/main/res/layout/activity_main.xml | 8 ++++- app/src/main/res/menu/menu_home_screen.xml | 9 +++++ app/src/main/res/values/dimens.xml | 1 + 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 app/src/main/res/menu/menu_home_screen.xml 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 19add4d..00aae38 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt @@ -4,13 +4,14 @@ import android.animation.ObjectAnimator import android.content.res.Configuration import android.graphics.Color import android.os.Bundle +import android.view.ContextThemeWrapper import android.view.GestureDetector +import android.view.Gravity import android.view.MotionEvent import android.view.animation.DecelerateInterpolator +import android.widget.PopupMenu import androidx.core.view.GestureDetectorCompat -import com.simplemobiletools.commons.extensions.appLaunched -import com.simplemobiletools.commons.extensions.beVisible -import com.simplemobiletools.commons.extensions.realScreenSize +import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.launcher.BuildConfig import com.simplemobiletools.launcher.R import com.simplemobiletools.launcher.fragments.AllAppsFragment @@ -105,6 +106,30 @@ class MainActivity : SimpleActivity(), FlingListener { } } + fun homeScreenLongPressed(x: Float, y: Float) { + main_holder.performHapticFeedback() + + popup_menu_anchor.x = x + popup_menu_anchor.y = y - resources.getDimension(R.dimen.long_press_anchor_offset_y) + val contextTheme = ContextThemeWrapper(this, getPopupMenuTheme()) + PopupMenu(contextTheme, popup_menu_anchor, Gravity.TOP or Gravity.END).apply { + inflate(R.menu.menu_home_screen) + setOnMenuItemClickListener { item -> + when (item.itemId) { + R.id.widgets -> { + showWidgetsFragment() + } + } + true + } + show() + } + } + + private fun showWidgetsFragment() { + + } + private class MyGestureListener(private val flingListener: FlingListener) : GestureDetector.SimpleOnGestureListener() { override fun onFling(event1: MotionEvent, event2: MotionEvent, velocityX: Float, velocityY: Float): Boolean { if (velocityY > 0) { @@ -114,6 +139,10 @@ class MainActivity : SimpleActivity(), FlingListener { } return true } + + override fun onLongPress(event: MotionEvent) { + (flingListener as MainActivity).homeScreenLongPressed(event.x, event.y) + } } override fun onFlingUp() { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index fb1b81e..08c9e1b 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -2,11 +2,17 @@ + android:layout_height="match_parent"> + + diff --git a/app/src/main/res/menu/menu_home_screen.xml b/app/src/main/res/menu/menu_home_screen.xml new file mode 100644 index 0000000..6149eef --- /dev/null +++ b/app/src/main/res/menu/menu_home_screen.xml @@ -0,0 +1,9 @@ + + + + diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index a9c8701..86b2344 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -1,3 +1,4 @@ 55dp + 70dp