Debian Codegraph @

src/imports/Components/Themes/Ambiance/1.2/PullToRefreshStyle.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
17 import QtQuick 2.4
18 import Lomiri.Components 1.2
19 import Lomiri.Components.Styles 1.2 as Style
20
21 Style.PullToRefreshStyle {
22 id: style
23 implicitHeight: refreshIndicatorItem.height + units.gu(5)
24
25 defaultContent: Label {
26 id: labelComponent
27 visible: control.enabled
28 text: releaseToRefresh ? i18n.dtr("lomiri-ui-toolkit", "Release to refresh...") : i18n.dtr("lomiri-ui-toolkit", "Pull to refresh...")
29 horizontalAlignment: Text.AlignHCenter
30 verticalAlignment: Text.AlignVCenter
31 Behavior on text {
32 SequentialAnimation {
33 LomiriNumberAnimation {
34 target: labelComponent
35 property: "opacity"
36 from: 1.0
37 to: 0.0
38 }
39 LomiriNumberAnimation {
40 target: labelComponent
41 property: "opacity"
42 from: 0.0
43 to: 1.0
44 }
45 }
46 }
47 }
48
49 // additional configuration properties provided by the Ambiance theme
50 // these properties can be used by the deriving themes to configure the label
51 // and the activity indicator
52 property Item label: contentLoader.item
53 property alias refreshIndicator: refreshIndicatorItem
54
55 /*
56 Local properties
57 */
58 readonly property PullToRefresh control: styledItem
59 // property to store Flickable's toipMargin at the time the pull or auto-refresh is started
60 property real flickableTopMargin: 0.0
61 // store when the drag has happened at the beginning of the Flickable's content
62 property bool wasAtYBeginning: false
63 // initial contentY value when pull started
64 property real initialContentY: 0.0
65 // drives the refreshing state
66 property bool refreshing: false
67 // point of release used in rebind animation between the ready-to-refresh and refreshing states
68 property real pointOfRelease
69 // specifies the component completion
70 property bool ready: false
71 // root item
72 property Item rootItem: QuickUtils.rootItem(control)
73
74 anchors.fill: parent
75
76 Component.onCompleted: {
77 /*
78 When the model attached to the component is refreshing during initialization,
79 this refresh will happen after the style gets completed. This refresh will
80 cause the style to enter in refreshing state, which alters the topMargin.
81 However in the same time the MainView Header will be also updated, so that
82 will also alter the topMargin. But when refreshing completes, the topMargin
83 will be restored to the default value before the animation, and the content
84 will be pushed under the header. We need to connect to the header changes
85 so we can reset the state and the topMargin.
86 */
87 if (rootItem && rootItem.__propagated && rootItem.__propagated.header) {
88 rootItem.__propagated.header.visibleChanged.connect(fixTopMargin);
89 rootItem.__propagated.header.heightChanged.connect(fixTopMargin);
90 }
91 ready = true;
92 }
93 function fixTopMargin() {
94 if (style.state === "refreshing") {
95 /*
96 Fetch the topMargin, force state to disabled (idle will be turned on
97 automatically when refreshing completes) and set the topMargin from
98 the header change.
99 */
100 var topMargin = control.target.topMargin;
101 style.state = "disabled";
102 control.target.topMargin = topMargin;
103 }
104 }
105
106 // visuals
107 Loader {
108 id: contentLoader
109 sourceComponent: control.content
110 onItemChanged: {
111 if (item) {
112 item.parent = style;
113 item.anchors.fill = style;
114 }
115 }
116 asynchronous: false
117 }
118
119 ActivityIndicator {
120 id: refreshIndicatorItem
121 running: false
122 anchors.centerIn: parent
123 }
124
125 // state and content controlling
126 Connections {
127 target: control
128 function onRefreshingChanged() {
129 if (!ready || !control.enabled) {
130 return;
131 }
132 if (!style.releaseToRefresh && target.refreshing) {
133 // not a manual refresh, update flickable's starting topMargin
134 style.flickableTopMargin = control.target.topMargin;
135 style.wasAtYBeginning = control.target.atYBeginning;
136 }
137 /*
138 We cannot bind refreshing state activation clause with the
139 control.refreshing property dirrectly as when the model is
140 refreshed automatically (not manually via the component), we
141 need to remember the flickable's topMargin in order to proceed
142 with a proper Flickable rebinding. If we use property binding,
143 the Connections' onRefreshingChanged will update the flickableTopMargin
144 only after the binding is evaluated.
145 */
146 style.refreshing = target.refreshing;
147 }
148 }
149 Connections {
150 target: control.target
151 function onMovementStarted() {
152 style.wasAtYBeginning = control.target.atYBeginning;
153 style.initialContentY = control.target.contentY;
154 style.refreshing = false;
155 style.releaseToRefresh = false;
156 }
157 function onMovementEnded() { style.wasAtYBeginning = control.target.atYBeginning }
158
159 // catch when to initiate refresh
160 function onDraggingChanged() {
161 if (!control.parent.dragging && style.releaseToRefresh) {
162 pointOfRelease = -(control.target.contentY - control.target.originY)
163 style.flickableTopMargin = control.target.topMargin;
164 style.refreshing = true;
165 style.releaseToRefresh = false;
166 }
167 }
168 function onContentYChanged() {
169 if (style.wasAtYBeginning && control.enabled && control.target.dragging) {
170 style.releaseToRefresh = ((style.initialContentY - control.target.contentY) > style.activationThreshold);
171 }
172 }
173 }
174
175 onStateChanged: {
176 /*
177 Label might not be ready when the component enters in refreshing
178 state, therefore the visible property will not be properly returned to
179 true. Because of the same reason we cannot have a PropertyChanges either
180 as the target is not yet ready at that point.
181 */
182 if (label) {
183 label.visible = (state === "idle" || state === "ready-to-refresh");
184 }
185 }
186 states: [
187 State {
188 name: "disabled"
189 when: !control.enabled
190 },
191 State {
192 name: "idle"
193 extend: ""
194 when: ready && control.enabled && !style.refreshing && !style.releaseToRefresh
195 },
196 State {
197 name: "ready-to-refresh"
198 when: ready && control.enabled && style.releaseToRefresh && !style.refreshing
199 },
200 State {
201 name: "refreshing"
202 when: ready && control.enabled && style.wasAtYBeginning && style.refreshing
203 PropertyChanges {
204 target: refreshIndicatorItem
205 running: true
206 }
207 PropertyChanges {
208 target: control.target
209 topMargin: style.flickableTopMargin + control.height
210 }
211 }
212 ]
213
214 transitions: [
215 Transition {
216 from: "ready-to-refresh"
217 to: "refreshing"
218 SequentialAnimation {
219 LomiriNumberAnimation {
220 target: control.target
221 property: "topMargin"
222 from: style.pointOfRelease
223 to: style.flickableTopMargin + control.height
224 }
225 ScriptAction {
226 script: control.refresh()
227 }
228 }
229 },
230 // transition to be applied when the model is auto-updating
231 Transition {
232 from: "idle"
233 to: "refreshing"
234 LomiriNumberAnimation {
235 target: control.target
236 property: "contentY"
237 from: -style.flickableTopMargin
238 to: -style.flickableTopMargin - control.height
239 }
240 },
241 Transition {
242 from: "refreshing"
243 to: "idle"
244 LomiriNumberAnimation {
245 target: control.target
246 property: "topMargin"
247 }
248 }
249 ]
250 }
251