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