on 2c3887e help sign in
unity8/qml/OrientedShell.qml
185 lines 7.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 "Rotation"
24Rectangle {
25 id: root
26 color: "black"
28 implicitWidth: units.gu(40)
29 implicitHeight: units.gu(71)
31 // NB: native and primary orientations here don't map exactly to their QScreen counterparts
32 readonly property int nativeOrientation: width > height ? Qt.LandscapeOrientation : Qt.PortraitOrientation
34 readonly property int primaryOrientation:
35 deviceConfiguration.primaryOrientation == deviceConfiguration.useNativeOrientation
36 ? nativeOrientation : deviceConfiguration.primaryOrientation
38 DeviceConfiguration {
39 id: deviceConfiguration
40 name: applicationArguments.deviceName
41 }
43 // to be overwritten by tests
44 property var usageModeSettings: GSettings { schema.id: "com.canonical.Unity8" }
45 property int physicalOrientation: Screen.orientation
46 property bool orientationLocked: OrientationLock.enabled
47 property var orientationLock: OrientationLock
49 property int orientation
50 onPhysicalOrientationChanged: {
51 if (!orientationLocked) {
52 orientation = physicalOrientation;
53 }
54 }
55 onOrientationLockedChanged: {
56 if (orientationLocked) {
57 orientationLock.savedOrientation = physicalOrientation;
58 } else {
59 orientation = physicalOrientation;
60 }
61 }
62 Component.onCompleted: {
63 if (orientationLocked) {
64 orientation = orientationLock.savedOrientation;
65 }
66 }
68 readonly property int supportedOrientations: shell.supportedOrientations
69 & deviceConfiguration.supportedOrientations
70 property int acceptedOrientationAngle: {
71 if (orientation & supportedOrientations) {
72 return Screen.angleBetween(nativeOrientation, orientation);
73 } else if (shell.orientation & supportedOrientations) {
74 // stay where we are
75 return shell.orientationAngle;
76 } else if (angleToOrientation(shell.mainAppWindowOrientationAngle) & supportedOrientations) {
77 return shell.mainAppWindowOrientationAngle;
78 } else {
79 // rotate to some supported orientation as we can't stay where we currently are
80 // TODO: Choose the closest to the current one
81 if (supportedOrientations & Qt.PortraitOrientation) {
82 return Screen.angleBetween(nativeOrientation, Qt.PortraitOrientation);
83 } else if (supportedOrientations & Qt.LandcscapeOrientation) {
84 return Screen.angleBetween(nativeOrientation, Qt.LandscapeOrientation);
85 } else if (supportedOrientations & Qt.InvertedPortraitOrientation) {
86 return Screen.angleBetween(nativeOrientation, Qt.InvertedPortraitOrientation);
87 } else if (supportedOrientations & Qt.InvertedLandscapeOrientation) {
88 return Screen.angleBetween(nativeOrientation, Qt.InvertedLandscapeOrientation);
89 } else {
90 // if all fails, fallback to primary orientation
91 return Screen.angleBetween(nativeOrientation, primaryOrientation);
92 }
93 }
94 }
96 function angleToOrientation(angle) {
97 switch (angle) {
98 case 0:
99 return nativeOrientation;
100 break;
101 case 90:
102 return nativeOrientation === Qt.PortraitOrientation ? Qt.InvertedLandscapeOrientation
103 : Qt.PortraitOrientation;
104 break;
105 case 180:
106 return nativeOrientation === Qt.PortraitOrientation ? Qt.InvertedPortraitOrientation
107 : Qt.InvertedLandscapeOrientation;
108 break;
109 case 270:
110 return nativeOrientation === Qt.PortraitOrientation ? Qt.LandscapeOrientation
111 : Qt.InvertedPortraitOrientation;
112 break;
113 default:
114 console.warn("angleToOrientation: Invalid orientation angle: " + angle);
115 return primaryOrientation;
116 }
117 }
119 RotationStates {
120 id: rotationStates
121 objectName: "rotationStates"
122 orientedShell: root
123 shell: shell
124 shellCover: shellCover
125 windowScreenshot: windowScreenshot
126 }
128 Shell {
129 id: shell
130 objectName: "shell"
131 width: root.width
132 height: root.height
133 orientation: root.angleToOrientation(orientationAngle)
134 primaryOrientation: root.primaryOrientation
135 nativeOrientation: root.nativeOrientation
136 nativeWidth: root.width
137 nativeHeight: root.height
139 // TODO: Factor in the connected input devices (eg: physical keyboard, mouse, touchscreen),
140 // what's the output device (eg: big TV, desktop monitor, phone display), etc.
141 usageScenario: {
142 if (root.usageModeSettings.usageMode === "Windowed") {
143 return "desktop";
144 } else if (root.usageModeSettings.usageMode === "Staged"
145 && deviceConfiguration.category === "desktop") {
146 return "tablet";
147 } else {
148 return deviceConfiguration.category;
149 }
150 }
152 property real transformRotationAngle
153 property real transformOriginX
154 property real transformOriginY
156 transform: Rotation {
157 origin.x: shell.transformOriginX; origin.y: shell.transformOriginY; axis { x: 0; y: 0; z: 1 }
158 angle: shell.transformRotationAngle
159 }
160 }
162 Rectangle {
163 id: shellCover
164 color: "black"
165 anchors.fill: parent
166 visible: false
167 }
169 WindowScreenshot {
170 id: windowScreenshot
171 visible: false
172 width: root.width
173 height: root.height
175 property real transformRotationAngle
176 property real transformOriginX
177 property real transformOriginY
179 transform: Rotation {
180 origin.x: windowScreenshot.transformOriginX; origin.y: windowScreenshot.transformOriginY;
181 axis { x: 0; y: 0; z: 1 }
182 angle: windowScreenshot.transformRotationAngle
183 }
184 }