Debian Codegraph @

src/imports/Lomiri/Contacts/PageWithBottomEdge.qml

raw
1 /*
2 * Copyright (C) 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 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 General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 /*
18 Example:
19
20 MainView {
21 objectName: "mainView"
22
23 applicationName: "com.ubuntu.developer.boiko.bottomedge"
24
25 width: units.gu(100)
26 height: units.gu(75)
27
28 Component {
29 id: pageComponent
30
31 PageWithBottomEdge {
32 id: mainPage
33 title: i18n.dtr("lomiri-addressbook-app", "Main Page")
34
35 Rectangle {
36 anchors.fill: parent
37 color: "white"
38 }
39
40 bottomEdgePageComponent: Page {
41 title: "Contents"
42 anchors.fill: parent
43 //anchors.topMargin: contentsPage.flickable.contentY
44
45 ListView {
46 anchors.fill: parent
47 model: 50
48 delegate: ListItems.Standard {
49 text: "One Content Item: " + index
50 }
51 }
52 }
53 bottomEdgeTitle: i18n.dtr("lomiri-addressbook-app", "Bottom edge action")
54 }
55 }
56
57 PageStack {
58 id: stack
59 Component.onCompleted: stack.push(pageComponent)
60 }
61 }
62
63 */
64
65 import QtQuick 2.4
66 import Lomiri.Components 1.3
67
68 Page {
69 id: page
70
71 property alias bottomEdgePageComponent: edgeLoader.sourceComponent
72 property alias bottomEdgePageSource: edgeLoader.source
73 property alias bottomEdgeTitle: tipLabel.text
74 property bool bottomEdgeEnabled: true
75 property int bottomEdgeExpandThreshold: page.height * 0.2
76 property int bottomEdgeExposedArea: bottomEdge.state !== "expanded" ? (page.height - bottomEdge.y - bottomEdge.tipHeight) : _areaWhenExpanded
77 property bool reloadBottomEdgePage: true
78
79 readonly property alias bottomEdgePage: edgeLoader.item
80 readonly property bool isReady: ((bottomEdge.y === 0) && bottomEdgePageLoaded && edgeLoader.item.active)
81 readonly property bool isCollapsed: (bottomEdge.y === page.height)
82 readonly property bool bottomEdgePageLoaded: (edgeLoader.status == Loader.Ready)
83
84 property bool _showEdgePageWhenReady: false
85 property int _areaWhenExpanded: 0
86
87 signal bottomEdgeReleased()
88 signal bottomEdgeDismissed()
89
90
91 function showBottomEdgePage(source, properties)
92 {
93 edgeLoader.setSource(source, properties)
94 _showEdgePageWhenReady = true
95 }
96
97 function setBottomEdgePage(source, properties)
98 {
99 edgeLoader.setSource(source, properties)
100 }
101
102 function _pushPage()
103 {
104 if (edgeLoader.status === Loader.Ready) {
105 edgeLoader.item.active = true
106 page.pageStack.push(edgeLoader.item)
107 if (edgeLoader.item.flickable) {
108 edgeLoader.item.flickable.contentY = -page.header.height
109 edgeLoader.item.flickable.returnToBounds()
110 }
111 if (edgeLoader.item.ready)
112 edgeLoader.item.ready()
113 }
114 }
115
116
117 Component.onCompleted: {
118 // avoid a binding on the expanded height value
119 var expandedHeight = height;
120 _areaWhenExpanded = expandedHeight;
121 }
122
123 onActiveChanged: {
124 if (active) {
125 bottomEdge.state = "collapsed"
126 }
127 }
128
129 onBottomEdgePageLoadedChanged: {
130 if (_showEdgePageWhenReady && bottomEdgePageLoaded) {
131 bottomEdge.state = "expanded"
132 _showEdgePageWhenReady = false
133 }
134 }
135
136 Rectangle {
137 id: bgVisual
138
139 color: "black"
140 anchors.fill: page
141 opacity: 0.7 * ((page.height - bottomEdge.y) / page.height)
142 z: 1
143 }
144
145 LomiriShape {
146 id: tip
147 objectName: "bottomEdgeTip"
148
149 property bool hidden: (activeFocus === false) || ((bottomEdge.y - units.gu(1)) < tip.y)
150
151 enabled: mouseArea.enabled
152 visible: page.bottomEdgeEnabled
153 anchors {
154 bottom: parent.bottom
155 horizontalCenter: bottomEdge.horizontalCenter
156 bottomMargin: hidden ? - height + units.gu(1) : -units.gu(1)
157 Behavior on bottomMargin {
158 SequentialAnimation {
159 // wait some msecs in case of the focus change again, to avoid flickering
160 PauseAnimation {
161 duration: 300
162 }
163 LomiriNumberAnimation {
164 duration: LomiriAnimation.SnapDuration
165 }
166 }
167 }
168 }
169
170 z: 1
171 width: tipLabel.paintedWidth + units.gu(6)
172 height: bottomEdge.tipHeight + units.gu(1)
173 backgroundColor: Theme.palette.normal.overlay
174 Label {
175 id: tipLabel
176
177 anchors {
178 top: parent.top
179 left: parent.left
180 right: parent.right
181 }
182 height: bottomEdge.tipHeight
183 verticalAlignment: Text.AlignVCenter
184 horizontalAlignment: Text.AlignHCenter
185 opacity: tip.hidden ? 0.0 : 1.0
186 Behavior on opacity {
187 LomiriNumberAnimation {
188 duration: LomiriAnimation.SnapDuration
189 }
190 }
191 }
192 }
193
194 Rectangle {
195 id: shadow
196
197 anchors {
198 left: parent.left
199 right: parent.right
200 bottom: parent.bottom
201 }
202 height: units.gu(1)
203 z: 1
204 opacity: 0.0
205 gradient: Gradient {
206 GradientStop { position: 0.0; color: "transparent" }
207 GradientStop { position: 1.0; color: Qt.rgba(0, 0, 0, 0.2) }
208 }
209 }
210
211 MouseArea {
212 id: mouseArea
213
214 property real previousY: -1
215 property string dragDirection: "None"
216
217 preventStealing: true
218 drag {
219 axis: Drag.YAxis
220 target: bottomEdge
221 minimumY: bottomEdge.pageStartY
222 maximumY: page.height
223 }
224 enabled: edgeLoader.status == Loader.Ready
225 visible: page.bottomEdgeEnabled
226
227 anchors {
228 left: parent.left
229 right: parent.right
230 bottom: parent.bottom
231
232 }
233 height: bottomEdge.tipHeight
234 z: 1
235
236 onReleased: {
237 page.bottomEdgeReleased()
238 if ((dragDirection === "BottomToTop") &&
239 bottomEdge.y < (page.height - bottomEdgeExpandThreshold - bottomEdge.tipHeight)) {
240 bottomEdge.state = "expanded"
241 } else {
242 bottomEdge.state = "collapsed"
243 }
244 previousY = -1
245 dragDirection = "None"
246 }
247
248 onPressed: {
249 previousY = mouse.y
250 tip.forceActiveFocus()
251 }
252
253 onMouseYChanged: {
254 var yOffset = previousY - mouseY
255 // skip if was a small move
256 if (Math.abs(yOffset) <= units.gu(2)) {
257 return
258 }
259 previousY = mouseY
260 dragDirection = yOffset > 0 ? "BottomToTop" : "TopToBottom"
261 }
262 }
263
264 Rectangle {
265 id: bottomEdge
266 objectName: "bottomEdge"
267
268 readonly property int tipHeight: units.gu(3)
269 readonly property int pageStartY: 0
270
271 z: 1
272 color: Theme.palette.normal.background
273 clip: true
274 anchors {
275 left: parent.left
276 right: parent.right
277 }
278 height: page.height
279 y: height
280 visible: !page.isCollapsed
281 state: "collapsed"
282 states: [
283 State {
284 name: "collapsed"
285 PropertyChanges {
286 target: bottomEdge
287 y: bottomEdge.height
288 }
289 },
290 State {
291 name: "expanded"
292 PropertyChanges {
293 target: bottomEdge
294 y: bottomEdge.pageStartY
295 }
296 },
297 State {
298 name: "floating"
299 when: mouseArea.drag.active
300 PropertyChanges {
301 target: shadow
302 opacity: 1.0
303 }
304 }
305 ]
306
307 transitions: [
308 Transition {
309 to: "expanded"
310 SequentialAnimation {
311 alwaysRunToEnd: true
312
313 SmoothedAnimation {
314 target: bottomEdge
315 property: "y"
316 duration: LomiriAnimation.FastDuration
317 easing.type: Easing.Linear
318 }
319 SmoothedAnimation {
320 target: edgeLoader
321 property: "anchors.topMargin"
322 to: - units.gu(4)
323 duration: LomiriAnimation.FastDuration
324 easing.type: Easing.Linear
325 }
326 SmoothedAnimation {
327 target: edgeLoader
328 property: "anchors.topMargin"
329 to: 0
330 duration: LomiriAnimation.FastDuration
331 easing: LomiriAnimation.StandardEasing
332 }
333 ScriptAction {
334 script: page._pushPage()
335 }
336 }
337 },
338 Transition {
339 from: "expanded"
340 to: "collapsed"
341 SequentialAnimation {
342 alwaysRunToEnd: true
343
344 ScriptAction {
345 script: {
346 Qt.inputMethod.hide()
347 edgeLoader.item.parent = edgeLoader
348 edgeLoader.item.anchors.fill = edgeLoader
349 edgeLoader.item.active = false
350 }
351 }
352 SmoothedAnimation {
353 target: bottomEdge
354 property: "y"
355 duration: LomiriAnimation.SlowDuration
356 }
357 ScriptAction {
358 script: {
359 // destroy current bottom page
360 if (page.reloadBottomEdgePage) {
361 edgeLoader.active = false
362 // tip will receive focus on page active true
363 } else {
364 tip.forceActiveFocus()
365 }
366
367 // notify
368 page.bottomEdgeDismissed()
369
370 edgeLoader.active = true
371 }
372 }
373 }
374 },
375 Transition {
376 from: "floating"
377 to: "collapsed"
378 SmoothedAnimation {
379 target: bottomEdge
380 property: "y"
381 duration: LomiriAnimation.FastDuration
382 }
383 }
384 ]
385
386 Loader {
387 id: edgeLoader
388
389 asynchronous: true
390 anchors.fill: parent
391 //WORKAROUND: The SDK move the page contents down to allocate space for the header we need to avoid that during the page dragging
392 Binding {
393 target: edgeLoader.status === Loader.Ready ? edgeLoader : null
394 property: "anchors.topMargin"
395 value: edgeLoader.item && edgeLoader.item.flickable ? edgeLoader.item.flickable.contentY : 0
396 when: !page.isReady
397 }
398
399 onLoaded: {
400 tip.forceActiveFocus()
401 if (page.isReady && edgeLoader.item.active !== true) {
402 page._pushPage()
403 }
404 }
405 }
406 }
407 }