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