Debian Codegraph @

src/imports/Components/Themes/Ambiance/2.0/PullToRefreshStyle.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
17 import QtQuick 2.4
18 import Lomiri.Components
19 import Lomiri.Components.Styles 2.0 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 control adding of control.height to the Flickable's topMargin.
60 property bool extendTopMargin: false
61 onExtendTopMarginChanged: {
62 if (extendTopMargin) {
63 control.target.topMargin += control.height;
64 } else {
65 control.target.topMargin -= control.height;
66 }
67 }
68
69 // store when the drag has happened at the beginning of the Flickable's content
70 property bool wasAtYBeginning: false
71 // initial contentY value when pull started
72 property real initialContentY: 0.0
73 // drives the refreshing state
74 property bool refreshing: false
75 // specifies the component completion
76 property bool ready: false
77 // root item
78 property Item rootItem: QuickUtils.rootItem(control)
79
80 anchors.fill: parent
81
82 Component.onCompleted: {
83 /*
84 When the model attached to the component is refreshing during initialization,
85 this refresh will happen after the style gets completed. This refresh will
86 cause the style to enter in refreshing state, which alters the topMargin.
87 However in the same time the MainView Header will be also updated, so that
88 will also alter the topMargin. But when refreshing completes, the topMargin
89 will be restored to the default value before the animation, and the content
90 will be pushed under the header. We need to connect to the header changes
91 so we can reset the state and the topMargin.
92 */
93 if (rootItem && rootItem.__propagated && rootItem.__propagated.header) {
94 rootItem.__propagated.header.visibleChanged.connect(fixTopMargin);
95 rootItem.__propagated.header.heightChanged.connect(fixTopMargin);
96 }
97 ready = true;
98 }
99
100 Component.onDestruction: {
101 if (rootItem && rootItem.__propagated && rootItem.__propagated.header) {
102 rootItem.__propagated.header.visibleChanged.disconnect(fixTopMargin);
103 rootItem.__propagated.header.heightChanged.disconnect(fixTopMargin);
104 }
105 }
106
107 function fixTopMargin() {
108 if (style.state === "refreshing") {
109 /*
110 Fetch the topMargin, force state to disabled (idle will be turned on
111 automatically when refreshing completes) and set the topMargin from
112 the header change.
113 */
114 var topMargin = control.target.topMargin;
115 style.state = "disabled";
116 control.target.topMargin = topMargin;
117 }
118 }
119
120 // visuals
121 Loader {
122 id: contentLoader
123 sourceComponent: control.content
124 onItemChanged: {
125 if (item) {
126 item.parent = style;
127 item.anchors.fill = style;
128 }
129 }
130 asynchronous: false
131 }
132
133 ActivityIndicator {
134 id: refreshIndicatorItem
135 running: false
136 anchors.centerIn: parent
137 }
138
139 onVisibleChanged: {
140 // Updates to contentY may be interrupted if the flickable becomes
141 // invisible (for example when a new Page is pushed on a stack
142 // while PullToRefresh is in refreshing state. See bug #1578619.
143 if (visible) {
144 // Make sure the flickable is back inside its bounds when
145 // the Page becomes visible again:
146 control.target.returnToBounds();
147 }
148 }
149
150 // state and content controlling
151 Connections {
152 target: control
153 function onRefreshingChanged() {
154 if (!ready || !control.enabled) {
155 return;
156 }
157 if (!style.releaseToRefresh && target.refreshing) {
158 // not a manual refresh
159 style.wasAtYBeginning = control.target.atYBeginning;
160 }
161 /*
162 We cannot bind refreshing state activation clause with the
163 control.refreshing property dirrectly as when the model is
164 refreshed automatically (not manually via the component), we
165 need to remember the flickable's topMargin in order to proceed
166 with a proper Flickable rebinding. If we use property binding,
167 the Connections' onRefreshingChanged will update the flickableTopMargin
168 only after the binding is evaluated.
169 */
170 style.refreshing = target.refreshing;
171 }
172 }
173 Connections {
174 target: control.target
175 function onMovementStarted() {
176 if (!style.refreshing) {
177 style.wasAtYBeginning = control.target.atYBeginning;
178 style.initialContentY = control.target.contentY;
179 style.releaseToRefresh = false;
180 }
181 }
182 function onMovementEnded() {
183 style.wasAtYBeginning = control.target.atYBeginning;
184 }
185
186 // catch when to initiate refresh
187 function onDraggingChanged() {
188 if (!control.parent.dragging && style.releaseToRefresh) {
189 style.refreshing = true;
190 style.releaseToRefresh = false;
191 }
192 }
193 function onContentYChanged() {
194 if (style.wasAtYBeginning && control.enabled && control.target.dragging) {
195 style.releaseToRefresh = ((style.initialContentY - control.target.contentY) > style.activationThreshold);
196 }
197 }
198 }
199
200 onStateChanged: {
201 /*
202 Label might not be ready when the component enters in refreshing
203 state, therefore the visible property will not be properly returned to
204 true. Because of the same reason we cannot have a PropertyChanges either
205 as the target is not yet ready at that point.
206 */
207 if (label) {
208 label.visible = (state === "idle" || state === "ready-to-refresh");
209 }
210 }
211 states: [
212 State {
213 name: "disabled"
214 when: !control.enabled
215 },
216 State {
217 name: "idle"
218 extend: ""
219 when: ready && control.enabled && !style.refreshing && !style.releaseToRefresh
220 },
221 State {
222 name: "ready-to-refresh"
223 when: ready && control.enabled && style.releaseToRefresh && !style.refreshing
224 },
225 State {
226 name: "refreshing"
227 when: ready && control.enabled && style.wasAtYBeginning && style.refreshing
228 PropertyChanges {
229 target: refreshIndicatorItem
230 running: true
231 }
232 PropertyChanges {
233 target: style
234 extendTopMargin: true
235 }
236 }
237 ]
238
239 transitions: [
240 Transition {
241 from: "ready-to-refresh"
242 to: "refreshing"
243 SequentialAnimation {
244 LomiriNumberAnimation {
245 target: control.target
246 property: "contentY"
247 to: style.initialContentY - control.height
248 }
249 ScriptAction {
250 script: control.refresh()
251 }
252 }
253 },
254 // transition to be applied when the model is auto-updating
255 Transition {
256 from: "idle"
257 to: "refreshing"
258 LomiriNumberAnimation {
259 target: control.target
260 property: "contentY"
261 to: style.initialContentY - control.height
262 }
263 },
264 Transition {
265 from: "refreshing"
266 to: "idle"
267 LomiriNumberAnimation {
268 target: control.target
269 property: "topMargin"
270 }
271 LomiriNumberAnimation {
272 target: control.target
273 property: "contentY"
274 to: style.initialContentY
275 }
276 }
277 ]
278 }
279