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