2 * Copyright (C) 2017 Canonical Ltd.
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.
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.
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/>.
18import Lomiri.Components 1.3
20import WindowManager 1.0
21import "MathUtils.js" as MathUtils
22import "../../Components"
26 implicitWidth: listView.contentWidth
27 readonly property int minimumWidth: {
28 var count = Math.min(3, listView.count);
29 return listView.itemWidth * count + listView.spacing * (count - 1)
32 property QtObject screen: null
33 property alias workspaceModel: listView.model
34 property var background // TODO: should be stored in the workspace data
35 property int selectedIndex: -1
36 property bool readOnly: true
37 property var activeWorkspace: null
39 signal commitScreenSetup();
41 signal clicked(var workspace);
45 schema.id: "com.lomiri.Shell"
54 var index = listView.getDropIndex(drag);
55 drag.source.workspace.assign(workspaceModel, index)
56 drag.source.inDropArea = true;
60 var index = listView.getDropIndex(drag);
61 if (listView.dropItemIndex == index) return;
62 listView.model.move(listView.dropItemIndex, index, 1);
63 listView.dropItemIndex = index;
67 drag.source.workspace.unassign()
68 listView.dropItemIndex = -1;
69 listView.hoveredWorkspaceIndex = -1;
70 drag.source.inDropArea = false;
74 drop.accept(Qt.MoveAction);
75 listView.dropItemIndex = -1;
76 drag.source.inDropArea = false;
84 listView.progressiveScroll(drag.x)
85 listView.updateDropProperties(drag)
88 listView.hoveredWorkspaceIndex = -1
91 var surface = drag.source.surface;
92 drag.source.surface = null;
93 var workspace = listView.model.get(listView.hoveredWorkspaceIndex);
94 WorkspaceManager.moveSurfaceToWorkspace(surface, workspace);
95 drop.accept(Qt.MoveAction)
96 if (listView.hoveredHalf == "right") {
101 listView.hoveredWorkspaceIndex = -1
105 onSelectedIndexChanged: {
106 listView.positionViewAtIndex(selectedIndex, ListView.Center);
110 // We need to clip the listview as it has left/right margins and it would
111 // overlap with items next to it and eat mouse input. However, we can't
112 // just clip at the actual bounds as the delegates have the close button
113 // on hover which reaches a bit outside, so lets some margins for the clipping
115 anchors.margins: -units.gu(2)
123 topMargin: -parent.anchors.margins
124 bottomMargin: -parent.anchors.margins
125 leftMargin: -itemWidth - parent.anchors.margins
126 rightMargin: -itemWidth - parent.anchors.margins
128 boundsBehavior: Flickable.StopAtBounds
130 Behavior on contentX {
131 SmoothedAnimation { duration: 200 }
134 property var clickedWorkspace: null
136 orientation: ListView.Horizontal
138 leftMargin: itemWidth
139 rightMargin: itemWidth
141 property int screenWidth: screen.availableModes[screen.currentModeIndex].size.width
142 property int screenHeight: screen.availableModes[screen.currentModeIndex].size.height
143 property int itemWidth: height * screenWidth / screenHeight
144 property int foldingAreaWidth: itemWidth / 2
145 property int maxAngle: 40
147 property real realContentX: contentX - originX + leftMargin
148 property int dropItemIndex: -1
149 property int hoveredWorkspaceIndex: -1
150 property string hoveredHalf: "" // left or right
152 function getDropIndex(drag) {
153 var coords = mapToItem(listView.contentItem, drag.x, drag.y)
154 var index = Math.floor((drag.x + listView.realContentX) / (listView.itemWidth + listView.spacing));
155 if (index < 0) index = 0;
156 var upperLimit = dropItemIndex == -1 ? listView.count : listView.count - 1
157 if (index > upperLimit) index = upperLimit;
161 function updateDropProperties(drag) {
162 var coords = mapToItem(listView.contentItem, drag.x, drag.y)
163 var index = Math.floor(drag.x + listView.realContentX) / (listView.itemWidth + listView.spacing);
165 listView.hoveredWorkspaceIndex = -1;
166 listView.hoveredHalf = "";
170 var upperLimit = dropItemIndex == -1 ? listView.count : listView.count - 1
171 if (index > upperLimit) index = upperLimit;
172 listView.hoveredWorkspaceIndex = index;
173 var pixelsInTile = (drag.x + listView.realContentX) % (listView.itemWidth + listView.spacing);
174 listView.hoveredHalf = (pixelsInTile / listView.itemWidth) < .5 ? "left" : "right";
177 function progressiveScroll(mouseX) {
178 var progress = Math.max(0, Math.min(1, (mouseX - listView.itemWidth) / (width - listView.leftMargin * 2 - listView.itemWidth * 2)))
179 listView.contentX = listView.originX + (listView.contentWidth - listView.width + listView.leftMargin + listView.rightMargin) * progress - listView.leftMargin
182 displaced: Transition { LomiriNumberAnimation { properties: "x" } }
185 id: workspaceDelegate
186 objectName: "delegate" + index
187 height: parent.height
188 width: listView.itemWidth
189 Behavior on width { LomiriNumberAnimation {} }
190 visible: listView.dropItemIndex !== index
192 property int itemX: -listView.realContentX + index * (listView.itemWidth + listView.spacing)
193 property int distanceFromLeft: itemX //- listView.leftMargin
194 property int distanceFromRight: listView.width - listView.leftMargin - listView.rightMargin - itemX - listView.itemWidth
196 property int itemAngle: {
198 if (distanceFromLeft < 0) {
199 var progress = (distanceFromLeft + listView.foldingAreaWidth) / listView.foldingAreaWidth
200 return MathUtils.linearAnimation(1, -1, 0, listView.maxAngle, Math.max(-1, Math.min(1, progress)));
204 if (index == listView.count - 1) {
205 if (distanceFromRight < 0) {
206 var progress = (distanceFromRight + listView.foldingAreaWidth) / listView.foldingAreaWidth
207 return MathUtils.linearAnimation(1, -1, 0, -listView.maxAngle, Math.max(-1, Math.min(1, progress)));
212 if (distanceFromLeft < listView.foldingAreaWidth) {
213 // itemX : 10gu = p : 100
214 var progress = distanceFromLeft / listView.foldingAreaWidth
215 return MathUtils.linearAnimation(1, -1, 0, listView.maxAngle, Math.max(-1, Math.min(1, progress)));
217 if (distanceFromRight < listView.foldingAreaWidth) {
218 var progress = distanceFromRight / listView.foldingAreaWidth
219 return MathUtils.linearAnimation(1, -1, 0, -listView.maxAngle, Math.max(-1, Math.min(1, progress)));
224 property int itemOffset: {
226 if (distanceFromLeft < 0) {
227 return -distanceFromLeft
231 if (index == listView.count - 1) {
232 if (distanceFromRight < 0) {
233 return distanceFromRight
238 if (itemX < -listView.foldingAreaWidth) {
241 if (distanceFromLeft < listView.foldingAreaWidth) {
242 return (listView.foldingAreaWidth - distanceFromLeft) / 2
245 if (distanceFromRight < -listView.foldingAreaWidth) {
246 return distanceFromRight
249 if (distanceFromRight < listView.foldingAreaWidth) {
250 return -(listView.foldingAreaWidth - distanceFromRight) / 2
256 z: itemOffset < 0 ? itemOffset : -itemOffset
260 axis { x: 0; y: 1; z: 0 }
261 origin { x: itemAngle < 0 ? listView.itemWidth : 0; y: height / 2 }
270 height: listView.height
271 width: listView.itemWidth - settings.launcherWidth
272 anchors.horizontalCenter: parent.horizontalCenter
274 background: root.background
275 screenHeight: listView.screenHeight
276 containsDragLeft: listView.hoveredWorkspaceIndex == index && listView.hoveredHalf == "left"
277 containsDragRight: listView.hoveredWorkspaceIndex == index && listView.hoveredHalf == "right"
278 isActive: workspace.isSameAs(root.activeWorkspace)
279 isSelected: index === root.selectedIndex
280 workspace: model.workspace
285 root.clicked(model.workspace)
288 model.workspace.activate();
295 objectName: "closeMouseArea"
296 anchors { left: parent.left; top: parent.top; leftMargin: -height / 2; topMargin: -height / 2 }
300 visible: !root.readOnly && listView.count > 1
303 model.workspace.unassign();
304 root.commitScreenSetup();
308 source: "../graphics/window-close.svg"
309 anchors.fill: closeMouseArea
310 anchors.margins: units.gu(1)
311 sourceSize.width: width
312 sourceSize.height: height
313 readonly property var mousePos: hoverMouseArea.mapToItem(workspaceDelegate, hoverMouseArea.mouseX, hoverMouseArea.mouseY)
314 readonly property bool shown: (hoverMouseArea.containsMouse || parent.containsMouse)
315 && mousePos.y < workspaceDelegate.width / 4
316 && mousePos.y > -units.gu(2)
317 && mousePos.x > -units.gu(2)
318 && mousePos.x < workspaceDelegate.height / 4
319 opacity: shown ? 1 : 0
321 Behavior on opacity { LomiriNumberAnimation { duration: LomiriAnimation.SnapDuration } }
331 propagateComposedEvents: true
332 anchors.leftMargin: listView.leftMargin
333 anchors.rightMargin: listView.rightMargin
334 enabled: !root.readOnly
336 property int draggedIndex: -1
338 property int startX: 0
339 property int startY: 0
342 if (!pressed || dragging) {
343 listView.progressiveScroll(mouseX)
347 if (Math.abs(mouseY - startY) > units.gu(3)) {
348 drag.axis = Drag.XAndYAxis;
353 var result = fakeDragItem.Drag.drop();
354 // if (result == Qt.IgnoreAction) {
355 // WorkspaceManager.destroyWorkspace(fakeDragItem.workspace);
357 root.commitScreenSetup();
361 property bool dragging: drag.active
364 var ws = listView.model.get(draggedIndex);
365 if (ws) ws.unassign();
372 if (listView.model.count < 2) return;
374 var coords = mapToItem(listView.contentItem, mouseX, mouseY)
375 draggedIndex = listView.indexAt(coords.x, coords.y)
376 var clickedItem = listView.itemAt(coords.x, coords.y)
378 var itemCoords = clickedItem.mapToItem(listView, -listView.leftMargin, 0);
379 fakeDragItem.x = itemCoords.x
380 fakeDragItem.y = itemCoords.y
381 fakeDragItem.workspace = listView.model.get(draggedIndex)
383 var mouseCoordsInItem = mapToItem(clickedItem, mouseX, mouseY);
384 fakeDragItem.Drag.hotSpot.x = mouseCoordsInItem.x
385 fakeDragItem.Drag.hotSpot.y = mouseCoordsInItem.y
387 drag.axis = Drag.YAxis;
388 drag.target = fakeDragItem;
393 height: listView.height
394 width: listView.itemWidth
396 background: root.background
397 screenHeight: screen.availableModes[screen.currentModeIndex].size.height
400 Drag.active: hoverMouseArea.drag.active
401 Drag.keys: ['workspace']
403 property bool inDropArea: false
408 opacity: parent.inDropArea ? 0 : 1
409 Behavior on opacity { LomiriNumberAnimation { } }
411 anchors.centerIn: parent
421 anchors.centerIn: parent
429 when: fakeDragItem.Drag.active
430 ParentChange { target: fakeDragItem; parent: shell }