Skip to content

Commit

Permalink
Closes mozilla-mobile#3211: Added endOfMenuAlwaysVisible to BrowserMe…
Browse files Browse the repository at this point in the history
…nu.show.
  • Loading branch information
Amejia481 committed Jun 11, 2019
1 parent cd3eb4d commit 5864050
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,22 @@ class BrowserMenu internal constructor(
private var currentPopup: PopupWindow? = null
private var menuList: RecyclerView? = null

/**
* @param anchor the view on which to pin the popup window.
* @param orientation the preferred orientation to show the popup window.
* @param endOfMenuAlwaysVisible when is set to true makes sure the bottom of the menu is always visible otherwise,
* the top of the menu is always visible.
*/
@SuppressLint("InflateParams")
fun show(anchor: View, orientation: Orientation = DOWN): PopupWindow {
fun show(anchor: View, orientation: Orientation = DOWN, endOfMenuAlwaysVisible: Boolean = false): PopupWindow {
val view = LayoutInflater.from(anchor.context).inflate(R.layout.mozac_browser_menu, null)

adapter.menu = this

menuList = view.findViewById<RecyclerView>(R.id.mozac_browser_menu_recyclerView).apply {
layoutManager = LinearLayoutManager(anchor.context, RecyclerView.VERTICAL, false)
layoutManager = LinearLayoutManager(anchor.context, RecyclerView.VERTICAL, false).also {
it.stackFromEnd = endOfMenuAlwaysVisible
}
adapter = this@BrowserMenu.adapter
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import android.content.Context
*
* @param items List of BrowserMenuItem objects to compose the menu from.
* @param extras Map of extra values that are added to emitted facts
* @param endOfMenuAlwaysVisible when is set to true makes sure the bottom of the menu is always visible otherwise,
* the top of the menu is always visible.
*/
class BrowserMenuBuilder(
val items: List<BrowserMenuItem>,
val extras: Map<String, Any> = emptyMap()
val extras: Map<String, Any> = emptyMap(),
val endOfMenuAlwaysVisible: Boolean = false
) {
fun build(context: Context): BrowserMenu {
val adapter = BrowserMenuAdapter(context, items)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.view.View
import android.widget.Button
import android.widget.PopupWindow
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.test.ext.junit.runners.AndroidJUnit4
import mozilla.components.browser.menu.item.SimpleBrowserMenuItem
Expand Down Expand Up @@ -69,6 +70,25 @@ class BrowserMenuTest {
assertEquals(2, recyclerAdapter.itemCount)
}

@Test
fun `endOfMenuAlwaysVisible will be forwarded to recyclerview layoutManager`() {
val items = listOf(
SimpleBrowserMenuItem("Hello") {},
SimpleBrowserMenuItem("World") {})

val adapter = spy(BrowserMenuAdapter(testContext, items))
val menu = BrowserMenu(adapter)

val anchor = Button(testContext)
val popup = menu.show(anchor, endOfMenuAlwaysVisible = true)

val recyclerView: RecyclerView = popup.contentView.findViewById(R.id.mozac_browser_menu_recyclerView)
assertNotNull(recyclerView)

val layoutManager = recyclerView.layoutManager as LinearLayoutManager
assertTrue(layoutManager.stackFromEnd)
}

@Test
fun `invalidate will be forwarded to recyclerview adapter`() {
val items = listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,12 @@ internal class DisplayToolbar(

setOnClickListener {
menu = menuBuilder?.build(context)
val endAlwaysVisible = menuBuilder?.endOfMenuAlwaysVisible ?: false
menu?.show(
anchor = this,
orientation = BrowserMenu.determineMenuOrientation(toolbar))

orientation = BrowserMenu.determineMenuOrientation(toolbar),
endOfMenuAlwaysVisible = endAlwaysVisible
)
emitOpenMenuFact(menuBuilder?.extras)
}
}
Expand Down
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ permalink: /changelog/
* [Gecko](https://github.com/mozilla-mobile/android-components/blob/master/buildSrc/src/main/java/Gecko.kt)
* [Configuration](https://github.com/mozilla-mobile/android-components/blob/master/buildSrc/src/main/java/Config.kt)

* **browser-menu**
* Added `endOfMenuAlwaysVisible` property/parameter to `BrowserMenuBuilder` constructor and to `BrowserMenu.show` function.
When is set to true makes sure the bottom of the menu is always visible, this allows use cases like [#3211](https://github.com/mozilla-mobile/android-components/issues/3211).

# 0.56.0

Expand Down

0 comments on commit 5864050

Please sign in to comment.