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