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