Commit 3e60f4e8 authored by mcarare's avatar mcarare
Browse files

For #7110: Add attached state listener for menu anchor.

Dismiss popup window when anchor is detached from window.
parent 7e7e1ea5
......@@ -30,7 +30,7 @@ import mozilla.components.support.ktx.android.view.onNextGlobalLayout
*/
open class BrowserMenu internal constructor(
internal val adapter: BrowserMenuAdapter
) {
) : View.OnAttachStateChangeListener {
protected var currentPopup: PopupWindow? = null
private var menuList: RecyclerView? = null
internal var currAnchor: View? = null
......@@ -86,6 +86,7 @@ open class BrowserMenu internal constructor(
}
displayPopup(view, anchor, orientation).also {
anchor.addOnAttachStateChangeListener(this@BrowserMenu)
currAnchor = anchor
}
}.also {
......@@ -149,6 +150,15 @@ open class BrowserMenu internal constructor(
UP,
DOWN
}
override fun onViewDetachedFromWindow(v: View?) {
currentPopup?.dismiss()
currAnchor?.removeOnAttachStateChangeListener(this)
}
override fun onViewAttachedToWindow(v: View?) {
// no-op
}
}
@VisibleForTesting
......
......@@ -357,6 +357,23 @@ class BrowserMenuTest {
verify(popupWindow).showAtLocation(anchor, Gravity.START or Gravity.TOP, 0, 0)
}
@Test
fun `popup is dismissed when anchor is detached`() {
val items = listOf(
SimpleBrowserMenuItem("Mock") {},
SimpleBrowserMenuItem("Menu") {})
val adapter = BrowserMenuAdapter(testContext, items)
val menu = BrowserMenu(adapter)
val anchor = Button(testContext)
val popupWindow = menu.show(anchor)
assertTrue(popupWindow.isShowing)
menu.onViewDetachedFromWindow(anchor)
assertFalse(popupWindow.isShowing)
}
private fun createMockViewWith(y: Int): View {
val view = spy(View(testContext))
doAnswer { invocation ->
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment