on 4e59999 help sign in
unity8/qml/OrientedShell.qml
213 lines 7.9 kB history raw
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 */
17import QtQuick 2.0
18import QtQuick.Window 2.0
19import Unity.Session 0.1
20import GSettings 1.0
21import "Components"
22import "Components/UnityInputInfo"
23import "Rotation"
25Rectangle {
26 id: root
27 color: "black"
29 implicitWidth: units.gu(40)
30 implicitHeight: units.gu(71)
32 // NB: native and primary orientations here don't map exactly to their QScreen counterparts
33 readonly property int nativeOrientation: width > height ? Qt.LandscapeOrientation : Qt.PortraitOrientation
35 readonly property int primaryOrientation:
36 deviceConfiguration.primaryOrientation == deviceConfiguration.useNativeOrientation
37 ? nativeOrientation : deviceConfiguration.primaryOrientation
39 DeviceConfiguration {
40 id: deviceConfiguration
41 name: applicationArguments.deviceName
42 }
45 // to be overwritten by tests
46 property var usageModeSettings: GSettings { schema.id: "com.canonical.Unity8" }
47 property var oskSettings: GSettings { schema.id: "com.canonical.keyboard.maliit" }
49 property int physicalOrientation: Screen.orientation
50 property bool orientationLocked: OrientationLock.enabled
51 property var orientationLock: OrientationLock
53 property int orientation
54 onPhysicalOrientationChanged: {
55 if (!orientationLocked) {
56 orientation = physicalOrientation;
57 }
58 }
59 onOrientationLockedChanged: {
60 if (orientationLocked) {
61 orientationLock.savedOrientation = physicalOrientation;
62 } else {
63 orientation = physicalOrientation;
64 }
65 }
66 Component.onCompleted: {
67 if (orientationLocked) {
68 orientation = orientationLock.savedOrientation;
69 }
70 // We need to manually update this on startup as the binding
71 // below doesn't seem to have any effect at that stage
72 oskSettings.stayHidden = UnityInputInfo.keyboards > 0
73 oskSettings.disableHeight = shell.usageScenario == "desktop"
74 }
76 Binding {
77 target: oskSettings
78 property: "stayHidden"
79 value: UnityInputInfo.keyboards > 0
80 }
82 Binding {
83 target: oskSettings
84 property: "disableHeight"
85 value: shell.usageScenario == "desktop"
86 }
88 readonly property int supportedOrientations: shell.supportedOrientations
89 & deviceConfiguration.supportedOrientations
90 property int acceptedOrientationAngle: {
91 if (orientation & supportedOrientations) {
92 return Screen.angleBetween(nativeOrientation, orientation);
93 } else if (shell.orientation & supportedOrientations) {
94 // stay where we are
95 return shell.orientationAngle;
96 } else if (angleToOrientation(shell.mainAppWindowOrientationAngle) & supportedOrientations) {
97 return shell.mainAppWindowOrientationAngle;
98 } else {
99 // rotate to some supported orientation as we can't stay where we currently are
100 // TODO: Choose the closest to the current one
101 if (supportedOrientations & Qt.PortraitOrientation) {
102 return Screen.angleBetween(nativeOrientation, Qt.PortraitOrientation);
103 } else if (supportedOrientations & Qt.LandcscapeOrientation) {
104 return Screen.angleBetween(nativeOrientation, Qt.LandscapeOrientation);
105 } else if (supportedOrientations & Qt.InvertedPortraitOrientation) {
106 return Screen.angleBetween(nativeOrientation, Qt.InvertedPortraitOrientation);
107 } else if (supportedOrientations & Qt.InvertedLandscapeOrientation) {
108 return Screen.angleBetween(nativeOrientation, Qt.InvertedLandscapeOrientation);
109 } else {
110 // if all fails, fallback to primary orientation
111 return Screen.angleBetween(nativeOrientation, primaryOrientation);
112 }
113 }
114 }
116 function angleToOrientation(angle) {
117 switch (angle) {
118 case 0:
119 return nativeOrientation;
120 break;
121 case 90:
122 return nativeOrientation === Qt.PortraitOrientation ? Qt.InvertedLandscapeOrientation
123 : Qt.PortraitOrientation;
124 break;
125 case 180:
126 return nativeOrientation === Qt.PortraitOrientation ? Qt.InvertedPortraitOrientation
127 : Qt.InvertedLandscapeOrientation;
128 break;
129 case 270:
130 return nativeOrientation === Qt.PortraitOrientation ? Qt.LandscapeOrientation
131 : Qt.InvertedPortraitOrientation;
132 break;
133 default:
134 console.warn("angleToOrientation: Invalid orientation angle: " + angle);
135 return primaryOrientation;
136 }
137 }
139 RotationStates {
140 id: rotationStates
141 objectName: "rotationStates"
142 orientedShell: root
143 shell: shell
144 shellCover: shellCover
145 windowScreenshot: windowScreenshot
146 }
148 Shell {
149 id: shell
150 objectName: "shell"
151 width: root.width
152 height: root.height
153 orientation: root.angleToOrientation(orientationAngle)
154 primaryOrientation: root.primaryOrientation
155 nativeOrientation: root.nativeOrientation
156 nativeWidth: root.width
157 nativeHeight: root.height
158 mode: applicationArguments.mode
160 // TODO: Factor in the connected input devices (eg: physical keyboard, mouse, touchscreen),
161 // what's the output device (eg: big TV, desktop monitor, phone display), etc.
162 usageScenario: {
163 if (root.usageModeSettings.usageMode === "Windowed") {
164 return "desktop";
165 } else if (root.usageModeSettings.usageMode === "Staged") {
166 if (deviceConfiguration.category === "phone") {
167 return "phone";
168 } else {
169 return "tablet";
170 }
171 } else { // automatic
172 if (UnityInputInfo.mice > deviceConfiguration.ignoredMice) {
173 return "desktop";
174 } else {
175 return deviceConfiguration.category;
176 }
177 }
178 }
180 property real transformRotationAngle
181 property real transformOriginX
182 property real transformOriginY
184 transform: Rotation {
185 origin.x: shell.transformOriginX; origin.y: shell.transformOriginY; axis { x: 0; y: 0; z: 1 }
186 angle: shell.transformRotationAngle
187 }
188 }
190 Rectangle {
191 id: shellCover
192 color: "black"
193 anchors.fill: parent
194 visible: false
195 }
197 WindowScreenshot {
198 id: windowScreenshot
199 visible: false
200 width: root.width
201 height: root.height
203 property real transformRotationAngle
204 property real transformOriginX
205 property real transformOriginY
207 transform: Rotation {
208 origin.x: windowScreenshot.transformOriginX; origin.y: windowScreenshot.transformOriginY;
209 axis { x: 0; y: 0; z: 1 }
210 angle: windowScreenshot.transformRotationAngle
211 }
212 }