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
136 // FIXME: Screen orientation changed event does not trigger properly
137 // so we rely on height getting changed when rotating hence updating the value as needed
138 readonly property bool screenIsLandscape: screen.orientation == Qt.LandscapeOrientation
139 || screen.orientation == Qt.InvertedLandscapeOrientation ? height > 0
142 // Get the screen size based on screen's current orientation
143 readonly property var screenSize: screen.availableModes[screen.currentModeIndex].size
144 readonly property real screenWidth: screenIsLandscape ? screenSize.width >= screenSize.height ? screenSize.width : screenSize.height
145 : screenSize.width >= screenSize.height ? screenSize.height : screenSize.width
146 readonly property real screenHeight: screenIsLandscape ? screenSize.width >= screenSize.height ? screenSize.height : screenSize.width
147 : screenSize.width >= screenSize.height ? screenSize.width : screenSize.height
149 readonly property real screenSpaceHeight: root.availableDesktopArea.height
150 readonly property real screenSpaceWidth: root.availableDesktopArea.width
151 readonly property real launcherWidth: screenWidth - screenSpaceWidth
152 property real itemWidth: height * screenSpaceWidth / screenSpaceHeight
153 property int foldingAreaWidth: itemWidth / 2
154 property int maxAngle: 40
156 property real realContentX: contentX - originX + leftMargin
157 property int dropItemIndex: -1
158 property int hoveredWorkspaceIndex: -1
159 property string hoveredHalf: "" // left or right
161 function getDropIndex(drag) {
162 var coords = mapToItem(listView.contentItem, drag.x, drag.y)
163 var index = Math.floor((drag.x + listView.realContentX) / (listView.itemWidth + listView.spacing));
164 if (index < 0) index = 0;
165 var upperLimit = dropItemIndex == -1 ? listView.count : listView.count - 1
166 if (index > upperLimit) index = upperLimit;
170 function updateDropProperties(drag) {
171 var coords = mapToItem(listView.contentItem, drag.x, drag.y)
172 var index = Math.floor(drag.x + listView.realContentX) / (listView.itemWidth + listView.spacing);
174 listView.hoveredWorkspaceIndex = -1;
175 listView.hoveredHalf = "";
179 var upperLimit = dropItemIndex == -1 ? listView.count : listView.count - 1
180 if (index > upperLimit) index = upperLimit;
181 listView.hoveredWorkspaceIndex = index;
182 var pixelsInTile = (drag.x + listView.realContentX) % (listView.itemWidth + listView.spacing);
183 listView.hoveredHalf = (pixelsInTile / listView.itemWidth) < .5 ? "left" : "right";
186 function progressiveScroll(mouseX) {
187 var progress = Math.max(0, Math.min(1, (mouseX - listView.itemWidth) / (width - listView.leftMargin * 2 - listView.itemWidth * 2)))
188 listView.contentX = listView.originX + (listView.contentWidth - listView.width + listView.leftMargin + listView.rightMargin) * progress - listView.leftMargin
191 displaced: Transition { LomiriNumberAnimation { properties: "x" } }
194 id: workspaceDelegate
195 objectName: "delegate" + index
196 height: parent.height
197 width: listView.itemWidth
198 Behavior on width { LomiriNumberAnimation {} }
199 visible: listView.dropItemIndex !== index
201 property int itemX: -listView.realContentX + index * (listView.itemWidth + listView.spacing)
202 property int distanceFromLeft: itemX //- listView.leftMargin
203 property int distanceFromRight: listView.width - listView.leftMargin - listView.rightMargin - itemX - listView.itemWidth
205 property int itemAngle: {
207 if (distanceFromLeft < 0) {
208 var progress = (distanceFromLeft + listView.foldingAreaWidth) / listView.foldingAreaWidth
209 return MathUtils.linearAnimation(1, -1, 0, listView.maxAngle, Math.max(-1, Math.min(1, progress)));
213 if (index == listView.count - 1) {
214 if (distanceFromRight < 0) {
215 var progress = (distanceFromRight + listView.foldingAreaWidth) / listView.foldingAreaWidth
216 return MathUtils.linearAnimation(1, -1, 0, -listView.maxAngle, Math.max(-1, Math.min(1, progress)));
221 if (distanceFromLeft < listView.foldingAreaWidth) {
222 // itemX : 10gu = p : 100
223 var progress = distanceFromLeft / listView.foldingAreaWidth
224 return MathUtils.linearAnimation(1, -1, 0, listView.maxAngle, Math.max(-1, Math.min(1, progress)));
226 if (distanceFromRight < listView.foldingAreaWidth) {
227 var progress = distanceFromRight / listView.foldingAreaWidth
228 return MathUtils.linearAnimation(1, -1, 0, -listView.maxAngle, Math.max(-1, Math.min(1, progress)));
233 property int itemOffset: {
235 if (distanceFromLeft < 0) {
236 return -distanceFromLeft
240 if (index == listView.count - 1) {
241 if (distanceFromRight < 0) {
242 return distanceFromRight
247 if (itemX < -listView.foldingAreaWidth) {
250 if (distanceFromLeft < listView.foldingAreaWidth) {
251 return (listView.foldingAreaWidth - distanceFromLeft) / 2
254 if (distanceFromRight < -listView.foldingAreaWidth) {
255 return distanceFromRight
258 if (distanceFromRight < listView.foldingAreaWidth) {
259 return -(listView.foldingAreaWidth - distanceFromRight) / 2
265 z: itemOffset < 0 ? itemOffset : -itemOffset
269 axis { x: 0; y: 1; z: 0 }
270 origin { x: itemAngle < 0 ? listView.itemWidth : 0; y: height / 2 }
279 height: listView.height
280 width: listView.itemWidth
282 background: root.background
283 screenHeight: listView.screenSpaceHeight
284 launcherWidth: listView.launcherWidth
285 containsDragLeft: listView.hoveredWorkspaceIndex == index && listView.hoveredHalf == "left"
286 containsDragRight: listView.hoveredWorkspaceIndex == index && listView.hoveredHalf == "right"
287 isActive: workspace.isSameAs(root.activeWorkspace)
288 isSelected: index === root.selectedIndex
289 workspace: model.workspace
294 root.clicked(model.workspace)
297 model.workspace.activate();
304 objectName: "closeMouseArea"
305 anchors { left: parent.left; top: parent.top; leftMargin: -height / 2; topMargin: -height / 2 }
309 visible: !root.readOnly && listView.count > 1
312 model.workspace.unassign();
313 root.commitScreenSetup();
317 source: "../graphics/window-close.svg"
318 anchors.fill: closeMouseArea
319 anchors.margins: units.gu(1)
320 sourceSize.width: width
321 sourceSize.height: height
322 readonly property var mousePos: hoverMouseArea.mapToItem(workspaceDelegate, hoverMouseArea.mouseX, hoverMouseArea.mouseY)
323 readonly property bool shown: (hoverMouseArea.containsMouse || parent.containsMouse)
324 && mousePos.y < workspaceDelegate.width / 4
325 && mousePos.y > -units.gu(2)
326 && mousePos.x > -units.gu(2)
327 && mousePos.x < workspaceDelegate.height / 4
328 opacity: shown ? 1 : 0
330 Behavior on opacity { LomiriNumberAnimation { duration: LomiriAnimation.SnapDuration } }
340 propagateComposedEvents: true
341 anchors.leftMargin: listView.leftMargin
342 anchors.rightMargin: listView.rightMargin
343 enabled: !root.readOnly
345 property int draggedIndex: -1
347 property int startX: 0
348 property int startY: 0
351 if (!pressed || dragging) {
352 listView.progressiveScroll(mouseX)
356 if (Math.abs(mouseY - startY) > units.gu(3)) {
357 drag.axis = Drag.XAndYAxis;
362 var result = fakeDragItem.Drag.drop();
363 // if (result == Qt.IgnoreAction) {
364 // WorkspaceManager.destroyWorkspace(fakeDragItem.workspace);
366 root.commitScreenSetup();
370 property bool dragging: drag.active
373 var ws = listView.model.get(draggedIndex);
374 if (ws) ws.unassign();
381 if (listView.model.count < 2) return;
383 var coords = mapToItem(listView.contentItem, mouseX, mouseY)
384 draggedIndex = listView.indexAt(coords.x, coords.y)
385 var clickedItem = listView.itemAt(coords.x, coords.y)
387 var itemCoords = clickedItem.mapToItem(listView, -listView.leftMargin, 0);
388 fakeDragItem.x = itemCoords.x
389 fakeDragItem.y = itemCoords.y
390 fakeDragItem.workspace = listView.model.get(draggedIndex)
392 var mouseCoordsInItem = mapToItem(clickedItem, mouseX, mouseY);
393 fakeDragItem.Drag.hotSpot.x = mouseCoordsInItem.x
394 fakeDragItem.Drag.hotSpot.y = mouseCoordsInItem.y
396 drag.axis = Drag.YAxis;
397 drag.target = fakeDragItem;
402 height: listView.height
403 width: listView.itemWidth
405 background: root.background
406 screenHeight: listView.screenSpaceHeight
407 launcherWidth: listView.launcherWidth
410 Drag.active: hoverMouseArea.drag.active
411 Drag.keys: ['workspace']
413 property bool inDropArea: false
418 opacity: parent.inDropArea ? 0 : 1
419 Behavior on opacity { LomiriNumberAnimation { } }
421 anchors.centerIn: parent
431 anchors.centerIn: parent
439 when: fakeDragItem.Drag.active
440 ParentChange { target: fakeDragItem; parent: shell }