Skip to content

Feature: Dim background #18

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

Open
wants to merge 10 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
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
62 changes: 61 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,18 +3,23 @@

package me.saket.cascade

import android.animation.TimeInterpolator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
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.ColorInt
import androidx.annotation.MenuRes
import androidx.appcompat.view.SupportMenuInflater
import androidx.appcompat.view.menu.MenuBuilder
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 = {},
@ColorInt val backgroundDimColor: Int? = null
)

fun show() {
Expand All @@ -69,6 +75,10 @@ open class CascadePopupMenu @JvmOverloads constructor(
popup.contentView.background = it
}

styler.backgroundDimColor?.let {
applyBackgroundDim(it)
}

showMenu(menuBuilder, goingForward = true)
popup.showAsDropDown(anchor, 0, 0, gravity)
}
Expand Down Expand Up @@ -128,6 +138,56 @@ open class CascadePopupMenu @JvmOverloads constructor(
}
}

private fun applyBackgroundDim(@ColorInt backgroundDimColor: Int) {
val container = (anchor.rootView as ViewGroup)
val overlay = View(context).apply {
alpha = 0f
setBackgroundColor(backgroundDimColor)
}
container.addView(overlay)

val alphaInInterpolator: TimeInterpolator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
popup.enterTransition?.interpolator ?: DecelerateInterpolator()
else
DecelerateInterpolator()

var alphaInDuration: Long = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
popup.enterTransition?.duration ?: 300L
} else {
300L
}

if (alphaInDuration == -1L) alphaInDuration = 300L

overlay.animate()
.alpha(1f)
.setInterpolator(alphaInInterpolator)
.setDuration(alphaInDuration)
.start()

popup.setOnDismissListener {
val alphaOutInterpolator: TimeInterpolator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
popup.exitTransition?.interpolator ?: DecelerateInterpolator()
else
DecelerateInterpolator()

var alphaOutDuration: Long = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
popup.exitTransition?.duration ?: 300L
} else {
300L
}

if (alphaOutDuration == -1L) alphaOutDuration = 300L

overlay.animate()
.withEndAction { container.removeView(overlay) }
.setInterpolator(alphaOutInterpolator)
.setDuration(alphaOutDuration)
.alpha(0f)
.start()
}
}

private fun Drawable.copy(): Drawable {
return constantState!!.newDrawable(context.resources, context.theme)
}
Expand Down
3 changes: 2 additions & 1 deletion sample/src/main/java/me/saket/cascade/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ class MainActivity : AppCompatActivity() {
menuItem = {
it.titleView.typeface = ResourcesCompat.getFont(this, R.font.work_sans_medium)
it.itemView.background = rippleDrawable()
}
},
backgroundDimColor = 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