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

Feature: Dim background #18

Open
wants to merge 10 commits into
base: trunk
Choose a base branch
from
Open
33 changes: 32 additions & 1 deletion cascade/src/main/java/me/saket/cascade/CascadePopupMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }
Copy link
Owner

Choose a reason for hiding this comment

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

Should there be a flag to enable this particular feature or just check the nullability of resource value?

Hmmmm a nullable Int looks good to me 👍. I'd suggest two things:

  • Rename overlayColor to backgroundDimColor.
  • Remove the lambda type. A color integer doesn't need to be lazily evaluated. It was necessary for the background Drawable to ensure a new instance is created on each lambda call because Drawables contain shared mutable state.

)

fun show() {
Expand All @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

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

I think an activity context isn't needed. Does anchor.rootView work here?

Copy link
Author

Choose a reason for hiding this comment

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

anchor.rootView does work.

val overlay = View(context, null, 0).apply {
Copy link
Owner

Choose a reason for hiding this comment

The 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
Copy link
Owner

Choose a reason for hiding this comment

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

It's important to sync these two animations here PopupWindow's transition. See popup.enterTransition and popup.exitTransition and reuse their duration and interpolator?

Copy link
Author

Choose a reason for hiding this comment

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

enterTransition & exitTransition are only available from Nougat. Have added default fallback values for animation properties.

interpolator = DecelerateInterpolator()
}
alphaInAnimator.start()

popup.setOnDismissListener {
val alphaOutAnimator = ObjectAnimator.ofFloat(overlay, View.ALPHA, 0f)
Copy link
Owner

Choose a reason for hiding this comment

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

Any reason for not using overlay.animate().alpha(0f) instead of an ObjectAnimator?

alphaOutAnimator.duration = 200
alphaOutAnimator.doOnEnd {
container.removeView(overlay)
}
alphaOutAnimator.start()
}
}

showMenu(menuBuilder, goingForward = true)
popup.showAsDropDown(anchor, 0, 0, gravity)
}
Expand Down
3 changes: 3 additions & 0 deletions sample/src/main/java/me/saket/cascade/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ class MainActivity : AppCompatActivity() {
menuItem = {
it.titleView.typeface = ResourcesCompat.getFont(this, R.font.work_sans_medium)
it.itemView.background = rippleDrawable()
},
overlayColor = {
ContextCompat.getColor(this, R.color.window_overlay_background)
}
)
}
Expand Down
1 change: 1 addition & 0 deletions sample/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<color name="color_accent">#356859</color>
<color name="color_control_normal">#356859</color>
<color name="window_background">#B5D2C3</color>
<color name="window_overlay_background">#80000000</color>

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/window_background</item>
Expand Down