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