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

added background dim functionality for CascadePopup #45

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from
Open
Changes from all 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
39 changes: 37 additions & 2 deletions cascade/src/main/java/me/saket/cascade/CascadePopupMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@

package me.saket.cascade

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.view.Gravity
import android.view.Menu
Expand All @@ -15,6 +20,7 @@ import android.view.View.SCROLLBARS_INSIDE_OVERLAY
import android.view.ViewGroup.LayoutParams
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import androidx.annotation.IntRange
import androidx.annotation.MenuRes
import androidx.appcompat.view.SupportMenuInflater
import androidx.appcompat.view.menu.MenuBuilder
Expand Down Expand Up @@ -68,7 +74,7 @@ open class CascadePopupMenu @JvmOverloads constructor(
}
}

fun show() {
fun show(@IntRange(from = 0, to = 255) dimAmount: Int? = null) {
// 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 @@ -83,7 +89,9 @@ open class CascadePopupMenu @JvmOverloads constructor(
styler.background()?.let {
popup.contentView.background = it
}

dimAmount?.let {
applyBackgroundDim(dimAmount)
}
showMenu(menuBuilder, goingForward = true)
popup.showAsDropDown(anchor, 0, 0, gravity)
}
Expand Down Expand Up @@ -150,6 +158,33 @@ open class CascadePopupMenu @JvmOverloads constructor(
fun dismiss() =
popup.dismiss()

private fun applyBackgroundDim(@IntRange(from = 0, to = 255) dimAmount: Int) {
val dim: Drawable = ColorDrawable(Color.BLACK)
dim.setBounds(0, 0, anchor.rootView.width, anchor.rootView.height)
dim.alpha = 0
val overlay = anchor.rootView.overlay
overlay.add(dim)
Copy link
Owner

Choose a reason for hiding this comment

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

can the showing and hiding of this overlay be animated?

Copy link
Author

Choose a reason for hiding this comment

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

Added animation for applying dim and removing.


/* FADE IN: Animating dim from 0 to dimAmount */
ObjectAnimator.ofInt(dim, "alpha", dimAmount).apply {
duration = 300
start()
}

popup.setOnDismissListener {
/* FADE OUT: Animating dim from dimAbout to 0 */
ObjectAnimator.ofInt(dim, "alpha", 0).apply {
duration = 300
addListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
overlay.clear()
}
})
start()
}
}
}

@get:JvmName("getDragToOpenListener")
@Deprecated("CascadeMenu doesn't support drag-to-open.", level = ERROR)
val dragToOpenListener: View.OnTouchListener
Expand Down