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 
24  // to be read from outside
25  readonly property bool fullscreen: application ? application.fullscreen : false
26  property alias interactive: sessionContainer.interactive
27  property bool orientationChangesEnabled: d.supportsSurfaceResize ? d.surfaceOldEnoughToBeResized : true
28 
29  // to be set from outside
30  property QtObject application
31  property int surfaceOrientationAngle
32 
33  QtObject {
34  id: d
35 
36  // helpers so that we don't have to check for the existence of an application everywhere
37  // (in order to avoid breaking qml binding due to a javascript exception)
38  readonly property string name: root.application ? root.application.name : ""
39  readonly property url icon: root.application ? root.application.icon : ""
40  readonly property int applicationState: root.application ? root.application.state : -1
41  readonly property string splashTitle: root.application ? root.application.splashTitle : ""
42  readonly property url splashImage: root.application ? root.application.splashImage : ""
43  readonly property bool splashShowHeader: root.application ? root.application.splashShowHeader : true
44  readonly property color splashColor: root.application ? root.application.splashColor : "#00000000"
45  readonly property color splashColorHeader: root.application ? root.application.splashColorHeader : "#00000000"
46  readonly property color splashColorFooter: root.application ? root.application.splashColorFooter : "#00000000"
47  readonly property url defaultScreenshot: (root.application && root.application.defaultScreenshot !== undefined) ? root.application.defaultScreenshot : ""
48 
49  // Whether the Application had a surface before but lost it.
50  property bool hadSurface: sessionContainer.surfaceContainer.hadSurface
51 
52  property bool needToTakeScreenshot:
53  ((sessionContainer.surface && d.surfaceInitialized) || d.hadSurface)
54  && screenshotImage.status === Image.Null
55  && d.applicationState === ApplicationInfoInterface.Stopped
56  onNeedToTakeScreenshotChanged: {
57  if (needToTakeScreenshot) {
58  screenshotImage.take();
59  }
60  }
61 
62  //FIXME - this is a hack to avoid the first few rendered frames as they
63  // might show the UI accommodating due to surface resizes on startup.
64  // Remove this when possible
65  property bool surfaceInitialized: false
66 
67  property bool supportsSurfaceResize:
68  application &&
69  ((application.supportedOrientations & Qt.PortraitOrientation)
70  || (application.supportedOrientations & Qt.InvertedPortraitOrientation))
71  &&
72  ((application.supportedOrientations & Qt.LandscapeOrientation)
73  || (application.supportedOrientations & Qt.InvertedLandscapeOrientation))
74 
75  property bool surfaceOldEnoughToBeResized: false
76  }
77 
78  Timer {
79  id: surfaceInitTimer
80  interval: 100
81  onTriggered: { if (sessionContainer.surface) {d.surfaceInitialized = true;} }
82  }
83 
84  Timer {
85  id: surfaceIsOldTimer
86  interval: 1000
87  onTriggered: { if (stateGroup.state === "surface") { d.surfaceOldEnoughToBeResized = true; } }
88  }
89 
90  Image {
91  id: screenshotImage
92  objectName: "screenshotImage"
93  source: d.defaultScreenshot
94  anchors.fill: parent
95  antialiasing: !root.interactive
96 
97  function take() {
98  // Save memory by using a half-resolution (thus quarter size) screenshot.
99  // Do not make this a binding, we can only take the screenshot once!
100  sourceSize.width = root.width / 2;
101  sourceSize.height = root.height / 2;
102 
103  // Format: "image://application/$APP_ID/$CURRENT_TIME_MS"
104  // eg: "image://application/calculator-app/123456"
105  var timeMs = new Date().getTime();
106  source = "image://application/" + root.application.appId + "/" + timeMs;
107  }
108  }
109 
110  Loader {
111  id: splashLoader
112  visible: active
113  active: false
114  anchors.fill: parent
115  sourceComponent: Component {
116  Splash {
117  id: splash
118  title: d.splashTitle ? d.splashTitle : d.name
119  imageSource: d.splashImage
120  icon: d.icon
121  showHeader: d.splashShowHeader
122  backgroundColor: d.splashColor
123  headerColor: d.splashColorHeader
124  footerColor: d.splashColorFooter
125  }
126  }
127  }
128 
129  SessionContainer {
130  id: sessionContainer
131  // A fake application might not even have a session property.
132  session: application && application.session ? application.session : null
133  anchors.fill: parent
134 
135  surfaceOrientationAngle: application && application.rotatesWindowContents ? root.surfaceOrientationAngle : 0
136 
137  onSurfaceChanged: {
138  if (sessionContainer.surface) {
139  surfaceInitTimer.start();
140  if (ApplicationManager.focusedApplicationId == application.appId) {
141  root.forceActiveFocus();
142  }
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 }