Lomiri
Loading...
Searching...
No Matches
OrientedShell.qml
1/*
2 * Copyright (C) 2015 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU 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 General Public License for more details.
12 *
13 * You should have received a copy of the GNU 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 QtQuick.Window 2.2 as QtQuickWindow
20import Lomiri.InputInfo 0.1
21import Lomiri.Session 0.1
22import WindowManager 1.0
23import Utils 0.1
24import GSettings 1.0
25import "Components"
26import "Rotation"
27// Workaround https://bugs.launchpad.net/lomiri/+source/lomiri/+bug/1473471
28import Lomiri.Components 1.3
29
30Item {
31 id: root
32
33 implicitWidth: units.gu(40)
34 implicitHeight: units.gu(71)
35
36 property alias deviceConfiguration: _deviceConfiguration
37 property alias orientations: d.orientations
38 property bool lightIndicators: false
39
40 // The active cover panel of a foldable is mounted at an angle to the
41 // orientation sensor frame, so its native orientation is offset from
42 // what its pixel geometry implies (deviceConfiguration.coverOrientationAngle).
43 property bool coverActive: false
44
45 property var screen: null
46 Connections {
47 target: screen
48 function onFormFactorChanged() { calculateUsageMode(); }
49 }
50
51 onWidthChanged: calculateUsageMode();
52 property var overrideDeviceName: Screens.count > 1 ? "desktop" : false
53
54 DeviceConfiguration {
55 id: _deviceConfiguration
56
57 // Override for convergence to set scale etc for second monitor
58 overrideName: root.overrideDeviceName
59 }
60
61 Item {
62 id: d
63
64 property Orientations orientations: Orientations {
65 id: orientations
66 // NB: native and primary orientations here don't map exactly to their QScreen counterparts
67 native_: root.coverActive
68 ? root.rotateOrientation(root.width > root.height ? Qt.LandscapeOrientation : Qt.PortraitOrientation,
69 -deviceConfiguration.coverOrientationAngle)
70 : (root.width > root.height ? Qt.LandscapeOrientation : Qt.PortraitOrientation)
71
72 primary: deviceConfiguration.primaryOrientation == deviceConfiguration.useNativeOrientation
73 ? native_ : deviceConfiguration.primaryOrientation
74
75 landscape: deviceConfiguration.landscapeOrientation
76 invertedLandscape: deviceConfiguration.invertedLandscapeOrientation
77 portrait: deviceConfiguration.portraitOrientation
78 invertedPortrait: deviceConfiguration.invertedPortraitOrientation
79 }
80 }
81
82 GSettings {
83 id: lomiriSettings
84 schema.id: "com.lomiri.Shell"
85 }
86
87 GSettings {
88 id: oskSettings
89 objectName: "oskSettings"
90 schema.id: "com.lomiri.keyboard.maliit"
91 }
92
93 property int physicalOrientation: QtQuickWindow.Screen.orientation
94 property bool orientationLocked: OrientationLock.enabled
95 property var orientationLock: OrientationLock
96
97 InputDeviceModel {
98 id: miceModel
99 deviceFilter: InputInfo.Mouse
100 property int oldCount: 0
101 }
102
103 InputDeviceModel {
104 id: touchPadModel
105 deviceFilter: InputInfo.TouchPad
106 property int oldCount: 0
107 }
108
109 InputDeviceModel {
110 id: keyboardsModel
111 deviceFilter: InputInfo.Keyboard
112 onDeviceAdded: forceOSKEnabled = autopilotDevicePresent();
113 onDeviceRemoved: forceOSKEnabled = autopilotDevicePresent();
114 }
115
116 InputDeviceModel {
117 id: touchScreensModel
118 deviceFilter: InputInfo.TouchScreen
119 }
120
121 Binding {
122 target: QuickUtils
123 property: "keyboardAttached"
124 value: keyboardsModel.count > 0
125 restoreMode: Binding.RestoreBinding
126 }
127
128 readonly property int pointerInputDevices: miceModel.count + touchPadModel.count
129 onPointerInputDevicesChanged: calculateUsageMode()
130
131 function calculateUsageMode() {
132 if (lomiriSettings.usageMode === undefined)
133 return; // gsettings isn't loaded yet, we'll try again in Component.onCompleted
134
135 if (deviceConfiguration.foldable) {
136 // A foldable is a phone that happens to have two panels; its
137 // touchscreens expose touchpad input nodes, which would otherwise
138 // flip the shell into windowed/desktop mode. Keep it staged.
139 if (lomiriSettings.usageMode !== "Staged")
140 lomiriSettings.usageMode = "Staged";
141 return;
142 }
143
144 console.log("Calculating new usage mode. Pointer devices:", pointerInputDevices, "current mode:", lomiriSettings.usageMode, "old device count", miceModel.oldCount + touchPadModel.oldCount, "root width:", root.width, "height:", root.height)
145 if (lomiriSettings.usageMode === "Windowed") {
146 if (Math.min(root.width, root.height) > units.gu(60)) {
147 if (pointerInputDevices === 0) {
148 // All pointer devices have been unplugged. Move to staged.
149 lomiriSettings.usageMode = "Staged";
150 }
151 } else {
152 // The display is not large enough, use staged.
153 lomiriSettings.usageMode = "Staged";
154 }
155 } else {
156 if (Math.min(root.width, root.height) > units.gu(60)) {
157 if (pointerInputDevices > 0 && pointerInputDevices > miceModel.oldCount + touchPadModel.oldCount) {
158 lomiriSettings.usageMode = "Windowed";
159 }
160 } else {
161 // Make sure we initialize to something sane
162 lomiriSettings.usageMode = "Staged";
163 }
164 }
165 miceModel.oldCount = miceModel.count;
166 touchPadModel.oldCount = touchPadModel.count;
167 }
168
169 /* FIXME: This exposes the NameRole as a work arround for lp:1542224.
170 * When QInputInfo exposes NameRole to QML, this should be removed.
171 */
172 property bool forceOSKEnabled: false
173 property var autopilotEmulatedDeviceNames: ["py-evdev-uinput"]
174 LomiriSortFilterProxyModel {
175 id: autopilotDevices
176 model: keyboardsModel
177 }
178
179 function autopilotDevicePresent() {
180 for(var i = 0; i < autopilotDevices.count; i++) {
181 var device = autopilotDevices.get(i);
182 if (autopilotEmulatedDeviceNames.indexOf(device.name) != -1) {
183 console.warn("Forcing the OSK to be enabled as there is an autopilot eumlated device present.")
184 return true;
185 }
186 }
187 return false;
188 }
189
190 property int orientation
191 onPhysicalOrientationChanged: {
192 if (!orientationLocked) {
193 orientation = physicalOrientation;
194 } else {
195 if (orientation !== physicalOrientation && !shell.showingGreeter) {
196 rotateButton.show()
197 } else {
198 rotateButton.hide()
199 }
200 }
201 }
202 onOrientationLockedChanged: {
203 if (orientationLocked) {
204 orientationLock.savedOrientation = physicalOrientation;
205 } else {
206 orientation = physicalOrientation;
207 }
208 }
209 Component.onCompleted: {
210 if (orientationLocked) {
211 orientation = orientationLock.savedOrientation;
212 }
213
214 calculateUsageMode();
215
216 // We need to manually update this on startup as the binding
217 // below doesn't seem to have any effect at that stage
218 oskSettings.disableHeight = !shell.oskEnabled || shell.usageScenario == "desktop"
219 }
220
221 Component.onDestruction: {
222 const from_workspaces = root.screen.workspaces;
223 const from_workspaces_size = from_workspaces.count;
224 for (var i = 0; i < from_workspaces_size; i++) {
225 const from = from_workspaces.get(i);
226 WorkspaceManager.destroyWorkspace(from);
227 }
228 }
229
230 // we must rotate to a supported orientation regardless of shell's preference
231 property bool orientationChangesEnabled:
232 (shell.orientation & supportedOrientations) === 0 ? true
233 : shell.orientationChangesEnabled
234
235 Binding {
236 target: oskSettings
237 restoreMode: Binding.RestoreBinding
238 property: "disableHeight"
239 value: !shell.oskEnabled || shell.usageScenario == "desktop"
240 }
241
242 Binding {
243 target: lomiriSettings
244 restoreMode: Binding.RestoreBinding
245 property: "oskSwitchVisible"
246 value: shell.hasKeyboard
247 }
248
249 readonly property int supportedOrientations: shell.supportedOrientations
250 & (deviceConfiguration.supportedOrientations == deviceConfiguration.useNativeOrientation
251 ? orientations.native_
252 : deviceConfiguration.supportedOrientations)
253
254 // During desktop mode switches back to phone mode Qt seems to swallow
255 // supported orientations by itself, not emitting them. Cause them to be emitted
256 // using the attached property here.
257 QtQuickWindow.Screen.orientationUpdateMask: supportedOrientations
258
259 property int acceptedOrientationAngle: {
260 if (orientation & supportedOrientations) {
261 return QtQuickWindow.Screen.angleBetween(orientations.native_, orientation);
262 } else if (shell.orientation & supportedOrientations) {
263 // stay where we are
264 return shell.orientationAngle;
265 } else if (angleToOrientation(shell.mainAppWindowOrientationAngle) & supportedOrientations) {
266 return shell.mainAppWindowOrientationAngle;
267 } else {
268 // rotate to some supported orientation as we can't stay where we currently are
269 // TODO: Choose the closest to the current one
270 if (supportedOrientations & Qt.PortraitOrientation) {
271 return QtQuickWindow.Screen.angleBetween(orientations.native_, Qt.PortraitOrientation);
272 } else if (supportedOrientations & Qt.LandscapeOrientation) {
273 return QtQuickWindow.Screen.angleBetween(orientations.native_, Qt.LandscapeOrientation);
274 } else if (supportedOrientations & Qt.InvertedPortraitOrientation) {
275 return QtQuickWindow.Screen.angleBetween(orientations.native_, Qt.InvertedPortraitOrientation);
276 } else if (supportedOrientations & Qt.InvertedLandscapeOrientation) {
277 return QtQuickWindow.Screen.angleBetween(orientations.native_, Qt.InvertedLandscapeOrientation);
278 } else {
279 // if all fails, fallback to primary orientation
280 return QtQuickWindow.Screen.angleBetween(orientations.native_, orientations.primary);
281 }
282 }
283 }
284
285 function rotateOrientation(orientation, angle) {
286 var seq = [Qt.PortraitOrientation, Qt.LandscapeOrientation,
287 Qt.InvertedPortraitOrientation, Qt.InvertedLandscapeOrientation];
288 var idx = seq.indexOf(orientation);
289 if (idx < 0)
290 return orientation;
291 return seq[((idx + Math.round(angle / 90)) % 4 + 4) % 4];
292 }
293
294 function angleToOrientation(angle) {
295 switch (angle) {
296 case 0:
297 return orientations.native_;
298 case 90:
299 return orientations.native_ === Qt.PortraitOrientation ? Qt.InvertedLandscapeOrientation
300 : Qt.PortraitOrientation;
301 case 180:
302 return orientations.native_ === Qt.PortraitOrientation ? Qt.InvertedPortraitOrientation
303 : Qt.InvertedLandscapeOrientation;
304 case 270:
305 return orientations.native_ === Qt.PortraitOrientation ? Qt.LandscapeOrientation
306 : Qt.InvertedPortraitOrientation;
307 default:
308 console.warn("angleToOrientation: Invalid orientation angle: " + angle);
309 return orientations.primary;
310 }
311 }
312
313 RotationStates {
314 id: rotationStates
315 objectName: "rotationStates"
316 orientedShell: root
317 shell: shell
318 shellCover: shellCover
319 shellSnapshot: shellSnapshot
320 }
321
322 Shell {
323 id: shell
324 objectName: "shell"
325 width: root.width
326 height: root.height
327 orientation: root.angleToOrientation(orientationAngle)
328 orientations: root.orientations
329 nativeWidth: root.width
330 nativeHeight: root.height
331 mode: applicationArguments.mode
332 hasMouse: pointerInputDevices > 0
333 hasKeyboard: keyboardsModel.count > 0
334 hasTouchscreen: touchScreensModel.count > 0
335 supportsMultiColorLed: deviceConfiguration.supportsMultiColorLed
336 lightIndicators: root.lightIndicators
337 oskEnabled: (!hasKeyboard && Screens.count === 1) ||
338 lomiriSettings.alwaysShowOsk || forceOSKEnabled
339
340 // Multiscreen support: in addition to judging by the device type, go by the screen type.
341 // This allows very flexible usecases beyond the typical "connect a phone to a monitor".
342 // Status quo setups:
343 // - phone + external monitor: virtual touchpad on the device
344 // - tablet + external monitor: dual-screen desktop
345 // - desktop: Has all the bells and whistles of a fully fledged PC/laptop shell
346 usageScenario: {
347 if (deviceConfiguration.foldable) return "phone";
348 if (lomiriSettings.usageMode === "Windowed") {
349 return "desktop";
350 } else if (deviceConfiguration.category === "phone") {
351 return "phone";
352 } else if (deviceConfiguration.category === "tablet") {
353 return "tablet";
354 } else {
355 if (screen.formFactor === Screen.Tablet) {
356 return "tablet";
357 } else if (screen.formFactor === Screen.Phone) {
358 return "phone";
359 } else {
360 return "desktop";
361 }
362 }
363 }
364
365 property real transformRotationAngle
366 property real transformOriginX
367 property real transformOriginY
368
369 transform: Rotation {
370 origin.x: shell.transformOriginX; origin.y: shell.transformOriginY; axis { x: 0; y: 0; z: 1 }
371 angle: shell.transformRotationAngle
372 }
373 }
374
375 RotateButton {
376 id: rotateButton
377
378 screenOrientation: root.orientation
379 screenOrientationLocked: root.orientationLocked
380
381 onClicked: {
382 orientationLock.savedOrientation = root.orientation
383 root.orientation = root.physicalOrientation
384 }
385 }
386
387 Rectangle {
388 id: shellCover
389 color: "black"
390 anchors.fill: parent
391 visible: false
392 }
393
394 ItemSnapshot {
395 id: shellSnapshot
396 target: shell
397 visible: false
398 width: root.width
399 height: root.height
400
401 property real transformRotationAngle
402 property real transformOriginX
403 property real transformOriginY
404
405 transform: Rotation {
406 origin.x: shellSnapshot.transformOriginX; origin.y: shellSnapshot.transformOriginY;
407 axis { x: 0; y: 0; z: 1 }
408 angle: shellSnapshot.transformRotationAngle
409 }
410 }
411}