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
19import WindowManager 1.0
20import "MathUtils.js" as MathUtils
21import "../../Components"
25 implicitWidth: listView.contentWidth
26 readonly property int minimumWidth: {
27 var count = Math.min(3, listView.count);
28 return listView.itemWidth * count + listView.spacing * (count - 1)
31 property QtObject screen: null
32 property alias workspaceModel: listView.model
33 property var background // TODO: should be stored in the workspace data
34 property int selectedIndex: -1
35 property bool readOnly: true
36 property var activeWorkspace: null
38 signal commitScreenSetup();
40 signal clicked(var workspace);
48 var index = listView.getDropIndex(drag);
49 drag.source.workspace.assign(workspaceModel, index)
50 drag.source.inDropArea = true;
54 var index = listView.getDropIndex(drag);
55 if (listView.dropItemIndex == index) return;
56 listView.model.move(listView.dropItemIndex, index, 1);
57 listView.dropItemIndex = index;
61 drag.source.workspace.unassign()
62 listView.dropItemIndex = -1;
63 listView.hoveredWorkspaceIndex = -1;
64 drag.source.inDropArea = false;
68 drop.accept(Qt.MoveAction);
69 listView.dropItemIndex = -1;
70 drag.source.inDropArea = false;
78 listView.progressiveScroll(drag.x)
79 listView.updateDropProperties(drag)
82 listView.hoveredWorkspaceIndex = -1
85 var surface = drag.source.surface;
86 drag.source.surface = null;
87 var workspace = listView.model.get(listView.hoveredWorkspaceIndex);
88 WorkspaceManager.moveSurfaceToWorkspace(surface, workspace);
89 drop.accept(Qt.MoveAction)
90 if (listView.hoveredHalf == "right") {
95 listView.hoveredWorkspaceIndex = -1
99 onSelectedIndexChanged: {
100 listView.positionViewAtIndex(selectedIndex, ListView.Center);
104 // We need to clip the listview as it has left/right margins and it would
105 // overlap with items next to it and eat mouse input. However, we can't
106 // just clip at the actual bounds as the delegates have the close button
107 // on hover which reaches a bit outside, so lets some margins for the clipping
109 anchors.margins: -units.gu(2)
117 topMargin: -parent.anchors.margins
118 bottomMargin: -parent.anchors.margins
119 leftMargin: -itemWidth - parent.anchors.margins
120 rightMargin: -itemWidth - parent.anchors.margins
122 boundsBehavior: Flickable.StopAtBounds
124 Behavior on contentX {
125 SmoothedAnimation { duration: 200 }
128 property var clickedWorkspace: null
130 orientation: ListView.Horizontal
132 leftMargin: itemWidth
133 rightMargin: itemWidth
135 property int screenWidth: screen.availableModes[screen.currentModeIndex].size.width
136 property int screenHeight: screen.availableModes[screen.currentModeIndex].size.height
137 property int itemWidth: height * screenWidth / screenHeight
138 property int foldingAreaWidth: itemWidth / 2
139 property int maxAngle: 40
141 property real realContentX: contentX - originX + leftMargin
142 property int dropItemIndex: -1
143 property int hoveredWorkspaceIndex: -1
144 property string hoveredHalf: "" // left or right
146 function getDropIndex(drag) {
147 var coords = mapToItem(listView.contentItem, drag.x, drag.y)
148 var index = Math.floor((drag.x + listView.realContentX) / (listView.itemWidth + listView.spacing));
149 if (index < 0) index = 0;
150 var upperLimit = dropItemIndex == -1 ? listView.count : listView.count - 1
151 if (index > upperLimit) index = upperLimit;
155 function updateDropProperties(drag) {
156 var coords = mapToItem(listView.contentItem, drag.x, drag.y)
157 var index = Math.floor(drag.x + listView.realContentX) / (listView.itemWidth + listView.spacing);
159 listView.hoveredWorkspaceIndex = -1;
160 listView.hoveredHalf = "";
164 var upperLimit = dropItemIndex == -1 ? listView.count : listView.count - 1
165 if (index > upperLimit) index = upperLimit;
166 listView.hoveredWorkspaceIndex = index;
167 var pixelsInTile = (drag.x + listView.realContentX) % (listView.itemWidth + listView.spacing);
168 listView.hoveredHalf = (pixelsInTile / listView.itemWidth) < .5 ? "left" : "right";
171 function progressiveScroll(mouseX) {
172 var progress = Math.max(0, Math.min(1, (mouseX - listView.itemWidth) / (width - listView.leftMargin * 2 - listView.itemWidth * 2)))
173 listView.contentX = listView.originX + (listView.contentWidth - listView.width + listView.leftMargin + listView.rightMargin) * progress - listView.leftMargin
176 displaced: Transition { LomiriNumberAnimation { properties: "x" } }
179 id: workspaceDelegate
180 objectName: "delegate" + index
181 height: parent.height
182 width: listView.itemWidth
183 Behavior on width { LomiriNumberAnimation {} }
184 visible: listView.dropItemIndex !== index
186 property int itemX: -listView.realContentX + index * (listView.itemWidth + listView.spacing)
187 property int distanceFromLeft: itemX //- listView.leftMargin
188 property int distanceFromRight: listView.width - listView.leftMargin - listView.rightMargin - itemX - listView.itemWidth
190 property int itemAngle: {
192 if (distanceFromLeft < 0) {
193 var progress = (distanceFromLeft + listView.foldingAreaWidth) / listView.foldingAreaWidth
194 return MathUtils.linearAnimation(1, -1, 0, listView.maxAngle, Math.max(-1, Math.min(1, progress)));
198 if (index == listView.count - 1) {
199 if (distanceFromRight < 0) {
200 var progress = (distanceFromRight + listView.foldingAreaWidth) / listView.foldingAreaWidth
201 return MathUtils.linearAnimation(1, -1, 0, -listView.maxAngle, Math.max(-1, Math.min(1, progress)));
206 if (distanceFromLeft < listView.foldingAreaWidth) {
207 // itemX : 10gu = p : 100
208 var progress = distanceFromLeft / listView.foldingAreaWidth
209 return MathUtils.linearAnimation(1, -1, 0, listView.maxAngle, Math.max(-1, Math.min(1, progress)));
211 if (distanceFromRight < listView.foldingAreaWidth) {
212 var progress = distanceFromRight / listView.foldingAreaWidth
213 return MathUtils.linearAnimation(1, -1, 0, -listView.maxAngle, Math.max(-1, Math.min(1, progress)));
218 property int itemOffset: {
220 if (distanceFromLeft < 0) {
221 return -distanceFromLeft
225 if (index == listView.count - 1) {
226 if (distanceFromRight < 0) {
227 return distanceFromRight
232 if (itemX < -listView.foldingAreaWidth) {
235 if (distanceFromLeft < listView.foldingAreaWidth) {
236 return (listView.foldingAreaWidth - distanceFromLeft) / 2
239 if (distanceFromRight < -listView.foldingAreaWidth) {
240 return distanceFromRight
243 if (distanceFromRight < listView.foldingAreaWidth) {
244 return -(listView.foldingAreaWidth - distanceFromRight) / 2
250 z: itemOffset < 0 ? itemOffset : -itemOffset
254 axis { x: 0; y: 1; z: 0 }
255 origin { x: itemAngle < 0 ? listView.itemWidth : 0; y: height / 2 }
264 height: listView.height
265 width: listView.itemWidth
267 background: root.background
268 screenHeight: listView.screenHeight
269 containsDragLeft: listView.hoveredWorkspaceIndex == index && listView.hoveredHalf == "left"
270 containsDragRight: listView.hoveredWorkspaceIndex == index && listView.hoveredHalf == "right"
271 isActive: workspace.isSameAs(root.activeWorkspace)
272 isSelected: index === root.selectedIndex
273 workspace: model.workspace
278 root.clicked(model.workspace)
281 model.workspace.activate();
288 objectName: "closeMouseArea"
289 anchors { left: parent.left; top: parent.top; leftMargin: -height / 2; topMargin: -height / 2 }
293 visible: !root.readOnly && listView.count > 1
296 model.workspace.unassign();
297 root.commitScreenSetup();
301 source: "../graphics/window-close.svg"
302 anchors.fill: closeMouseArea
303 anchors.margins: units.gu(1)
304 sourceSize.width: width
305 sourceSize.height: height
306 readonly property var mousePos: hoverMouseArea.mapToItem(workspaceDelegate, hoverMouseArea.mouseX, hoverMouseArea.mouseY)
307 readonly property bool shown: (hoverMouseArea.containsMouse || parent.containsMouse)
308 && mousePos.y < workspaceDelegate.width / 4
309 && mousePos.y > -units.gu(2)
310 && mousePos.x > -units.gu(2)
311 && mousePos.x < workspaceDelegate.height / 4
312 opacity: shown ? 1 : 0
314 Behavior on opacity { LomiriNumberAnimation { duration: LomiriAnimation.SnapDuration } }
324 propagateComposedEvents: true
325 anchors.leftMargin: listView.leftMargin
326 anchors.rightMargin: listView.rightMargin
327 enabled: !root.readOnly
329 property int draggedIndex: -1
331 property int startX: 0
332 property int startY: 0
335 if (!pressed || dragging) {
336 listView.progressiveScroll(mouseX)
340 if (Math.abs(mouseY - startY) > units.gu(3)) {
341 drag.axis = Drag.XAndYAxis;
346 var result = fakeDragItem.Drag.drop();
347 // if (result == Qt.IgnoreAction) {
348 // WorkspaceManager.destroyWorkspace(fakeDragItem.workspace);
350 root.commitScreenSetup();
354 property bool dragging: drag.active
357 var ws = listView.model.get(draggedIndex);
358 if (ws) ws.unassign();
365 if (listView.model.count < 2) return;
367 var coords = mapToItem(listView.contentItem, mouseX, mouseY)
368 draggedIndex = listView.indexAt(coords.x, coords.y)
369 var clickedItem = listView.itemAt(coords.x, coords.y)
371 var itemCoords = clickedItem.mapToItem(listView, -listView.leftMargin, 0);
372 fakeDragItem.x = itemCoords.x
373 fakeDragItem.y = itemCoords.y
374 fakeDragItem.workspace = listView.model.get(draggedIndex)
376 var mouseCoordsInItem = mapToItem(clickedItem, mouseX, mouseY);
377 fakeDragItem.Drag.hotSpot.x = mouseCoordsInItem.x
378 fakeDragItem.Drag.hotSpot.y = mouseCoordsInItem.y
380 drag.axis = Drag.YAxis;
381 drag.target = fakeDragItem;
386 height: listView.height
387 width: listView.itemWidth
389 background: root.background
390 screenHeight: screen.availableModes[screen.currentModeIndex].size.height
393 Drag.active: hoverMouseArea.drag.active
394 Drag.keys: ['workspace']
396 property bool inDropArea: false
401 opacity: parent.inDropArea ? 0 : 1
402 Behavior on opacity { LomiriNumberAnimation { } }
404 anchors.centerIn: parent
414 anchors.centerIn: parent
422 when: fakeDragItem.Drag.active
423 ParentChange { target: fakeDragItem; parent: shell }