on 49bb8b9 help sign in
unity8/qml/OrientedShell.qml
241 lines 8.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.4
18import QtQuick.Window 2.2
19import Unity.InputInfo 0.1
20import Unity.Session 0.1
21import GSettings 1.0
22import "Components"
23import "Rotation"
24// Workaround https://bugs.launchpad.net/ubuntu/+source/unity8/+bug/1473471
25import Ubuntu.Components 1.3
27Rectangle {
28 id: root
29 color: "black"
31 implicitWidth: units.gu(40)
32 implicitHeight: units.gu(71)
34 DeviceConfiguration {
35 id: deviceConfiguration
36 name: applicationArguments.deviceName
37 }
39 property alias orientations: d.orientations
41 Item {
42 id: d
44 property Orientations orientations: Orientations {
45 id: orientations
46 // NB: native and primary orientations here don't map exactly to their QScreen counterparts
47 native_: root.width > root.height ? Qt.LandscapeOrientation : Qt.PortraitOrientation
49 primary: deviceConfiguration.primaryOrientation == deviceConfiguration.useNativeOrientation
50 ? native_ : deviceConfiguration.primaryOrientation
52 landscape: deviceConfiguration.landscapeOrientation
53 invertedLandscape: deviceConfiguration.invertedLandscapeOrientation
54 portrait: deviceConfiguration.portraitOrientation
55 invertedPortrait: deviceConfiguration.invertedPortraitOrientation
56 }
57 }
58 // to be overwritten by tests
59 property var unity8Settings: Unity8Settings {}
60 property var oskSettings: GSettings { schema.id: "com.canonical.keyboard.maliit" }
62 property int physicalOrientation: Screen.orientation
63 property bool orientationLocked: OrientationLock.enabled
64 property var orientationLock: OrientationLock
66 InputDeviceModel {
67 id: miceModel
68 deviceFilter: InputInfo.Mouse
69 }
71 InputDeviceModel {
72 id: keyboardsModel
73 deviceFilter: InputInfo.Keyboard
74 }
76 property int orientation
77 onPhysicalOrientationChanged: {
78 if (!orientationLocked) {
79 orientation = physicalOrientation;
80 }
81 }
82 onOrientationLockedChanged: {
83 if (orientationLocked) {
84 orientationLock.savedOrientation = physicalOrientation;
85 } else {
86 orientation = physicalOrientation;
87 }
88 }
89 Component.onCompleted: {
90 if (orientationLocked) {
91 orientation = orientationLock.savedOrientation;
92 }
93 // We need to manually update this on startup as the binding
94 // below doesn't seem to have any effect at that stage
95 oskSettings.stayHidden = keyboardsModel.count > 0
96 oskSettings.disableHeight = shell.usageScenario == "desktop"
97 }
99 // we must rotate to a supported orientation regardless of shell's preference
100 property bool orientationChangesEnabled:
101 (shell.orientation & supportedOrientations) === 0 ? true
102 : shell.orientationChangesEnabled
105 Binding {
106 target: oskSettings
107 property: "stayHidden"
108 value: keyboardsModel.count > 0
109 }
111 Binding {
112 target: oskSettings
113 property: "disableHeight"
114 value: shell.usageScenario == "desktop"
115 }
117 readonly property int supportedOrientations: shell.supportedOrientations
118 & (deviceConfiguration.supportedOrientations == deviceConfiguration.useNativeOrientation
119 ? orientations.native_
120 : deviceConfiguration.supportedOrientations)
122 property int acceptedOrientationAngle: {
123 if (orientation & supportedOrientations) {
124 return Screen.angleBetween(orientations.native_, orientation);
125 } else if (shell.orientation & supportedOrientations) {
126 // stay where we are
127 return shell.orientationAngle;
128 } else if (angleToOrientation(shell.mainAppWindowOrientationAngle) & supportedOrientations) {
129 return shell.mainAppWindowOrientationAngle;
130 } else {
131 // rotate to some supported orientation as we can't stay where we currently are
132 // TODO: Choose the closest to the current one
133 if (supportedOrientations & Qt.PortraitOrientation) {
134 return Screen.angleBetween(orientations.native_, Qt.PortraitOrientation);
135 } else if (supportedOrientations & Qt.LandscapeOrientation) {
136 return Screen.angleBetween(orientations.native_, Qt.LandscapeOrientation);
137 } else if (supportedOrientations & Qt.InvertedPortraitOrientation) {
138 return Screen.angleBetween(orientations.native_, Qt.InvertedPortraitOrientation);
139 } else if (supportedOrientations & Qt.InvertedLandscapeOrientation) {
140 return Screen.angleBetween(orientations.native_, Qt.InvertedLandscapeOrientation);
141 } else {
142 // if all fails, fallback to primary orientation
143 return Screen.angleBetween(orientations.native_, orientations.primary);
144 }
145 }
146 }
148 function angleToOrientation(angle) {
149 switch (angle) {
150 case 0:
151 return orientations.native_;
152 case 90:
153 return orientations.native_ === Qt.PortraitOrientation ? Qt.InvertedLandscapeOrientation
154 : Qt.PortraitOrientation;
155 case 180:
156 return orientations.native_ === Qt.PortraitOrientation ? Qt.InvertedPortraitOrientation
157 : Qt.InvertedLandscapeOrientation;
158 case 270:
159 return orientations.native_ === Qt.PortraitOrientation ? Qt.LandscapeOrientation
160 : Qt.InvertedPortraitOrientation;
161 default:
162 console.warn("angleToOrientation: Invalid orientation angle: " + angle);
163 return orientations.primary;
164 }
165 }
167 RotationStates {
168 id: rotationStates
169 objectName: "rotationStates"
170 orientedShell: root
171 shell: shell
172 shellCover: shellCover
173 windowScreenshot: windowScreenshot
174 }
176 Shell {
177 id: shell
178 objectName: "shell"
179 width: root.width
180 height: root.height
181 orientation: root.angleToOrientation(orientationAngle)
182 orientations: root.orientations
183 nativeWidth: root.width
184 nativeHeight: root.height
185 mode: applicationArguments.mode
186 hasMouse: miceModel.count > deviceConfiguration.ignoredMice
188 // TODO: Factor in the connected input devices (eg: physical keyboard, mouse, touchscreen),
189 // what's the output device (eg: big TV, desktop monitor, phone display), etc.
190 usageScenario: {
191 if (root.unity8Settings.usageMode === "Windowed") {
192 return "desktop";
193 } else if (root.unity8Settings.usageMode === "Staged") {
194 if (deviceConfiguration.category === "phone") {
195 return "phone";
196 } else {
197 return "tablet";
198 }
199 } else { // automatic
200 if (miceModel.count > deviceConfiguration.ignoredMice) {
201 return "desktop";
202 } else {
203 return deviceConfiguration.category;
204 }
205 }
206 }
208 property real transformRotationAngle
209 property real transformOriginX
210 property real transformOriginY
212 transform: Rotation {
213 origin.x: shell.transformOriginX; origin.y: shell.transformOriginY; axis { x: 0; y: 0; z: 1 }
214 angle: shell.transformRotationAngle
215 }
216 }
218 Rectangle {
219 id: shellCover
220 color: "black"
221 anchors.fill: parent
222 visible: false
223 }
225 WindowScreenshot {
226 id: windowScreenshot
227 visible: false
228 width: root.width
229 height: root.height
231 property real transformRotationAngle
232 property real transformOriginX
233 property real transformOriginY
235 transform: Rotation {
236 origin.x: windowScreenshot.transformOriginX; origin.y: windowScreenshot.transformOriginY;
237 axis { x: 0; y: 0; z: 1 }
238 angle: windowScreenshot.transformRotationAngle
239 }
240 }