-
Notifications
You must be signed in to change notification settings - Fork 66
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
Feature: Dim background #18
base: trunk
Are you sure you want to change the base?
Changes from 3 commits
070a99b
0e84ae0
893c130
5f3f37f
63a8424
9941961
76307c0
669c09b
c6f76db
3625a53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,24 +3,29 @@ | |
|
||
package me.saket.cascade | ||
|
||
import android.animation.ObjectAnimator | ||
import android.annotation.SuppressLint | ||
import android.app.Activity | ||
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.ViewGroup | ||
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 android.view.animation.DecelerateInterpolator | ||
import androidx.annotation.MenuRes | ||
import androidx.appcompat.view.SupportMenuInflater | ||
import androidx.appcompat.view.menu.MenuBuilder | ||
import androidx.appcompat.view.menu.MenuItemImpl | ||
import androidx.appcompat.view.menu.SubMenuBuilder | ||
import androidx.appcompat.widget.PopupMenu | ||
import androidx.core.animation.doOnEnd | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import androidx.recyclerview.widget.RecyclerView | ||
import androidx.recyclerview.widget.RecyclerView.RecycledViewPool | ||
|
@@ -52,7 +57,8 @@ open class CascadePopupMenu @JvmOverloads constructor( | |
val background: () -> Drawable? = { null }, | ||
val menuList: (RecyclerView) -> Unit = {}, | ||
val menuTitle: (MenuHeaderViewHolder) -> Unit = {}, | ||
val menuItem: (MenuItemViewHolder) -> Unit = {} | ||
val menuItem: (MenuItemViewHolder) -> Unit = {}, | ||
val overlayColor: () -> Int? = { null } | ||
) | ||
|
||
fun show() { | ||
|
@@ -69,6 +75,31 @@ open class CascadePopupMenu @JvmOverloads constructor( | |
popup.contentView.background = it | ||
} | ||
|
||
styler.overlayColor()?.let { | ||
check(context is Activity) { "Activity context is required in order to add an overlay view" } | ||
val container = (context.window.decorView as ViewGroup) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think an activity context isn't needed. Does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
val overlay = View(context, null, 0).apply { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use the single param constructor? |
||
alpha = 0f | ||
setBackgroundColor(it) | ||
} | ||
container.addView(overlay) | ||
|
||
val alphaInAnimator = ObjectAnimator.ofFloat(overlay, View.ALPHA, 1f).apply { | ||
duration = 500 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's important to sync these two animations here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
interpolator = DecelerateInterpolator() | ||
} | ||
alphaInAnimator.start() | ||
|
||
popup.setOnDismissListener { | ||
val alphaOutAnimator = ObjectAnimator.ofFloat(overlay, View.ALPHA, 0f) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason for not using |
||
alphaOutAnimator.duration = 200 | ||
alphaOutAnimator.doOnEnd { | ||
container.removeView(overlay) | ||
} | ||
alphaOutAnimator.start() | ||
} | ||
} | ||
|
||
showMenu(menuBuilder, goingForward = true) | ||
popup.showAsDropDown(anchor, 0, 0, gravity) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmmm a nullable Int looks good to me 👍. I'd suggest two things:
overlayColor
tobackgroundDimColor
.background
Drawable to ensure a new instance is created on each lambda call because Drawables contain shared mutable state.