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.customtabs
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import mozilla.components.browser.menu.BrowserMenuBuilder
|
|
|
|
import mozilla.components.browser.menu.item.BrowserMenuDivider
|
|
|
|
import mozilla.components.browser.menu.item.BrowserMenuImageText
|
|
|
|
import mozilla.components.browser.menu.item.BrowserMenuItemToolbar
|
2019-07-02 23:41:29 +02:00
|
|
|
import mozilla.components.browser.menu.item.BrowserMenuSwitch
|
2019-04-01 17:47:25 +02:00
|
|
|
import mozilla.components.browser.menu.item.SimpleBrowserMenuItem
|
2019-05-02 21:32:04 +02:00
|
|
|
import mozilla.components.browser.session.Session
|
|
|
|
import mozilla.components.browser.session.SessionManager
|
2019-04-01 17:47:25 +02:00
|
|
|
import org.mozilla.fenix.R
|
|
|
|
import org.mozilla.fenix.components.toolbar.ToolbarMenu
|
2019-10-25 05:55:24 +02:00
|
|
|
import org.mozilla.fenix.theme.ThemeManager
|
2019-04-01 17:47:25 +02:00
|
|
|
|
|
|
|
class CustomTabToolbarMenu(
|
|
|
|
private val context: Context,
|
2019-05-02 21:32:04 +02:00
|
|
|
private val sessionManager: SessionManager,
|
|
|
|
private val sessionId: String?,
|
2019-04-01 17:47:25 +02:00
|
|
|
private val onItemTapped: (ToolbarMenu.Item) -> Unit = {}
|
|
|
|
) : ToolbarMenu {
|
|
|
|
override val menuBuilder by lazy { BrowserMenuBuilder(menuItems) }
|
|
|
|
|
2019-05-02 21:32:04 +02:00
|
|
|
private val session: Session?
|
|
|
|
get() = sessionId?.let { sessionManager.findSessionById(it) }
|
|
|
|
|
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 = {
|
2019-05-02 21:32:04 +02:00
|
|
|
session?.canGoBack ?: true
|
2019-04-01 17:47:25 +02:00
|
|
|
},
|
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 = {
|
2019-05-02 21:32:04 +02:00
|
|
|
session?.canGoForward ?: true
|
2019-04-01 17:47:25 +02:00
|
|
|
},
|
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 = {
|
2019-05-02 21:32:04 +02:00
|
|
|
val loading = session?.loading
|
2019-04-01 17:47:25 +02:00
|
|
|
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
|
|
|
|
) {
|
2019-05-02 21:32:04 +02:00
|
|
|
if (session?.loading == true) {
|
2019-04-01 17:47:25 +02:00
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.Stop)
|
|
|
|
} else {
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.Reload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BrowserMenuItemToolbar(listOf(back, forward, refresh))
|
|
|
|
}
|
|
|
|
|
|
|
|
private val menuItems by lazy {
|
|
|
|
listOf(
|
2019-04-26 22:54:38 +02:00
|
|
|
menuToolbar,
|
2019-04-01 17:47:25 +02:00
|
|
|
|
|
|
|
BrowserMenuDivider(),
|
|
|
|
|
2019-04-26 22:54:38 +02:00
|
|
|
BrowserMenuImageText(
|
|
|
|
context.getString(R.string.browser_menu_share),
|
2019-04-30 19:01:10 +02:00
|
|
|
R.drawable.mozac_ic_share,
|
2019-05-31 00:49:58 +02:00
|
|
|
textColorResource = ThemeManager.resolveAttribute(
|
2019-04-30 19:01:10 +02:00
|
|
|
R.attr.primaryText,
|
|
|
|
context
|
|
|
|
),
|
2019-05-31 00:49:58 +02:00
|
|
|
iconTintColorResource = ThemeManager.resolveAttribute(
|
2019-04-30 19:01:10 +02:00
|
|
|
R.attr.primaryText,
|
|
|
|
context
|
|
|
|
)
|
2019-04-26 22:54:38 +02:00
|
|
|
) {
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.Share)
|
|
|
|
},
|
|
|
|
|
2019-07-02 23:41:29 +02:00
|
|
|
BrowserMenuSwitch(context.getString(R.string.browser_menu_desktop_site),
|
|
|
|
{ session?.desktopMode ?: false }, { checked ->
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.RequestDesktop(checked))
|
|
|
|
}),
|
|
|
|
|
|
|
|
BrowserMenuImageText(
|
|
|
|
context.getString(R.string.browser_menu_find_in_page),
|
|
|
|
R.drawable.mozac_ic_search,
|
|
|
|
ThemeManager.resolveAttribute(R.attr.primaryText, context)
|
|
|
|
) {
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.FindInPage)
|
|
|
|
},
|
|
|
|
|
2019-04-01 17:47:25 +02:00
|
|
|
SimpleBrowserMenuItem(
|
|
|
|
{
|
|
|
|
val appName = context.getString(R.string.app_name)
|
|
|
|
context.getString(R.string.browser_menu_open_in_fenix, appName)
|
|
|
|
}(),
|
2019-05-31 00:49:58 +02:00
|
|
|
textColorResource = ThemeManager.resolveAttribute(
|
2019-04-01 17:47:25 +02:00
|
|
|
R.attr.primaryText,
|
|
|
|
context
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
onItemTapped.invoke(ToolbarMenu.Item.OpenInFenix)
|
|
|
|
},
|
|
|
|
|
2019-04-26 22:54:38 +02:00
|
|
|
BrowserMenuDivider(),
|
|
|
|
|
|
|
|
SimpleBrowserMenuItem(
|
|
|
|
{
|
|
|
|
val appName = context.getString(R.string.app_name)
|
2019-10-25 05:55:24 +02:00
|
|
|
context.getString(R.string.browser_menu_powered_by2, appName).toUpperCase()
|
2019-04-26 22:54:38 +02:00
|
|
|
}(),
|
|
|
|
ToolbarMenu.CAPTION_TEXT_SIZE,
|
2019-05-31 00:49:58 +02:00
|
|
|
ThemeManager.resolveAttribute(R.attr.primaryText, context)
|
2019-04-26 22:54:38 +02:00
|
|
|
)
|
2019-04-01 17:47:25 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|