|
@@ -2,6 +2,7 @@
|
|
|
|
|
|
package eu.kanade.tachiyomi.util.view
|
|
|
|
|
|
+import android.annotation.SuppressLint
|
|
|
import android.graphics.Point
|
|
|
import android.view.Gravity
|
|
|
import android.view.Menu
|
|
@@ -9,14 +10,18 @@ import android.view.MenuItem
|
|
|
import android.view.View
|
|
|
import androidx.annotation.MenuRes
|
|
|
import androidx.annotation.StringRes
|
|
|
+import androidx.appcompat.view.menu.MenuBuilder
|
|
|
import androidx.appcompat.widget.PopupMenu
|
|
|
import androidx.appcompat.widget.TooltipCompat
|
|
|
+import androidx.core.content.ContextCompat
|
|
|
+import androidx.core.view.forEach
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
import com.google.android.material.chip.Chip
|
|
|
import com.google.android.material.chip.ChipGroup
|
|
|
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
|
|
|
import com.google.android.material.snackbar.Snackbar
|
|
|
import eu.kanade.tachiyomi.R
|
|
|
+import eu.kanade.tachiyomi.util.system.getResourceColor
|
|
|
|
|
|
/**
|
|
|
* Returns coordinates of view.
|
|
@@ -63,7 +68,7 @@ inline fun View.setTooltip(@StringRes stringRes: Int) {
|
|
|
inline fun View.popupMenu(
|
|
|
@MenuRes menuRes: Int,
|
|
|
noinline initMenu: (Menu.() -> Unit)? = null,
|
|
|
- noinline onMenuItemClick: MenuItem.() -> Boolean
|
|
|
+ noinline onMenuItemClick: MenuItem.() -> Unit
|
|
|
): PopupMenu {
|
|
|
val popup = PopupMenu(context, this, Gravity.NO_GRAVITY, R.attr.actionOverflowMenuStyle, 0)
|
|
|
popup.menuInflater.inflate(menuRes, popup.menu)
|
|
@@ -71,7 +76,50 @@ inline fun View.popupMenu(
|
|
|
if (initMenu != null) {
|
|
|
popup.menu.initMenu()
|
|
|
}
|
|
|
- popup.setOnMenuItemClickListener { it.onMenuItemClick() }
|
|
|
+ popup.setOnMenuItemClickListener {
|
|
|
+ it.onMenuItemClick()
|
|
|
+ true
|
|
|
+ }
|
|
|
+
|
|
|
+ popup.show()
|
|
|
+ return popup
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Shows a popup menu on top of this view.
|
|
|
+ *
|
|
|
+ * @param items menu item names to inflate the menu with. List of itemId to stringRes pairs.
|
|
|
+ * @param selectedItemId optionally show a checkmark beside an item with this itemId.
|
|
|
+ * @param onMenuItemClick function to execute when a menu item is clicked.
|
|
|
+ */
|
|
|
+@SuppressLint("RestrictedApi")
|
|
|
+inline fun View.popupMenu(
|
|
|
+ items: List<Pair<Int, Int>>,
|
|
|
+ selectedItemId: Int? = null,
|
|
|
+ noinline onMenuItemClick: MenuItem.() -> Unit
|
|
|
+): PopupMenu {
|
|
|
+ val popup = PopupMenu(context, this, Gravity.NO_GRAVITY, R.attr.actionOverflowMenuStyle, 0)
|
|
|
+ items.forEach { (id, stringRes) ->
|
|
|
+ popup.menu.add(0, id, 0, stringRes)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (selectedItemId != null) {
|
|
|
+ (popup.menu as? MenuBuilder)?.setOptionalIconsVisible(true)
|
|
|
+ val emptyIcon = ContextCompat.getDrawable(context, R.drawable.ic_blank_24dp)
|
|
|
+ popup.menu.forEach { item ->
|
|
|
+ item.icon = when (item.itemId) {
|
|
|
+ selectedItemId -> ContextCompat.getDrawable(context, R.drawable.ic_check_24dp)?.mutate()?.apply {
|
|
|
+ setTint(context.getResourceColor(android.R.attr.textColorPrimary))
|
|
|
+ }
|
|
|
+ else -> emptyIcon
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ popup.setOnMenuItemClickListener {
|
|
|
+ it.onMenuItemClick()
|
|
|
+ true
|
|
|
+ }
|
|
|
|
|
|
popup.show()
|
|
|
return popup
|