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