Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Closes #10091 Added style property to WebExtensionBrowserMenuBuilder'…
Browse files Browse the repository at this point in the history
…s constructor
  • Loading branch information
Amejia481 authored and mergify[bot] committed Apr 19, 2021
1 parent f779e57 commit fe5bb39
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package mozilla.components.browser.menu

import android.content.Context
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import mozilla.components.browser.menu.item.BackPressMenuItem
import mozilla.components.browser.menu.item.BrowserMenuDivider
import mozilla.components.browser.menu.item.BrowserMenuImageText
Expand All @@ -21,8 +22,7 @@ import mozilla.components.browser.state.store.BrowserStore
* the web extension menu item would return an add-on manager menu item instead.
*
* @param store [BrowserStore] required to render web extension browser actions
* @param webExtIconTintColorResource Optional ID of color resource to tint the icons of back and
* add-ons manager menu items.
* @param style Indicates how items should look like.
* @param onAddonsManagerTapped Callback to be invoked when add-ons manager menu item is selected.
* @param appendExtensionSubMenuAtStart Used when the menu does not have a [WebExtensionPlaceholderMenuItem]
* to specify the place the extensions sub-menu should be inserted. True if web extension sub menu
Expand All @@ -35,7 +35,7 @@ class WebExtensionBrowserMenuBuilder(
extras: Map<String, Any> = emptyMap(),
endOfMenuAlwaysVisible: Boolean = false,
private val store: BrowserStore,
@ColorRes private val webExtIconTintColorResource: Int = NO_ID,
private val style: Style = Style(),
private val onAddonsManagerTapped: () -> Unit = {},
private val appendExtensionSubMenuAtStart: Boolean = false
) : BrowserMenuBuilder(items, extras, endOfMenuAlwaysVisible) {
Expand Down Expand Up @@ -64,14 +64,14 @@ class WebExtensionBrowserMenuBuilder(
val webExtMenuItem = if (filteredExtensionMenuItems.isNotEmpty()) {
val backPressMenuItem = BackPressMenuItem(
label = context.getString(R.string.mozac_browser_menu_addons),
imageResource = R.drawable.mozac_ic_back,
iconTintColorResource = webExtIconTintColorResource
imageResource = style.backPressMenuItemDrawableRes,
iconTintColorResource = style.webExtIconTintColorResource
)

val addonsManagerMenuItem = BrowserMenuImageText(
label = context.getString(R.string.mozac_browser_menu_addons_manager),
imageResource = R.drawable.mozac_ic_extensions,
iconTintColorResource = webExtIconTintColorResource
imageResource = style.addonsManagerMenuItemDrawableRes,
iconTintColorResource = style.webExtIconTintColorResource
) {
onAddonsManagerTapped.invoke()
}
Expand All @@ -91,16 +91,16 @@ class WebExtensionBrowserMenuBuilder(

ParentBrowserMenuItem(
label = context.getString(R.string.mozac_browser_menu_addons),
imageResource = R.drawable.mozac_ic_extensions,
iconTintColorResource = webExtIconTintColorResource,
imageResource = style.addonsManagerMenuItemDrawableRes,
iconTintColorResource = style.webExtIconTintColorResource,
subMenu = webExtMenu,
endOfMenuAlwaysVisible = endOfMenuAlwaysVisible
)
} else {
BrowserMenuImageText(
label = context.getString(R.string.mozac_browser_menu_addons),
imageResource = R.drawable.mozac_ic_extensions,
iconTintColorResource = webExtIconTintColorResource
imageResource = style.addonsManagerMenuItemDrawableRes,
iconTintColorResource = style.webExtIconTintColorResource
) {
onAddonsManagerTapped.invoke()
}
Expand All @@ -126,4 +126,16 @@ class WebExtensionBrowserMenuBuilder(
val adapter = BrowserMenuAdapter(context, menuItems)
return BrowserMenu(adapter)
}

/**
* Allows to customize how items should look like.
*/
data class Style(
@ColorRes
val webExtIconTintColorResource: Int = NO_ID,
@DrawableRes
val backPressMenuItemDrawableRes: Int = R.drawable.mozac_ic_back,
@DrawableRes
val addonsManagerMenuItemDrawableRes: Int = R.drawable.mozac_ic_extensions
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private val defaultHighlight = BrowserMenuHighlightableItem.Highlight(0, 0, 0, 0
class BrowserMenuHighlightableItem(
private val label: String,
@DrawableRes private val startImageResource: Int,
@ColorRes private val iconTintColorResource: Int = NO_ID,
@ColorRes iconTintColorResource: Int = NO_ID,
@ColorRes private val textColorResource: Int = NO_ID,
override val isCollapsingMenuLimit: Boolean = false,
override val isSticky: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.VisibleForTesting
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.getColor
Expand Down Expand Up @@ -52,9 +53,11 @@ internal fun TextView.setColorResource(@ColorRes textColorResource: Int) {
open class BrowserMenuImageText(
private val label: String,
@DrawableRes
private val imageResource: Int,
@VisibleForTesting
internal val imageResource: Int,
@ColorRes
private val iconTintColorResource: Int = NO_ID,
@VisibleForTesting
internal val iconTintColorResource: Int = NO_ID,
@ColorRes
private val textColorResource: Int = NO_ID,
override val isCollapsingMenuLimit: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import mozilla.components.support.ktx.android.util.dpToPx
class BrowserMenuImageTextCheckboxButton(
@DrawableRes imageResource: Int,
private val label: String,
@ColorRes internal val iconTintColorResource: Int = NO_ID,
@ColorRes iconTintColorResource: Int = NO_ID,
@ColorRes internal val textColorResource: Int = NO_ID,
@VisibleForTesting internal val labelListener: () -> Unit,
@DrawableRes val primaryStateIconResource: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,47 @@ class WebExtensionBrowserMenuBuilderTest {
assertTrue(isAddonsManagerTapped)
}

@Test
fun `GIVEN style is provided WHEN creating extension menu THEN styles should be applied to items`() {
val browserAction = WebExtensionBrowserAction("browser_action", true, mock(), "", 0, 0) {}
val extensions = mapOf(
"id" to WebExtensionState(
"id",
"url",
"name",
true,
browserAction = browserAction
)
)

val store = BrowserStore(BrowserState(extensions = extensions))
val style = WebExtensionBrowserMenuBuilder.Style(
addonsManagerMenuItemDrawableRes = R.drawable.mozac_ic_extensions,
backPressMenuItemDrawableRes = R.drawable.mozac_ic_back
)
val builder = WebExtensionBrowserMenuBuilder(
listOf(mockMenuItem()),
store = store,
style = style,
appendExtensionSubMenuAtStart = true
)

val menu = builder.build(testContext)
val anchor = ImageButton(testContext)
menu.show(anchor)

val parentMenuItem = menu.adapter.visibleItems[0] as ParentBrowserMenuItem
val subMenuItemIndex = parentMenuItem.subMenu.adapter.visibleItems.lastIndex
val backPressMenuItem = parentMenuItem.subMenu.adapter.visibleItems[0] as BackPressMenuItem
val addonsManagerItem = parentMenuItem.subMenu.adapter.visibleItems[subMenuItemIndex] as BrowserMenuImageText

assertEquals(style.backPressMenuItemDrawableRes, backPressMenuItem.imageResource)
assertEquals(style.webExtIconTintColorResource, backPressMenuItem.iconTintColorResource)

assertEquals(style.webExtIconTintColorResource, addonsManagerItem.iconTintColorResource)
assertEquals(style.webExtIconTintColorResource, addonsManagerItem.iconTintColorResource)
}

@Test
fun `web extension sub menu add-ons manager sub menu item invokes onAddonsManagerTapped when clicked`() {
val browserAction = WebExtensionBrowserAction("browser_action", true, mock(), "", 0, 0) {}
Expand Down
10 changes: 2 additions & 8 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,13 @@ permalink: /changelog/
* **browser-menu**:
* 🌟️ New StickyHeaderLinearLayoutManager and StickyFooterLinearLayoutManager that can be used to keep an item from being scrolled off-screen.
* To use this set `isSticky = true` for any menu item of the menu. Since only one sticky item is supported if more items have this property the sticky item will be the one closest to the top the menu anchor.

* **browser-menu**:
* 🚒 Bug fixed [issue #](https://github.com/mozilla-mobile/android-components/issues/10032) - Fix a recent issue with ExpandableLayout - user touches on an expanded menu might not have any effect on Samsung devices.

* **browser-menu**:
* 🚒 Bug fixed [issue #](https://github.com/mozilla-mobile/android-components/issues/10005) - Fix a recent issue with BrowserMenu#show() - endOfMenuAlwaysVisible not being applied.

* **browser-menu**:
* 🚒 Bug fixed [issue #](https://github.com/mozilla-mobile/android-components/issues/9922) - The browser menu will have it's dynamic width calculated only once, before the first layout.

* **browser-menu**
* 🌟️ BrowserMenu support a bottom collapsed/expandable layout through a new ExpandableLayout that will wrap a menu layout before being used in a PopupWindow and automatically allow the collapse/expand behaviors.
* To use this set `isCollapsingMenuLimit = true` for any menu item of a bottom anchored menu.
* 🌟️ `WebExtensionBrowserMenuBuilder` provide a new way to customize how items look like via `Style()` where the `tintColor`, `backPressDrawable` and `addonsManagerDrawable` can be customized.
* ⚠️ **This is a breaking change**: `WebExtensionBrowserMenuBuilder.webExtIconTintColorResource` constructor parameter has been removed, please use `WebExtensionBrowserMenuBuilder`.`Style` instead. For more details see [issue #9787](https://github.com/mozilla-mobile/android-components/issues/10091).

* **browser-toolbar**
* **feature-session**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ open class DefaultComponents(private val applicationContext: Context) {
WebExtensionBrowserMenuBuilder(
menuItems,
store = store,
webExtIconTintColorResource = R.color.photonGrey90,
style = WebExtensionBrowserMenuBuilder.Style(
webExtIconTintColorResource = R.color.photonGrey90
),
onAddonsManagerTapped = {
val intent = Intent(applicationContext, AddonsActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
Expand Down

0 comments on commit fe5bb39

Please sign in to comment.