Debian Codegraph @

src/imports/Components/Themes/Ambiance/1.3/SectionsStyle.qml

raw
1 /*
2 * Copyright 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 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 import QtQuick 2.4
17 import QtQuick.Window 2.2
18 import QtGraphicalEffects 1.0
19 import Lomiri.Components 1.3
20 import Lomiri.Components.Private 1.3
21 import Lomiri.Components.Styles 1.3 as Style
22
23 Style.SectionsStyle {
24 id: sectionsStyle
25
26 implicitWidth: sectionsListView.contentWidth + 2 * listViewContainer.listViewMargins
27 implicitHeight: Screen.height > units.gu(50) ? units.gu(4) : units.gu(3)
28
29 sectionColor: enabled
30 ? theme.palette.normal.backgroundTertiaryText
31 : theme.palette.disabled.backgroundTertiaryText
32
33 underlineColor: theme.palette.normal.base
34
35 selectedSectionColor: enabled
36 ? theme.palette.selected.positionText
37 : theme.palette.selectedDisabled.positionText
38
39 pressedBackgroundColor: theme.palette.highlighted.background
40
41 textSize: Label.Medium
42
43 /*!
44 The spacing on the left and right sides of the label
45 inside a section button.
46 */
47 property real horizontalLabelSpacing: units.gu(2)
48
49 /*!
50 The height of the bar underlining the sections.
51 */
52 property real underlineHeight: units.dp(2)
53
54 // We don't clip listview on purpose, so we have to clip here to prevent Sections element
55 // from painting outside its area.
56 clip: true
57
58 //This item is needed for the OpacityMask feature. It is needed to make sure that when we
59 //bring a list element into view, that element won't be covered by the opacity mask. So we
60 //disable clipping on the list but we give it margins. This way when an item is repositioned
61 //to be within the listview, that item will not be positioned under the opacity mask. (which is
62 //what would have happened if the listview were filling the parent)
63 Item {
64 id: listViewContainer
65 anchors.fill: parent
66
67 property real listViewMargins: units.gu(2)
68
69 //We need to set this to 0.0 when OpacityMask will draw this listview for us.
70 //we don't set visible: false because we still want to get the input events!
71 opacity: 1.0
72
73 ListView {
74 id: sectionsListView
75 objectName: "sections_listview"
76
77 property bool animateContentX: false
78
79 // Position the selected item correctly.
80 // For a scrollable ListView, if the item was already fully visible,
81 // no repositioning is needed. If the item was (partially) invisible,
82 // position it so that it becomes fully visible.
83 // If the ListView is not scrollable, the first item will be aligned with
84 // the left of the ListView.
85 function positionItem(item) {
86 if (item !== null) {
87 //stop the flick before doing computations
88 if (moving) {
89 return;
90 }
91 if (dragging || flicking) {
92 cancelFlick();
93 }
94 if (contentXAnim.running) {
95 contentXAnim.stop();
96 }
97
98 contentXAnim.from = contentX;
99 //make sure we don't overshoot bounds
100 if (sectionsListView.contentWidth <= sectionsListView.width) {
101 // No scrolling, position the sections on the left.
102 contentXAnim.to = originX;
103 } else {
104 // Position the selected Item so that it is fully visible.
105 contentXAnim.to = MathUtils.clamp(contentX, originX, originX + contentWidth - width);
106 }
107 if (contentXAnim.from !== contentXAnim.to) {
108 contentXAnim.start();
109 }
110 }
111 }
112
113 anchors {
114 fill: parent
115 leftMargin: listViewContainer.listViewMargins
116 rightMargin: listViewContainer.listViewMargins
117 }
118
119 //this is just to disable keyboard navigation to avoid messing with contentX/contentWidth while
120 //the view is moving
121 focus: !moving
122
123 onWidthChanged: positionItem(currentItem)
124 //make sure that the currentItem is in the middle when everything is initialized
125 Component.onCompleted: positionItem(currentItem)
126
127 orientation: ListView.Horizontal
128 boundsBehavior: Flickable.StopAtBounds
129
130 model: styledItem.model
131
132 //We need this to make sure that we have delegates for the whole width, since we have
133 //clip disabled.
134 displayMarginBeginning: listViewContainer.listViewMargins
135 displayMarginEnd: listViewContainer.listViewMargins
136
137 currentIndex: styledItem.selectedIndex
138 onCurrentIndexChanged: {
139 styledItem.selectedIndex = currentIndex;
140 }
141 onCurrentItemChanged: {
142 //adjust contentX so that the item is kept in the middle
143 //don't use ListView.ApplyRange because that does an awkward animation when you select an item
144 //*while* the current item is outside of screen
145 positionItem(currentItem);
146 }
147
148 highlightFollowsCurrentItem: false
149 highlight: Item {
150 //show the highlight on top of the delegate
151 z: 2
152 x: sectionsListView.currentItem ? sectionsListView.currentItem.x : -width
153 width: sectionsListView.currentItem ? sectionsListView.currentItem.width : 0
154 height: sectionsListView.currentItem ? sectionsListView.currentItem.height : 0
155 // Hide the highlight underline when the ListItem has the focus frame enabled:
156 visible: sectionsListView.currentItem &&
157 !sectionsListView.currentItem.keyNavigationFocus
158
159 Rectangle {
160 anchors {
161 bottom: parent.bottom
162 left: parent.left
163 right: parent.right
164 }
165 height: sectionsStyle.underlineHeight
166 color: sectionsStyle.selectedSectionColor
167 }
168 Behavior on x { LomiriNumberAnimation {} }
169 }
170
171 delegate: ListItem {
172 id: sectionButton
173 activeFocusOnTab: false
174 objectName: "section_button_" + index
175 width: label.width + 2 * sectionsStyle.horizontalLabelSpacing
176 height: sectionsStyle.height
177 // No need to clip
178 contentItem.clip: false
179
180 property bool selected: index === styledItem.selectedIndex
181 onClicked: {
182 styledItem.selectedIndex = index;
183 sectionsListView.forceActiveFocus();
184 }
185
186 // FIXME: This line may be removed when bug #1573215 is fixed and
187 // ListItems in a horizontal ListView don't show the divider by default
188 divider.visible: false
189
190 // Section title
191 Label {
192 id: label
193 objectName: "section_button_label_" + index
194 // modelData may be either a string, or an Action
195 text: modelData.hasOwnProperty("text") ? modelData.text : modelData
196 textSize: sectionsStyle.textSize
197 font.weight: Font.Light
198 anchors.centerIn: parent
199 color: sectionButton.selected ?
200 sectionsStyle.selectedSectionColor :
201 sectionsStyle.sectionColor
202
203 Behavior on color {
204 ColorAnimation { duration: LomiriAnimation.SlowDuration }
205 }
206 }
207
208 // Section title underline
209 Rectangle {
210 id: underline
211 anchors {
212 bottom: parent.bottom
213 left: parent.left
214 right: parent.right
215 }
216 height: sectionsStyle.underlineHeight
217 color: sectionsStyle.underlineColor
218 // Don't cover the focus frame:
219 visible: !sectionButton.keyNavigationFocus
220 }
221 }
222
223 SmoothedAnimation {
224 objectName: "sections_scroll_animation"
225 id: contentXAnim
226 target: sectionsListView
227 property: "contentX"
228 duration: LomiriAnimation.FastDuration
229 velocity: units.gu(10)
230 }
231 }
232 }
233
234 MouseArea {
235 // Detect hovering over the left and right areas to show the scrolling chevrons.
236 id: hoveringArea
237
238 property real iconsDisabledOpacity: 0.3
239
240 anchors.fill: parent
241 hoverEnabled: true
242
243 property bool pressedLeft: false
244 property bool pressedRight: false
245 onPressed: {
246 pressedLeft = leftIcon.contains(mouse);
247 pressedRight = rightIcon.contains(mouse);
248 mouse.accepted = pressedLeft || pressedRight;
249 }
250 onClicked: {
251 // positionViewAtIndex() does not provide animation
252 var scrollDirection = 0;
253 if (pressedLeft && leftIcon.contains(mouse)) {
254 scrollDirection = -1;
255 } else if (pressedRight && rightIcon.contains(mouse)) {
256 scrollDirection = 1;
257 } else {
258 // User pressed on the left or right icon, and then released inside of the
259 // MouseArea but outside of the icon that was pressed.
260 return;
261 }
262 if (contentXAnim.running) contentXAnim.stop();
263 var newContentX = sectionsListView.contentX + (sectionsListView.width * scrollDirection);
264 contentXAnim.from = sectionsListView.contentX;
265 // make sure we don't overshoot bounds
266 contentXAnim.to = MathUtils.clamp(
267 newContentX,
268 0.0, // estimation of originX is some times wrong when scrolling towards the beginning
269 sectionsListView.originX + sectionsListView.contentWidth - sectionsListView.width);
270 contentXAnim.start();
271 }
272
273 Icon {
274 id: leftIcon
275 objectName: "left_scroll_icon"
276 // return whether the pressed event was done inside the area of the icon
277 function contains(mouse) {
278 return (mouse.x < listViewContainer.listViewMargins &&
279 !sectionsListView.atXBeginning);
280 }
281 anchors {
282 left: parent.left
283 leftMargin: (listViewContainer.listViewMargins - width) / 2
284 bottom: parent.bottom
285 bottomMargin: sectionsStyle.height < units.gu(4) ? units.gu(1) : units.gu(2)
286 }
287 width: units.gu(1)
288 height: units.gu(1)
289 visible: false
290 asynchronous: true
291 opacity: visible
292 ? sectionsListView.atXBeginning ? hoveringArea.iconsDisabledOpacity : 1.0
293 : 0.0
294 name: "toolkit_chevron-rtl_1gu"
295 Behavior on opacity {
296 LomiriNumberAnimation {
297 duration: LomiriAnimation.FastDuration
298 }
299 }
300 }
301
302 Icon {
303 id: rightIcon
304 objectName: "right_scroll_icon"
305 // return whether the pressed event was done inside the area of the icon
306 function contains(mouse) {
307 return (mouse.x > (hoveringArea.width - listViewContainer.listViewMargins) &&
308 !sectionsListView.atXEnd);
309 }
310 anchors {
311 right: parent.right
312 rightMargin: (listViewContainer.listViewMargins - width) / 2
313 bottom: parent.bottom
314 bottomMargin: sectionsStyle.height < units.gu(4) ? units.gu(1) : units.gu(2)
315 }
316 width: units.gu(1)
317 height: units.gu(1)
318 visible: false
319 asynchronous: true
320 opacity: visible
321 ? sectionsListView.atXEnd ? hoveringArea.iconsDisabledOpacity : 1.0
322 : 0.0
323 name: "toolkit_chevron-ltr_1gu"
324 Behavior on opacity {
325 LomiriNumberAnimation {
326 duration: LomiriAnimation.FastDuration
327 }
328 }
329 }
330 }
331
332 LinearGradient {
333 id: gradient
334
335 visible: false
336 anchors.fill: parent
337 start: Qt.point(0,0)
338 end: Qt.point(width,0)
339
340 property real gradientWidth: listViewContainer.listViewMargins / gradient.width
341 //the width is gradientWidth, but we want the gradient to actually start/finish at gradientSplitPosition
342 //just to leave some margin.
343 property real gradientSplitPosition: 2/3 * gradientWidth
344
345 gradient: Gradient {
346 //left gradient
347 GradientStop { position: 0.0 ; color: Qt.rgba(1,1,1,0) }
348 GradientStop { position: gradient.gradientSplitPosition ; color: Qt.rgba(1,1,1,0) }
349 GradientStop { position: gradient.gradientWidth; color: Qt.rgba(1,1,1,1) }
350 //right gradient
351 GradientStop { position: 1.0 - gradient.gradientWidth; color: Qt.rgba(1,1,1,1) }
352 GradientStop { position: 1.0 - gradient.gradientSplitPosition; color: Qt.rgba(1,1,1,0) }
353 GradientStop { position: 1.0; color: Qt.rgba(1,1,1,0) }
354 }
355
356 Rectangle {
357 // full opacity for the underline at the bottom in the mask below
358 anchors {
359 left: parent.left
360 right: parent.right
361 bottom: parent.bottom
362 }
363 height: sectionsStyle.underlineHeight
364 color: "white"
365 }
366 }
367
368 OpacityMask {
369 id: mask
370 anchors.fill: parent
371 visible: false
372 source: listViewContainer
373 maskSource: gradient
374 }
375
376 states: [
377 State {
378 name: "hovering"
379 when: hoveringArea.containsMouse
380 PropertyChanges { target: mask; visible: true }
381 PropertyChanges { target: listViewContainer; opacity: 0.0 }
382 PropertyChanges { target: leftIcon; visible: true; }
383 PropertyChanges { target: rightIcon; visible: true; }
384 }
385 ]
386 }