Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Commit

Permalink
[components] Add MenuStyle support to menu2
Browse files Browse the repository at this point in the history
  • Loading branch information
NotWoods committed Aug 21, 2020
1 parent 32fc5a2 commit 6a22862
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout
import mozilla.components.browser.menu2.ext.displayPopup
import mozilla.components.browser.menu2.view.MenuView
import mozilla.components.concept.menu.MenuController
import mozilla.components.concept.menu.MenuStyle
import mozilla.components.concept.menu.Orientation
import mozilla.components.concept.menu.Side
import mozilla.components.concept.menu.candidate.MenuCandidate
Expand All @@ -22,9 +23,11 @@ import mozilla.components.support.base.observer.ObserverRegistry
/**
* Controls a popup menu composed of MenuCandidate objects.
* @param visibleSide Sets the menu to open with either the start or end visible.
* @param style Custom styling for this menu controller.
*/
class BrowserMenuController(
private val visibleSide: Side = Side.START
private val visibleSide: Side = Side.START,
private val style: MenuStyle? = null
) : MenuController, Observable<MenuController.Observer> by ObserverRegistry() {

private var currentPopupInfo: PopupMenuInfo? = null
Expand All @@ -48,6 +51,7 @@ class BrowserMenuController(
// Show nested list if present, or the standard menu candidates list.
submitList(menuCandidates)
setVisibleSide(visibleSide)
style?.let { setStyle(it) }
}

return MenuPopupWindow(view).apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.annotation.VisibleForTesting
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import mozilla.components.browser.menu2.R
import mozilla.components.browser.menu2.adapter.MenuCandidateListAdapter
import mozilla.components.concept.menu.MenuStyle
import mozilla.components.concept.menu.Side
import mozilla.components.concept.menu.candidate.MenuCandidate
import mozilla.components.concept.menu.candidate.NestedMenuCandidate
Expand All @@ -36,6 +38,7 @@ class MenuView @JvmOverloads constructor(
dismiss = { onDismiss() },
reopenMenu = { onReopenMenu(it) }
)
private val cardView: CardView
private val recyclerView: RecyclerView

/**
Expand All @@ -51,6 +54,7 @@ class MenuView @JvmOverloads constructor(
init {
View.inflate(context, R.layout.mozac_browser_menu2_view, this)

cardView = findViewById(R.id.mozac_browser_menu_cardView)
recyclerView = findViewById(R.id.mozac_browser_menu_recyclerView)
recyclerView.layoutManager = layoutManager
recyclerView.adapter = menuAdapter
Expand All @@ -75,6 +79,10 @@ class MenuView @JvmOverloads constructor(
}
}

fun setStyle(style: MenuStyle) {
style.backgroundColor?.let { cardView.setCardBackgroundColor(it) }
}

@VisibleForTesting
internal fun scrollOnceToTheBottom(recyclerView: RecyclerView) {
recyclerView.onNextGlobalLayout {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<androidx.cardview.widget.CardView
style="@style/Mozac.Browser.Menu2"
android:id="@+id/mozac_browser_menu_cardView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="@dimen/mozac_browser_menu2_corner_radius"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

package mozilla.components.browser.menu2.view

import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Build
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.test.ext.junit.runners.AndroidJUnit4
import mozilla.components.browser.menu2.R
import mozilla.components.concept.menu.MenuStyle
import mozilla.components.concept.menu.Side
import mozilla.components.concept.menu.candidate.DecorativeTextMenuCandidate
import mozilla.components.support.test.any
Expand All @@ -34,11 +38,13 @@ class MenuViewTest {
DecorativeTextMenuCandidate("World")
)
private lateinit var menuView: MenuView
private lateinit var cardView: CardView
private lateinit var recyclerView: RecyclerView

@Before
fun setup() {
menuView = spy(MenuView(testContext))
cardView = menuView.findViewById(R.id.mozac_browser_menu_cardView)
recyclerView = menuView.findViewById(R.id.mozac_browser_menu_recyclerView)
}

Expand Down Expand Up @@ -77,4 +83,10 @@ class MenuViewTest {
assertTrue(layoutManager.stackFromEnd)
verify(menuView, never()).scrollOnceToTheBottom(any())
}

@Test
fun `setStyle changes background color`() {
menuView.setStyle(MenuStyle(backgroundColor = Color.BLUE))
assertEquals(ColorStateList.valueOf(Color.BLUE), cardView.cardBackgroundColor)
}
}

0 comments on commit 6a22862

Please sign in to comment.