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  function forceSurfaceActiveFocusIfReady() {
65  if (sessionContainer.surface !== null &&
66  sessionContainer.surface.focus &&
67  sessionContainer.surface.parent === sessionContainer.surfaceContainer &&
68  sessionContainer.surface.enabled) {
69  sessionContainer.surface.forceActiveFocus();
70  }
71  }
72  }
73 
74  Timer {
75  id: surfaceInitTimer
76  interval: 100
77  onTriggered: { if (sessionContainer.surface) {d.surfaceInitialized = true;} }
78  }
79 
80  Connections {
81  target: sessionContainer.surface
82  // FIXME: I would rather not need to do this, but currently it doesn't get
83  // active focus without it and I don't know why.
84  onFocusChanged: d.forceSurfaceActiveFocusIfReady();
85  onParentChanged: d.forceSurfaceActiveFocusIfReady();
86  onEnabledChanged: d.forceSurfaceActiveFocusIfReady();
87  }
88 
89  Image {
90  id: screenshotImage
91  objectName: "screenshotImage"
92  source: ""
93  anchors.fill: parent
94 
95  function take() {
96  // Format: "image://application/$APP_ID/$CURRENT_TIME_MS"
97  // eg: "image://application/calculator-app/123456"
98  var timeMs = new Date().getTime();
99  source = "image://application/" + root.application.appId + "/" + timeMs;
100  }
101 
102  // Save memory by using a half-resolution (thus quarter size) screenshot
103  sourceSize.width: root.width / 2
104  sourceSize.height: root.height / 2
105  }
106 
107  Loader {
108  id: splashLoader
109  visible: active
110  active: false
111  anchors.fill: parent
112  sourceComponent: Component {
113  Splash {
114  id: splash
115  title: d.splashTitle ? d.splashTitle : d.name
116  imageSource: d.splashImage
117  icon: d.icon
118  showHeader: d.splashShowHeader
119  backgroundColor: d.splashColor
120  headerColor: d.splashColorHeader
121  footerColor: d.splashColorFooter
122  }
123  }
124  }
125 
126  SessionContainer {
127  id: sessionContainer
128  session: application ? application.session : null
129  anchors.fill: parent
130  orientation: root.orientation
131 
132  onSurfaceChanged: {
133  if (sessionContainer.surface) {
134  surfaceInitTimer.start();
135  d.forceSurfaceActiveFocusIfReady();
136  } else {
137  d.surfaceInitialized = false;
138  }
139  }
140  }
141 
142  StateGroup {
143  objectName: "applicationWindowStateGroup"
144  states: [
145  State {
146  name: "void"
147  when:
148  d.hadSurface && (!sessionContainer.surface || !d.surfaceInitialized)
149  &&
150  screenshotImage.status !== Image.Ready
151  },
152  State {
153  name: "splashScreen"
154  when:
155  !d.hadSurface && (!sessionContainer.surface || !d.surfaceInitialized)
156  &&
157  screenshotImage.status !== Image.Ready
158  },
159  State {
160  name: "surface"
161  when:
162  (sessionContainer.surface && d.surfaceInitialized)
163  &&
164  (d.applicationState !== ApplicationInfoInterface.Stopped
165  || screenshotImage.status !== Image.Ready)
166  },
167  State {
168  name: "screenshot"
169  when:
170  screenshotImage.status === Image.Ready
171  &&
172  (d.applicationState === ApplicationInfoInterface.Stopped
173  || !sessionContainer.surface || !d.surfaceInitialized)
174  }
175  ]
176 
177  transitions: [
178  Transition {
179  from: ""; to: "splashScreen"
180  PropertyAction { target: splashLoader; property: "active"; value: true }
181  PropertyAction { target: sessionContainer.surfaceContainer
182  property: "visible"; value: false }
183  },
184  Transition {
185  from: "splashScreen"; to: "surface"
186  SequentialAnimation {
187  PropertyAction { target: sessionContainer.surfaceContainer
188  property: "opacity"; value: 0.0 }
189  PropertyAction { target: sessionContainer.surfaceContainer
190  property: "visible"; value: true }
191  UbuntuNumberAnimation { target: sessionContainer.surfaceContainer; property: "opacity";
192  from: 0.0; to: 1.0
193  duration: UbuntuAnimation.BriskDuration }
194  PropertyAction { target: splashLoader; property: "active"; value: false }
195  }
196  },
197  Transition {
198  from: "surface"; to: "splashScreen"
199  SequentialAnimation {
200  PropertyAction { target: splashLoader; property: "active"; value: true }
201  PropertyAction { target: sessionContainer.surfaceContainer
202  property: "visible"; value: true }
203  UbuntuNumberAnimation { target: splashLoader; property: "opacity";
204  from: 0.0; to: 1.0
205  duration: UbuntuAnimation.BriskDuration }
206  PropertyAction { target: sessionContainer.surfaceContainer
207  property: "visible"; value: false }
208  }
209  },
210  Transition {
211  from: "surface"; to: "screenshot"
212  SequentialAnimation {
213  PropertyAction { target: screenshotImage
214  property: "visible"; value: true }
215  UbuntuNumberAnimation { target: screenshotImage; property: "opacity";
216  from: 0.0; to: 1.0
217  duration: UbuntuAnimation.BriskDuration }
218  PropertyAction { target: sessionContainer.surfaceContainer
219  property: "visible"; value: false }
220  ScriptAction { script: { if (sessionContainer.session) { sessionContainer.session.release(); } } }
221  }
222  },
223  Transition {
224  from: "screenshot"; to: "surface"
225  SequentialAnimation {
226  PropertyAction { target: sessionContainer.surfaceContainer
227  property: "visible"; value: true }
228  UbuntuNumberAnimation { target: screenshotImage; property: "opacity";
229  from: 1.0; to: 0.0
230  duration: UbuntuAnimation.BriskDuration }
231  PropertyAction { target: screenshotImage; property: "visible"; value: false }
232  PropertyAction { target: screenshotImage; property: "source"; value: "" }
233  }
234  },
235  Transition {
236  from: "surface"; to: "void"
237  SequentialAnimation {
238  PropertyAction { target: sessionContainer.surfaceContainer; property: "visible"; value: false }
239  ScriptAction { script: { if (sessionContainer.session) { sessionContainer.session.release(); } } }
240  }
241  },
242  Transition {
243  from: "void"; to: "surface"
244  SequentialAnimation {
245  PropertyAction { target: sessionContainer.surfaceContainer; property: "opacity"; value: 0.0 }
246  PropertyAction { target: sessionContainer.surfaceContainer; property: "visible"; value: true }
247  UbuntuNumberAnimation { target: sessionContainer.surfaceContainer; property: "opacity";
248  from: 0.0; to: 1.0
249  duration: UbuntuAnimation.BriskDuration }
250  }
251  }
252  ]
253  }
254 
255 }