-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.qml
297 lines (285 loc) · 10.7 KB
/
main.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.12
ApplicationWindow {
id: root
width: 600
height: 750
visible: true
title: "QuickBehaviors Demo"
property int counter: 0
property bool toggle: true
property list<Page> demoPages: [
DemoPage {
title: "CrossFadeBehavior"
description: `
\`CrossFadeBehavior\` allows to easily and declaratively crossfade 2 graphical copies
of an \`Item\` when one of its property changes,
one made before the property has changed and one made after.
`
demoItems: [
CounterText {
objectName: "No Behavior"
},
CounterText {
objectName: 'CrossFadeBehavior (default on opacity)'
CrossFadeBehavior on text {}
},
CounterText {
objectName: 'CrossFadeBehavior, sequential'
CrossFadeBehavior on text { sequential: true}
},
CounterText {
objectName: 'CrossFadeBehavior on scale'
CrossFadeBehavior on text { fadeProperty: "scale" }
},
CounterText {
objectName: 'CrossFadeBehavior on scale and opacity'
CrossFadeBehavior on text { fadeProperties: ["scale", "opacity"] }
},
CounterText {
objectName: 'CrossFadeBehavior on x'
CrossFadeBehavior on text { fadeProperty: "x"; exitValue: fadeTarget.width + 20 }
},
CounterText {
objectName: 'CrossFadeBehavior with custom animations'
CrossFadeBehavior on text {
//sorry, verbose code ahead
id: textCfb
exitAnimation: ParallelAnimation {
id: exit
property Item target
NumberAnimation {
target: exit.target
property: "opacity"
to: 0
easing.type: Easing.InQuad
}
NumberAnimation {
target: exit.target
property: "x"
to: textCfb.fadeTarget.width
easing.type: Easing.InQuad
}
}
enterAnimation: ParallelAnimation {
id: enter
property Item target
NumberAnimation {
target: enter.target
property: "opacity"
from: 0
to: 1
easing.type: Easing.InQuad
}
NumberAnimation {
target: enter.target
property: "x"
from: -textCfb.fadeTarget.width
to: 0
easing.type: Easing.OutQuad
}
}
}
},
CounterText {
id: counterText
objectName: "CrossFadeBehavior on custom target (and on scale)"
CrossFadeBehavior on text { fadeTarget: counterText.textItem; fadeProperty: "scale" }
}
]
},
DemoPage {
title: "CrossFadeBehavior Image"
description: `
\`CrossFadeBehavior\` has an optional property named \`delayWhile\`,
it can be used to delay the starts of the animations while a condition is met.
In the last of the following examples the \`delayWhile\` property is bound to the Image's \`status\` property,
this allows to only start the animations if the Image new source has actually changed and loaded.
`
demoItems: [
EmojiImage {
objectName: "No Behavior"
},
EmojiImage {
objectName: 'CrossFadeBehavior'
CrossFadeBehavior on source {}
},
EmojiImage {
objectName: 'CrossFadeBehavior with delayWhile loading'
CrossFadeBehavior on source { delayWhile: fadeTarget.status === Image.Loading }
},
EmojiImage {
objectName: 'CrossFadeBehavior on scale with delayWhile loading'
CrossFadeBehavior on source { fadeProperty: "scale"; delayWhile: fadeTarget.status === Image.Loading }
}
]
},
DemoPage {
title: "FadeBehavior"
description:`
\`FadeBehavior\` is a simpler Behavior than \`CrossFadeBehavior\`,
instead of making and animating graphical copies of the target object, it directly animates it.
This means it can only be sequential, animating the oject out,
actually changing the target property and animating the object back in.
`
demoItems: [
CounterText {
objectName: "No Behavior"
},
CounterText {
objectName: 'FadeBehavior (default on scale)'
FadeBehavior on text {}
},
CounterText {
objectName: 'FadeBehavior on opacity'
FadeBehavior on text { fadeProperty: "opacity" }
},
CounterText {
objectName: 'FadeBehavior on x'
FadeBehavior on text { fadeProperty: "x"; fadeValue: fadeTarget.width + 20 }
},
CounterText {
objectName: 'FadeBehavior on custom target'
FadeBehavior on text { fadeTarget: targetProperty.object.textItem }
}
]
},
DemoPage {
title: "VisibleBehavior"
description:`
\`VisibleBehavior\` is a special case of \`FadeBehavior\`.
It can declaratively animates an Item when its visibility changes and only the relevant animation is ran
(the enter animation when the Item goes visible and the exit one when it is being hidden).
This allow avoiding the infamous following snippet:
property bool shown: someCondition
visible: opacity
opacity: shown ? 1 : 0
Behavior on opacity { NumberAnimation {} }
and results in clearer code with no added custom property:
visible: someCondition
VisibleBehavior on visible {}
`
demoItems: [
HidingRectangle {
objectName: "No Behavior"
},
HidingRectangle {
objectName: 'VisibleBehavior (default on opacity)'
VisibleBehavior on visible {}
},
HidingRectangle {
objectName: 'VisibleBehavior on scale'
VisibleBehavior on visible { fadeProperty: "scale" }
},
HidingRectangle {
objectName: 'VisibleBehavior on width'
VisibleBehavior on visible { fadeProperty: "width" }
},
HidingRectangle {
objectName: 'VisibleBehavior on x'
VisibleBehavior on visible { fadeProperty: "x"; fadeValue: fadeTarget.width + 20 }
},
HidingRectangle {
objectName: 'VisibleBehavior on custom property bound to width and height'
property real progress: 1
width: 50 * progress
height: 50 * progress
VisibleBehavior on visible { fadeProperty: "progress" }
}
]
}
]
Timer {
running: true
repeat: true
interval: 1500
onTriggered: {
root.counter = (root.counter + 1) % 10;
root.toggle = !root.toggle;
}
}
component DemoPage: Page {
id: page
property string description
required property list<Item> demoItems
header: Label {
text: page.description
wrapMode: Text.Wrap
padding: 10
font.weight: Font.DemiBold
textFormat: Text.MarkdownText
}
ListView {
id: listView
spacing: 20
clip: true
anchors {
fill: parent
margins: 20
}
model: page.demoItems
delegate: RowLayout {
width: listView.width
Label {
id: label
text: modelData.objectName
Layout.fillWidth: true
font.pixelSize: 14
elide: Text.ElideRight
}
Item {
width: 50
height: 50
children: modelData
}
}
}
}
component CounterText: Rectangle {
property string text: root.counter
readonly property Item textItem: textItem
width: 50
height: 50
radius: width / 2
border.width: 2
Text {
id: textItem
anchors.centerIn: parent
anchors.alignWhenCentered: false
text: parent.text
font.pixelSize: 30
}
}
component EmojiImage: Image {
width: 50
fillMode: Image.PreserveAspectFit
cache: false
horizontalAlignment: Image.AlignHCenter
verticalAlignment: Image.AlignVCenter
readonly property url smilingFaceUrl: "http://deelay.me/20/http://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/60/google/56/white-smiling-face_263a.png"
readonly property url winkingFaceUrl: "http://deelay.me/20/http://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/60/google/56/winking-face_1f609.png"
source: root.toggle ? smilingFaceUrl : winkingFaceUrl
}
component HidingRectangle: Rectangle {
width: 50
height: 50
border.width: 2
visible: root.toggle
}
header: TabBar {
id: tabBar
Repeater {
model: root.demoPages
TabButton {
text: modelData.title
}
}
}
StackLayout {
currentIndex: tabBar.currentIndex
anchors.fill: parent
children: root.demoPages
}
}