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  readonly property alias surfaceInitialized: d.surfaceInitialized
32 
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
40 
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
47 
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  }
66 
67  QtObject {
68  id: d
69 
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  }
84 
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"
96 
97  // Whether the Application had a surface before but lost it.
98  property bool hadSurface: false
99 
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
104 
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))
112 
113  property bool surfaceOldEnoughToBeResized: false
114 
115  property Item focusedSurface: promptSurfacesRepeater.count === 0 ? surfaceContainer
116  : promptSurfacesRepeater.first
117  onFocusedSurfaceChanged: {
118  if (focusedSurface) {
119  focusedSurface.focus = true;
120  }
121  }
122  }
123 
124  Binding {
125  target: root.application
126  property: "initialSurfaceSize"
127  value: Qt.size(root.requestedWidth, root.requestedHeight)
128  }
129 
130  Timer {
131  id: surfaceInitTimer
132  interval: 100
133  onTriggered: {
134  if (root.surface && root.surface.live) {d.surfaceInitialized = true;}
135  }
136  }
137 
138  Timer {
139  id: surfaceIsOldTimer
140  interval: 1000
141  onTriggered: { if (stateGroup.state === "surface") { d.surfaceOldEnoughToBeResized = true; } }
142  }
143 
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
153 
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  }
164 
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
181 
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  }
189 
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  }
198 
199  Repeater {
200  id: promptSurfacesRepeater
201  objectName: "promptSurfacesRepeater"
202  // show only along with the top-most application surface
203  model: {
204  if (root.application && root.surface === root.application.surfaceList.first) {
205  return root.application.promptSurfaceList;
206  } else {
207  return null;
208  }
209  }
210  delegate: SurfaceContainer {
211  id: promptSurfaceContainer
212  interactive: index === 0 && root.interactive
213  surface: model.surface
214  width: root.width
215  height: root.height
216  requestedWidth: root.requestedWidth
217  requestedHeight: root.requestedHeight
218  isPromptSurface: true
219  z: surfaceContainer.z + (promptSurfacesRepeater.count - index)
220  property int index: model.index
221  onIndexChanged: updateFirst()
222  Component.onCompleted: updateFirst()
223  function updateFirst() {
224  if (index === 0) {
225  promptSurfacesRepeater.first = promptSurfaceContainer;
226  }
227  }
228  }
229  onCountChanged: {
230  if (count === 0) {
231  first = null;
232  }
233  }
234  property Item first: null
235  }
236 
237  StateGroup {
238  id: stateGroup
239  objectName: "applicationWindowStateGroup"
240  states: [
241  State {
242  name: "void"
243  when:
244  d.hadSurface && (!root.surface || !d.surfaceInitialized)
245  &&
246  screenshotImage.status !== Image.Ready
247  },
248  State {
249  name: "splashScreen"
250  when:
251  !d.hadSurface && (!root.surface || !d.surfaceInitialized)
252  &&
253  screenshotImage.status !== Image.Ready
254  },
255  State {
256  name: "surface"
257  when:
258  (root.surface && d.surfaceInitialized)
259  &&
260  (d.liveSurface ||
261  (d.applicationState !== ApplicationInfoInterface.Running
262  && screenshotImage.status !== Image.Ready))
263  PropertyChanges {
264  target: root
265  implicitWidth: surfaceContainer.implicitWidth
266  implicitHeight: surfaceContainer.implicitHeight
267  }
268  },
269  State {
270  name: "screenshot"
271  when:
272  screenshotImage.status === Image.Ready
273  &&
274  (d.applicationState !== ApplicationInfoInterface.Running
275  || !root.surface || !d.surfaceInitialized)
276  },
277  State {
278  // This is a dead end. From here we expect the surface to be removed from the model
279  // shortly after we stop referencing to it in our SurfaceContainer.
280  name: "closed"
281  when:
282  // The surface died while the application is running. It must have been closed
283  // by the shell or the application decided to destroy it by itself
284  root.surface && d.surfaceInitialized && !d.liveSurface
285  && d.applicationState === ApplicationInfoInterface.Running
286  }
287  ]
288 
289  transitions: [
290  Transition {
291  from: ""; to: "splashScreen"
292  PropertyAction { target: splashLoader; property: "active"; value: true }
293  PropertyAction { target: surfaceContainer
294  property: "visible"; value: false }
295  },
296  Transition {
297  from: "splashScreen"; to: "surface"
298  SequentialAnimation {
299  PropertyAction { target: surfaceContainer
300  property: "opacity"; value: 0.0 }
301  PropertyAction { target: surfaceContainer
302  property: "visible"; value: true }
303  UbuntuNumberAnimation { target: surfaceContainer; property: "opacity";
304  from: 0.0; to: 1.0
305  duration: UbuntuAnimation.BriskDuration }
306  ScriptAction { script: {
307  splashLoader.active = false;
308  surfaceIsOldTimer.start();
309  } }
310  }
311  },
312  Transition {
313  from: "surface"; to: "splashScreen"
314  SequentialAnimation {
315  ScriptAction { script: {
316  surfaceIsOldTimer.stop();
317  d.surfaceOldEnoughToBeResized = false;
318  splashLoader.active = true;
319  surfaceContainer.visible = true;
320  } }
321  UbuntuNumberAnimation { target: splashLoader; property: "opacity";
322  from: 0.0; to: 1.0
323  duration: UbuntuAnimation.BriskDuration }
324  PropertyAction { target: surfaceContainer
325  property: "visible"; value: false }
326  }
327  },
328  Transition {
329  from: "surface"; to: "screenshot"
330  SequentialAnimation {
331  ScriptAction { script: {
332  surfaceIsOldTimer.stop();
333  d.surfaceOldEnoughToBeResized = false;
334  screenshotImage.visible = true;
335  } }
336  UbuntuNumberAnimation { target: screenshotImage; property: "opacity";
337  from: 0.0; to: 1.0
338  duration: UbuntuAnimation.BriskDuration }
339  ScriptAction { script: {
340  surfaceContainer.visible = false;
341  surfaceContainer.surface = null;
342  d.hadSurface = true;
343  } }
344  }
345  },
346  Transition {
347  from: "screenshot"; to: "surface"
348  SequentialAnimation {
349  PropertyAction { target: surfaceContainer
350  property: "visible"; value: true }
351  UbuntuNumberAnimation { target: screenshotImage; property: "opacity";
352  from: 1.0; to: 0.0
353  duration: UbuntuAnimation.BriskDuration }
354  ScriptAction { script: {
355  screenshotImage.visible = false;
356  screenshotImage.source = "";
357  surfaceIsOldTimer.start();
358  } }
359  }
360  },
361  Transition {
362  from: "splashScreen"; to: "screenshot"
363  SequentialAnimation {
364  PropertyAction { target: screenshotImage
365  property: "visible"; value: true }
366  UbuntuNumberAnimation { target: screenshotImage; property: "opacity";
367  from: 0.0; to: 1.0
368  duration: UbuntuAnimation.BriskDuration }
369  PropertyAction { target: splashLoader; property: "active"; value: false }
370  }
371  },
372  Transition {
373  from: "surface"; to: "void"
374  ScriptAction { script: {
375  surfaceIsOldTimer.stop();
376  d.surfaceOldEnoughToBeResized = false;
377  surfaceContainer.visible = false;
378  } }
379  },
380  Transition {
381  from: "void"; to: "surface"
382  SequentialAnimation {
383  PropertyAction { target: surfaceContainer; property: "opacity"; value: 0.0 }
384  PropertyAction { target: surfaceContainer; property: "visible"; value: true }
385  UbuntuNumberAnimation { target: surfaceContainer; property: "opacity";
386  from: 0.0; to: 1.0
387  duration: UbuntuAnimation.BriskDuration }
388  ScriptAction { script: {
389  surfaceIsOldTimer.start();
390  } }
391  }
392  },
393  Transition {
394  to: "closed"
395  SequentialAnimation {
396  ScriptAction { script: {
397  surfaceContainer.visible = false;
398  surfaceContainer.surface = null;
399  d.hadSurface = true;
400  } }
401  }
402  }
403  ]
404  }
405 }