2019-08-16 17:14:42 +00:00
|
|
|
/*
|
2021-03-01 20:03:25 +00:00
|
|
|
* SPDX-FileCopyrightText: 2019 Marco Martin <mart@kde.org>
|
2019-08-16 17:14:42 +00:00
|
|
|
*
|
2021-03-01 20:03:25 +00:00
|
|
|
* SPDX-License-Identifier: LGPL-2.0-or-later
|
2019-08-16 17:14:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import QtQuick 2.4
|
|
|
|
|
|
|
|
|
|
import org.kde.plasma.private.containmentlayoutmanager 1.0 as ContainmentLayoutManager
|
2022-04-06 02:06:05 +00:00
|
|
|
import org.kde.plasma.private.mobileshell 1.0 as MobileShell
|
2019-08-22 16:11:14 +00:00
|
|
|
|
2019-08-27 16:04:14 +00:00
|
|
|
Item {
|
2019-08-16 17:14:42 +00:00
|
|
|
id: root
|
|
|
|
|
|
|
|
|
|
property ContainmentLayoutManager.AppletsLayout appletsLayout
|
|
|
|
|
property FavoriteStrip favoriteStrip
|
2021-04-07 16:41:58 +00:00
|
|
|
property ContainmentLayoutManager.ItemContainer currentlyDraggedDelegate
|
2019-08-29 11:45:01 +00:00
|
|
|
property bool active
|
2022-04-06 02:06:05 +00:00
|
|
|
property QtObject model: MobileShell.ApplicationListModel
|
2019-08-16 17:14:42 +00:00
|
|
|
|
|
|
|
|
readonly property Item spacer: Item {
|
2021-02-15 10:44:48 +00:00
|
|
|
width: favoriteStrip.cellWidth
|
|
|
|
|
height: favoriteStrip.cellHeight
|
2019-08-16 17:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-22 16:11:14 +00:00
|
|
|
function startDrag(item) {
|
2021-02-15 10:44:48 +00:00
|
|
|
showSpacer(item, 0, 0);
|
2019-08-16 17:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-23 09:23:46 +00:00
|
|
|
function dragItem(delegate, dragCenterX, dragCenterY) {
|
2019-08-22 16:11:14 +00:00
|
|
|
// newPosition
|
|
|
|
|
var newRow = 0;
|
2019-08-16 17:14:42 +00:00
|
|
|
|
2019-08-22 16:11:14 +00:00
|
|
|
var newContainer = internal.containerForItem(delegate, dragCenterX, dragCenterY);
|
2021-02-15 10:44:48 +00:00
|
|
|
if (!newContainer) {
|
|
|
|
|
newContainer = appletsLayout;
|
|
|
|
|
}
|
2020-07-21 10:23:50 +00:00
|
|
|
|
2019-08-22 16:11:14 +00:00
|
|
|
// Put it in the favorites strip
|
|
|
|
|
if (newContainer == favoriteStrip) {
|
2019-08-28 14:21:29 +00:00
|
|
|
var pos = favoriteStrip.flow.mapFromItem(delegate, 0, 0);
|
2019-08-22 16:11:14 +00:00
|
|
|
newRow = Math.floor((pos.x + dragCenterX) / delegate.width);
|
|
|
|
|
|
2022-04-06 02:06:05 +00:00
|
|
|
//root.model.setLocation(delegate.modelData.index, MobileShell.ApplicationListModel.Favorites);
|
2019-08-16 17:14:42 +00:00
|
|
|
|
2021-02-15 10:44:48 +00:00
|
|
|
showSpacer(delegate, dragCenterX, dragCenterY);
|
|
|
|
|
root.model.moveItem(delegate.modelData.index, newRow);
|
2019-08-22 16:11:14 +00:00
|
|
|
|
|
|
|
|
// Put it on desktop
|
2021-02-15 10:44:48 +00:00
|
|
|
} else {
|
2019-08-22 16:11:14 +00:00
|
|
|
var pos = appletsLayout.mapFromItem(delegate, 0, 0);
|
2022-04-06 02:06:05 +00:00
|
|
|
//root.model.setLocation(delegate.modelData.index, MobileShell.ApplicationListModel.Desktop);
|
2019-08-30 15:03:35 +00:00
|
|
|
|
2021-02-15 10:44:48 +00:00
|
|
|
showSpacer(delegate, dragCenterX, dragCenterY);
|
2019-08-22 16:11:14 +00:00
|
|
|
return;
|
|
|
|
|
|
2021-02-15 10:44:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function dropItem(item, dragCenterX, dragCenterY) {
|
|
|
|
|
internal.positionItem(item, dragCenterX, dragCenterY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function showSpacer(item, dragCenterX, dragCenterY) {
|
|
|
|
|
var container = internal.containerForItem(item, dragCenterX, dragCenterY);
|
|
|
|
|
|
|
|
|
|
internal.raiseContainer(container);
|
|
|
|
|
|
|
|
|
|
appletsLayout.hidePlaceHolder();
|
|
|
|
|
|
|
|
|
|
if (container == appletsLayout) {
|
|
|
|
|
spacer.visible = false;
|
|
|
|
|
spacer.parent = root;
|
|
|
|
|
appletsLayout.releaseSpace(item);
|
|
|
|
|
internal.putItemInDragSpace(item);
|
|
|
|
|
var pos = appletsLayout.mapFromItem(item, 0, 0);
|
|
|
|
|
appletsLayout.showPlaceHolderAt(Qt.rect(pos.x, pos.y, item.width, item.height));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var child = internal.nearestChild(item, dragCenterX, dragCenterY, container);
|
|
|
|
|
|
|
|
|
|
if (!child) {
|
|
|
|
|
spacer.visible = false;
|
|
|
|
|
spacer.parent = container.flow
|
|
|
|
|
spacer.visible = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spacer.visible = false;
|
|
|
|
|
spacer.parent = container.flow
|
|
|
|
|
|
|
|
|
|
var pos = container.flow.mapFromItem(item, dragCenterX, dragCenterY);
|
|
|
|
|
|
|
|
|
|
if (pos.x < child.x + child.width / 2) {
|
2022-04-07 14:52:12 +00:00
|
|
|
MobileShell.ShellUtil.stackItemBefore(spacer, child);
|
2019-08-16 17:14:42 +00:00
|
|
|
} else {
|
2022-04-07 14:52:12 +00:00
|
|
|
MobileShell.ShellUtil.stackItemAfter(spacer, child);
|
2021-02-15 10:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal.putItemInDragSpace(item);
|
|
|
|
|
|
|
|
|
|
spacer.visible = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function showSpacerAtPos(x, y, container) {
|
|
|
|
|
var pos = container.flow.mapFromGlobal(x, y);
|
|
|
|
|
internal.raiseContainer(container);
|
|
|
|
|
|
|
|
|
|
appletsLayout.hidePlaceHolder();
|
|
|
|
|
|
|
|
|
|
if (container == appletsLayout) {
|
|
|
|
|
spacer.visible = false;
|
|
|
|
|
spacer.parent = root;
|
|
|
|
|
appletsLayout.showPlaceHolderAt(Qt.rect(pos.x, pos.y, appletsLayout.cellWidth, appletsLayout.cellHeight));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-08-22 16:11:14 +00:00
|
|
|
|
2021-02-15 10:44:48 +00:00
|
|
|
var child = internal.nearestChildFromPos(x, y, container);
|
2019-08-22 16:11:14 +00:00
|
|
|
|
2021-02-15 10:44:48 +00:00
|
|
|
if (!child) {
|
|
|
|
|
spacer.visible = false;
|
|
|
|
|
spacer.parent = container.flow
|
|
|
|
|
spacer.visible = true;
|
|
|
|
|
return;
|
2019-08-16 17:14:42 +00:00
|
|
|
}
|
2021-02-15 10:44:48 +00:00
|
|
|
|
|
|
|
|
spacer.visible = false;
|
|
|
|
|
spacer.parent = container.flow
|
|
|
|
|
|
|
|
|
|
if (pos.x < child.x + child.width / 2) {
|
2022-04-07 14:52:12 +00:00
|
|
|
MobileShell.ShellUtil.stackItemBefore(spacer, child);
|
2021-02-15 10:44:48 +00:00
|
|
|
} else {
|
2022-04-07 14:52:12 +00:00
|
|
|
MobileShell.ShellUtil.stackItemAfter(spacer, child);
|
2021-02-15 10:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spacer.visible = true;
|
2019-08-16 17:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-15 10:44:48 +00:00
|
|
|
function hideSpacer () {
|
|
|
|
|
spacer.visible = false;
|
|
|
|
|
spacer.parent = root;
|
2019-08-22 16:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Those should never be accessed from outside
|
2019-08-27 16:04:14 +00:00
|
|
|
QtObject {
|
2019-08-22 16:11:14 +00:00
|
|
|
id: internal
|
|
|
|
|
function raiseContainer(container) {
|
|
|
|
|
container.z = 1;
|
|
|
|
|
|
|
|
|
|
if (container == appletsLayout) {
|
|
|
|
|
favoriteStrip.z = 0;
|
|
|
|
|
} else if (container == favoriteStrip) {
|
|
|
|
|
appletsLayout.z = 0;
|
|
|
|
|
} else {
|
|
|
|
|
appletsLayout.z = 0;
|
|
|
|
|
favoriteStrip.z = 0;
|
|
|
|
|
}
|
2019-08-22 11:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-22 16:11:14 +00:00
|
|
|
function containerForItem(item, dragCenterX, dragCenterY) {
|
2021-04-07 16:41:58 +00:00
|
|
|
if (!item.modelData) {
|
|
|
|
|
return appletsLayout;
|
|
|
|
|
} else if (favoriteStrip.contains(Qt.point(0,favoriteStrip.frame.mapFromItem(item, dragCenterX, dragCenterY).y))
|
2022-04-06 02:06:05 +00:00
|
|
|
&& (item.modelData.applicationLocation == MobileShell.ApplicationListModel.Favorites
|
2021-02-15 10:44:48 +00:00
|
|
|
|| root.model.favoriteCount < root.model.maxFavoriteCount)) {
|
2019-08-22 16:11:14 +00:00
|
|
|
return favoriteStrip;
|
|
|
|
|
} else {
|
2021-02-15 10:44:48 +00:00
|
|
|
return appletsLayout;
|
2019-08-22 11:14:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-27 16:04:14 +00:00
|
|
|
function putItemInDragSpace(item) {
|
|
|
|
|
var pos = root.mapFromItem(item, 0, 0);
|
|
|
|
|
item.parent = root;
|
2019-08-22 16:11:14 +00:00
|
|
|
|
|
|
|
|
item.x = pos.x;
|
|
|
|
|
item.y = pos.y;
|
2019-08-22 11:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-22 16:11:14 +00:00
|
|
|
function putInContainerLayout(item, container) {
|
2019-08-23 10:54:19 +00:00
|
|
|
var pos = container.flow.mapFromItem(item, 0, 0);
|
2019-08-22 11:14:28 +00:00
|
|
|
|
2019-08-22 16:11:14 +00:00
|
|
|
if (container == appletsLayout) {
|
|
|
|
|
item.parent = container;
|
|
|
|
|
} else {
|
|
|
|
|
item.parent = container.flow;
|
|
|
|
|
}
|
2019-08-16 17:14:42 +00:00
|
|
|
|
2019-08-22 16:11:14 +00:00
|
|
|
item.x = pos.x;
|
|
|
|
|
item.y = pos.y;
|
2019-08-22 14:59:30 +00:00
|
|
|
}
|
2019-08-16 17:14:42 +00:00
|
|
|
|
2019-08-23 10:54:19 +00:00
|
|
|
function nearestChild(item, dragCenterX, dragCenterY, container) {
|
2019-08-22 16:11:14 +00:00
|
|
|
var distance = Number.POSITIVE_INFINITY;
|
|
|
|
|
var child;
|
2019-08-23 10:54:19 +00:00
|
|
|
var pos = container.flow.mapFromItem(item, dragCenterX, dragCenterY);
|
2019-08-22 16:11:14 +00:00
|
|
|
|
2019-08-23 10:54:19 +00:00
|
|
|
// Search Right
|
2019-08-22 16:11:14 +00:00
|
|
|
for (var i = 0; i < item.width * 2; i += item.width/2) {
|
|
|
|
|
var candidate = container.flow.childAt(
|
2019-08-23 10:54:19 +00:00
|
|
|
Math.min(container.flow.width, Math.max(0, pos.x + i)),
|
|
|
|
|
Math.min(container.flow.height-1, Math.max(0, pos.y)));
|
2021-02-15 10:44:48 +00:00
|
|
|
|
2019-08-22 16:11:14 +00:00
|
|
|
if (candidate && i < distance) {
|
|
|
|
|
child = candidate;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-22 09:19:35 +00:00
|
|
|
|
2019-08-23 10:54:19 +00:00
|
|
|
// Search Left
|
2019-08-22 16:11:14 +00:00
|
|
|
for (var i = 0; i < item.width * 2; i += item.width/2) {
|
2019-08-23 10:54:19 +00:00
|
|
|
var candidate = container.flow.childAt(Math.min(container.flow.width, Math.max(0, pos.x - i)), Math.min(container.flow.height-1, Math.max(0, pos.y)));
|
2021-02-15 10:44:48 +00:00
|
|
|
|
2019-08-22 16:11:14 +00:00
|
|
|
if (candidate && i < distance) {
|
|
|
|
|
child = candidate;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-16 17:14:42 +00:00
|
|
|
|
2019-08-22 16:11:14 +00:00
|
|
|
if (!child) {
|
|
|
|
|
if (item.y < container.flow.height/2) {
|
|
|
|
|
child = container.flow.children[0];
|
|
|
|
|
} else {
|
|
|
|
|
child = container.flow.children[container.flow.children.length - 1];
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-16 17:14:42 +00:00
|
|
|
|
2019-08-22 16:11:14 +00:00
|
|
|
return child;
|
2019-08-16 17:14:42 +00:00
|
|
|
}
|
2019-08-22 09:19:35 +00:00
|
|
|
|
2019-08-16 17:14:42 +00:00
|
|
|
|
2021-02-15 10:44:48 +00:00
|
|
|
function nearestChildFromPos(x, y, container) {
|
|
|
|
|
var distance = Number.POSITIVE_INFINITY;
|
|
|
|
|
var child;
|
|
|
|
|
var pos = container.flow.mapFromGlobal(x, y);
|
|
|
|
|
|
|
|
|
|
// Search Right
|
|
|
|
|
for (var i = 0; i < appletsLayout.cellWidth * 2; i += appletsLayout.cellWidth/2) {
|
|
|
|
|
var candidate = container.flow.childAt(
|
|
|
|
|
Math.min(container.flow.width, Math.max(0, pos.x + i)),
|
|
|
|
|
Math.min(container.flow.height-1, Math.max(0, pos.y)));
|
2019-08-28 09:59:38 +00:00
|
|
|
|
2021-02-15 10:44:48 +00:00
|
|
|
if (candidate && i < distance) {
|
|
|
|
|
child = candidate;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-08-22 16:11:14 +00:00
|
|
|
}
|
2019-08-16 17:14:42 +00:00
|
|
|
|
2021-02-15 10:44:48 +00:00
|
|
|
// Search Left
|
|
|
|
|
for (var i = 0; i < appletsLayout.cellWidth * 2; i += appletsLayout.cellWidth/2) {
|
|
|
|
|
var candidate = container.flow.childAt(Math.min(container.flow.width, Math.max(0, pos.x - i)), Math.min(container.flow.height-1, Math.max(0, pos.y)));
|
2019-08-16 17:14:42 +00:00
|
|
|
|
2021-02-15 10:44:48 +00:00
|
|
|
if (candidate && i < distance) {
|
|
|
|
|
child = candidate;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-08-22 16:11:14 +00:00
|
|
|
}
|
2019-08-16 17:14:42 +00:00
|
|
|
|
2021-02-15 10:44:48 +00:00
|
|
|
if (!child) {
|
|
|
|
|
/* if (item.y < container.flow.height/2) {
|
|
|
|
|
child = container.flow.children[0];
|
|
|
|
|
} else {
|
|
|
|
|
child = container.flow.children[container.flow.children.length - 1];
|
|
|
|
|
}*/
|
2019-08-22 16:11:14 +00:00
|
|
|
}
|
2019-08-22 11:14:28 +00:00
|
|
|
|
2021-02-15 10:44:48 +00:00
|
|
|
return child;
|
2019-08-16 17:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-15 10:44:48 +00:00
|
|
|
|
2019-08-22 16:11:14 +00:00
|
|
|
function positionItem(item, dragCenterX, dragCenterY) {
|
2021-04-08 07:49:33 +00:00
|
|
|
hideSpacer();
|
2019-08-22 16:11:14 +00:00
|
|
|
var container = containerForItem(item, dragCenterX, dragCenterY);
|
|
|
|
|
|
|
|
|
|
raiseContainer(container);
|
|
|
|
|
|
|
|
|
|
if (container == appletsLayout) {
|
2021-04-07 16:41:58 +00:00
|
|
|
if (item.modelData) {
|
2022-04-06 02:06:05 +00:00
|
|
|
root.model.setLocation(item.modelData.index, MobileShell.ApplicationListModel.Desktop);
|
2021-04-07 16:41:58 +00:00
|
|
|
}
|
2019-08-28 09:59:38 +00:00
|
|
|
var pos = appletsLayout.mapFromItem(item, 0, 0);
|
2019-08-27 16:04:14 +00:00
|
|
|
item.parent = appletsLayout;
|
2019-08-28 09:59:38 +00:00
|
|
|
item.x = pos.x;
|
|
|
|
|
item.y = pos.y;
|
|
|
|
|
appletsLayout.hidePlaceHolder();
|
2019-08-22 16:11:14 +00:00
|
|
|
appletsLayout.positionItem(item);
|
2019-08-30 16:21:25 +00:00
|
|
|
|
2019-08-22 16:11:14 +00:00
|
|
|
return;
|
2019-08-30 16:21:25 +00:00
|
|
|
} else if (container == favoriteStrip) {
|
2022-04-06 02:06:05 +00:00
|
|
|
root.model.setLocation(item.modelData.index, MobileShell.ApplicationListModel.Favorites);
|
2019-08-30 16:21:25 +00:00
|
|
|
} else {
|
2022-04-06 02:06:05 +00:00
|
|
|
root.model.setLocation(item.modelData.index, MobileShell.ApplicationListModel.Grid);
|
2019-08-22 16:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var child = nearestChild(item, dragCenterX, dragCenterY, container);
|
|
|
|
|
|
2019-09-03 10:11:39 +00:00
|
|
|
putInContainerLayout(item, container);
|
2022-04-07 14:52:12 +00:00
|
|
|
MobileShell.ShellUtil.stackItemBefore(item, spacer);
|
2019-09-03 10:11:39 +00:00
|
|
|
spacer.visible = false;
|
2021-02-15 10:44:48 +00:00
|
|
|
spacer.parent = root;
|
2019-08-16 17:14:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|