2019-04-01 17:47:25 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2019-07-12 20:38:15 +02:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2019-04-01 17:47:25 +02:00
|
|
|
|
|
|
|
package org.mozilla.fenix.components.toolbar
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import mozilla.components.browser.menu.BrowserMenuBuilder
|
|
|
|
import mozilla.components.browser.menu.item.BrowserMenuDivider
|
2019-05-24 23:18:27 +02:00
|
|
|
import mozilla.components.browser.menu.item.BrowserMenuHighlightableItem
|
2019-04-01 17:47:25 +02:00
|
|
|
import mozilla.components.browser.menu.item.BrowserMenuImageText
|
|
|
|
import mozilla.components.browser.menu.item.BrowserMenuItemToolbar
|
2019-09-25 10:23:27 +02:00
|
|
|
import mozilla.components.browser.menu.item.BrowserMenuImageSwitch
|
2019-05-29 23:58:41 +02:00
|
|
|
import org.mozilla.fenix.HomeActivity
|
2019-04-01 17:47:25 +02:00
|
|
|
import org.mozilla.fenix.R
|
2019-08-29 21:45:11 +02:00
|
|
|
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
|
2019-05-29 23:58:41 +02:00
|
|
|
import org.mozilla.fenix.ext.asActivity
|
2019-04-01 17:47:25 +02:00
|
|
|
import org.mozilla.fenix.ext.components
|
2019-08-29 21:45:11 +02:00
|
|
|
import org.mozilla.fenix.theme.ThemeManager
|
2019-09-10 22:29:21 +02:00
|
|
|
import org.mozilla.fenix.utils.Settings
|
2019-04-01 17:47:25 +02:00
|
|
|
|
|
|
|
class DefaultToolbarMenu(
|
|
|
|
private val context: Context,
|
2019-05-29 23:31:06 +02:00
|
|
|
private val hasAccountProblem: Boolean = false,
|
2019-04-01 17:47:25 +02:00
|
|
|
private val requestDesktopStateProvider: () -> Boolean = { false },
|
|
|
|
private val onItemTapped: (ToolbarMenu.Item) -> Unit = {}
|
|
|
|
) : ToolbarMenu {
|
|
|
|
|
2019-06-12 18:17:33 +02:00
|
|
|
override val menuBuilder by lazy { BrowserMenuBuilder(menuItems, endOfMenuAlwaysVisible = true) }
|
2019-04-01 17:47:25 +02:00
|
|
|
|
|
|
|
override val menuToolbar by lazy {
|
|
|
|
val back = BrowserMenuItemToolbar.TwoStateButton(
|
|
|
|
primaryImageResource = mozilla.components.ui.icons.R.drawable.mozac_ic_back,
|
|
|
|
primaryContentDescription = context.getString(R.string.browser_menu_back),
|
2019-05-31 00:49:58 +02:00
|
|
|
primaryImageTintResource = ThemeManager.resolveAttribute(
|
2019-04-01 17:47:25 +02:00
|
|
|
R.attr.primaryText,
|
|
|
|
context
|
|
|
|
),
|
|
|
|
isInPrimaryState = {
|
|
|
|
context.components.core.sessionManager.selectedSession?.canGoBack ?: true
|
|
|
|
},
|
2019-05-31 00:49:58 +02:00
|
|
|
secondaryImageTintResource = ThemeManager.resolveAttribute(
|
2019-06-06 03:37:16 +02:00
|
|
|
R.attr.disabled,
|
2019-04-01 17:47:25 +02:00
|
|
|
context
|
|
|
|
),
|
|
|
|
disableInSecondaryState = true
|
|
|
|
) {
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.Back)
|
|
|
|
}
|
|
|
|
|
|
|
|
val forward = BrowserMenuItemToolbar.TwoStateButton(
|
|
|
|
primaryImageResource = mozilla.components.ui.icons.R.drawable.mozac_ic_forward,
|
|
|
|
primaryContentDescription = context.getString(R.string.browser_menu_forward),
|
2019-05-31 00:49:58 +02:00
|
|
|
primaryImageTintResource = ThemeManager.resolveAttribute(
|
2019-04-01 17:47:25 +02:00
|
|
|
R.attr.primaryText,
|
|
|
|
context
|
|
|
|
),
|
|
|
|
isInPrimaryState = {
|
|
|
|
context.components.core.sessionManager.selectedSession?.canGoForward ?: true
|
|
|
|
},
|
2019-05-31 00:49:58 +02:00
|
|
|
secondaryImageTintResource = ThemeManager.resolveAttribute(
|
2019-06-06 03:37:16 +02:00
|
|
|
R.attr.disabled,
|
2019-04-01 17:47:25 +02:00
|
|
|
context
|
|
|
|
),
|
|
|
|
disableInSecondaryState = true
|
|
|
|
) {
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.Forward)
|
|
|
|
}
|
|
|
|
|
|
|
|
val refresh = BrowserMenuItemToolbar.TwoStateButton(
|
|
|
|
primaryImageResource = mozilla.components.ui.icons.R.drawable.mozac_ic_refresh,
|
|
|
|
primaryContentDescription = context.getString(R.string.browser_menu_refresh),
|
2019-05-31 00:49:58 +02:00
|
|
|
primaryImageTintResource = ThemeManager.resolveAttribute(
|
2019-04-01 17:47:25 +02:00
|
|
|
R.attr.primaryText,
|
|
|
|
context
|
|
|
|
),
|
|
|
|
isInPrimaryState = {
|
|
|
|
val loading = context.components.core.sessionManager.selectedSession?.loading
|
|
|
|
loading == false
|
|
|
|
},
|
|
|
|
secondaryImageResource = mozilla.components.ui.icons.R.drawable.mozac_ic_stop,
|
|
|
|
secondaryContentDescription = context.getString(R.string.browser_menu_stop),
|
2019-05-31 00:49:58 +02:00
|
|
|
secondaryImageTintResource = ThemeManager.resolveAttribute(
|
2019-04-01 17:47:25 +02:00
|
|
|
R.attr.primaryText,
|
|
|
|
context
|
|
|
|
),
|
|
|
|
disableInSecondaryState = false
|
|
|
|
) {
|
|
|
|
if (context.components.core.sessionManager.selectedSession?.loading == true) {
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.Stop)
|
|
|
|
} else {
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.Reload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BrowserMenuItemToolbar(listOf(back, forward, refresh))
|
|
|
|
}
|
|
|
|
|
|
|
|
private val menuItems by lazy {
|
2019-05-29 23:58:41 +02:00
|
|
|
val items = mutableListOf(
|
2019-04-01 17:47:25 +02:00
|
|
|
BrowserMenuImageText(
|
|
|
|
context.getString(R.string.browser_menu_help),
|
|
|
|
R.drawable.ic_help,
|
2019-05-31 00:49:58 +02:00
|
|
|
ThemeManager.resolveAttribute(R.attr.primaryText, context)
|
2019-04-01 17:47:25 +02:00
|
|
|
) {
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.Help)
|
|
|
|
},
|
|
|
|
|
2019-05-24 23:18:27 +02:00
|
|
|
BrowserMenuHighlightableItem(
|
|
|
|
label = context.getString(R.string.browser_menu_settings),
|
|
|
|
imageResource = R.drawable.ic_settings,
|
2019-05-29 23:31:06 +02:00
|
|
|
iconTintColorResource = if (hasAccountProblem)
|
2019-05-24 23:18:27 +02:00
|
|
|
R.color.sync_error_text_color else
|
2019-05-31 00:49:58 +02:00
|
|
|
ThemeManager.resolveAttribute(R.attr.primaryText, context),
|
2019-05-29 23:31:06 +02:00
|
|
|
textColorResource = if (hasAccountProblem)
|
2019-05-24 23:18:27 +02:00
|
|
|
R.color.sync_error_text_color else
|
2019-05-31 00:49:58 +02:00
|
|
|
ThemeManager.resolveAttribute(R.attr.primaryText, context),
|
2019-05-29 23:31:06 +02:00
|
|
|
highlight = if (hasAccountProblem) {
|
2019-05-24 23:18:27 +02:00
|
|
|
BrowserMenuHighlightableItem.Highlight(
|
2019-08-28 14:10:06 +02:00
|
|
|
endImageResource = R.drawable.ic_alert,
|
2019-08-01 18:25:46 +02:00
|
|
|
backgroundResource = R.drawable.sync_error_background_with_ripple,
|
|
|
|
colorResource = R.color.sync_error_background_color
|
2019-05-24 23:18:27 +02:00
|
|
|
)
|
|
|
|
} else null
|
2019-04-01 17:47:25 +02:00
|
|
|
) {
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.Settings)
|
|
|
|
},
|
|
|
|
|
|
|
|
BrowserMenuImageText(
|
2019-04-23 22:49:14 +02:00
|
|
|
context.getString(R.string.browser_menu_your_library),
|
2019-04-01 17:47:25 +02:00
|
|
|
R.drawable.ic_library,
|
2019-05-31 00:49:58 +02:00
|
|
|
ThemeManager.resolveAttribute(R.attr.primaryText, context)
|
2019-04-01 17:47:25 +02:00
|
|
|
) {
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.Library)
|
|
|
|
},
|
|
|
|
|
2019-09-25 10:23:27 +02:00
|
|
|
BrowserMenuImageSwitch(
|
|
|
|
R.drawable.ic_desktop,
|
2019-08-29 21:45:11 +02:00
|
|
|
context.getString(R.string.browser_menu_desktop_site),
|
|
|
|
requestDesktopStateProvider
|
|
|
|
) { checked ->
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.RequestDesktop(checked))
|
|
|
|
},
|
|
|
|
|
|
|
|
BrowserMenuImageText(
|
|
|
|
context.getString(R.string.browser_menu_add_to_homescreen),
|
|
|
|
R.drawable.ic_add_to_homescreen,
|
|
|
|
ThemeManager.resolveAttribute(R.attr.primaryText, context)
|
|
|
|
) {
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.AddToHomeScreen)
|
|
|
|
}.apply {
|
|
|
|
visible = ::shouldShowAddToHomescreen
|
|
|
|
},
|
2019-04-01 17:47:25 +02:00
|
|
|
|
|
|
|
BrowserMenuImageText(
|
|
|
|
context.getString(R.string.browser_menu_find_in_page),
|
|
|
|
R.drawable.mozac_ic_search,
|
2019-05-31 00:49:58 +02:00
|
|
|
ThemeManager.resolveAttribute(R.attr.primaryText, context)
|
2019-04-01 17:47:25 +02:00
|
|
|
) {
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.FindInPage)
|
|
|
|
},
|
|
|
|
|
|
|
|
BrowserMenuImageText(
|
|
|
|
context.getString(R.string.browser_menu_private_tab),
|
|
|
|
R.drawable.ic_private_browsing,
|
2019-05-31 00:49:58 +02:00
|
|
|
ThemeManager.resolveAttribute(R.attr.primaryText, context)
|
2019-04-01 17:47:25 +02:00
|
|
|
) {
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.NewPrivateTab)
|
|
|
|
},
|
|
|
|
|
|
|
|
BrowserMenuImageText(
|
|
|
|
context.getString(R.string.browser_menu_new_tab),
|
|
|
|
R.drawable.ic_new,
|
2019-05-31 00:49:58 +02:00
|
|
|
ThemeManager.resolveAttribute(R.attr.primaryText, context)
|
2019-04-01 17:47:25 +02:00
|
|
|
) {
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.NewTab)
|
|
|
|
},
|
|
|
|
|
|
|
|
BrowserMenuImageText(
|
|
|
|
context.getString(R.string.browser_menu_share),
|
|
|
|
R.drawable.mozac_ic_share,
|
2019-05-31 00:49:58 +02:00
|
|
|
ThemeManager.resolveAttribute(R.attr.primaryText, context)
|
2019-04-01 17:47:25 +02:00
|
|
|
) {
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.Share)
|
|
|
|
},
|
|
|
|
|
|
|
|
BrowserMenuImageText(
|
|
|
|
context.getString(R.string.browser_menu_report_issue),
|
|
|
|
R.drawable.ic_report_issues,
|
2019-05-31 00:49:58 +02:00
|
|
|
ThemeManager.resolveAttribute(R.attr.primaryText, context)
|
2019-04-01 17:47:25 +02:00
|
|
|
) {
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.ReportIssue)
|
2019-05-29 23:58:41 +02:00
|
|
|
}
|
|
|
|
)
|
2019-04-01 17:47:25 +02:00
|
|
|
|
2019-08-07 22:02:08 +02:00
|
|
|
if ((context.asActivity() as? HomeActivity)?.browsingModeManager?.mode == BrowsingMode.Normal) {
|
2019-05-29 23:58:41 +02:00
|
|
|
items.add(
|
|
|
|
BrowserMenuImageText(
|
|
|
|
context.getString(R.string.browser_menu_save_to_collection),
|
|
|
|
R.drawable.ic_tab_collection,
|
2019-05-31 00:49:58 +02:00
|
|
|
ThemeManager.resolveAttribute(R.attr.primaryText, context)
|
2019-05-29 23:58:41 +02:00
|
|
|
) {
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.SaveToCollection)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2019-04-29 20:14:24 +02:00
|
|
|
|
2019-09-10 22:29:21 +02:00
|
|
|
if (Settings.getInstance(context).shouldDeleteBrowsingDataOnQuit) {
|
|
|
|
items.add(
|
|
|
|
BrowserMenuImageText(
|
|
|
|
context.getString(R.string.delete_browsing_data_on_quit_action),
|
|
|
|
R.drawable.ic_exit,
|
|
|
|
ThemeManager.resolveAttribute(R.attr.primaryText, context)
|
|
|
|
) {
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.Quit)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-05-29 23:58:41 +02:00
|
|
|
items.add(
|
|
|
|
BrowserMenuDivider()
|
|
|
|
)
|
2019-04-01 17:47:25 +02:00
|
|
|
|
2019-05-29 23:58:41 +02:00
|
|
|
items.add(
|
2019-04-01 17:47:25 +02:00
|
|
|
menuToolbar
|
|
|
|
)
|
2019-05-29 23:58:41 +02:00
|
|
|
|
|
|
|
items
|
2019-04-01 17:47:25 +02:00
|
|
|
}
|
2019-08-29 21:45:11 +02:00
|
|
|
|
|
|
|
private fun shouldShowAddToHomescreen(): Boolean {
|
|
|
|
return context.components.useCases.webAppUseCases.isPinningSupported() &&
|
|
|
|
context.components.core.sessionManager.selectedSession != null
|
|
|
|
}
|
2019-04-01 17:47:25 +02:00
|
|
|
}
|