summaryrefslogtreecommitdiffstats
path: root/examples/webenginequick/lifecycle/WebTabButton.qml
blob: c26a53f54f9a1cbf7cc365f6578579dbc59f4153 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

import QtQuick
import QtQuick.Controls
import QtQuick.Controls.Material
import QtQuick.Layouts
import QtWebEngine

TabButton {
    id: root

    property WebTab tab

    text: root.tab.title

    ToolTip.delay: 1000
    ToolTip.visible: root.hovered
    ToolTip.text: root.text

    padding: 6

    contentItem: RowLayout {
        Item {
            implicitWidth: 16
            implicitHeight: 16
            BusyIndicator {
                visible: root.tab.loading
                anchors.fill: parent
                leftInset: 0
                topInset: 0
                rightInset: 0
                bottomInset: 0
                padding: 0
            }
            Image {
                visible: !root.tab.loading
                source: root.tab.icon
                anchors.fill: parent
            }
        }
        Label {
            Layout.fillWidth: true
            Layout.leftMargin: 4
            Layout.rightMargin: 4
            text: root.text
            elide: Text.ElideRight
            color: {
                switch (root.tab.lifecycleState) {
                case WebEngineView.LifecycleState.Active:
                    return Material.color(Material.Grey, Material.Shade100)
                case WebEngineView.LifecycleState.Frozen:
                    return Material.color(Material.Blue, Material.Shade400)
                case WebEngineView.LifecycleState.Discarded:
                    return Material.color(Material.Red, Material.Shade400)
                }
            }

        }
        WebToolButton {
            action: root.tab.closeAction
            text: "✕"
            ToolTip.text: action.text
        }
    }

    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.RightButton
        propagateComposedEvents: true
        onClicked: contextMenu.popup()
    }

    Menu {
        id: contextMenu
        Control {
            contentItem: Label {
                text: qsTr("Manual lifecycle control")
            }
            verticalPadding: 9
            horizontalPadding: 14
        }
        Repeater {
            model: [root.tab.activateAction, root.tab.freezeAction, root.tab.discardAction]
            RadioButton {
                action: modelData
                verticalPadding: 9
                horizontalPadding: 14
            }
        }
        Control {
            contentItem: Label {
                text: qsTr("Recommended: %1").arg(recommendedStateText)
                property string recommendedStateText: {
                    switch (root.tab.recommendedState) {
                    case WebEngineView.LifecycleState.Active:
                        return root.tab.activateAction.text
                    case WebEngineView.LifecycleState.Frozen:
                        return root.tab.freezeAction.text
                    case WebEngineView.LifecycleState.Discarded:
                        return root.tab.discardAction.text
                    }
                }
                color: Material.hintTextColor
            }
            font.pointSize: 8
            verticalPadding: 9
            horizontalPadding: 14
        }
    }
}