Lomiri
ApplicationWindow.qml
1/*
2 * Copyright 2014-2016 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
17import QtQuick 2.12
18import Lomiri.Components 1.3
19import QtMir.Application 0.1
20
21FocusScope {
22 id: root
23 implicitWidth: requestedWidth
24 implicitHeight: requestedHeight
25
26 // to be read from outside
27 property alias interactive: surfaceContainer.interactive
28 property bool orientationChangesEnabled: d.supportsSurfaceResize ? d.surfaceOldEnoughToBeResized : true
29 readonly property string title: surface && surface.name !== "" ? surface.name : d.name
30 readonly property QtObject focusedSurface: d.focusedSurface.surface
31 readonly property alias surfaceInitialized: d.surfaceInitialized
32
33 // to be set from outside
34 property QtObject surface
35 property QtObject application
36 property int surfaceOrientationAngle
37 property int requestedWidth: -1
38 property int requestedHeight: -1
39 property real splashRotation: 0
40
41 readonly property int minimumWidth: surface ? surface.minimumWidth : 0
42 readonly property int minimumHeight: surface ? surface.minimumHeight : 0
43 readonly property int maximumWidth: surface ? surface.maximumWidth : 0
44 readonly property int maximumHeight: surface ? surface.maximumHeight : 0
45 readonly property int widthIncrement: surface ? surface.widthIncrement : 0
46 readonly property int heightIncrement: surface ? surface.heightIncrement : 0
47
48 Connections {
49 target: surface
50 onReady: {
51 d.surfaceInitialized = true;
52 d.hadSurface = true;
53 d.surfaceOldEnoughToBeResized = true;
54 }
55 }
56
57 Component.onCompleted: {
58
59 if (surface && surface.live && surface.isReady) {
60 d.surfaceInitialized = true;
61 d.hadSurface = true;
62 d.surfaceOldEnoughToBeResized = true;
63 }
64 }
65
66 onSurfaceChanged: {
67 // The order in which the instructions are executed here matters, to that the correct state
68 // transitions in stateGroup take place.
69 // More specifically, the moment surfaceContainer.surface gets updated relative to the
70 // other instructions.
71 if (surface) {
72 surfaceContainer.surface = surface;
73 } else {
74 d.surfaceInitialized = false;
75 surfaceContainer.surface = null;
76 }
77 }
78
79 QtObject {
80 id: d
81
82 // helpers so that we don't have to check for the existence of an application everywhere
83 // (in order to avoid breaking qml binding due to a javascript exception)
84 readonly property string name: root.application ? root.application.name : ""
85 readonly property url icon: root.application ? root.application.icon : ""
86 readonly property int applicationState: root.application ? root.application.state : -1
87 readonly property string splashTitle: root.application ? root.application.splashTitle : ""
88 readonly property url splashImage: root.application ? root.application.splashImage : ""
89 readonly property bool splashShowHeader: root.application ? root.application.splashShowHeader : true
90 readonly property color splashColor: root.application ? root.application.splashColor : "#00000000"
91 readonly property color splashColorHeader: root.application ? root.application.splashColorHeader : "#00000000"
92 readonly property color splashColorFooter: root.application ? root.application.splashColorFooter : "#00000000"
93
94 // Whether the Application had a surface before but lost it.
95 property bool hadSurface: false
96
97 //FIXME - this is a hack to avoid the first few rendered frames as they
98 // might show the UI accommodating due to surface resizes on startup.
99 // Remove this when possible
100 property bool surfaceInitialized: false
101
102 readonly property bool supportsSurfaceResize:
103 application &&
104 ((application.supportedOrientations & Qt.PortraitOrientation)
105 || (application.supportedOrientations & Qt.InvertedPortraitOrientation))
106 &&
107 ((application.supportedOrientations & Qt.LandscapeOrientation)
108 || (application.supportedOrientations & Qt.InvertedLandscapeOrientation))
109
110 property bool surfaceOldEnoughToBeResized: false
111
112 property Item focusedSurface: promptSurfacesRepeater.count === 0 ? surfaceContainer
113 : promptSurfacesRepeater.first
114 onFocusedSurfaceChanged: {
115 if (focusedSurface) {
116 focusedSurface.focus = true;
117 }
118 }
119 }
120
121 Binding {
122 target: root.application
123 property: "initialSurfaceSize"
124 value: Qt.size(root.requestedWidth, root.requestedHeight)
125 }
126
127 Timer {
128 id: surfaceInitTimer
129 interval: 100
130 repeat: true
131 running: root.surface && !d.surfaceInitialized
132 onTriggered: {
133 if (root.surface && root.surface.isReady) {
134 d.surfaceInitialized = true;
135 d.hadSurface = true;
136 d.surfaceOldEnoughToBeResized = true;
137 }
138 }
139 }
140
141 Loader {
142 id: splashLoader
143 visible: active
144 active: false
145 anchors.fill: parent
146 z: 1
147 sourceComponent: Component {
148 Splash {
149 id: splash
150 title: d.splashTitle ? d.splashTitle : d.name
151 imageSource: d.splashImage
152 icon: d.icon
153 showHeader: d.splashShowHeader
154 backgroundColor: d.splashColor
155 headerColor: d.splashColorHeader
156 footerColor: d.splashColorFooter
157
158 rotation: root.splashRotation
159 anchors.centerIn: parent
160 width: rotation == 0 || rotation == 180 ? root.width : root.height
161 height: rotation == 0 || rotation == 180 ? root.height : root.width
162 }
163 }
164 }
165
166 SurfaceContainer {
167 id: surfaceContainer
168 anchors.fill: parent
169 opacity: splashLoader.active ? 0.7 : 1.0
170 z: splashLoader.z + 1
171 requestedWidth: root.requestedWidth
172 requestedHeight: root.requestedHeight
173 surfaceOrientationAngle: application && application.rotatesWindowContents ? root.surfaceOrientationAngle : 0
174
175 Behavior on opacity {
176 NumberAnimation { }
177 }
178 }
179
180 Repeater {
181 id: promptSurfacesRepeater
182 objectName: "promptSurfacesRepeater"
183 // show only along with the top-most application surface
184 model: {
185 if (root.application && (
186 root.surface === root.application.surfaceList.first ||
187 root.application.surfaceList.count === 0)) {
188 return root.application.promptSurfaceList;
189 } else {
190 return null;
191 }
192 }
193 delegate: SurfaceContainer {
194 id: promptSurfaceContainer
195 interactive: index === 0 && root.interactive
196 surface: model.surface
197 width: root.width
198 height: root.height
199 requestedWidth: root.requestedWidth
200 requestedHeight: root.requestedHeight
201 isPromptSurface: true
202 z: surfaceContainer.z + (promptSurfacesRepeater.count - index)
203 property int index: model.index
204 onIndexChanged: updateFirst()
205 Component.onCompleted: updateFirst()
206 function updateFirst() {
207 if (index === 0) {
208 promptSurfacesRepeater.first = promptSurfaceContainer;
209 }
210 }
211 }
212 onCountChanged: {
213 if (count === 0) {
214 first = null;
215 }
216 }
217 property Item first: null
218 }
219
220 StateGroup {
221 id: stateGroup
222 objectName: "applicationWindowStateGroup"
223 states: [
224 State {
225 name: "surface"
226 when: (root.surface && d.surfaceInitialized)
227 PropertyChanges { target: splashLoader; active: false }
228 },
229 State {
230 name: "splash"
231 when: (!root.surface || !d.surfaceInitialized) || !root.surface.live || d.hadSurface
232 PropertyChanges { target: splashLoader; active: true }
233 }
234 ]
235 }
236}