| 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 1.3 |
| 19 |
import Lomiri.Components.Styles 1.3 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 |
style.wasAtYBeginning = control.target.atYBeginning; |
| 177 |
style.initialContentY = control.target.contentY; |
| 178 |
style.refreshing = false; |
| 179 |
style.releaseToRefresh = false; |
| 180 |
} |
| 181 |
function onMovementEnded() { |
| 182 |
style.wasAtYBeginning = control.target.atYBeginning; |
| 183 |
} |
| 184 |
|
| 185 |
// catch when to initiate refresh |
| 186 |
function onDraggingChanged() { |
| 187 |
if (!control.parent.dragging && style.releaseToRefresh) { |
| 188 |
style.refreshing = true; |
| 189 |
style.releaseToRefresh = false; |
| 190 |
} |
| 191 |
} |
| 192 |
function onContentYChanged() { |
| 193 |
if (style.wasAtYBeginning && control.enabled && control.target.dragging) { |
| 194 |
style.releaseToRefresh = ((style.initialContentY - control.target.contentY) > style.activationThreshold); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
onStateChanged: { |
| 200 |
/* |
| 201 |
Label might not be ready when the component enters in refreshing |
| 202 |
state, therefore the visible property will not be properly returned to |
| 203 |
true. Because of the same reason we cannot have a PropertyChanges either |
| 204 |
as the target is not yet ready at that point. |
| 205 |
*/ |
| 206 |
if (label) { |
| 207 |
label.visible = (state === "idle" || state === "ready-to-refresh"); |
| 208 |
} |
| 209 |
} |
| 210 |
states: [ |
| 211 |
State { |
| 212 |
name: "disabled" |
| 213 |
when: !control.enabled |
| 214 |
}, |
| 215 |
State { |
| 216 |
name: "idle" |
| 217 |
extend: "" |
| 218 |
when: ready && control.enabled && !style.refreshing && !style.releaseToRefresh |
| 219 |
}, |
| 220 |
State { |
| 221 |
name: "ready-to-refresh" |
| 222 |
when: ready && control.enabled && style.releaseToRefresh && !style.refreshing |
| 223 |
}, |
| 224 |
State { |
| 225 |
name: "refreshing" |
| 226 |
when: ready && control.enabled && style.wasAtYBeginning && style.refreshing |
| 227 |
PropertyChanges { |
| 228 |
target: refreshIndicatorItem |
| 229 |
running: true |
| 230 |
} |
| 231 |
PropertyChanges { |
| 232 |
target: style |
| 233 |
extendTopMargin: true |
| 234 |
} |
| 235 |
} |
| 236 |
] |
| 237 |
|
| 238 |
transitions: [ |
| 239 |
Transition { |
| 240 |
from: "ready-to-refresh" |
| 241 |
to: "refreshing" |
| 242 |
SequentialAnimation { |
| 243 |
LomiriNumberAnimation { |
| 244 |
target: control.target |
| 245 |
property: "contentY" |
| 246 |
to: style.initialContentY - control.height |
| 247 |
} |
| 248 |
ScriptAction { |
| 249 |
script: control.refresh() |
| 250 |
} |
| 251 |
} |
| 252 |
}, |
| 253 |
// transition to be applied when the model is auto-updating |
| 254 |
Transition { |
| 255 |
from: "idle" |
| 256 |
to: "refreshing" |
| 257 |
LomiriNumberAnimation { |
| 258 |
target: control.target |
| 259 |
property: "contentY" |
| 260 |
to: style.initialContentY - control.height |
| 261 |
} |
| 262 |
}, |
| 263 |
Transition { |
| 264 |
from: "refreshing" |
| 265 |
to: "idle" |
| 266 |
LomiriNumberAnimation { |
| 267 |
target: control.target |
| 268 |
property: "topMargin" |
| 269 |
} |
| 270 |
LomiriNumberAnimation { |
| 271 |
target: control.target |
| 272 |
property: "contentY" |
| 273 |
to: style.initialContentY |
| 274 |
} |
| 275 |
} |
| 276 |
] |
| 277 |
} |
| 278 |
|