Unity 8
ApplicationWindow.qml
1 /*
2  * Copyright 2014-2015 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: sessionContainer.implicitWidth
24  implicitHeight: sessionContainer.implicitHeight
25 
26  // to be read from outside
27  readonly property bool fullscreen: application ? application.fullscreen : false
28  property alias interactive: sessionContainer.interactive
29  property bool orientationChangesEnabled: d.supportsSurfaceResize ? d.surfaceOldEnoughToBeResized : true
30  readonly property string title: sessionContainer.surface && sessionContainer.surface.name !== "" ?
31  sessionContainer.surface.name : d.name
32 
33  // to be set from outside
34  property QtObject application
35  property int surfaceOrientationAngle
36  property alias resizeSurface: sessionContainer.resizeSurface
37 
38  QtObject {
39  id: d
40 
41  // helpers so that we don't have to check for the existence of an application everywhere
42  // (in order to avoid breaking qml binding due to a javascript exception)
43  readonly property string name: root.application ? root.application.name : ""
44  readonly property url icon: root.application ? root.application.icon : ""
45  readonly property int applicationState: root.application ? root.application.state : -1
46  readonly property string splashTitle: root.application ? root.application.splashTitle : ""
47  readonly property url splashImage: root.application ? root.application.splashImage : ""
48  readonly property bool splashShowHeader: root.application ? root.application.splashShowHeader : true
49  readonly property color splashColor: root.application ? root.application.splashColor : "#00000000"
50  readonly property color splashColorHeader: root.application ? root.application.splashColorHeader : "#00000000"
51  readonly property color splashColorFooter: root.application ? root.application.splashColorFooter : "#00000000"
52  readonly property url defaultScreenshot: (root.application && root.application.defaultScreenshot !== undefined) ? root.application.defaultScreenshot : ""
53 
54  // Whether the Application had a surface before but lost it.
55  property bool hadSurface: sessionContainer.surfaceContainer.hadSurface
56 
57  property bool needToTakeScreenshot:
58  ((sessionContainer.surface && d.surfaceInitialized) || d.hadSurface)
59  && screenshotImage.status === Image.Null
60  && d.applicationState === ApplicationInfoInterface.Stopped
61  onNeedToTakeScreenshotChanged: {
62  if (needToTakeScreenshot) {
63  screenshotImage.take();
64  }
65  }
66 
67  //FIXME - this is a hack to avoid the first few rendered frames as they
68  // might show the UI accommodating due to surface resizes on startup.
69  // Remove this when possible
70  property bool surfaceInitialized: false
71 
72  property bool supportsSurfaceResize:
73  application &&
74  ((application.supportedOrientations & Qt.PortraitOrientation)
75  || (application.supportedOrientations & Qt.InvertedPortraitOrientation))
76  &&
77  ((application.supportedOrientations & Qt.LandscapeOrientation)
78  || (application.supportedOrientations & Qt.InvertedLandscapeOrientation))
79 
80  property bool surfaceOldEnoughToBeResized: false
81  }
82 
83  Timer {
84  id: surfaceInitTimer
85  interval: 100
86  onTriggered: { if (sessionContainer.surface) {d.surfaceInitialized = true;} }
87  }
88 
89  Timer {
90  id: surfaceIsOldTimer
91  interval: 1000
92  onTriggered: { if (stateGroup.state === "surface") { d.surfaceOldEnoughToBeResized = true; } }
93  }
94 
95  Image {
96  id: screenshotImage
97  objectName: "screenshotImage"
98  source: d.defaultScreenshot
99  anchors.fill: parent
100  antialiasing: !root.interactive
101 
102  function take() {
103  // Save memory by using a half-resolution (thus quarter size) screenshot.
104  // Do not make this a binding, we can only take the screenshot once!
105  sourceSize.width = root.width / 2;
106  sourceSize.height = root.height / 2;
107 
108  // Format: "image://application/$APP_ID/$CURRENT_TIME_MS"
109  // eg: "image://application/calculator-app/123456"
110  var timeMs = new Date().getTime();
111  source = "image://application/" + root.application.appId + "/" + timeMs;
112  }
113  }
114 
115  Loader {
116  id: splashLoader
117  visible: active
118  active: false
119  anchors.fill: parent
120  sourceComponent: Component {
121  Splash {
122  id: splash
123  title: d.splashTitle ? d.splashTitle : d.name
124  imageSource: d.splashImage
125  icon: d.icon
126  showHeader: d.splashShowHeader
127  backgroundColor: d.splashColor
128  headerColor: d.splashColorHeader
129  footerColor: d.splashColorFooter
130  }
131  }
132  }
133 
134  SessionContainer {
135  id: sessionContainer
136  // A fake application might not even have a session property.
137  session: application && application.session ? application.session : null
138  anchors.fill: parent
139 
140  surfaceOrientationAngle: application && application.rotatesWindowContents ? root.surfaceOrientationAngle : 0
141 
142  onSurfaceChanged: {
143  if (sessionContainer.surface) {
144  surfaceInitTimer.start();
145  } else {
146  d.surfaceInitialized = false;
147  }
148  }
149 
150  focus: true
151  }
152 
153  StateGroup {
154  id: stateGroup
155  objectName: "applicationWindowStateGroup"
156  states: [
157  State {
158  name: "void"
159  when:
160  d.hadSurface && (!sessionContainer.surface || !d.surfaceInitialized)
161  &&
162  screenshotImage.status !== Image.Ready
163  },
164  State {
165  name: "splashScreen"
166  when:
167  !d.hadSurface && (!sessionContainer.surface || !d.surfaceInitialized)
168  &&
169  screenshotImage.status !== Image.Ready
170  },
171  State {
172  name: "surface"
173  when:
174  (sessionContainer.surface && d.surfaceInitialized)
175  &&
176  (d.applicationState !== ApplicationInfoInterface.Stopped
177  || screenshotImage.status !== Image.Ready)
178  },
179  State {
180  name: "screenshot"
181  when:
182  screenshotImage.status === Image.Ready
183  &&
184  (d.applicationState === ApplicationInfoInterface.Stopped
185  || !sessionContainer.surface || !d.surfaceInitialized)
186  }
187  ]
188 
189  transitions: [
190  Transition {
191  from: ""; to: "splashScreen"
192  PropertyAction { target: splashLoader; property: "active"; value: true }
193  PropertyAction { target: sessionContainer.surfaceContainer
194  property: "visible"; value: false }
195  },
196  Transition {
197  from: "splashScreen"; to: "surface"
198  SequentialAnimation {
199  PropertyAction { target: sessionContainer.surfaceContainer
200  property: "opacity"; value: 0.0 }
201  PropertyAction { target: sessionContainer.surfaceContainer
202  property: "visible"; value: true }
203  UbuntuNumberAnimation { target: sessionContainer.surfaceContainer; property: "opacity";
204  from: 0.0; to: 1.0
205  duration: UbuntuAnimation.BriskDuration }
206  ScriptAction { script: {
207  splashLoader.active = false;
208  surfaceIsOldTimer.start();
209  } }
210  }
211  },
212  Transition {
213  from: "surface"; to: "splashScreen"
214  SequentialAnimation {
215  ScriptAction { script: {
216  surfaceIsOldTimer.stop();
217  d.surfaceOldEnoughToBeResized = false;
218  splashLoader.active = true;
219  sessionContainer.surfaceContainer.visible = true;
220  } }
221  UbuntuNumberAnimation { target: splashLoader; property: "opacity";
222  from: 0.0; to: 1.0
223  duration: UbuntuAnimation.BriskDuration }
224  PropertyAction { target: sessionContainer.surfaceContainer
225  property: "visible"; value: false }
226  }
227  },
228  Transition {
229  from: "surface"; to: "screenshot"
230  SequentialAnimation {
231  ScriptAction { script: {
232  surfaceIsOldTimer.stop();
233  d.surfaceOldEnoughToBeResized = false;
234  screenshotImage.visible = true;
235  } }
236  UbuntuNumberAnimation { target: screenshotImage; property: "opacity";
237  from: 0.0; to: 1.0
238  duration: UbuntuAnimation.BriskDuration }
239  ScriptAction { script: {
240  sessionContainer.surfaceContainer.visible = false;
241  if (sessionContainer.session) { sessionContainer.session.release(); }
242  } }
243  }
244  },
245  Transition {
246  from: "screenshot"; to: "surface"
247  SequentialAnimation {
248  PropertyAction { target: sessionContainer.surfaceContainer
249  property: "visible"; value: true }
250  UbuntuNumberAnimation { target: screenshotImage; property: "opacity";
251  from: 1.0; to: 0.0
252  duration: UbuntuAnimation.BriskDuration }
253  ScriptAction { script: {
254  screenshotImage.visible = false;
255  screenshotImage.source = "";
256  surfaceIsOldTimer.start();
257  } }
258  }
259  },
260  Transition {
261  from: "splashScreen"; to: "screenshot"
262  SequentialAnimation {
263  PropertyAction { target: screenshotImage
264  property: "visible"; value: true }
265  UbuntuNumberAnimation { target: screenshotImage; property: "opacity";
266  from: 0.0; to: 1.0
267  duration: UbuntuAnimation.BriskDuration }
268  PropertyAction { target: splashLoader; property: "active"; value: false }
269  }
270  },
271  Transition {
272  from: "surface"; to: "void"
273  ScriptAction { script: {
274  surfaceIsOldTimer.stop();
275  d.surfaceOldEnoughToBeResized = false;
276  sessionContainer.surfaceContainer.visible = false;
277  if (sessionContainer.session) { sessionContainer.session.release(); }
278  } }
279  },
280  Transition {
281  from: "void"; to: "surface"
282  SequentialAnimation {
283  PropertyAction { target: sessionContainer.surfaceContainer; property: "opacity"; value: 0.0 }
284  PropertyAction { target: sessionContainer.surfaceContainer; property: "visible"; value: true }
285  UbuntuNumberAnimation { target: sessionContainer.surfaceContainer; property: "opacity";
286  from: 0.0; to: 1.0
287  duration: UbuntuAnimation.BriskDuration }
288  ScriptAction { script: {
289  surfaceIsOldTimer.start();
290  } }
291  }
292  }
293  ]
294  }
295 
296 }