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