TabTrayItemMenu.kt 2.71 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* This Source Code Form is subject to the terms of the Mozilla Public
 * 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/. */

package org.mozilla.fenix.tabtray

import android.content.Context
import mozilla.components.browser.menu.BrowserMenuBuilder
import mozilla.components.browser.menu.item.SimpleBrowserMenuItem
import org.mozilla.fenix.R
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.components

class TabTrayItemMenu(
    private val context: Context,
16
    private val shouldShowShareAllTabs: () -> Boolean,
17
    private val shouldShowSelectTabs: () -> Boolean,
18
19
20
21
22
23
24
    private val hasOpenTabs: () -> Boolean,
    private val onItemTapped: (Item) -> Unit = {}
) {

    sealed class Item {
        object ShareAllTabs : Item()
        object OpenTabSettings : Item()
25
        object SelectTabs : Item()
26
27
28
29
30
31
32
33
34
        object CloseAllTabs : Item()
        object OpenRecentlyClosed : Item()
    }

    val menuBuilder by lazy { BrowserMenuBuilder(menuItems) }

    private val menuItems by lazy {
        listOf(
            SimpleBrowserMenuItem(
35
                context.getString(R.string.tabs_tray_select_tabs),
36
37
                textColorResource = R.color.primary_text_normal_theme
            ) {
38
39
                onItemTapped.invoke(Item.SelectTabs)
            }.apply { visible = shouldShowSelectTabs },
40
41
42
43
44
45
46

            SimpleBrowserMenuItem(
                context.getString(R.string.tab_tray_menu_item_share),
                textColorResource = R.color.primary_text_normal_theme
            ) {
                context.components.analytics.metrics.track(Event.TabsTrayShareAllTabsPressed)
                onItemTapped.invoke(Item.ShareAllTabs)
47
            }.apply { visible = { shouldShowShareAllTabs() && hasOpenTabs() } },
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

            SimpleBrowserMenuItem(
                context.getString(R.string.tab_tray_menu_tab_settings),
                textColorResource = R.color.primary_text_normal_theme
            ) {
                onItemTapped.invoke(Item.OpenTabSettings)
            },

            SimpleBrowserMenuItem(
                context.getString(R.string.tab_tray_menu_recently_closed),
                textColorResource = R.color.primary_text_normal_theme
            ) {
                onItemTapped.invoke(Item.OpenRecentlyClosed)
            },

            SimpleBrowserMenuItem(
                context.getString(R.string.tab_tray_menu_item_close),
                textColorResource = R.color.primary_text_normal_theme
            ) {
                context.components.analytics.metrics.track(Event.TabsTrayCloseAllTabsPressed)
                onItemTapped.invoke(Item.CloseAllTabs)
            }.apply { visible = hasOpenTabs }
        )
    }
}