Debian Codegraph @

modules/Lomiri/Components/Extras/qml5/TabsBar/Tab.qml

raw
1 /*
2 * Copyright (C) 2016 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authored-by: Florian Boucault <[email protected]>
17 * Andrew Hayzen <[email protected]>
18 */
19 import QtQuick 2.4
20 import Lomiri.Components 1.3
21
22 Item {
23 id: tab
24
25 property color outsideColor
26 property color backgroundColor: "white"
27 property color foregroundColor: "black"
28 property color contourColor
29 property color actionColor
30 property color highlightColor
31 property string title
32 property bool isFocused
33 property bool isBeforeFocusedTab
34 property bool isHovered: false
35 property string iconName: ""
36 property url iconSource: ""
37 property string fallbackIcon: ""
38 signal close
39
40 implicitHeight: units.gu(3)
41 implicitWidth: units.gu(27)
42
43 Rectangle {
44 id: hoverFeedback
45 anchors {
46 fill: parent
47 leftMargin: -tabSeparator.width
48 }
49 color: tab.highlightColor
50 visible: tab.isHovered && !tab.isFocused
51 opacity: visible ? 1.0 : 0.0
52 Behavior on opacity {
53 LomiriNumberAnimation { duration: LomiriAnimation.FastDuration }
54 }
55 }
56
57 Rectangle {
58 anchors {
59 top: parent.top
60 bottom: tabContour.top
61 left: parent.left
62 right: parent.right
63 }
64 color: tab.outsideColor
65 visible: tab.isFocused
66 }
67
68 TabContour {
69 id: tabContour
70 anchors {
71 fill: parent
72 topMargin: units.dp(2)
73 }
74 visible: tab.isFocused
75 backgroundColor: tab.backgroundColor
76 contourColor: tab.contourColor
77 outsideColor: tab.outsideColor
78 }
79
80 Rectangle {
81 id: tabSeparator
82 anchors {
83 right: parent.right
84 verticalCenter: parent.verticalCenter
85 }
86
87 width: units.dp(1)
88 height: units.gu(2)
89 color: tab.contourColor
90 visible: !tab.isFocused && !tab.isBeforeFocusedTab
91 }
92
93 MouseArea {
94 id: tabCloseButton
95 objectName: "tabCloseButton"
96
97 anchors {
98 top: parent.top
99 bottom: parent.bottom
100 left: parent.left
101 }
102 width: units.gu(3)
103 hoverEnabled: true
104 onClicked: tab.close()
105
106 Rectangle {
107 anchors.centerIn: parent
108 property real size: units.gu(2)
109 width: size
110 height: size
111 radius: size
112 color: tab.highlightColor
113 visible: tabCloseButton.containsMouse || tabCloseButton.pressed
114 opacity: visible ? 1.0 : 0.0
115 Behavior on opacity {
116 LomiriNumberAnimation { duration: LomiriAnimation.FastDuration }
117 }
118 }
119
120 Icon {
121 width: units.gu(1)
122 height: width
123 anchors.centerIn: parent
124 asynchronous: true
125 name: "close"
126 color: tab.actionColor
127 }
128 }
129
130 Loader {
131 id: iconContainer
132 active: iconName !== "" || iconSource.toString() !== "" || fallbackIcon !== ""
133 anchors {
134 left: tabCloseButton.right
135 verticalCenter: parent.verticalCenter
136 }
137 asynchronous: true
138 height: parent.height - units.gu(1)
139 width: height
140 visible: status === Loader.Ready
141
142 Component.onCompleted: {
143 setSource(
144 Qt.resolvedUrl("TabIcon.qml"),
145 {
146 "iconName": Qt.binding(function() { return iconName; }),
147 "iconSource": Qt.binding(function() { return iconSource; }),
148 "fallbackIcon": Qt.binding(function() { return fallbackIcon; })
149 }
150 )
151 }
152 // When the tab is destroyed (as it is out of view) set the source
153 // to empty, otherwise if the loader is still loading
154 // the context doesn't exist and errors occur in the console
155 Component.onDestruction: source = "";
156 }
157
158 Label {
159 textSize: Label.Small
160 anchors {
161 left: iconContainer.visible ? iconContainer.right : tabCloseButton.right
162 leftMargin: iconContainer.visible ? units.gu(0.5) : 0
163 verticalCenter: parent.verticalCenter
164 right: parent.right
165 rightMargin: units.gu(0.5)
166 }
167 elide: Text.ElideRight
168 text: tab.title
169 color: tab.foregroundColor
170 }
171 }