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