Unity 8
WorkspaceSwitcher.qml
1 /*
2  * Copyright (C) 2014-2016 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  */
16 
17 import QtQuick 2.4
18 import Ubuntu.Components 1.3
19 import "Spread"
20 import WindowManager 1.0
21 import Unity.Application 0.1
22 
23 Item {
24  id: root
25 
26  opacity: d.shown ? 1 : 0
27  visible: opacity > 0
28  Behavior on opacity { UbuntuNumberAnimation {} }
29 
30  property var screensProxy: Screens.createProxy();
31  property string background
32 
33  readonly property alias active: d.active
34 
35  function showLeft() {
36  show();
37  d.previousWorkspace();
38  }
39  function showRight() {
40  show();
41  d.nextWorkspace();
42  }
43  function showUp() {
44  show();
45  d.previousScreen();
46  }
47  function showDown() {
48  show();
49  d.nextScreen();
50  }
51 
52  function show() {
53  hideTimer.stop();
54  d.altPressed = true;
55  d.ctrlPressed = true;
56  d.active = true;
57  d.shown = true;
58  focus = true;
59 
60  d.highlightedScreenIndex = screensProxy.activeScreen;
61  var activeScreen = screensProxy.get(screensProxy.activeScreen);
62  d.highlightedWorkspaceIndex = activeScreen.workspaces.indexOf(activeScreen.currentWorkspace)
63  }
64 
65  QtObject {
66  id: d
67 
68  property bool active: false
69  property bool shown: false
70  property bool altPressed: false
71  property bool ctrlPressed: false
72 
73  property int rowHeight: root.height - units.gu(4)
74 
75  property int highlightedScreenIndex: -1
76  property int highlightedWorkspaceIndex: -1
77 
78  function previousWorkspace() {
79  highlightedWorkspaceIndex = Math.max(highlightedWorkspaceIndex - 1, 0);
80  }
81  function nextWorkspace() {
82  var screen = screensProxy.get(highlightedScreenIndex);
83  highlightedWorkspaceIndex = Math.min(highlightedWorkspaceIndex + 1, screen.workspaces.count - 1);
84  }
85  function previousScreen() {
86  highlightedScreenIndex = Math.max(highlightedScreenIndex - 1, 0);
87  var screen = screensProxy.get(highlightedScreenIndex);
88  highlightedWorkspaceIndex = Math.min(highlightedWorkspaceIndex, screen.workspaces.count - 1)
89  }
90  function nextScreen() {
91  highlightedScreenIndex = Math.min(highlightedScreenIndex + 1, screensProxy.count - 1);
92  var screen = screensProxy.get(highlightedScreenIndex);
93  highlightedWorkspaceIndex = Math.min(highlightedWorkspaceIndex, screen.workspaces.count - 1)
94  }
95  }
96 
97  Timer {
98  id: hideTimer
99  interval: 300
100  onTriggered: d.shown = false;
101  }
102 
103  Keys.onPressed: {
104  switch (event.key) {
105  case Qt.Key_Left:
106  d.previousWorkspace();
107  break;
108  case Qt.Key_Right:
109  d.nextWorkspace()
110  break;
111  case Qt.Key_Up:
112  d.previousScreen();
113  break;
114  case Qt.Key_Down:
115  d.nextScreen();
116  }
117  }
118  Keys.onReleased: {
119  switch (event.key) {
120  case Qt.Key_Alt:
121  d.altPressed = false;
122  break;
123  case Qt.Key_Control:
124  d.ctrlPressed = false;
125  break;
126  }
127 
128  if (!d.altPressed && !d.ctrlPressed) {
129  d.active = false;
130  hideTimer.start();
131  focus = false;
132  screensProxy.get(d.highlightedScreenIndex).workspaces.get(d.highlightedWorkspaceIndex).activate();
133  }
134  }
135 
136  UbuntuShape {
137  backgroundColor: "#F2111111"
138  clip: true
139  width: Math.min(parent.width, screensColumn.width + units.gu(4))
140  anchors.horizontalCenter: parent.horizontalCenter
141  height: parent.height
142 
143  Column {
144  id: screensColumn
145  anchors {
146  top: parent.top; topMargin: units.gu(2) - d.highlightedScreenIndex * (d.rowHeight + screensColumn.spacing)
147  left: parent.left; leftMargin: units.gu(2)
148  }
149  width: screensRepeater.itemAt(d.highlightedScreenIndex).width
150  spacing: units.gu(2)
151  Behavior on anchors.topMargin { UbuntuNumberAnimation {} }
152  Behavior on width { UbuntuNumberAnimation {} }
153 
154  Repeater {
155  id: screensRepeater
156  model: screensProxy
157 
158  delegate: Item {
159  height: d.rowHeight
160  width: workspaces.width
161  anchors.horizontalCenter: parent.horizontalCenter
162  opacity: d.highlightedScreenIndex == index ? 1 : 0
163  Behavior on opacity { UbuntuNumberAnimation {} }
164 
165  UbuntuShape {
166  id: header
167  anchors { left: parent.left; top: parent.top; right: parent.right }
168  height: units.gu(4)
169  backgroundColor: "white"
170 
171  Label {
172  anchors { left: parent.left; top: parent.top; right: parent.right; margins: units.gu(1) }
173  text: model.screen.name
174  color: UbuntuColors.ash
175  }
176  }
177 
178  Workspaces {
179  id: workspaces
180  height: parent.height - header.height - units.gu(2)
181  width: Math.min(implicitWidth, root.width - units.gu(4))
182 
183  anchors.bottom: parent.bottom
184  anchors.bottomMargin: units.gu(1)
185  anchors.horizontalCenter: parent.horizontalCenter
186  screen: model.screen
187  background: root.background
188  selectedIndex: d.highlightedScreenIndex == index ? d.highlightedWorkspaceIndex : -1
189 
190  workspaceModel: model.screen.workspaces
191  }
192  }
193  }
194  }
195  }
196 }