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
37 property Item availableDesktopArea
39 signal commitScreenSetup();
41 signal clicked(var workspace);
49 var index = listView.getDropIndex(drag);
50 drag.source.workspace.assign(workspaceModel, index)
51 drag.source.inDropArea = true;
55 var index = listView.getDropIndex(drag);
56 if (listView.dropItemIndex == index) return;
57 listView.model.move(listView.dropItemIndex, index, 1);
58 listView.dropItemIndex = index;
62 drag.source.workspace.unassign()
63 listView.dropItemIndex = -1;
64 listView.hoveredWorkspaceIndex = -1;
65 drag.source.inDropArea = false;
69 drop.accept(Qt.MoveAction);
70 listView.dropItemIndex = -1;
71 drag.source.inDropArea = false;
79 listView.progressiveScroll(drag.x)
80 listView.updateDropProperties(drag)
83 listView.hoveredWorkspaceIndex = -1
86 var surface = drag.source.surface;
87 drag.source.surface = null;
88 var workspace = listView.model.get(listView.hoveredWorkspaceIndex);
89 WorkspaceManager.moveSurfaceToWorkspace(surface, workspace);
90 drop.accept(Qt.MoveAction)
91 if (listView.hoveredHalf == "right") {
96 listView.hoveredWorkspaceIndex = -1
100 onSelectedIndexChanged: {
101 listView.positionViewAtIndex(selectedIndex, ListView.Center);
105 // We need to clip the listview as it has left/right margins and it would
106 // overlap with items next to it and eat mouse input. However, we can't
107 // just clip at the actual bounds as the delegates have the close button
108 // on hover which reaches a bit outside, so lets some margins for the clipping
110 anchors.margins: -units.gu(2)
118 topMargin: -parent.anchors.margins
119 bottomMargin: -parent.anchors.margins
120 leftMargin: -itemWidth - parent.anchors.margins
121 rightMargin: -itemWidth - parent.anchors.margins
123 boundsBehavior: Flickable.StopAtBounds
125 Behavior on contentX {
126 SmoothedAnimation { duration: 200 }
129 property var clickedWorkspace: null
131 orientation: ListView.Horizontal
133 leftMargin: itemWidth
134 rightMargin: itemWidth
135 contentX: anchors.leftMargin
137 // FIXME: Screen orientation changed event does not trigger properly
138 // so we rely on width/height getting changed when rotating hence updating the value as needed
139 readonly property bool screenIsLandscape: screen.orientation == Qt.LandscapeOrientation
140 || screen.orientation == Qt.InvertedLandscapeOrientation ? width > 0 || height > 0
141 : width < 0 || height < 0
143 // Get the screen size based on screen's current orientation
144 readonly property var screenSize: screen.availableModes[screen.currentModeIndex].size
145 readonly property real screenWidth: screenIsLandscape ? screenSize.width >= screenSize.height ? screenSize.width : screenSize.height
146 : screenSize.width >= screenSize.height ? screenSize.height : screenSize.width
147 readonly property real screenHeight: screenIsLandscape ? screenSize.width >= screenSize.height ? screenSize.height : screenSize.width
148 : screenSize.width >= screenSize.height ? screenSize.width : screenSize.height
150 readonly property real screenSpaceHeight: root.availableDesktopArea.height
151 readonly property real screenSpaceWidth: root.availableDesktopArea.width
152 readonly property real launcherWidth: screenWidth - screenSpaceWidth
153 property real itemWidth: height * screenSpaceWidth / screenSpaceHeight
154 property int foldingAreaWidth: itemWidth / 2
155 property int maxAngle: 40
157 property real realContentX: contentX - originX + leftMargin
158 property int dropItemIndex: -1
159 property int hoveredWorkspaceIndex: -1
160 property string hoveredHalf: "" // left or right
162 function getDropIndex(drag) {
163 var coords = mapToItem(listView.contentItem, drag.x, drag.y)
164 var index = Math.floor((drag.x + listView.realContentX) / (listView.itemWidth + listView.spacing));
165 if (index < 0) index = 0;
166 var upperLimit = dropItemIndex == -1 ? listView.count : listView.count - 1
167 if (index > upperLimit) index = upperLimit;
171 function updateDropProperties(drag) {
172 var coords = mapToItem(listView.contentItem, drag.x, drag.y)
173 var index = Math.floor(drag.x + listView.realContentX) / (listView.itemWidth + listView.spacing);
175 listView.hoveredWorkspaceIndex = -1;
176 listView.hoveredHalf = "";
180 var upperLimit = dropItemIndex == -1 ? listView.count : listView.count - 1
181 if (index > upperLimit) index = upperLimit;
182 listView.hoveredWorkspaceIndex = index;
183 var pixelsInTile = (drag.x + listView.realContentX) % (listView.itemWidth + listView.spacing);
184 listView.hoveredHalf = (pixelsInTile / listView.itemWidth) < .5 ? "left" : "right";
187 function progressiveScroll(mouseX) {
188 var progress = Math.max(0, Math.min(1, (mouseX - listView.itemWidth) / (width - listView.leftMargin * 2 - listView.itemWidth * 2)))
189 listView.contentX = listView.originX + (listView.contentWidth - listView.width + listView.leftMargin + listView.rightMargin) * progress - listView.leftMargin
192 displaced: Transition { LomiriNumberAnimation { properties: "x" } }
195 id: workspaceDelegate
196 objectName: "delegate" + index
197 height: parent.height
198 width: listView.itemWidth
199 Behavior on width { LomiriNumberAnimation {} }
200 visible: listView.dropItemIndex !== index
202 property int itemX: -listView.realContentX + index * (listView.itemWidth + listView.spacing)
203 property int distanceFromLeft: itemX //- listView.leftMargin
204 property int distanceFromRight: listView.width - listView.leftMargin - listView.rightMargin - itemX - listView.itemWidth
206 property int itemAngle: {
208 if (distanceFromLeft < 0) {
209 var progress = (distanceFromLeft + listView.foldingAreaWidth) / listView.foldingAreaWidth
210 return MathUtils.linearAnimation(1, -1, 0, listView.maxAngle, Math.max(-1, Math.min(1, progress)));
214 if (index == listView.count - 1) {
215 if (distanceFromRight < 0) {
216 var progress = (distanceFromRight + listView.foldingAreaWidth) / listView.foldingAreaWidth
217 return MathUtils.linearAnimation(1, -1, 0, -listView.maxAngle, Math.max(-1, Math.min(1, progress)));
222 if (distanceFromLeft < listView.foldingAreaWidth) {
223 // itemX : 10gu = p : 100
224 var progress = distanceFromLeft / listView.foldingAreaWidth
225 return MathUtils.linearAnimation(1, -1, 0, listView.maxAngle, Math.max(-1, Math.min(1, progress)));
227 if (distanceFromRight < listView.foldingAreaWidth) {
228 var progress = distanceFromRight / listView.foldingAreaWidth
229 return MathUtils.linearAnimation(1, -1, 0, -listView.maxAngle, Math.max(-1, Math.min(1, progress)));
234 property int itemOffset: {
236 if (distanceFromLeft < 0) {
237 return -distanceFromLeft
241 if (index == listView.count - 1) {
242 if (distanceFromRight < 0) {
243 return distanceFromRight
248 if (itemX < -listView.foldingAreaWidth) {
251 if (distanceFromLeft < listView.foldingAreaWidth) {
252 return (listView.foldingAreaWidth - distanceFromLeft) / 2
255 if (distanceFromRight < -listView.foldingAreaWidth) {
256 return distanceFromRight
259 if (distanceFromRight < listView.foldingAreaWidth) {
260 return -(listView.foldingAreaWidth - distanceFromRight) / 2
266 z: itemOffset < 0 ? itemOffset : -itemOffset
270 axis { x: 0; y: 1; z: 0 }
271 origin { x: itemAngle < 0 ? listView.itemWidth : 0; y: height / 2 }
280 height: listView.height
281 width: listView.itemWidth
283 background: root.background
284 screenHeight: listView.screenSpaceHeight
285 launcherWidth: listView.launcherWidth
286 containsDragLeft: listView.hoveredWorkspaceIndex == index && listView.hoveredHalf == "left"
287 containsDragRight: listView.hoveredWorkspaceIndex == index && listView.hoveredHalf == "right"
288 isActive: workspace.isSameAs(root.activeWorkspace)
289 isSelected: index === root.selectedIndex
290 workspace: model.workspace
295 root.clicked(model.workspace)
298 model.workspace.activate();
305 objectName: "closeMouseArea"
306 anchors { left: parent.left; top: parent.top; leftMargin: -height / 2; topMargin: -height / 2 }
310 visible: !root.readOnly && listView.count > 1
313 model.workspace.unassign();
314 root.commitScreenSetup();
318 source: "../graphics/window-close.svg"
319 anchors.fill: closeMouseArea
320 anchors.margins: units.gu(1)
321 sourceSize.width: width
322 sourceSize.height: height
323 readonly property var mousePos: hoverMouseArea.mapToItem(workspaceDelegate, hoverMouseArea.mouseX, hoverMouseArea.mouseY)
324 readonly property bool shown: (hoverMouseArea.containsMouse || parent.containsMouse)
325 && mousePos.y < workspaceDelegate.width / 4
326 && mousePos.y > -units.gu(2)
327 && mousePos.x > -units.gu(2)
328 && mousePos.x < workspaceDelegate.height / 4
329 opacity: shown ? 1 : 0
331 Behavior on opacity { LomiriNumberAnimation { duration: LomiriAnimation.SnapDuration } }
341 propagateComposedEvents: true
342 anchors.leftMargin: listView.leftMargin
343 anchors.rightMargin: listView.rightMargin
344 enabled: !root.readOnly
346 property int draggedIndex: -1
348 property int startX: 0
349 property int startY: 0
352 if (!pressed || dragging) {
353 listView.progressiveScroll(mouseX)
357 if (Math.abs(mouseY - startY) > units.gu(3)) {
358 drag.axis = Drag.XAndYAxis;
363 var result = fakeDragItem.Drag.drop();
364 // if (result == Qt.IgnoreAction) {
365 // WorkspaceManager.destroyWorkspace(fakeDragItem.workspace);
367 root.commitScreenSetup();
371 property bool dragging: drag.active
374 var ws = listView.model.get(draggedIndex);
375 if (ws) ws.unassign();
382 if (listView.model.count < 2) return;
384 var coords = mapToItem(listView.contentItem, mouseX, mouseY)
385 draggedIndex = listView.indexAt(coords.x, coords.y)
386 var clickedItem = listView.itemAt(coords.x, coords.y)
388 var itemCoords = clickedItem.mapToItem(listView, -listView.leftMargin, 0);
389 fakeDragItem.x = itemCoords.x
390 fakeDragItem.y = itemCoords.y
391 fakeDragItem.workspace = listView.model.get(draggedIndex)
393 var mouseCoordsInItem = mapToItem(clickedItem, mouseX, mouseY);
394 fakeDragItem.Drag.hotSpot.x = mouseCoordsInItem.x
395 fakeDragItem.Drag.hotSpot.y = mouseCoordsInItem.y
397 drag.axis = Drag.YAxis;
398 drag.target = fakeDragItem;
403 height: listView.height
404 width: listView.itemWidth
406 background: root.background
407 screenHeight: listView.screenSpaceHeight
408 launcherWidth: listView.launcherWidth
411 Drag.active: hoverMouseArea.drag.active
412 Drag.keys: ['workspace']
414 property bool inDropArea: false
419 opacity: parent.inDropArea ? 0 : 1
420 Behavior on opacity { LomiriNumberAnimation { } }
422 anchors.centerIn: parent
432 anchors.centerIn: parent
440 when: fakeDragItem.Drag.active
441 ParentChange { target: fakeDragItem; parent: shell }