Debian Codegraph @

src/imports/Components/Themes/Ambiance/1.3/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 1.3
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/chevron.png")
42
43 property string 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
171 // The indicator image must be visible after the selected tab button, when the
172 // tab bar is not in selection mode, or after the "last" button (starting with
173 // the selected one), when the tab bar is in selection mode.
174 property bool isLastAfterSelected: index === (styledItem.model.selectedIndex === 0 ?
175 repeater.count-1 :
176 styledItem.model.selectedIndex - 1)
177 opacity: (styledItem.selectionMode ? isLastAfterSelected : selected) ? 1 : 0
178 Behavior on opacity {
179 NumberAnimation {
180 duration: headerTextFadeDuration
181 easing.type: Easing.InOutQuad
182 }
183 }
184 }
185
186 Label {
187 id: text
188 color: selected ? headerTextSelectedColor : headerTextColor
189
190 Behavior on color {
191 ColorAnimation {
192 duration: headerTextFadeDuration
193 easing.type: Easing.InOutQuad
194 }
195 }
196
197 anchors {
198 left: parent.left
199 leftMargin: headerTextLeftMargin
200 rightMargin: headerTextRightMargin
201 baseline: parent.bottom
202 baselineOffset: -headerTextBottomMargin
203 }
204 text: (model.hasOwnProperty("tab") && tab.hasOwnProperty("title")) ? tab.title : title
205 textSize: headerAdaptiveLabelSize
206 font.weight: headerFontWeight
207 style: headerTextStyle
208 styleColor: headerTextStyleColor
209 }
210
211 onClicked: {
212 if (mouseArea.enteringSelectionMode) {
213 mouseArea.enteringSelectionMode = false;
214 } else if (opacity > 0.0) {
215 styledItem.model.selectedIndex = index;
216 if (!styledItem.alwaysSelectionMode) {
217 styledItem.selectionMode = false;
218 }
219 button.select();
220 }
221 }
222
223 onPressedChanged: {
224 // Catch release after a press with a delay that is too
225 // long to make it a click, but don't unset interacting when
226 // the user starts dragging. In that case it will be unset in
227 // buttonView.onDragEnded.
228 if (!pressed && !buttonView.dragging) {
229 // unset interacting which was set in mouseArea.onPressed
230 mouseArea.interacting = false;
231 }
232 }
233
234 // Select this button
235 function select() {
236 buttonView.selectedButtonIndex = button.buttonIndex;
237 buttonView.updateOffset(button.offset);
238 }
239 }
240 }
241 }
242 }
243
244 /*!
245 Used by autopilot tests to determine when an animation finishes moving.
246 \internal
247 */
248 readonly property alias animating: offsetAnimation.running
249
250 PathView {
251 id: buttonView
252 anchors {
253 top: parent.top
254 bottom: parent.bottom
255 left: parent.left
256 }
257 width: needsScrolling ? parent.width : buttonRowWidth
258
259 // set to the width of one tabButtonRow in Component.onCompleted.
260 property real buttonRowWidth: buttonRow1 ? buttonRow1.width : 0
261
262 // set by the delegate when the components are completed.
263 property Row buttonRow1
264 property Row buttonRow2
265
266 // Track which button was last clicked
267 property int selectedButtonIndex: -1
268
269 delegate: tabButtonRow
270 model: 2 // The second buttonRow shows the buttons that disappear on the left
271 property bool needsScrolling: buttonRowWidth > parent.width
272 interactive: needsScrolling
273 clip: needsScrolling
274
275 highlightRangeMode: PathView.NoHighlightRange
276 offset: 0
277 path: Path {
278 startX: -buttonView.buttonRowWidth/2
279 PathLine {
280 x: buttonView.buttonRowWidth*1.5
281 }
282 }
283
284 // x - y (mod a), for (x - y) <= a
285 function cyclicDistance(x, y, a) {
286 var r = x - y;
287 return Math.min(Math.abs(r), Math.abs(r - a));
288 }
289
290 // Select the closest of the two buttons that represent the given tab index
291 function selectButton(tabIndex) {
292 if (!tabsModel || tabIndex < 0 || tabIndex >= tabsModel.count) return;
293 if (buttonView.buttonRow1 && buttonView.buttonRow2) {
294 var b1 = buttonView.buttonRow1.children[tabIndex];
295 var b2 = buttonView.buttonRow2.children[tabIndex];
296
297 // find the button with the nearest offset
298 var d1 = cyclicDistance(b1.offset, buttonView.offset, 2);
299 var d2 = cyclicDistance(b2.offset, buttonView.offset, 2);
300 if (d1 < d2) {
301 b1.select();
302 } else {
303 b2.select();
304 }
305 }
306 }
307
308 function updateOffset(newOffset) {
309 if (!newOffset) return; // do not update the offset when its value is NaN
310 if (offset - newOffset < -1) newOffset = newOffset - 2;
311 offset = newOffset;
312 }
313
314 Behavior on offset {
315 SmoothedAnimation {
316 id: offsetAnimation
317 velocity: buttonPositioningVelocity
318 easing.type: Easing.InOutQuad
319 }
320 }
321
322 onDragEnded: {
323 // unset interacting which was set in mouseArea.onPressed
324 mouseArea.interacting = false;
325 mouseArea.enteringSelectionMode = false;
326 }
327
328 Timer {
329 id: idleTimer
330 interval: tabBarStyle.deactivateTime
331 running: styledItem.selectionMode && !styledItem.alwaysSelectionMode
332 onTriggered: styledItem.selectionMode = false
333 function conditionalRestartOrStop() {
334 if (Qt.application.active &&
335 styledItem.selectionMode &&
336 !styledItem.alwaysSelectionMode &&
337 !mouseArea.interacting) {
338 idleTimer.restart();
339 } else {
340 idleTimer.stop();
341 }
342 }
343 }
344
345 // disable the timer when the application is not active and reset
346 // it when the application is resumed.
347 Connections {
348 target: Qt.application
349 function onActiveChanged() { idleTimer.conditionalRestartOrStop() }
350 }
351 Connections {
352 target: styledItem
353 function onSelectionModeChanged() { idleTimer.conditionalRestartOrStop() }
354 }
355 }
356
357 MouseArea {
358 // a tabBar not in selection mode can be put in selection mode by pressing
359 id: mouseArea
360 anchors.fill: parent
361
362 // set in onPressed, and unset in button.onPressedChanged or buttonView.onDragEnded
363 // because after not accepting the mouse, the released event will go to
364 // the buttonView or individual buttons.
365 property bool interacting: false
366 onInteractingChanged: idleTimer.conditionalRestartOrStop()
367
368 // When pressing to enter selection mode, a release should not be interpreted
369 // as a click on a button to select a new tab.
370 property bool enteringSelectionMode: false
371
372 // This MouseArea is always enabled, even when the tab bar is in selection mode,
373 // so that press events are detected and tabBarStyle.pressed is updated.
374 onPressed: {
375 mouseArea.interacting = true;
376 if (!styledItem.selectionMode) {
377 mouseArea.enteringSelectionMode = true;
378 }
379 styledItem.selectionMode = true;
380 mouse.accepted = false;
381 }
382 }
383
384 Component.onCompleted: {
385 tabBarStyle.sync();
386 }
387 }