Bug 1862246 - Rename mozac_ic_add_to_home_screen_24 to mozac_ic_add_to_homescreen_24
[gecko.git] / mobile / android / focus-android / app / src / main / java / org / mozilla / focus / menu / browser / CustomTabMenu.kt
blobc6f79f64681c8a544f6a733734d1873d7ac13516
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 package org.mozilla.focus.menu.browser
6 import android.content.Context
7 import android.graphics.Typeface
8 import mozilla.components.browser.menu.WebExtensionBrowserMenuBuilder
9 import mozilla.components.browser.menu.item.BrowserMenuCategory
10 import mozilla.components.browser.menu.item.BrowserMenuDivider
11 import mozilla.components.browser.menu.item.BrowserMenuImageSwitch
12 import mozilla.components.browser.menu.item.BrowserMenuImageText
13 import mozilla.components.browser.menu.item.BrowserMenuItemToolbar
14 import mozilla.components.browser.menu.item.SimpleBrowserMenuItem
15 import mozilla.components.browser.menu.item.WebExtensionPlaceholderMenuItem
16 import mozilla.components.browser.state.selector.findCustomTab
17 import mozilla.components.browser.state.state.CustomTabSessionState
18 import mozilla.components.browser.state.store.BrowserStore
19 import mozilla.components.feature.webcompat.reporter.WebCompatReporterFeature
20 import org.mozilla.focus.R
21 import org.mozilla.focus.menu.ToolbarMenu
22 import org.mozilla.focus.theme.resolveAttribute
24 class CustomTabMenu(
25     private val context: Context,
26     private val store: BrowserStore,
27     private val currentTabId: String,
28     private val onItemTapped: (ToolbarMenu.Item) -> Unit = {},
29 ) : ToolbarMenu {
31     private val selectedSession: CustomTabSessionState?
32         get() = store.state.findCustomTab(currentTabId)
34     override val menuBuilder by lazy {
35         WebExtensionBrowserMenuBuilder(
36             items = menuItems,
37             store = store,
38         )
39     }
41     override val menuToolbar by lazy {
42         val back = BrowserMenuItemToolbar.TwoStateButton(
43             primaryImageResource = R.drawable.mozac_ic_back_24,
44             primaryContentDescription = context.getString(R.string.content_description_back),
45             primaryImageTintResource = context.theme.resolveAttribute(R.attr.primaryText),
46             isInPrimaryState = {
47                 selectedSession?.content?.canGoBack ?: false
48             },
49             secondaryImageTintResource = context.theme.resolveAttribute(R.attr.disabled),
50             disableInSecondaryState = true,
51             longClickListener = { onItemTapped.invoke(ToolbarMenu.CustomTabItem.Back) },
52         ) {
53             onItemTapped.invoke(ToolbarMenu.CustomTabItem.Back)
54         }
56         val forward = BrowserMenuItemToolbar.TwoStateButton(
57             primaryImageResource = R.drawable.mozac_ic_forward_24,
58             primaryContentDescription = context.getString(R.string.content_description_forward),
59             primaryImageTintResource = context.theme.resolveAttribute(R.attr.primaryText),
60             isInPrimaryState = {
61                 selectedSession?.content?.canGoForward ?: true
62             },
63             secondaryImageTintResource = context.theme.resolveAttribute(R.attr.disabled),
64             disableInSecondaryState = true,
65             longClickListener = { onItemTapped.invoke(ToolbarMenu.CustomTabItem.Forward) },
66         ) {
67             onItemTapped.invoke(ToolbarMenu.CustomTabItem.Forward)
68         }
70         val refresh = BrowserMenuItemToolbar.TwoStateButton(
71             primaryImageResource = R.drawable.mozac_ic_arrow_clockwise_24,
72             primaryContentDescription = context.getString(R.string.content_description_reload),
73             primaryImageTintResource = context.theme.resolveAttribute(R.attr.primaryText),
74             isInPrimaryState = {
75                 selectedSession?.content?.loading == false
76             },
77             secondaryImageResource = R.drawable.mozac_ic_stop,
78             secondaryContentDescription = context.getString(R.string.content_description_stop),
79             secondaryImageTintResource = context.theme.resolveAttribute(R.attr.primaryText),
80             disableInSecondaryState = false,
81             longClickListener = { onItemTapped.invoke(ToolbarMenu.CustomTabItem.Reload) },
82         ) {
83             if (selectedSession?.content?.loading == true) {
84                 onItemTapped.invoke(ToolbarMenu.CustomTabItem.Stop)
85             } else {
86                 onItemTapped.invoke(ToolbarMenu.CustomTabItem.Reload)
87             }
88         }
89         BrowserMenuItemToolbar(listOf(back, forward, refresh))
90     }
92     private val menuItems by lazy {
93         val findInPage = BrowserMenuImageText(
94             label = context.getString(R.string.find_in_page),
95             imageResource = R.drawable.mozac_ic_search_24,
96         ) {
97             onItemTapped.invoke(ToolbarMenu.CustomTabItem.FindInPage)
98         }
100         val desktopMode = BrowserMenuImageSwitch(
101             imageResource = R.drawable.mozac_ic_device_desktop_24,
102             label = context.getString(R.string.preference_performance_request_desktop_site2),
103             initialState = {
104                 selectedSession?.content?.desktopMode ?: true
105             },
106         ) { checked ->
107             onItemTapped.invoke(ToolbarMenu.CustomTabItem.RequestDesktop(checked))
108         }
110         val reportSiteIssue = WebExtensionPlaceholderMenuItem(
111             id = WebCompatReporterFeature.WEBCOMPAT_REPORTER_EXTENSION_ID,
112             iconTintColorResource = context.theme.resolveAttribute(R.attr.primaryText),
113         )
115         val addToHomescreen = BrowserMenuImageText(
116             label = context.getString(R.string.menu_add_to_home_screen),
117             imageResource = R.drawable.mozac_ic_add_to_homescreen_24,
118         ) {
119             onItemTapped.invoke(ToolbarMenu.CustomTabItem.AddToHomeScreen)
120         }
122         val appName = context.getString(R.string.app_name)
123         val openInFocus = SimpleBrowserMenuItem(
124             label = context.getString(R.string.menu_open_with_default_browser2, appName),
125         ) {
126             onItemTapped.invoke(ToolbarMenu.CustomTabItem.OpenInBrowser)
127         }
129         val openInApp = SimpleBrowserMenuItem(
130             label = context.getString(R.string.menu_open_with_a_browser2),
131         ) {
132             onItemTapped.invoke(ToolbarMenu.CustomTabItem.OpenInApp)
133         }
135         val poweredBy = BrowserMenuCategory(
136             label = context.getString(R.string.menu_custom_tab_branding, context.getString(R.string.app_name)),
137             textSize = CAPTION_TEXT_SIZE,
138             textColorResource = context.theme.resolveAttribute(R.attr.secondaryText),
139             backgroundColorResource = context.theme.resolveAttribute(R.attr.colorPrimary),
140             textStyle = Typeface.NORMAL,
141         )
143         listOfNotNull(
144             menuToolbar,
145             BrowserMenuDivider(),
146             findInPage,
147             desktopMode,
148             reportSiteIssue,
149             BrowserMenuDivider(),
150             addToHomescreen,
151             openInFocus,
152             openInApp,
153             poweredBy,
154         )
155     }
157     companion object {
158         private const val CAPTION_TEXT_SIZE = 12f
159     }