Debian Codegraph @

src/imports/Components/Themes/Ambiance/1.2/PageHeadStyle.qml

raw
1 /*
2 * Copyright 2014 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 Lomiri.Components 1.2
18 import Lomiri.Components.Popups 1.0
19 import Lomiri.Components.ListItems 1.0 as ListItem
20 import Lomiri.Components.Styles 1.2 as Style
21
22 Style.PageHeadStyle {
23 id: headerStyle
24 objectName: "PageHeadStyle" // used in unit tests
25 contentHeight: units.gu(7)
26 fontWeight: Font.Light
27 fontSize: "x-large"
28 textLeftMargin: units.gu(2)
29 maximumNumberOfActions: 3
30
31 /*!
32 The color of the buttons in the header.
33 */
34 property color buttonColor: styledItem.config.foregroundColor
35
36 /*!
37 The color of the title text.
38 */
39 property color titleColor: styledItem.config.foregroundColor
40
41 /*!
42 The background color of the tabs panel and the actions overflow panel.
43 */
44 property color panelBackgroundColor: styledItem.panelColor
45
46 /*!
47 The background color of the tapped item in the panel.
48 */
49 property color panelHighlightColor: Theme.palette.selected.background
50
51 /*!
52 The foreground color (icon and text) of actions in the panel.
53 */
54 property color panelForegroundColor: Theme.palette.selected.backgroundText
55
56 /*!
57 The text color of unselected sections and the section divider.
58 */
59 property color sectionColor: Theme.palette.selected.backgroundText
60
61 /*!
62 The text color of the selected section.
63 */
64 property color selectedSectionColor: LomiriColors.orange
65
66 /*!
67 The background color of the pressed section.
68 */
69 property color sectionHighlightColor: Theme.palette.selected.background
70
71 implicitHeight: headerStyle.contentHeight + divider.height
72
73 // FIXME: Workaround to get sectionsRepeater.count in autopilot tests,
74 // see also FIXME in AppHeader where this property is used.
75 property alias __sections_repeater_for_autopilot: sectionsRepeater
76
77 // Used by unit tests and autopilot tests to wait for animations to finish
78 readonly property bool animating: headerStyle.state == "OUT"
79 || leftAnchor.anchors.leftMargin < 0
80
81 // for Lomiri
82 // FIXME: Remove this property when we introduce a header preset that does not
83 // have a separator.
84 property alias __separator_visible: divider.visible
85
86 StyledItem {
87 id: divider
88 anchors {
89 bottom: parent.bottom
90 left: parent.left
91 right: parent.right
92 }
93
94 height: sectionsRow.visible ? units.gu(3) : units.gu(2)
95
96 // separatorSource and separatorBottomSource are needed for the deprecated
97 // HeadSeparatorImageStyle.
98 property url separatorSource: headerStyle.separatorSource
99 property url separatorBottomSource: headerStyle.separatorBottomSource
100
101 // backgroundColor is used in the new HeadDividerStyle
102 property color backgroundColor: styledItem.dividerColor
103
104 style: Theme.createStyleComponent("HeadDividerStyle.qml", divider)
105
106 property PageHeadSections sections: styledItem.config.sections
107
108 Row {
109 id: sectionsRow
110 anchors.centerIn: parent
111 width: childrenRect.width
112 height: parent.height
113 enabled: divider.sections.enabled
114 visible: divider.sections.model !== undefined
115 opacity: enabled ? 1.0 : 0.5
116
117 Repeater {
118 id: sectionsRepeater
119 model: divider.sections.model
120 objectName: "page_head_sections_repeater"
121 AbstractButton {
122 id: sectionButton
123 anchors.verticalCenter: parent ? parent.verticalCenter : undefined
124 objectName: "section_button_" + index
125 enabled: sectionsRow.enabled
126 width: label.width + units.gu(4)
127 height: sectionsRow.height + units.gu(2)
128 property bool selected: index === divider.sections.selectedIndex
129 onClicked: divider.sections.selectedIndex = index;
130
131 Rectangle {
132 visible: parent.pressed
133 anchors {
134 verticalCenter: parent.verticalCenter
135 left: parent.left
136 right: parent.right
137 rightMargin: verticalDividerLine.width
138 }
139 height: sectionsRow.height
140 color: headerStyle.sectionHighlightColor
141 }
142
143 Label {
144 id: label
145 text: modelData
146 fontSize: "small"
147 anchors.centerIn: sectionButton
148 horizontalAlignment: Text.AlignHCenter
149 color: sectionButton.selected ?
150 headerStyle.selectedSectionColor :
151 headerStyle.sectionColor
152 }
153
154 // vertical divider line
155 Rectangle {
156 id: verticalDividerLine
157 anchors {
158 verticalCenter: parent.verticalCenter
159 right: parent.right
160 }
161 height: units.dp(10)
162 width: units.dp(1)
163 visible: index < sectionsRepeater.model.length - 1
164 color: headerStyle.sectionColor
165 opacity: 0.2
166 }
167 }
168 }
169 }
170 }
171
172 states: [
173 State {
174 name: "IN"
175 PropertyChanges {
176 target: allContents
177 opacity: 1.0
178 }
179 },
180 State {
181 name: "OUT"
182 PropertyChanges {
183 target: allContents
184 opacity: 0.0
185 }
186 }
187 ]
188
189 function animateOut() {
190 state = "OUT";
191 }
192 function animateIn() {
193 state = "IN";
194 }
195
196 signal animateOutFinished()
197 signal animateInFinished()
198
199 transitions: [
200 Transition {
201 id: transitionOut
202 from: "IN"
203 to: "OUT"
204 SequentialAnimation {
205 ParallelAnimation {
206 LomiriNumberAnimation {
207 target: allContents
208 property: "opacity"
209 from: 1.0
210 to: 0.0
211 }
212 LomiriNumberAnimation {
213 target: leftAnchor
214 properties: "anchors.leftMargin"
215 from: 0.0
216 to: -units.gu(5)
217 }
218 LomiriNumberAnimation {
219 target: rightAnchor
220 properties: "anchors.rightMargin"
221 from: 0
222 to: -units.gu(5)
223 }
224 }
225 ScriptAction {
226 script: headerStyle.animateOutFinished()
227 }
228 }
229 },
230 Transition {
231 id: transitionIn
232 from: "OUT"
233 to: "IN"
234 SequentialAnimation {
235 ParallelAnimation {
236 LomiriNumberAnimation {
237 target: allContents
238 property: "opacity"
239 from: 0.0
240 to: 1.0
241 }
242 LomiriNumberAnimation {
243 target: leftAnchor
244 properties: "anchors.leftMargin"
245 from: -units.gu(5)
246 to: 0
247 }
248 LomiriNumberAnimation {
249 target: rightAnchor
250 properties: "anchors.rightMargin"
251 from: -units.gu(5)
252 to: 0
253 }
254 }
255 ScriptAction {
256 script: headerStyle.animateInFinished()
257 }
258 }
259 }
260 ]
261
262 Item {
263 id: allContents
264 anchors.fill: parent
265
266 Item {
267 id: leftAnchor
268 anchors {
269 top: parent.top
270 bottom: parent.bottom
271 left: parent.left
272 leftMargin: 0
273 }
274 width: 0
275 }
276 Item {
277 id: rightAnchor
278 anchors {
279 top: parent.top
280 bottom: parent.bottom
281 right: parent.right
282 rightMargin: 0
283 }
284 width: 0
285 }
286
287 Item {
288 id: leftButtonContainer
289 anchors {
290 left: leftAnchor.right
291 top: parent.top
292 leftMargin: width > 0 ? units.gu(1) : 0
293 }
294 width: childrenRect.width
295 height: headerStyle.contentHeight
296
297 PageHeadButton {
298 id: customBackButton
299 objectName: "customBackButton"
300 action: styledItem.config.backAction
301 visible: null !== styledItem.config.backAction &&
302 styledItem.config.backAction.visible
303 color: styledItem.config.foregroundColor
304 }
305
306 PageHeadButton {
307 id: backButton
308 objectName: "backButton"
309
310 iconName: "back"
311 visible: styledItem.pageStack !== null &&
312 styledItem.pageStack !== undefined &&
313 styledItem.pageStack.depth > 1 &&
314 !styledItem.config.backAction
315
316 text: "back"
317 color: styledItem.config.foregroundColor
318
319 onTriggered: {
320 styledItem.pageStack.pop();
321 }
322 }
323
324 PageHeadButton {
325 id: tabsButton
326 objectName: "tabsButton"
327
328 iconName: "navigation-menu"
329 visible: styledItem.tabsModel !== null &&
330 styledItem.tabsModel !== undefined &&
331 !backButton.visible &&
332 !customBackButton.visible
333 text: visible ? styledItem.tabsModel.count + " tabs" : ""
334 color: headerStyle.buttonColor
335
336 onTriggered: PopupUtils.open(tabsPopoverComponent, tabsButton)
337
338 Component {
339 id: tabsPopoverComponent
340
341 OverflowPanel {
342 id: tabsPopover
343 objectName: "tabsPopover"
344 callerMargin: -units.gu(1) + units.dp(4)
345 contentWidth: units.gu(20)
346
347 Binding {
348 target: tabsPopover.__foreground.__styleInstance
349 property: "color"
350 value: headerStyle.panelBackgroundColor
351 when: tabsPopover.__foreground &&
352 tabsPopover.__foreground.__styleInstance
353 }
354
355 Column {
356 anchors {
357 left: parent.left
358 top: parent.top
359 right: parent.right
360 }
361 Repeater {
362 model: styledItem.tabsModel
363 AbstractButton {
364 objectName: "tabButton" + index
365 onClicked: {
366 styledItem.tabsModel.selectedIndex = index;
367 tabsPopover.hide();
368 }
369 implicitHeight: units.gu(6) + bottomDividerLine.height
370 width: parent ? parent.width : units.gu(31)
371
372 Rectangle {
373 visible: parent.pressed
374 anchors {
375 left: parent.left
376 right: parent.right
377 top: parent.top
378 }
379 height: parent.height - bottomDividerLine.height
380 color: headerStyle.panelHighlightColor
381 }
382
383 Label {
384 anchors {
385 verticalCenter: parent.verticalCenter
386 verticalCenterOffset: units.dp(-1)
387 left: parent.left
388 leftMargin: units.gu(2)
389 right: parent.right
390 }
391 fontSize: "medium"
392 elide: Text.ElideRight
393 text: tab.title // FIXME: only "title" doesn't work with i18n.tr(). Why not?
394 color: headerStyle.panelForegroundColor
395 }
396
397 ListItem.ThinDivider {
398 id: bottomDividerLine
399 anchors.bottom: parent.bottom
400 visible: index < styledItem.tabsModel.count - 1
401 }
402 }
403 }
404 }
405 }
406 }
407 }
408 }
409
410 Item {
411 id: foreground
412 anchors {
413 left: leftButtonContainer.right
414 top: parent.top
415 // don't keep a margin if there is already a button with spacing
416 leftMargin: leftButtonContainer.width > 0 ? 0 : headerStyle.textLeftMargin
417 }
418 width: parent.width - anchors.leftMargin
419 - leftButtonContainer.anchors.leftMargin - leftButtonContainer.width
420 - actionsContainer.anchors.rightMargin - actionsContainer.width
421 height: headerStyle.contentHeight
422
423 Label {
424 objectName: "header_title_label"
425 LayoutMirroring.enabled: Qt.application.layoutDirection == Qt.RightToLeft
426 visible: !contentsContainer.visible && styledItem.config.preset === ""
427 anchors {
428 left: parent.left
429 right: parent.right
430 verticalCenter: parent.verticalCenter
431 }
432 text: styledItem.title
433 font.weight: headerStyle.fontWeight
434 fontSize: headerStyle.fontSize
435 color: headerStyle.titleColor
436 elide: Text.ElideRight
437 }
438
439 Item {
440 // This Item is used to make the custom header item invisible
441 // when styledItem.contents is unset and its parent is not updated
442 // when the bindings below is no longer active
443 id: contentsContainer
444 anchors.fill: parent
445 visible: styledItem.contents || styledItem.config.contents
446 }
447 Binding {
448 target: styledItem.contents
449 property: "anchors.fill"
450 value: contentsContainer
451 when: styledItem.contents
452 }
453 Binding {
454 target: styledItem.contents
455 property: "parent"
456 value: contentsContainer
457 when: styledItem.contents
458 }
459 Binding {
460 target: styledItem.config.contents
461 property: "parent"
462 value: contentsContainer
463 when: styledItem.config.contents && !styledItem.contents
464 }
465 }
466
467 Row {
468 id: actionsContainer
469
470 property var visibleActions: getVisibleActions(styledItem.config.actions)
471 function getVisibleActions(actions) {
472 var visibleActionList = [];
473 for (var i in actions) {
474 var action = actions[i];
475 if (action && action.hasOwnProperty("visible") && action.visible) {
476 visibleActionList.push(action);
477 }
478 }
479 return visibleActionList;
480 }
481
482 QtObject {
483 id: numberOfSlots
484 property int requested: actionsContainer.visibleActions.length
485 property int left: tabsButton.visible || backButton.visible ||
486 customBackButton.visible ? 1 : 0
487 property int right: headerStyle.maximumNumberOfActions - left
488 property int overflow: actionsOverflowButton.visible ? 1 : 0
489 property int used: Math.min(right - overflow, requested)
490 }
491
492 anchors {
493 top: parent.top
494 right: rightAnchor.left
495 rightMargin: actionsContainer.width > 0 ? units.gu(1) : 0
496 }
497 width: childrenRect.width
498 height: headerStyle.contentHeight
499
500 Repeater {
501 model: numberOfSlots.used
502 PageHeadButton {
503 id: actionButton
504 objectName: action.objectName + "_header_button"
505 action: actionsContainer.visibleActions[index]
506 color: headerStyle.buttonColor
507 state: styledItem.config.preset === "select" ?
508 "IconAndLabel" : ""
509 }
510 }
511
512 PageHeadButton {
513 id: actionsOverflowButton
514 objectName: "actions_overflow_button"
515 visible: numberOfSlots.requested > numberOfSlots.right
516 // Ensure resetting of X when this button is not visible to avoid
517 // miscalculation of actionsContainer.width. Fixes bug #1408481.
518 onVisibleChanged: if (!visible) x = 0
519 iconName: "contextual-menu"
520 color: headerStyle.buttonColor
521 height: actionsContainer.height
522 onTriggered: PopupUtils.open(actionsOverflowPopoverComponent, actionsOverflowButton)
523
524 Component {
525 id: actionsOverflowPopoverComponent
526
527 OverflowPanel {
528 id: actionsOverflowPopover
529 objectName: "actions_overflow_popover"
530 callerMargin: -units.gu(1) + units.dp(4)
531 contentWidth: units.gu(20)
532
533 Binding {
534 target: actionsOverflowPopover.__foreground.__styleInstance
535 property: "color"
536 value: headerStyle.panelBackgroundColor
537 when: actionsOverflowPopover.__foreground &&
538 actionsOverflowPopover.__foreground.__styleInstance
539 }
540
541 // Ensure the popover closes when actions change and
542 // the list item below may be destroyed before its
543 // onClicked is executed. See bug
544 // https://bugs.launchpad.net/lomiri-ui-toolkit/+bug/1326963
545 Connections {
546 target: styledItem.config
547 function onActionsChanged() {
548 actionsOverflowPopover.hide();
549 }
550 }
551 Connections {
552 target: styledItem
553 function onConfigChanged() {
554 actionsOverflowPopover.hide();
555 }
556 }
557
558 Column {
559 anchors {
560 left: parent.left
561 top: parent.top
562 right: parent.right
563 }
564 Repeater {
565 id: overflowRepeater
566 model: numberOfSlots.requested - numberOfSlots.used
567 AbstractButton {
568 action: actionsContainer.visibleActions[numberOfSlots.used + index]
569 objectName: action.objectName + "_header_overflow_button"
570 onClicked: actionsOverflowPopover.hide()
571 implicitHeight: units.gu(6) + bottomDividerLine.height
572 width: parent ? parent.width : units.gu(31)
573
574 Rectangle {
575 visible: parent.pressed
576 anchors {
577 left: parent.left
578 right: parent.right
579 top: parent.top
580 }
581 height: parent.height - bottomDividerLine.height
582 color: headerStyle.panelHighlightColor
583 }
584
585 Icon {
586 id: actionIcon
587 source: action.iconSource
588 color: headerStyle.panelForegroundColor
589 anchors {
590 verticalCenter: parent.verticalCenter
591 verticalCenterOffset: units.dp(-1)
592 left: parent.left
593 leftMargin: units.gu(2)
594 }
595 width: units.gu(2)
596 height: units.gu(2)
597 opacity: action.enabled ? 1.0 : 0.5
598 }
599
600 Label {
601 anchors {
602 verticalCenter: parent.verticalCenter
603 verticalCenterOffset: units.dp(-1)
604 left: actionIcon.right
605 leftMargin: units.gu(2)
606 right: parent.right
607 }
608 fontSize: "small"
609 elide: Text.ElideRight
610 text: action.text
611 color: headerStyle.panelForegroundColor
612 opacity: action.enabled ? 1.0 : 0.5
613 }
614
615 ListItem.ThinDivider {
616 id: bottomDividerLine
617 anchors.bottom: parent.bottom
618 visible: index !== overflowRepeater.count - 1
619 }
620 }
621 }
622 }
623 }
624 }
625 }
626 }
627 }
628 }