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