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