Debian Codegraph @

src/imports/Components/1.2/AppHeader.qml

raw
1 /*
2 * Copyright 2013-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
17 import QtQuick 2.4
18 import Lomiri.Components 1.2 as Components
19
20 /*!
21 \internal
22 \qmltype AppHeader
23 \inqmlmodule Lomiri.Components
24 \ingroup lomiri
25 */
26 Components.StyledItem {
27 id: header
28
29 anchors {
30 left: parent.left
31 right: parent.right
32 }
33 y: 0
34
35 /*!
36 Animate showing and hiding of the header.
37 */
38 property bool animate: true
39
40 /*!
41 The background color of the divider. Value set by MainView.
42 */
43 property color dividerColor
44
45 /*!
46 The background color of the panel. Value set by MainView.
47 */
48 property color panelColor
49
50 Behavior on y {
51 enabled: animate && !(header.flickable && header.flickable.moving)
52 SmoothedAnimation {
53 duration: Components.LomiriAnimation.BriskDuration
54 }
55 }
56
57 /*! \internal */
58 onHeightChanged: {
59 internal.checkFlickableMargins();
60 internal.movementEnded();
61 }
62
63 visible: title || contents || tabsModel
64 onVisibleChanged: {
65 internal.checkFlickableMargins();
66 }
67 enabled: header.y === 0
68
69 /*!
70 Show the header
71 */
72 function show() {
73 // Enable the header as soon as it finished animating
74 // to the fully visible state:
75 header.enabled = Qt.binding(function() { return header.y === 0; });
76 header.y = 0;
77 }
78
79 /*!
80 Hide the header
81 */
82 function hide() {
83 // Disable the header immediately (the update of the y-value
84 // is delayed because of the Behavior defined on it):
85 header.enabled = false;
86 header.y = - header.height;
87 }
88
89 /*!
90 The text to display in the header
91 */
92 property string title: ""
93 onTitleChanged: {
94 header.show();
95 }
96
97 /*!
98 The contents of the header. If this is set, \l title will be ignored.
99 */
100 property Item contents: null
101 onContentsChanged: {
102 header.show();
103 }
104
105 /*!
106 A model of tabs to represent in the header.
107 This is automatically set by \l Tabs.
108 */
109 property var tabsModel: null
110
111 /*!
112 If it is possible to pop this PageStack, a back button will be
113 shown in the header.
114 */
115 property var pageStack: null
116
117 /*!
118 \deprecated
119 \qmlproperty list<Action> actions
120 The list of actions actions that will be shown in the header.
121 DEPRECATED. Use Page.head.actions instead.
122 */
123 property var actions
124 onActionsChanged: print("WARNING: Header.actions property is DEPRECATED. "+
125 "Use Page.head.actions instead.")
126
127 /*!
128 \internal
129 \deprecated
130 Action shown before the title. Setting this will disable the back
131 button and tabs drawer button in the new header and replace it with a button
132 representing the action below.
133 DEPRECATED. Use Page.head.backAction property instead.
134 */
135 property var __customBackAction: null
136
137 // FIXME: Currently autopilot can only get visual items, but once bug #1273956
138 // is fixed to support non-visual items, a QtObject may be used.
139 // --timp - 2014-03-20
140 Item {
141 // FIXME: This is a workaround to be able to get the properties of
142 // tabsModel in an autopilot test.
143 objectName: "tabsModelProperties"
144 property int count: tabsModel ? tabsModel.count : 0
145 property int selectedIndex: tabsModel ? tabsModel.selectedIndex : -1
146 }
147 Item {
148 // FIXME: This is a workaround to be able to get the properties of
149 // the sections in an autopilot test.
150 objectName: "sectionsProperties"
151 property int selectedIndex: header.config ? header.config.sections.selectedIndex : -1
152 }
153
154 /*!
155 The flickable that controls the movement of the header.
156 Will be set automatically by Pages inside a MainView, but can
157 be overridden.
158 */
159 property Flickable flickable: null
160 onFlickableChanged: {
161 internal.checkFlickableMargins();
162 internal.connectFlickable();
163 header.show();
164 }
165
166 /*!
167 Set by \l MainView
168 */
169 property bool useDeprecatedToolbar: true
170
171 /*!
172 Configuration of the header.
173 */
174 property PageHeadConfiguration config: null
175
176 /*!
177 The header is not fully opened or fully closed.
178
179 This property is true if the header is animating towards a fully
180 opened or fully closed state, or if the header is moving due to user
181 interaction with the flickable.
182
183 The value of moving is always false when using an old version of
184 PageHeadConfiguration (which does not have the visible property).
185
186 Used in tst_header_locked_visible.qml.
187 */
188 readonly property bool moving: false
189
190 QtObject {
191 id: internal
192
193 /*!
194 Track the y-position inside the flickable.
195 */
196 property real previousContentY: 0
197
198 /*!
199 The previous flickable to disconnect events
200 */
201 property Flickable previousFlickable: null
202
203 /*!
204 Disconnect previous flickable, and connect the new one.
205 */
206 function connectFlickable() {
207 if (previousFlickable) {
208 previousFlickable.contentYChanged.disconnect(internal.scrollContents);
209 previousFlickable.movementEnded.disconnect(internal.movementEnded);
210 previousFlickable.interactiveChanged.disconnect(internal.interactiveChanged);
211 }
212 if (flickable) {
213 // Connect flicking to movements of the header
214 previousContentY = flickable.contentY;
215 flickable.contentYChanged.connect(internal.scrollContents);
216 flickable.movementEnded.connect(internal.movementEnded);
217 flickable.interactiveChanged.connect(internal.interactiveChanged);
218 flickable.contentHeightChanged.connect(internal.contentHeightChanged);
219 }
220 previousFlickable = flickable;
221 }
222
223 /*!
224 Update the position of the header to scroll with the flickable.
225 */
226 function scrollContents() {
227 // Avoid updating header.y when rebounding or being dragged over the bounds.
228 if (!flickable.atYBeginning && !flickable.atYEnd) {
229 var deltaContentY = flickable.contentY - previousContentY;
230 header.y = Components.MathUtils.clamp(header.y - deltaContentY, -header.height, 0);
231 }
232 previousContentY = flickable.contentY;
233 }
234
235 /*!
236 Fully show or hide the header, depending on its current y.
237 */
238 function movementEnded() {
239 if (flickable && flickable.contentY < 0) header.show();
240 else if (header.y < -header.height/2) header.hide();
241 else header.show();
242 }
243
244 /*
245 Content height of flickable changed
246 */
247 function contentHeightChanged() {
248 if (flickable && flickable.height >= flickable.contentHeight) header.show();
249 }
250
251 /*
252 Flickable became interactive or non-interactive.
253 */
254 function interactiveChanged() {
255 if (flickable && !flickable.interactive) header.show();
256 }
257
258 /*
259 Check the topMargin of the flickable and set it if needed to avoid
260 contents becoming unavailable behind the header.
261 */
262 function checkFlickableMargins() {
263 if (header.flickable) {
264 var headerHeight = header.visible ? header.height : 0
265 if (flickable.topMargin !== headerHeight) {
266 var previousHeaderHeight = flickable.topMargin;
267 flickable.topMargin = headerHeight;
268 // push down contents when header grows,
269 // pull up contents when header shrinks.
270 flickable.contentY -= headerHeight - previousHeaderHeight;
271 }
272 }
273 }
274 }
275
276 style: header.useDeprecatedToolbar ? Theme.createStyleComponent("HeaderStyle.qml", header) :
277 Theme.createStyleComponent("PageHeadStyle.qml", header)
278 }