Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to show at anchor location. #28

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions cascade/src/main/java/me/saket/cascade/CascadePopupMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ package me.saket.cascade
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.Drawable
import android.view.Gravity
import android.view.Menu
import android.view.MenuItem
import android.view.SubMenu
import android.view.View
import android.view.*
geftimov marked this conversation as resolved.
Show resolved Hide resolved
import android.view.View.SCROLLBARS_INSIDE_OVERLAY
import android.view.ViewGroup.LayoutParams
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
Expand All @@ -27,7 +23,7 @@ import androidx.recyclerview.widget.RecyclerView.RecycledViewPool
import me.saket.cascade.internal.OverScrollIfContentScrolls
import me.saket.cascade.internal.dip
import me.saket.cascade.internal.setCallback
import java.util.Stack
import java.util.*
geftimov marked this conversation as resolved.
Show resolved Hide resolved
import kotlin.DeprecationLevel.ERROR

open class CascadePopupMenu @JvmOverloads constructor(
Expand Down Expand Up @@ -68,7 +64,7 @@ open class CascadePopupMenu @JvmOverloads constructor(
}
}

fun show() {
fun show(atLocation: Boolean = false) {
geftimov marked this conversation as resolved.
Show resolved Hide resolved
// PopupWindow moves the popup to align with the anchor if a fixed width
// is known before hand. Note to self: If fixedWidth ever needs to be
// removed, copy over MenuPopup.measureIndividualMenuWidth().
Expand All @@ -85,7 +81,14 @@ open class CascadePopupMenu @JvmOverloads constructor(
}

showMenu(menuBuilder, goingForward = true)
popup.showAsDropDown(anchor, 0, 0, gravity)

if (atLocation) {
val outLocation = IntArray(2)
anchor.getLocationOnScreen(outLocation)
popup.showAtLocation(anchor, gravity, outLocation[0], outLocation[1])
} else {
popup.showAsDropDown(anchor, 0, 0, gravity)
}
}

/**
Expand Down
19 changes: 16 additions & 3 deletions sample/src/main/java/me/saket/cascade/sample/SampleActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.os.Bundle
import android.util.TypedValue
import android.view.SubMenu
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.content.res.AppCompatResources
import androidx.appcompat.widget.Toolbar
Expand All @@ -38,11 +39,23 @@ class SampleActivity : AppCompatActivity() {
val menuButton = toolbar.findViewById<View>(R.id.overflow_menu)
showcaseMenuButton(toolbar, menuButton)
menuButton.setOnClickListener {
showCascadeMenu(anchor = menuButton)
showCascadeMenu(anchor = menuButton, showAtLocation = false)
}
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
showCascadeMenu(anchor = button, showAtLocation = true)
}
val buttonSecond = findViewById<Button>(R.id.buttonSecond)
buttonSecond.setOnClickListener {
showCascadeMenu(anchor = buttonSecond, showAtLocation = true)
}
val buttonThird = findViewById<Button>(R.id.buttonThird)
buttonThird.setOnClickListener {
showCascadeMenu(anchor = buttonThird, showAtLocation = false)
}
}

private fun showCascadeMenu(anchor: View) {
private fun showCascadeMenu(anchor: View, showAtLocation: Boolean) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private fun showCascadeMenu(anchor: View, showAtLocation: Boolean) {
private fun showCascadeMenu(anchor: View, fromAnchor: Boolean) {

val popupMenu = CascadePopupMenu(this, anchor, styler = cascadeMenuStyler())
popupMenu.menu.apply {
MenuCompat.setGroupDividerEnabled(this, true)
Expand Down Expand Up @@ -85,7 +98,7 @@ class SampleActivity : AppCompatActivity() {
}
}
}
popupMenu.show()
popupMenu.show(showAtLocation)
}

private fun cascadeMenuStyler(): CascadePopupMenu.Styler {
Expand Down
48 changes: 38 additions & 10 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="@string/app_name"
app:titleTextColor="?attr/colorAccent" />
android:layout_height="match_parent"
android:orientation="vertical">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="@string/app_name"
app:titleTextColor="?attr/colorAccent" />

<com.google.android.material.button.MaterialButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="16dp"
android:text="@string/button_text"
app:backgroundTint="@color/color_accent" />

<com.google.android.material.button.MaterialButton
android:id="@+id/buttonThird"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="16dp"
android:text="@string/button_text"
app:backgroundTint="@color/color_accent" />

<com.google.android.material.button.MaterialButton
android:id="@+id/buttonSecond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="16dp"
android:text="@string/button_text"
app:backgroundTint="@color/color_accent" />

</FrameLayout>
1 change: 1 addition & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name">Cascade</string>
<string name="button_text">Press</string>
</resources>