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