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