on bedf8bc help sign in
unity8/qml/Stage/ApplicationWindow.qml
430 lines 18 kB history raw
1/*
2 * Copyright 2014-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 */
17import QtQuick 2.4
18import Ubuntu.Components 1.3
19import Unity.Application 0.1
21FocusScope {
22 id: root
23 implicitWidth: requestedWidth
24 implicitHeight: requestedHeight
26 // to be read from outside
27 property alias interactive: surfaceContainer.interactive
28 property bool orientationChangesEnabled: d.supportsSurfaceResize ? d.surfaceOldEnoughToBeResized : true
29 readonly property string title: surface && surface.name !== "" ? surface.name : d.name
30 readonly property QtObject focusedSurface: d.focusedSurface.surface
32 // to be set from outside
33 property QtObject surface
34 property QtObject application
35 property int surfaceOrientationAngle
36 property alias resizeSurface: surfaceContainer.resizeSurface
37 property int requestedWidth: -1
38 property int requestedHeight: -1
39 property real splashRotation: 0
41 readonly property int minimumWidth: surface ? surface.minimumWidth : 0
42 readonly property int minimumHeight: surface ? surface.minimumHeight : 0
43 readonly property int maximumWidth: surface ? surface.maximumWidth : 0
44 readonly property int maximumHeight: surface ? surface.maximumHeight : 0
45 readonly property int widthIncrement: surface ? surface.widthIncrement : 0
46 readonly property int heightIncrement: surface ? surface.heightIncrement : 0
48 onSurfaceChanged: {
49 // The order in which the instructions are executed here matters, to that the correct state
50 // transitions in stateGroup take place.
51 // More specifically, the moment surfaceContainer.surface gets updated relative to the
52 // other instructions.
53 if (surface) {
54 surfaceContainer.surface = surface;
55 d.liveSurface = surface.live;
56 d.hadSurface = false;
57 surfaceInitTimer.start();
58 } else {
59 if (d.surfaceInitialized) {
60 d.hadSurface = true;
61 }
62 d.surfaceInitialized = false;
63 surfaceContainer.surface = null;
64 }
65 }
67 QtObject {
68 id: d
70 property bool liveSurface: false;
71 property var con: Connections {
72 target: root.surface
73 onLiveChanged: d.liveSurface = root.surface.live
74 }
75 // using liveSurface instead of root.surface.live because with the latter
76 // this expression is not reevaluated when root.surface changes
77 readonly property bool needToTakeScreenshot: root.surface && d.surfaceInitialized && !d.liveSurface
78 && applicationState !== ApplicationInfoInterface.Running
79 onNeedToTakeScreenshotChanged: {
80 if (needToTakeScreenshot && screenshotImage.status === Image.Null) {
81 screenshotImage.take();
82 }
83 }
85 // helpers so that we don't have to check for the existence of an application everywhere
86 // (in order to avoid breaking qml binding due to a javascript exception)
87 readonly property string name: root.application ? root.application.name : ""
88 readonly property url icon: root.application ? root.application.icon : ""
89 readonly property int applicationState: root.application ? root.application.state : -1
90 readonly property string splashTitle: root.application ? root.application.splashTitle : ""
91 readonly property url splashImage: root.application ? root.application.splashImage : ""
92 readonly property bool splashShowHeader: root.application ? root.application.splashShowHeader : true
93 readonly property color splashColor: root.application ? root.application.splashColor : "#00000000"
94 readonly property color splashColorHeader: root.application ? root.application.splashColorHeader : "#00000000"
95 readonly property color splashColorFooter: root.application ? root.application.splashColorFooter : "#00000000"
97 // Whether the Application had a surface before but lost it.
98 property bool hadSurface: false
100 //FIXME - this is a hack to avoid the first few rendered frames as they
101 // might show the UI accommodating due to surface resizes on startup.
102 // Remove this when possible
103 property bool surfaceInitialized: false
105 readonly property bool supportsSurfaceResize:
106 application &&
107 ((application.supportedOrientations & Qt.PortraitOrientation)
108 || (application.supportedOrientations & Qt.InvertedPortraitOrientation))
109 &&
110 ((application.supportedOrientations & Qt.LandscapeOrientation)
111 || (application.supportedOrientations & Qt.InvertedLandscapeOrientation))
113 property bool surfaceOldEnoughToBeResized: false
115 property Item focusedSurface: promptSurfacesRepeater.count === 0 ? surfaceContainer
116 : promptSurfacesRepeater.first
117 onFocusedSurfaceChanged: {
118 if (focusedSurface) {
119 focusedSurface.focus = true;
120 }
121 }
122 }
124 Binding {
125 target: root.application
126 property: "initialSurfaceSize"
127 value: Qt.size(root.requestedWidth, root.requestedHeight)
128 }
130 Timer {
131 id: surfaceInitTimer
132 interval: 100
133 onTriggered: {
134 if (root.surface && root.surface.live) {d.surfaceInitialized = true;}
135 }
136 }
138 Timer {
139 id: surfaceIsOldTimer
140 interval: 1000
141 onTriggered: { if (stateGroup.state === "surface") { d.surfaceOldEnoughToBeResized = true; } }
142 }
144 Image {
145 id: screenshotImage
146 objectName: "screenshotImage"
147 anchors.fill: parent
148 antialiasing: !root.interactive
149 z: 1
151 function take() {
152 // Save memory by using a half-resolution (thus quarter size) screenshot.
153 // Do not make this a binding, we can only take the screenshot once!
154 surfaceContainer.grabToImage(
155 function(result) {
156 screenshotImage.source = result.url;
157 },
158 Qt.size(root.width / 2, root.height / 2));
159 }
160 }
162 Loader {
163 id: splashLoader
164 visible: active
165 active: false
166 anchors.fill: parent
167 z: screenshotImage.z + 1
168 sourceComponent: Component {
169 Splash {
170 id: splash
171 title: d.splashTitle ? d.splashTitle : d.name
172 imageSource: d.splashImage
173 icon: d.icon
174 showHeader: d.splashShowHeader
175 backgroundColor: d.splashColor
176 headerColor: d.splashColorHeader
177 footerColor: d.splashColorFooter
179 rotation: root.splashRotation
180 anchors.centerIn: parent
181 width: rotation == 0 || rotation == 180 ? root.width : root.height
182 height: rotation == 0 || rotation == 180 ? root.height : root.width
183 }
184 }
185 }
187 SurfaceContainer {
188 id: surfaceContainer
189 anchors.fill: parent
190 z: splashLoader.z + 1
191 requestedWidth: root.requestedWidth
192 requestedHeight: root.requestedHeight
193 surfaceOrientationAngle: application && application.rotatesWindowContents ? root.surfaceOrientationAngle : 0
194 }
196 Repeater {
197 id: promptSurfacesRepeater
198 objectName: "promptSurfacesRepeater"
199 // show only along with the top-most application surface
200 model: {
201 if (root.application && root.surface === root.application.surfaceList.first) {
202 return root.application.promptSurfaceList;
203 } else {
204 return null;
205 }
206 }
207 delegate: SurfaceContainer {
208 id: promptSurfaceContainer
209 interactive: index === 0 && root.interactive
210 surface: model.surface
211 width: root.width
212 height: root.height
213 requestedWidth: root.requestedWidth
214 requestedHeight: root.requestedHeight
215 isPromptSurface: true
216 z: surfaceContainer.z + (promptSurfacesRepeater.count - index)
217 property int index: model.index
218 onIndexChanged: updateFirst()
219 Component.onCompleted: updateFirst()
220 function updateFirst() {
221 if (index === 0) {
222 promptSurfacesRepeater.first = promptSurfaceContainer;
223 }
224 }
225 }
226 onCountChanged: {
227 if (count === 0) {
228 first = null;
229 }
230 }
231 property Item first: null
232 }
234// // SurfaceContainer size drives ApplicationWindow size
235// Binding {
236// target: root; property: "implicitWidth"
237// value: stateGroup.state === "surface" ? surfaceContainer.implicitWidth : root.requestedWidth
238// when: root.requestedWidth >= 0
239// }
240// Binding {
241// target: root; property: "implicitHeight"
242// value: stateGroup.state === "surface" ? surfaceContainer.implicitHeight : root.requestedHeight
243// when: root.requestedHeight >= 0
244// }
246// // ApplicationWindow size drives SurfaceContainer size
247// Binding {
248// target: surfaceContainer; property: "width"; value: root.width
249// when: root.requestedWidth < 0
250// }
251// Binding {
252// target: surfaceContainer; property: "height"; value: root.height
253// when: root.requestedHeight < 0
254// }
256 StateGroup {
257 id: stateGroup
258 objectName: "applicationWindowStateGroup"
259 states: [
260 State {
261 name: "void"
262 when:
263 d.hadSurface && (!root.surface || !d.surfaceInitialized)
264 &&
265 screenshotImage.status !== Image.Ready
266 },
267 State {
268 name: "splashScreen"
269 when:
270 !d.hadSurface && (!root.surface || !d.surfaceInitialized)
271 &&
272 screenshotImage.status !== Image.Ready
273 },
274 State {
275 name: "surface"
276 when:
277 (root.surface && d.surfaceInitialized)
278 &&
279 (d.liveSurface ||
280 (d.applicationState !== ApplicationInfoInterface.Running
281 && screenshotImage.status !== Image.Ready))
282 PropertyChanges {
283 target: root
284 implicitWidth: surfaceContainer.implicitWidth
285 implicitHeight: surfaceContainer.implicitHeight
286 }
287 },
288 State {
289 name: "screenshot"
290 when:
291 screenshotImage.status === Image.Ready
292 &&
293 (d.applicationState !== ApplicationInfoInterface.Running
294 || !root.surface || !d.surfaceInitialized)
295 },
296 State {
297 // This is a dead end. From here we expect the surface to be removed from the model
298 // shortly after we stop referencing to it in our SurfaceContainer.
299 name: "closed"
300 when:
301 // The surface died while the application is running. It must have been closed
302 // by the shell or the application decided to destroy it by itself
303 root.surface && d.surfaceInitialized && !d.liveSurface
304 && d.applicationState === ApplicationInfoInterface.Running
305 }
306 ]
308 transitions: [
309 Transition {
310 from: ""; to: "splashScreen"
311 PropertyAction { target: splashLoader; property: "active"; value: true }
312 PropertyAction { target: surfaceContainer
313 property: "visible"; value: false }
314 },
315 Transition {
316 from: "splashScreen"; to: "surface"
317 SequentialAnimation {
318 PropertyAction { target: surfaceContainer
319 property: "opacity"; value: 0.0 }
320 PropertyAction { target: surfaceContainer
321 property: "visible"; value: true }
322 UbuntuNumberAnimation { target: surfaceContainer; property: "opacity";
323 from: 0.0; to: 1.0
324 duration: UbuntuAnimation.BriskDuration }
325 ScriptAction { script: {
326 splashLoader.active = false;
327 surfaceIsOldTimer.start();
328 } }
329 }
330 },
331 Transition {
332 from: "surface"; to: "splashScreen"
333 SequentialAnimation {
334 ScriptAction { script: {
335 surfaceIsOldTimer.stop();
336 d.surfaceOldEnoughToBeResized = false;
337 splashLoader.active = true;
338 surfaceContainer.visible = true;
339 } }
340 UbuntuNumberAnimation { target: splashLoader; property: "opacity";
341 from: 0.0; to: 1.0
342 duration: UbuntuAnimation.BriskDuration }
343 PropertyAction { target: surfaceContainer
344 property: "visible"; value: false }
345 }
346 },
347 Transition {
348 from: "surface"; to: "screenshot"
349 SequentialAnimation {
350 ScriptAction { script: {
351 surfaceIsOldTimer.stop();
352 d.surfaceOldEnoughToBeResized = false;
353 screenshotImage.visible = true;
354 } }
355 UbuntuNumberAnimation { target: screenshotImage; property: "opacity";
356 from: 0.0; to: 1.0
357 duration: UbuntuAnimation.BriskDuration }
358 ScriptAction { script: {
359 surfaceContainer.visible = false;
360 surfaceContainer.surface = null;
361 d.hadSurface = true;
362 } }
363 }
364 },
365 Transition {
366 from: "screenshot"; to: "surface"
367 SequentialAnimation {
368 PropertyAction { target: surfaceContainer
369 property: "visible"; value: true }
370 UbuntuNumberAnimation { target: screenshotImage; property: "opacity";
371 from: 1.0; to: 0.0
372 duration: UbuntuAnimation.BriskDuration }
373 ScriptAction { script: {
374 screenshotImage.visible = false;
375 screenshotImage.source = "";
376 surfaceIsOldTimer.start();
377 } }
378 }
379 },
380 Transition {
381 from: "splashScreen"; to: "screenshot"
382 SequentialAnimation {
383 PropertyAction { target: screenshotImage
384 property: "visible"; value: true }
385 UbuntuNumberAnimation { target: screenshotImage; property: "opacity";
386 from: 0.0; to: 1.0
387 duration: UbuntuAnimation.BriskDuration }
388 PropertyAction { target: splashLoader; property: "active"; value: false }
389 }
390 },
391 Transition {
392 from: "surface"; to: "void"
393 ScriptAction { script: {
394 surfaceIsOldTimer.stop();
395 d.surfaceOldEnoughToBeResized = false;
396 surfaceContainer.visible = false;
397 } }
398 },
399 Transition {
400 from: "void"; to: "surface"
401 SequentialAnimation {
402 PropertyAction { target: surfaceContainer; property: "opacity"; value: 0.0 }
403 PropertyAction { target: surfaceContainer; property: "visible"; value: true }
404 UbuntuNumberAnimation { target: surfaceContainer; property: "opacity";
405 from: 0.0; to: 1.0
406 duration: UbuntuAnimation.BriskDuration }
407 ScriptAction { script: {
408 surfaceIsOldTimer.start();
409 } }
410 }
411 },
412 Transition {
413 to: "closed"
414 SequentialAnimation {
415 ScriptAction { script: {
416 surfaceContainer.visible = false;
417 surfaceContainer.surface = null;
418 d.hadSurface = true;
419 } }
420 }
421 }
422 ]
423 }
425// Rectangle {
426// anchors.fill: parent
427// color: "yellow"
428// opacity: .4
429// }