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