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