Debian Codegraph @

src/imports/Components/Themes/Ambiance/1.3/ScrollingActionBarStyle.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 1.3
18 import Lomiri.Components.Styles 1.3 as Style
19
20 Style.ActionBarStyle {
21 id: actionBarStyle
22 implicitHeight: units.gu(5)
23
24 clip: true // avoid showing the icons that are scrolled out of the view.
25 backgroundColor: theme.palette.normal.background
26 buttons {
27 foregroundColor: theme.palette.normal.backgroundText
28 pressedForegroundColor: buttons.foregroundColor
29 disabledForegroundColor: theme.palette.disabled.backgroundText
30 backgroundColor: "transparent" // background is already colored
31 pressedBackgroundColor: theme.palette.highlighted.background
32 disabledBackgroundColor: buttons.backgroundColor
33 }
34
35 property QtObject scrollButtons: Style.ActionItemProperties {
36 foregroundColor: actionBarStyle.buttons.foregroundColor
37 pressedForegroundColor: actionBarStyle.buttons.pressedForegroundColor
38 backgroundColor: actionBarStyle.backgroundColor // must be opaque to hide the icon buttons
39 pressedBackgroundColor: actionBarStyle.buttons.pressedBackgroundColor
40 property real width: units.gu(4)
41 property int fadeDuration: LomiriAnimation.FastDuration
42 }
43
44 /*!
45 The default action delegate if the styled item does
46 not provide a delegate.
47 */
48 // FIXME: This ListItem { AbstractButton { } } construction can be avoided
49 // when StyledItem supports cursor keys navigation, see bug #1573616.
50 defaultDelegate: ListItem {
51 width: units.gu(4)
52 height: actionBarStyle.height
53 enabled: modelData.enabled
54 objectName: modelData.objectName + "_button"
55 onClicked: button.trigger()
56 AbstractButton {
57 id: button
58 anchors.fill: parent
59 style: IconButtonStyle {
60 foregroundColor: button.pressed ?
61 actionBarStyle.buttons.pressedForegroundColor :
62 button.enabled ?
63 actionBarStyle.buttons.foregroundColor :
64 actionBarStyle.buttons.disabledForegroundColor
65 backgroundColor: button.pressed ?
66 actionBarStyle.buttons.pressedBackgroundColor :
67 button.enabled ?
68 actionBarStyle.buttons.backgroundColor :
69 actionBarStyle.buttons.disabledBackgroundColor
70 }
71 action: modelData
72 activeFocusOnTab: false
73 }
74 divider.visible: false
75 }
76
77 Rectangle {
78 color: actionBarStyle.backgroundColor
79 id: listViewContainer
80 anchors.fill: parent
81
82 property var visibleActions: getVisibleActions(styledItem.actions)
83 function getVisibleActions(actions) {
84 var visibleActionList = [];
85 for (var i = 0; i < actions.length; i++) {
86 var action = actions[i];
87 if (action && action.hasOwnProperty("visible") && action.visible) {
88 visibleActionList.push(action);
89 }
90 }
91 return visibleActionList;
92 }
93
94 LomiriListView {
95 id: actionsListView
96 objectName: "actions_listview"
97 anchors {
98 right: parent.right
99 top: parent.top
100 bottom: parent.bottom
101 rightMargin: scrollButtons.width
102 }
103 displayMarginBeginning: scrollButtons.width
104 displayMarginEnd: scrollButtons.width
105 leftMargin: -scrollButtons.width
106 rightMargin: -scrollButtons.width
107 width: listViewContainer.width - 2*scrollButtons.width
108 layoutDirection: Qt.RightToLeft
109
110 orientation: ListView.Horizontal
111 boundsBehavior: Flickable.StopAtBounds
112 delegate: styledItem.delegate
113 model: listViewContainer.visibleActions
114
115 highlight: Rectangle {
116 color: "transparent"
117 border {
118 color: theme.palette.selected.focus
119 width: 2
120 }
121 visible: actionsListView.activeFocus
122 width: actionsListView.currentItem.width
123 height: actionsListView.currentItem.height
124 }
125
126 // This gives the correct behavior when scrollButtons.width is
127 // larger/smaller/equal compared to the width of the delegate.
128 onCurrentIndexChanged: positionViewAtIndex(currentIndex, ListView.Contain)
129
130 SmoothedAnimation {
131 objectName: "actions_scroll_animation"
132 id: contentXAnim
133 target: actionsListView
134 property: "contentX"
135 duration: LomiriAnimation.FastDuration
136 }
137
138 // direction == 1 to show more icons on the left
139 // direction == -1 to show more icons on the right
140 function scroll(direction) {
141 if (contentXAnim.running) contentXAnim.stop();
142 var newContentX = actionsListView.contentX - (actionsListView.width * direction);
143 contentXAnim.from = actionsListView.contentX;
144 // make sure we don't overshoot bounds
145 contentXAnim.to = MathUtils.clamp(
146 newContentX,
147 originX + scrollButtons.width,
148 actionsListView.originX + actionsListView.contentWidth
149 - actionsListView.width - scrollButtons.width);
150 contentXAnim.start();
151 }
152 }
153
154 Component {
155 id: scrollButtonComponent
156 AbstractButton {
157 id: scrollButton
158 width: actionBarStyle.scrollButtons.width
159 enabled: opacity === 1.0
160 onClicked: actionsListView.scroll(scrollDirection)
161 opacity: buttonOpacity
162 objectName: buttonName
163 Behavior on opacity {
164 LomiriNumberAnimation {
165 duration: actionBarStyle.scrollButtons.fadeDuration
166 }
167 }
168 Rectangle {
169 anchors.fill: parent
170 color: scrollButton.pressed ? actionBarStyle.scrollButtons.pressedBackgroundColor
171 : actionBarStyle.scrollButtons.backgroundColor
172 }
173 Icon {
174 anchors.centerIn: parent
175 width: units.gu(1)
176 height: units.gu(1)
177 name: scrollDirection === 1 ? "toolkit_arrow-left" : "toolkit_arrow-right"
178 color: scrollButton.pressed ? actionBarStyle.scrollButtons.pressedForegroundColor
179 : actionBarStyle.scrollButtons.foregroundColor
180 }
181 }
182 }
183 Loader {
184 anchors {
185 left: parent.left
186 top: parent.top
187 bottom: parent.bottom
188 }
189 sourceComponent: scrollButtonComponent
190 property real buttonOpacity: actionsListView.atXBeginning ? 0.0 : 1.0
191 property int scrollDirection: 1
192 property string buttonName: "leading_scroll_button"
193 }
194 Loader {
195 anchors {
196 right: parent.right
197 top: parent.top
198 bottom: parent.bottom
199 }
200 sourceComponent: scrollButtonComponent
201 property real buttonOpacity: actionsListView.atXEnd ? 0.0 : 1.0
202 property int scrollDirection: -1
203 property string buttonName: "trailing_scroll_button"
204 }
205 }
206 }