on bc88941 help sign in
unity8/qml/Stage/ApplicationWindow.qml
407 lines 17 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
31 readonly property alias surfaceInitialized: d.surfaceInitialized
33 // to be set from outside
34 property QtObject surface
35 property QtObject application
36 property int surfaceOrientationAngle
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 fillMode: Image.PreserveAspectCrop
149 horizontalAlignment: Image.AlignLeft
150 verticalAlignment: Image.AlignTop
151 antialiasing: !root.interactive
152 z: 1
154 function take() {
155 // Save memory by using a half-resolution (thus quarter size) screenshot.
156 // Do not make this a binding, we can only take the screenshot once!
157 surfaceContainer.grabToImage(
158 function(result) {
159 screenshotImage.source = result.url;
160 },
161 Qt.size(root.width / 2, root.height / 2));
162 }
163 }
165 Loader {
166 id: splashLoader
167 visible: active
168 active: false
169 anchors.fill: parent
170 z: screenshotImage.z + 1
171 sourceComponent: Component {
172 Splash {
173 id: splash
174 title: d.splashTitle ? d.splashTitle : d.name
175 imageSource: d.splashImage
176 icon: d.icon
177 showHeader: d.splashShowHeader
178 backgroundColor: d.splashColor
179 headerColor: d.splashColorHeader
180 footerColor: d.splashColorFooter
182 rotation: root.splashRotation
183 anchors.centerIn: parent
184 width: rotation == 0 || rotation == 180 ? root.width : root.height
185 height: rotation == 0 || rotation == 180 ? root.height : root.width
186 }
187 }
188 }
190 SurfaceContainer {
191 id: surfaceContainer
192 anchors.fill: parent
193 z: splashLoader.z + 1
194 requestedWidth: root.requestedWidth
195 requestedHeight: root.requestedHeight
196 surfaceOrientationAngle: application && application.rotatesWindowContents ? root.surfaceOrientationAngle : 0
197 }
199 Repeater {
200 id: promptSurfacesRepeater
201 objectName: "promptSurfacesRepeater"
202 // show only along with the top-most application surface
203 model: {
204 if (root.application && (
205 root.surface === root.application.surfaceList.first ||
206 root.application.surfaceList.count === 0)) {
207 return root.application.promptSurfaceList;
208 } else {
209 return null;
210 }
211 }
212 delegate: SurfaceContainer {
213 id: promptSurfaceContainer
214 interactive: index === 0 && root.interactive
215 surface: model.surface
216 width: root.width
217 height: root.height
218 requestedWidth: root.requestedWidth
219 requestedHeight: root.requestedHeight
220 isPromptSurface: true
221 z: surfaceContainer.z + (promptSurfacesRepeater.count - index)
222 property int index: model.index
223 onIndexChanged: updateFirst()
224 Component.onCompleted: updateFirst()
225 function updateFirst() {
226 if (index === 0) {
227 promptSurfacesRepeater.first = promptSurfaceContainer;
228 }
229 }
230 }
231 onCountChanged: {
232 if (count === 0) {
233 first = null;
234 }
235 }
236 property Item first: null
237 }
239 StateGroup {
240 id: stateGroup
241 objectName: "applicationWindowStateGroup"
242 states: [
243 State {
244 name: "void"
245 when:
246 d.hadSurface && (!root.surface || !d.surfaceInitialized)
247 &&
248 screenshotImage.status !== Image.Ready
249 },
250 State {
251 name: "splashScreen"
252 when:
253 !d.hadSurface && (!root.surface || !d.surfaceInitialized)
254 &&
255 screenshotImage.status !== Image.Ready
256 },
257 State {
258 name: "surface"
259 when:
260 (root.surface && d.surfaceInitialized)
261 &&
262 (d.liveSurface ||
263 (d.applicationState !== ApplicationInfoInterface.Running
264 && screenshotImage.status !== Image.Ready))
265 PropertyChanges {
266 target: root
267 implicitWidth: surfaceContainer.implicitWidth
268 implicitHeight: surfaceContainer.implicitHeight
269 }
270 },
271 State {
272 name: "screenshot"
273 when:
274 screenshotImage.status === Image.Ready
275 &&
276 (d.applicationState !== ApplicationInfoInterface.Running
277 || !root.surface || !d.surfaceInitialized)
278 },
279 State {
280 // This is a dead end. From here we expect the surface to be removed from the model
281 // shortly after we stop referencing to it in our SurfaceContainer.
282 name: "closed"
283 when:
284 // The surface died while the application is running. It must have been closed
285 // by the shell or the application decided to destroy it by itself
286 root.surface && d.surfaceInitialized && !d.liveSurface
287 && d.applicationState === ApplicationInfoInterface.Running
288 }
289 ]
291 transitions: [
292 Transition {
293 from: ""; to: "splashScreen"
294 PropertyAction { target: splashLoader; property: "active"; value: true }
295 PropertyAction { target: surfaceContainer
296 property: "visible"; value: false }
297 },
298 Transition {
299 from: "splashScreen"; to: "surface"
300 SequentialAnimation {
301 PropertyAction { target: surfaceContainer
302 property: "opacity"; value: 0.0 }
303 PropertyAction { target: surfaceContainer
304 property: "visible"; value: true }
305 UbuntuNumberAnimation { target: surfaceContainer; property: "opacity";
306 from: 0.0; to: 1.0
307 duration: UbuntuAnimation.BriskDuration }
308 ScriptAction { script: {
309 splashLoader.active = false;
310 surfaceIsOldTimer.start();
311 } }
312 }
313 },
314 Transition {
315 from: "surface"; to: "splashScreen"
316 SequentialAnimation {
317 ScriptAction { script: {
318 surfaceIsOldTimer.stop();
319 d.surfaceOldEnoughToBeResized = false;
320 splashLoader.active = true;
321 surfaceContainer.visible = true;
322 } }
323 UbuntuNumberAnimation { target: splashLoader; property: "opacity";
324 from: 0.0; to: 1.0
325 duration: UbuntuAnimation.BriskDuration }
326 PropertyAction { target: surfaceContainer
327 property: "visible"; value: false }
328 }
329 },
330 Transition {
331 from: "surface"; to: "screenshot"
332 SequentialAnimation {
333 ScriptAction { script: {
334 surfaceIsOldTimer.stop();
335 d.surfaceOldEnoughToBeResized = false;
336 screenshotImage.visible = true;
337 } }
338 UbuntuNumberAnimation { target: screenshotImage; property: "opacity";
339 from: 0.0; to: 1.0
340 duration: UbuntuAnimation.BriskDuration }
341 ScriptAction { script: {
342 surfaceContainer.visible = false;
343 surfaceContainer.surface = null;
344 d.hadSurface = true;
345 } }
346 }
347 },
348 Transition {
349 from: "screenshot"; to: "surface"
350 SequentialAnimation {
351 PropertyAction { target: surfaceContainer
352 property: "visible"; value: true }
353 UbuntuNumberAnimation { target: screenshotImage; property: "opacity";
354 from: 1.0; to: 0.0
355 duration: UbuntuAnimation.BriskDuration }
356 ScriptAction { script: {
357 screenshotImage.visible = false;
358 screenshotImage.source = "";
359 surfaceIsOldTimer.start();
360 } }
361 }
362 },
363 Transition {
364 from: "splashScreen"; to: "screenshot"
365 SequentialAnimation {
366 PropertyAction { target: screenshotImage
367 property: "visible"; value: true }
368 UbuntuNumberAnimation { target: screenshotImage; property: "opacity";
369 from: 0.0; to: 1.0
370 duration: UbuntuAnimation.BriskDuration }
371 PropertyAction { target: splashLoader; property: "active"; value: false }
372 }
373 },
374 Transition {
375 from: "surface"; to: "void"
376 ScriptAction { script: {
377 surfaceIsOldTimer.stop();
378 d.surfaceOldEnoughToBeResized = false;
379 surfaceContainer.visible = false;
380 } }
381 },
382 Transition {
383 from: "void"; to: "surface"
384 SequentialAnimation {
385 PropertyAction { target: surfaceContainer; property: "opacity"; value: 0.0 }
386 PropertyAction { target: surfaceContainer; property: "visible"; value: true }
387 UbuntuNumberAnimation { target: surfaceContainer; property: "opacity";
388 from: 0.0; to: 1.0
389 duration: UbuntuAnimation.BriskDuration }
390 ScriptAction { script: {
391 surfaceIsOldTimer.start();
392 } }
393 }
394 },
395 Transition {
396 to: "closed"
397 SequentialAnimation {
398 ScriptAction { script: {
399 surfaceContainer.visible = false;
400 surfaceContainer.surface = null;
401 d.hadSurface = true;
402 } }
403 }
404 }
405 ]
406 }