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 flag to dim background behind popup #43

Merged
merged 1 commit into from
Apr 15, 2019
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class MaterialRecyclerViewPopupWindow(
companion object {

private const val TAG = "MaterialRVPopupWindow"
private const val DEFAULT_BACKGROUND_DIM_AMOUNT = 0.3f

private var sClipToWindowEnabledMethod: Method? = null
private var sGetMaxAvailableHeightMethod: Method? = null
Expand Down Expand Up @@ -89,6 +90,14 @@ class MaterialRecyclerViewPopupWindow(

private val contextThemeWrapper: Context

private val windowManager: WindowManager by lazy {
context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
}

private val backgroundDimEnabled: Boolean

private val backgroundDimAmount: Float

init {
contextThemeWrapper = ContextThemeWrapper(context, null)
contextThemeWrapper.setTheme(defStyleRes)
Expand All @@ -101,10 +110,12 @@ class MaterialRecyclerViewPopupWindow(
popupMinWidth = contextThemeWrapper.resources.getDimensionPixelSize(R.dimen.mpm_popup_menu_min_width)
popupWidthUnit = contextThemeWrapper.resources.getDimensionPixelSize(R.dimen.mpm_popup_menu_width_unit)

val a = context.obtainStyledAttributes(null, androidx.appcompat.R.styleable.ListPopupWindow, 0, defStyleRes)
val a = context.obtainStyledAttributes(null, R.styleable.MaterialRecyclerViewPopupWindow, 0, defStyleRes)

dropDownHorizontalOffset = a.getDimensionPixelOffset(androidx.appcompat.R.styleable.ListPopupWindow_android_dropDownHorizontalOffset, 0)
dropDownVerticalOffset = a.getDimensionPixelOffset(androidx.appcompat.R.styleable.ListPopupWindow_android_dropDownVerticalOffset, 0)
dropDownHorizontalOffset = a.getDimensionPixelOffset(R.styleable.MaterialRecyclerViewPopupWindow_android_dropDownHorizontalOffset, 0)
dropDownVerticalOffset = a.getDimensionPixelOffset(R.styleable.MaterialRecyclerViewPopupWindow_android_dropDownVerticalOffset, 0)
backgroundDimEnabled = a.getBoolean(R.styleable.MaterialRecyclerViewPopupWindow_android_backgroundDimEnabled, false)
backgroundDimAmount = a.getFloat(R.styleable.MaterialRecyclerViewPopupWindow_android_backgroundDimAmount, DEFAULT_BACKGROUND_DIM_AMOUNT)

a.recycle()
}
Expand Down Expand Up @@ -158,6 +169,10 @@ class MaterialRecyclerViewPopupWindow(
dropDownVerticalOffset, dropDownGravity
)
}

if (backgroundDimEnabled) {
addBackgroundDimming()
}
}

/**
Expand Down Expand Up @@ -359,4 +374,12 @@ class MaterialRecyclerViewPopupWindow(

return menuWidth
}

private fun addBackgroundDimming() {
Copy link
Owner

Choose a reason for hiding this comment

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

Cool, didn't know that adding a dim can be done with few lines of code like this!

val decorView = popup.contentView.rootView
val layoutParams = decorView.layoutParams as WindowManager.LayoutParams
layoutParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND
layoutParams.dimAmount = backgroundDimAmount
windowManager.updateViewLayout(decorView, layoutParams)
}
}
9 changes: 8 additions & 1 deletion material-popup-menu/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@

<attr name="mpm_separatorColor" format="color|reference" />

</resources>
<declare-styleable name="MaterialRecyclerViewPopupWindow">
<attr name="android:dropDownHorizontalOffset" />
<attr name="android:dropDownVerticalOffset" />
<attr name="android:backgroundDimEnabled" />
<attr name="android:backgroundDimAmount" />
</declare-styleable>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,22 @@ class DarkActivity : AppCompatActivity() {
popupMenu.show(this@DarkActivity, view)
}

@OnClick(R.id.dimmedBackgroundTextView)
fun onDimmedBackgroundClicked(view: View) {
val popupMenu = popupMenu {
style = R.style.Widget_MPM_Menu_Dark_Dimmed
section {
item {
label = "Copy"
}
item {
label = "Paste"
}
}
}
popupMenu.show(this@DarkActivity, view)
}

@OnClick(R.id.customItemsTextView)
fun onCustomItemsClicked(view: View) {
val popupMenu = popupMenu {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,22 @@ class LightActivity : AppCompatActivity() {
popupMenu.show(this@LightActivity, view)
}

@OnClick(R.id.dimmedBackgroundTextView)
fun onDimmedBackgroundClicked(view: View) {
val popupMenu = popupMenu {
style = R.style.Widget_MPM_Menu_Dimmed
section {
item {
label = "Copy"
}
item {
label = "Paste"
}
}
}
popupMenu.show(this@LightActivity, view)
}

@OnClick(R.id.conditionalItemsTextView)
fun onConditionalItemsClicked(view: View) {
val conditionalPopupMenuBuilder = popupMenuBuilder {
Expand Down
11 changes: 10 additions & 1 deletion sample/src/main/res/layout/activity_dark.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,23 @@
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/customColorsTextView" />

<TextView
android:id="@+id/dimmedBackgroundTextView"
style="@style/TitleLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dimmed_background"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/customOffsetsTextView" />

<TextView
android:id="@+id/conditionalItemsTextView"
style="@style/TitleLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/conditional_items"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/customOffsetsTextView" />
app:layout_constraintTop_toBottomOf="@+id/dimmedBackgroundTextView" />

<TextView
android:id="@+id/conditionalItemsCopySubtitleTextView"
Expand Down
11 changes: 10 additions & 1 deletion sample/src/main/res/layout/activity_light.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,23 @@
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/customColorsTextView" />

<TextView
android:id="@+id/dimmedBackgroundTextView"
style="@style/TitleLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dimmed_background"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/customOffsetsTextView" />

<TextView
android:id="@+id/conditionalItemsTextView"
style="@style/TitleLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/conditional_items"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/customOffsetsTextView" />
app:layout_constraintTop_toBottomOf="@+id/dimmedBackgroundTextView" />

<TextView
android:id="@+id/conditionalItemsCopySubtitleTextView"
Expand Down
1 change: 1 addition & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
<string name="custom_items">Custom items</string>
<string name="custom_item_label">Enabled</string>
<string name="copy">Copy</string>
<string name="dimmed_background">Dimmed background</string>
</resources>
8 changes: 8 additions & 0 deletions sample/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,12 @@
<item name="android:dropDownHorizontalOffset">24dp</item>
</style>

<style name="Widget.MPM.Menu.Dimmed">
<item name="android:backgroundDimEnabled">true</item>
</style>

<style name="Widget.MPM.Menu.Dark.Dimmed">
<item name="android:backgroundDimEnabled">true</item>
</style>

</resources>