Debian Codegraph @

src/imports/Components/Themes/Ambiance/2.0/ActionBarStyle.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 Lomiri.Components
18 import Lomiri.Components.Popups
19 import Lomiri.Components.Styles as Style
20
21 Style.ActionBarStyle {
22 id: actionBarStyle
23 implicitWidth: actionsContainer.implicitWidth
24 implicitHeight: units.gu(5)
25
26 overflowIconName: "contextual-menu"
27
28 // Unused with the standard action icon buttons, but may be used with a custom delegate.
29 overflowText: "More"
30
31 backgroundColor: theme.palette.normal.background
32 buttons {
33 foregroundColor: theme.palette.normal.backgroundText
34 pressedForegroundColor: buttons.foregroundColor
35 disabledForegroundColor: theme.palette.disabled.backgroundText
36 backgroundColor: "transparent" // action bar background is already colored
37 pressedBackgroundColor: theme.palette.highlighted.background
38 disabledBackgroundColor: buttons.backgroundColor
39 }
40
41 /*!
42 The default action delegate if the styled item does
43 not provide a delegate.
44 */
45 defaultDelegate: Component {
46 AbstractButton {
47 id: button
48 style: IconButtonStyle {
49 foregroundColor: button.pressed ?
50 actionBarStyle.buttons.pressedForegroundColor :
51 button.enabled ?
52 actionBarStyle.buttons.foregroundColor :
53 actionBarStyle.buttons.disabledForegroundColor
54 backgroundColor: button.pressed ?
55 actionBarStyle.buttons.pressedBackgroundColor :
56 button.enabled ?
57 actionBarStyle.buttons.backgroundColor :
58 actionBarStyle.buttons.disabledBackgroundColor
59 }
60 objectName: action.objectName + "_button"
61 height: parent ? parent.height : undefined
62 action: modelData
63 }
64 }
65
66 defaultNumberOfSlots: 3
67
68 Rectangle {
69 id: background
70 anchors.fill: parent
71 color: actionBarStyle.backgroundColor
72 }
73
74 Component {
75 id: fadeInComponent
76 SequentialAnimation {
77 id: fadeIn
78 property alias target: opacityAnimation.target
79 ScriptAction {
80 script: fadeIn.target.opacity = 0.0;
81 }
82 LomiriNumberAnimation {
83 id: opacityAnimation
84 from: 0.0
85 to: 1.0
86 property: "opacity"
87 alwaysRunToEnd: true
88 duration: LomiriAnimation.BriskDuration
89 }
90 ScriptAction {
91 script: fadeIn.destroy()
92 }
93 }
94 }
95
96 Row {
97 id: actionsContainer
98
99 property var visibleActions: getVisibleActions(styledItem.actions)
100 function getVisibleActions(actions): list{
101 var visibleActionList = [];
102 for (var i in actions) {
103 var action = actions[i];
104 if (action && action.hasOwnProperty("visible") && action.visible) {
105 visibleActionList.push(action);
106 }
107 }
108 return visibleActionList;
109 }
110 function getReversedActions(actions): list {
111 var newlist = [];
112 for (var i=actions.length-1; i >= 0; i--) {
113 newlist.push(actions[i]);
114 }
115 return newlist;
116 }
117
118 property var directActions: getReversedActions(visibleActions.slice(0, numberOfSlots.used))
119 property var barActions: overflowAction.visible
120 ? directActions.concat(overflowAction)
121 : directActions
122 property var overflowActions: visibleActions.slice(numberOfSlots.used,
123 numberOfSlots.requested)
124
125 QtObject {
126 id: numberOfSlots
127 property int requested: actionsContainer.visibleActions.length
128 property int available: styledItem.numberOfSlots
129 property int overflow: overflowAction.visible ? 1 : 0
130 // when numberOfSlots < 1, show the overflow button, but set used
131 // to 0 so that all actions will appear in the overflow panel.
132 property int used: Math.min(Math.max(0, available - overflow), requested)
133 }
134
135 anchors {
136 top: parent.top
137 bottom: parent.bottom
138 }
139
140 Repeater {
141 id: actionsRepeater
142 objectName: "actions_repeater"
143 model: actionsContainer.barActions
144 delegate: styledItem.delegate
145 property int previousCount: count
146 onCountChanged: {
147 // after all itemAdded signals
148 previousCount = count;
149 }
150
151 function fadeIn(item) {
152 var fadeObject = fadeInComponent.createObject(actionBarStyle,
153 {"target": item});
154 fadeObject.target = item;
155 fadeObject.start();
156 }
157 onItemAdded: function(index, item) {
158 if (count <= previousCount) return; // no items added
159 if (index === 0) fadeIn(item);
160 }
161 }
162
163 Action {
164 id: overflowAction
165 iconSource: actionBarStyle.overflowIconSource
166 iconName: actionBarStyle.overflowIconName
167 text: actionBarStyle.overflowText
168 objectName: "overflow_action"
169 visible: numberOfSlots.requested > numberOfSlots.available
170 onTriggered: {
171 var overflowButton = actionsRepeater.itemAt(actionsRepeater.count - 1);
172 PopupUtils.open(actionsOverflowPopoverComponent, overflowButton);
173 }
174 }
175
176 Component {
177 id: actionsOverflowPopoverComponent
178 OverflowPanel {
179 id: actionsOverflowPopover
180 objectName: "actions_overflow_panel"
181
182 // Ensure the popover closes when actions change and
183 // the list item below may be destroyed before its
184 // onClicked is executed. See bug
185 // https://bugs.launchpad.net/lomiri-ui-toolkit/+bug/1326963
186 Connections {
187 target: styledItem
188 function onActionsChanged() {
189 actionsOverflowPopover.hide();
190 }
191 }
192 actions: actionsContainer.overflowActions
193 }
194 }
195 }
196 }