Debian Codegraph @

src/imports/Components/Themes/Ambiance/2.0/TabBarStyle.qml

raw
1 /*
2 * Copyright 2015 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
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 Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.4
18 import Lomiri.Components
19
20 Item {
21 id: tabBarStyle
22
23 // used to detect when the user is interacting with the tab bar by pressing it
24 // or dragging the tab bar buttons.
25 readonly property bool pressed: mouseArea.interacting
26
27 // styling properties, public API
28 property color headerTextColor: theme.palette.normal.backgroundText
29 property color headerTextSelectedColor: theme.palette.selected.backgroundText
30
31 // Don't start transitions because of updates to selectionMode before styledItem is completed.
32 // This fixes bug #1246792: "Disable tabs scrolling animations at startup"
33 property bool animate: false
34 Binding {
35 target: tabBarStyle
36 property: "animate"
37 when: styledItem.width > 0
38 value: styledItem.animate
39 }
40 property int headerTextFadeDuration: animate ? 350 : 0
41 property url indicatorImageSource: Qt.resolvedUrl("../artwork/[email protected]")
42
43 property int headerAdaptiveLabelSize: Label.XLarge
44 property int headerTextStyle: Text.Normal
45 property color headerTextStyleColor: theme.palette.normal.backgroundText
46 property int headerFontWeight: Font.Light
47 property real headerTextLeftMargin: units.gu(2)
48 property real headerTextRightMargin: units.gu(2)
49 property real headerTextBottomMargin: units.gu(2)
50
51 property real buttonPositioningVelocity: animate ? 1.0 : -1
52 // The time of inactivity before leaving selection mode automatically
53 property int deactivateTime: 5000
54
55 /*
56 The function assures the visuals stay on the selected tab. This can be called
57 by the stack components holding the tabs (i.e. Tabs, ListView, etc) and only
58 when the changes happen on the list element values, which is not reported
59 automaytically through ListModel changes.
60 */
61 function sync() {
62 buttonView.selectButton(styledItem.model.selectedIndex);
63 }
64
65 property var tabsModel : styledItem && styledItem.model ? styledItem.model : []
66
67 Connections {
68 target: styledItem
69
70 function onSelectionModeChanged() {
71 if (!styledItem.selectionMode) {
72 buttonView.selectButton(styledItem.model.selectedIndex);
73 }
74 }
75 }
76
77 Connections {
78 target: styledItem.model
79 function onSelectedIndexChanged() { buttonView.selectButton(styledItem.model.selectedIndex) }
80 }
81
82 /*
83 Prevent events that are not accepted by tab buttons or mouseArea below
84 from passing through the TabBar.
85 */
86 MouseArea {
87 anchors.fill: parent
88 onReleased: {
89 mouseArea.enteringSelectionMode = false;
90 }
91 }
92
93 Component {
94 id: tabButtonRow
95 Row {
96 id: theRow
97 anchors {
98 top: parent.top
99 bottom: parent.bottom
100 }
101 width: childrenRect.width
102 property int rowNumber: modelData
103
104 Component.onCompleted: {
105 if (rowNumber === 0) {
106 buttonView.buttonRow1 = theRow;
107 } else {
108 buttonView.buttonRow2 = theRow;
109 }
110 }
111
112 Repeater {
113 id: repeater
114 model: tabsModel
115
116 AbstractButton {
117 id: button
118 anchors {
119 top: parent ? parent.top : undefined
120 bottom: parent ? parent.bottom : undefined
121 }
122 width: text.paintedWidth + text.anchors.leftMargin + text.anchors.rightMargin
123
124 // When the tab bar is in selection mode, show both buttons corresponing to
125 // the tab index as selected, but when it is not in selection mode only one
126 // to avoid seeing fading animations of the unselected button when switching
127 // tabs from outside the tab bar.
128 property bool selected: (styledItem.selectionMode && buttonView.needsScrolling) ?
129 styledItem.model.selectedIndex === index :
130 buttonView.selectedButtonIndex === button.buttonIndex
131 property real offset: theRow.rowNumber + 1 - button.x / theRow.width;
132 property int buttonIndex: index + theRow.rowNumber*repeater.count
133
134 // Use opacity 0 to hide instead of setting visibility to false in order to
135 // make fading work well, and not to mess up width/offset computations
136 opacity: isVisible() ? 1.0 : 0.0
137 function isVisible() {
138 if (selected) return true;
139 if (!styledItem.selectionMode) return false;
140 if (buttonView.needsScrolling) return true;
141
142 // When we don't need scrolling, we want to avoid showing a button that is fading
143 // while sliding in from the right side when a new button was selected
144 var numTabs = tabsModel.count;
145 var minimum = buttonView.selectedButtonIndex;
146 var maximum = buttonView.selectedButtonIndex + numTabs - 1;
147 if (MathUtils.clamp(buttonIndex, minimum, maximum) === buttonIndex) return true;
148 // working modulus numTabs:
149 if (buttonIndex < buttonView.selectedButtonIndex - numTabs) return true;
150 return false;
151 }
152
153 Behavior on opacity {
154 NumberAnimation {
155 duration: headerTextFadeDuration
156 easing.type: Easing.InOutQuad
157 }
158 }
159
160 Image {
161 id: indicatorImage
162 source: indicatorImageSource
163 anchors {
164 bottom: parent.bottom
165 bottomMargin: headerTextBottomMargin
166 }
167 x: button.width - width
168 // FIXME: temporary hack for the chevron's height to match the font size
169 height: 0.82*sourceSize.height
170 scale: units.gridUnit / 27
171
172 // The indicator image must be visible after the selected tab button, when the
173 // tab bar is not in selection mode, or after the "last" button (starting with
174 // the selected one), when the tab bar is in selection mode.
175 property bool isLastAfterSelected: index === (styledItem.model.selectedIndex === 0 ?
176 repeater.count-1 :
177 styledItem.model.selectedIndex - 1)
178 opacity: (styledItem.selectionMode ? isLastAfterSelected : selected) ? 1 : 0
179 Behavior on opacity {
180 NumberAnimation {
181 duration: headerTextFadeDuration
182 easing.type: Easing.InOutQuad
183 }
184 }
185 }
186
187 Label {
188 id: text
189 color: selected ? headerTextSelectedColor : headerTextColor
190
191 Behavior on color {
192 ColorAnimation {
193 duration: headerTextFadeDuration
194 easing.type: Easing.InOutQuad
195 }
196 }
197
198 anchors {
199 left: parent.left
200 leftMargin: headerTextLeftMargin
201 rightMargin: headerTextRightMargin
202 baseline: parent.bottom
203 baselineOffset: -headerTextBottomMargin
204 }
205 text: (model.hasOwnProperty("tab") && tab.hasOwnProperty("title")) ? tab.title : title
206 textSize: headerAdaptiveLabelSize
207 font.weight: headerFontWeight
208 style: headerTextStyle
209 styleColor: headerTextStyleColor
210 }
211
212 onClicked: {
213 if (mouseArea.enteringSelectionMode) {
214 mouseArea.enteringSelectionMode = false;
215 } else if (opacity > 0.0) {
216 styledItem.model.selectedIndex = index;
217 if (!styledItem.alwaysSelectionMode) {
218 styledItem.selectionMode = false;
219 }
220 button.select();
221 }
222 }
223
224 onPressedChanged: {
225 // Catch release after a press with a delay that is too
226 // long to make it a click, but don't unset interacting when
227 // the user starts dragging. In that case it will be unset in
228 // buttonView.onDragEnded.
229 if (!pressed && !buttonView.dragging) {
230 // unset interacting which was set in mouseArea.onPressed
231 mouseArea.interacting = false;
232 }
233 }
234
235 // Select this button
236 function select() {
237 buttonView.selectedButtonIndex = button.buttonIndex;
238 buttonView.updateOffset(button.offset);
239 }
240 }
241 }
242 }
243 }
244
245 /*!
246 Used by autopilot tests to determine when an animation finishes moving.
247 \internal
248 */
249 readonly property alias animating: offsetAnimation.running
250
251 PathView {
252 id: buttonView
253 anchors {
254 top: parent.top
255 bottom: parent.bottom
256 left: parent.left
257 }
258 width: needsScrolling ? parent.width : buttonRowWidth
259
260 // set to the width of one tabButtonRow in Component.onCompleted.
261 property real buttonRowWidth: buttonRow1 ? buttonRow1.width : 0
262
263 // set by the delegate when the components are completed.
264 property Row buttonRow1
265 property Row buttonRow2
266
267 // Track which button was last clicked
268 property int selectedButtonIndex: -1
269
270 delegate: tabButtonRow
271 model: 2 // The second buttonRow shows the buttons that disappear on the left
272 property bool needsScrolling: buttonRowWidth > parent.width
273 interactive: needsScrolling
274 clip: needsScrolling
275
276 highlightRangeMode: PathView.NoHighlightRange
277 offset: 0
278 path: Path {
279 startX: -buttonView.buttonRowWidth/2
280 PathLine {
281 x: buttonView.buttonRowWidth*1.5
282 }
283 }
284
285 // x - y (mod a), for (x - y) <= a
286 function cyclicDistance(x, y, a) {
287 var r = x - y;
288 return Math.min(Math.abs(r), Math.abs(r - a));
289 }
290
291 // Select the closest of the two buttons that represent the given tab index
292 function selectButton(tabIndex) {
293 if (!tabsModel || tabIndex < 0 || tabIndex >= tabsModel.count) return;
294 if (buttonView.buttonRow1 && buttonView.buttonRow2) {
295 var b1 = buttonView.buttonRow1.children[tabIndex];
296 var b2 = buttonView.buttonRow2.children[tabIndex];
297
298 // find the button with the nearest offset
299 var d1 = cyclicDistance(b1.offset, buttonView.offset, 2);
300 var d2 = cyclicDistance(b2.offset, buttonView.offset, 2);
301 if (d1 < d2) {
302 b1.select();
303 } else {
304 b2.select();
305 }
306 }
307 }
308
309 function updateOffset(newOffset) {
310 if (!newOffset) return; // do not update the offset when its value is NaN
311 if (offset - newOffset < -1) newOffset = newOffset - 2;
312 offset = newOffset;
313 }
314
315 Behavior on offset {
316 SmoothedAnimation {
317 id: offsetAnimation
318 velocity: buttonPositioningVelocity
319 easing.type: Easing.InOutQuad
320 }
321 }
322
323 onDragEnded: {
324 // unset interacting which was set in mouseArea.onPressed
325 mouseArea.interacting = false;
326 mouseArea.enteringSelectionMode = false;
327 }
328
329 Timer {
330 id: idleTimer
331 interval: tabBarStyle.deactivateTime
332 running: styledItem.selectionMode && !styledItem.alwaysSelectionMode
333 onTriggered: styledItem.selectionMode = false
334 function conditionalRestartOrStop() {
335 if (Qt.application.active &&
336 styledItem.selectionMode &&
337 !styledItem.alwaysSelectionMode &&
338 !mouseArea.interacting) {
339 idleTimer.restart();
340 } else {
341 idleTimer.stop();
342 }
343 }
344 }
345
346 // disable the timer when the application is not active and reset
347 // it when the application is resumed.
348 Connections {
349 target: Qt.application
350 function onActiveChanged() { idleTimer.conditionalRestartOrStop() }
351 }
352 Connections {
353 target: styledItem
354 function onSelectionModeChanged() { idleTimer.conditionalRestartOrStop() }
355 }
356 }
357
358 MouseArea {
359 // a tabBar not in selection mode can be put in selection mode by pressing
360 id: mouseArea
361 anchors.fill: parent
362
363 // set in onPressed, and unset in button.onPressedChanged or buttonView.onDragEnded
364 // because after not accepting the mouse, the released event will go to
365 // the buttonView or individual buttons.
366 property bool interacting: false
367 onInteractingChanged: idleTimer.conditionalRestartOrStop()
368
369 // When pressing to enter selection mode, a release should not be interpreted
370 // as a click on a button to select a new tab.
371 property bool enteringSelectionMode: false
372
373 // This MouseArea is always enabled, even when the tab bar is in selection mode,
374 // so that press events are detected and tabBarStyle.pressed is updated.
375 onPressed: function(mouse) {
376 mouseArea.interacting = true;
377 if (!styledItem.selectionMode) {
378 mouseArea.enteringSelectionMode = true;
379 }
380 styledItem.selectionMode = true;
381 mouse.accepted = false;
382 }
383 }
384
385 Component.onCompleted: {
386 tabBarStyle.sync();
387 }
388 }