Unity 8
ScreensAndWorkspaces.qml
1 import QtQuick 2.4
2 import Ubuntu.Components 1.3
3 import Ubuntu.Components.Popups 1.3
4 import WindowManager 1.0
5 import Unity.Application 0.1
6 import ".."
7 
8 Item {
9  id: root
10 
11  property string background
12 
13  property var screensProxy: Screens.createProxy();
14 
15  property QtObject activeWorkspace: null
16 
17  signal closeSpread();
18 
19  Row {
20  id: row
21  anchors.bottom: parent.bottom
22  anchors.horizontalCenter: parent.horizontalCenter
23  Behavior on anchors.horizontalCenterOffset { NumberAnimation { duration: UbuntuAnimation.SlowDuration } }
24  spacing: units.gu(1)
25 
26  property var selectedIndex: undefined
27 
28  Repeater {
29  model: screensProxy
30 
31  delegate: Item {
32  height: root.height - units.gu(6)
33  width: workspaces.width
34 
35  Item {
36  id: header
37  anchors { left: parent.left; top: parent.top; right: parent.right }
38  height: units.gu(7)
39  z: 1
40 
41  property bool isCurrent: {
42  // another screen is selected.
43  if (row.selectedIndex != undefined && row.selectedIndex != index) return false;
44 
45  // this screen is active.
46  if (WMScreen.active && WMScreen.isSameAs(model.screen) && WMScreen.currentWorkspace.isSameAs(activeWorkspace)) return true;
47  if (model.screen.workspaces.indexOf(activeWorkspace) >= 0) return true;
48 
49  // not active.
50  return false;
51  }
52 
53  property bool isSelected: screenMA.containsMouse
54  onIsSelectedChanged: {
55  if (isSelected) {
56  row.selectedIndex = Qt.binding(function() { return index; });
57  } else if (row.selectedIndex === index) {
58  row.selectedIndex = undefined;
59  }
60  }
61 
62  UbuntuShape {
63  anchors.fill: parent
64  backgroundColor: "white"
65  opacity: header.isCurrent || header.isSelected ? 1.0 : 0.5
66  }
67 
68  DropArea {
69  anchors.fill: parent
70  keys: ["workspace"]
71 
72  onEntered: {
73  workspaces.workspaceModel.insert(workspaces.workspaceModel.count, {text: drag.source.text})
74  drag.source.inDropArea = true;
75  }
76 
77  onExited: {
78  workspaces.workspaceModel.remove(workspaces.workspaceModel.count - 1, 1)
79  drag.source.inDropArea = false;
80  }
81 
82  onDropped: {
83  drag.source.inDropArea = false;
84  }
85  }
86 
87  Column {
88  anchors.fill: parent
89  anchors.margins: units.gu(1)
90 
91  Label {
92  text: model.screen.name
93  color: header.isCurrent || header.isSelected ? "black" : "white"
94  }
95 
96  Label {
97  text: model.screen.outputTypeName
98  color: header.isCurrent || header.isSelected ? "black" : "white"
99  fontSize: "x-small"
100  }
101 
102  Label {
103  text: screen.availableModes[screen.currentModeIndex].size.width + "x" + screen.availableModes[screen.currentModeIndex].size.height
104  color: header.isCurrent || header.isSelected ? "black" : "white"
105  fontSize: "x-small"
106  }
107  }
108 
109  Icon {
110  anchors {
111  top: parent.top
112  right: parent.right
113  margins: units.gu(1)
114  }
115  width: units.gu(3)
116  height: width
117  source: "image://theme/select"
118  color: header.isCurrent || header.isSelected ? "black" : "white"
119  visible: model.screen.active
120  }
121 
122  MouseArea {
123  id: screenMA
124  hoverEnabled: true
125  anchors.fill: parent
126 
127  onClicked: {
128  var obj = screensMenuComponent.createObject(header)
129  obj.open(mouseX, mouseY)
130  }
131  }
132 
133  Component {
134  id: screensMenuComponent
135  UbuntuShape {
136  id: screensMenu
137  width: units.gu(20)
138  height: contentColumn.childrenRect.height
139  backgroundColor: "white"
140 
141  function open(mouseX, mouseY) {
142  x = Math.max(0, Math.min(mouseX - width / 2, parent.width - width))
143  y = mouseY + units.gu(1)
144  }
145 
146  InverseMouseArea {
147  anchors.fill: parent
148  onClicked: {
149  screensMenu.destroy()
150  }
151  }
152 
153  Column {
154  id: contentColumn
155  width: parent.width
156  ListItem {
157  height: layout.height
158  highlightColor: "transparent"
159  ListItemLayout {
160  id: layout
161  title.text: qsTr("Add workspace")
162  title.color: "black"
163  }
164  onClicked: {
165  screen.workspaces.addWorkspace();
166  Screens.sync(root.screensProxy);
167  screensMenu.destroy();
168  }
169  }
170  }
171  }
172  }
173  }
174 
175  Workspaces {
176  id: workspaces
177  height: parent.height - header.height - units.gu(2)
178  width: {
179  var width = 0;
180  if (screensProxy.count == 1) {
181  width = Math.min(implicitWidth, root.width - units.gu(8));
182  } else {
183  width = Math.min(implicitWidth, model.screen.active ? root.width - units.gu(48) : units.gu(40))
184  }
185  return Math.max(workspaces.minimumWidth, width);
186  }
187 
188  Behavior on width { UbuntuNumberAnimation {} }
189  anchors.bottom: parent.bottom
190  anchors.bottomMargin: units.gu(1)
191  anchors.horizontalCenter: parent.horizontalCenter
192  screen: model.screen
193  background: root.background
194 
195  workspaceModel: model.screen.workspaces
196  activeWorkspace: root.activeWorkspace
197  readOnly: false
198 
199  onCommitScreenSetup: Screens.sync(root.screensProxy)
200  onCloseSpread: root.closeSpread();
201 
202  onClicked: {
203  root.activeWorkspace = workspace;
204  }
205  }
206  }
207  }
208  }
209 
210  Rectangle {
211  anchors { left: parent.left; top: parent.top; bottom: parent.bottom; topMargin: units.gu(6); bottomMargin: units.gu(1) }
212  width: units.gu(5)
213  color: "#33000000"
214  visible: (row.width - root.width + units.gu(10)) / 2 - row.anchors.horizontalCenterOffset > units.gu(5)
215  MouseArea {
216  id: leftScrollArea
217  anchors.fill: parent
218  hoverEnabled: true
219  onPressed: mouse.accepted = false;
220  }
221  DropArea {
222  id: leftFakeDropArea
223  anchors.fill: parent
224  keys: ["application", "workspace"]
225  }
226  }
227  Rectangle {
228  anchors { right: parent.right; top: parent.top; bottom: parent.bottom; topMargin: units.gu(6); bottomMargin: units.gu(1) }
229  width: units.gu(5)
230  color: "#33000000"
231  visible: (row.width - root.width + units.gu(10)) / 2 + row.anchors.horizontalCenterOffset > units.gu(5)
232  MouseArea {
233  id: rightScrollArea
234  anchors.fill: parent
235  hoverEnabled: true
236  onPressed: mouse.accepted = false;
237  }
238  DropArea {
239  id: rightFakeDropArea
240  anchors.fill: parent
241  keys: ["application", "workspace"]
242  }
243  }
244  Timer {
245  repeat: true
246  running: leftScrollArea.containsMouse || rightScrollArea.containsMouse || leftFakeDropArea.containsDrag || rightFakeDropArea.containsDrag
247  interval: UbuntuAnimation.SlowDuration
248  triggeredOnStart: true
249  onTriggered: {
250  var newOffset = row.anchors.horizontalCenterOffset;
251  var maxOffset = Math.max((row.width - root.width + units.gu(10)) / 2, 0);
252  if (leftScrollArea.containsMouse || leftFakeDropArea.containsDrag) {
253  newOffset += units.gu(20)
254  } else {
255  newOffset -= units.gu(20)
256  }
257  newOffset = Math.max(-maxOffset, Math.min(maxOffset, newOffset));
258  row.anchors.horizontalCenterOffset = newOffset;
259  }
260  }
261 }