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