Skip to content

Commit

Permalink
Send the mutable list of menu items as parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Ionut Cristian Bedregeanu authored and mergify[bot] committed Jul 9, 2021
1 parent 676c7b4 commit 5ce0c5b
Showing 1 changed file with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import mozilla.components.browser.state.store.BrowserStore

/**
* Browser menu builder with web extension support. It allows [WebExtensionBrowserMenu] to add
* web extension browser actions in a nested menu item.
* web extension browser actions in a nested menu item. If there are no web extensions installed
* and @param showAddonsInMenu is true 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 style Indicates how items should look like.
Expand All @@ -42,21 +43,21 @@ class WebExtensionBrowserMenuBuilder(
private val showAddonsInMenu: Boolean = true
) : BrowserMenuBuilder(items, extras, endOfMenuAlwaysVisible) {

private val finalList = items.toMutableList()

/**
* Builds and returns a browser menu with combination of [items] and web extension browser actions.
*/
override fun build(context: Context): BrowserMenu {
val extensionMenuItems =
WebExtensionBrowserMenu.getOrUpdateWebExtensionMenuItems(store.state, store.state.selectedTab)

val finalList = items.toMutableList()

val filteredExtensionMenuItems = extensionMenuItems.filter { webExtensionBrowserMenuItem ->
replaceMenuPlaceholderWithExtensions(webExtensionBrowserMenuItem)
replaceMenuPlaceholderWithExtensions(finalList, webExtensionBrowserMenuItem)
}

val menuItems = if (showAddonsInMenu) {
showAddonsMenuItems(context, filteredExtensionMenuItems)
createAddonsMenuItems(context, finalList, filteredExtensionMenuItems)
} else {
finalList
}
Expand All @@ -65,23 +66,27 @@ class WebExtensionBrowserMenuBuilder(
return BrowserMenu(adapter)
}

private fun replaceMenuPlaceholderWithExtensions(menuItem: WebExtensionBrowserMenuItem): Boolean {
private fun replaceMenuPlaceholderWithExtensions(
items: MutableList<BrowserMenuItem>,
menuItem: WebExtensionBrowserMenuItem
): Boolean {
// Check if we have a placeholder
val index = items.indexOfFirst { browserMenuItem ->
(browserMenuItem as? WebExtensionPlaceholderMenuItem)?.id == menuItem.id
}
// Replace placeholder with corresponding web extension, and remove it from extensions menu list
if (index != -1) {
menuItem.setIconTint(
(finalList[index] as? WebExtensionPlaceholderMenuItem)?.iconTintColorResource
(items[index] as? WebExtensionPlaceholderMenuItem)?.iconTintColorResource
)
finalList[index] = menuItem
items[index] = menuItem
}
return index == -1
}

private fun showAddonsMenuItems(
private fun createAddonsMenuItems(
context: Context,
items: MutableList<BrowserMenuItem>,
filteredExtensionMenuItems: List<WebExtensionBrowserMenuItem>
): List<BrowserMenuItem> {
val addonsMenuItem = if (filteredExtensionMenuItems.isNotEmpty()) {
Expand Down Expand Up @@ -128,20 +133,20 @@ class WebExtensionBrowserMenuBuilder(
onAddonsManagerTapped.invoke()
}
}
val mainMenuIndex = finalList.indexOfFirst { browserMenuItem ->
val mainMenuIndex = items.indexOfFirst { browserMenuItem ->
(browserMenuItem as? WebExtensionPlaceholderMenuItem)?.id ==
WebExtensionPlaceholderMenuItem.MAIN_EXTENSIONS_MENU_ID
}

return if (mainMenuIndex != -1) {
finalList[mainMenuIndex] = addonsMenuItem
finalList
items[mainMenuIndex] = addonsMenuItem
items
// if we do not have a placeholder we should add the extension submenu at top or bottom
} else {
if (appendExtensionSubMenuAtStart) {
listOf(addonsMenuItem) + finalList
listOf(addonsMenuItem) + items
} else {
finalList + addonsMenuItem
items + addonsMenuItem
}
}
}
Expand Down

0 comments on commit 5ce0c5b

Please sign in to comment.