fix dragging to appletslayout

still a crash on the other way around
This commit is contained in:
Marco Martin 2019-08-22 18:11:14 +02:00
parent 8c1b75a777
commit 0d78a4e1d7
2 changed files with 182 additions and 161 deletions

View file

@ -46,57 +46,25 @@ ContainmentLayoutManager.ItemContainer {
property real dragCenterX property real dragCenterX
property real dragCenterY property real dragCenterY
editModeCondition: ContainmentLayoutManager.ItemContainer.AfterPressAndHold//model.ApplicationOnDesktopRole ? ContainmentLayoutManager.ItemContainer.AfterPressAndHold: ContainmentLayoutManager.ItemContainer.Manual editModeCondition: ContainmentLayoutManager.ItemContainer.AfterPressAndHold
onEditModeChanged: {//FIXME: remove onEditModeChanged: {//FIXME: remove
plasmoid.editMode = editMode plasmoid.editMode = editMode
} }
onDragActiveChanged: { onDragActiveChanged: {
if (dragActive) { if (dragActive) {
// Must be 0, 0 as at this point dragCenterX and dragCenterY are on the drag before" // Must be 0, 0 as at this point dragCenterX and dragCenterY are on the drag before"
launcherDragManager.showSpacer(delegate, 0, 0); launcherDragManager.startDrag(delegate);
} else { } else {
launcherDragManager.positionItem(delegate, dragCenterX, dragCenterY); launcherDragManager.dropItem(delegate, dragCenterX, dragCenterY);
plasmoid.editMode = false; plasmoid.editMode = false;
editMode = false; editMode = false;
} }
} }
onUserDrag: { onUserDrag: {
// newPosition
var newRow = 0;
dragCenterX = dragCenter.x; dragCenterX = dragCenter.x;
dragCenterY = dragCenter.y; dragCenterY = dragCenter.y;
var newContainer = launcherDragManager.containerForItem(delegate, dragCenter.x, dragCenter.y); launcherDragManager.dragItem(delegate, index, dragCenter.x, dragCenter.y);
// Put it in the favorites strip
if (newContainer == favoriteStrip) {
var pos = favoriteStrip.mapFromItem(delegate, 0, 0);
newRow = Math.floor((pos.x + dragCenter.x) / delegate.width);
plasmoid.nativeInterface.applicationListModel.setLocation(index, ApplicationListModel.Favorites);
// Put it on desktop
} else if (newContainer == appletsLayout) {
var pos = appletsLayout.mapFromItem(delegate, 0, 0);
plasmoid.nativeInterface.applicationListModel.setLocation(index, ApplicationListModel.Desktop);
print("!!!!!!!!!!!!"+pos.x+" "+pos.y)
// delegate.x = pos.x
// delegate.y = pos.y
return;
// Put it in the general view
} else {
newRow = Math.round(newContainer.flow.width / delegate.width) * Math.floor((delegate.y + dragCenter.y) / delegate.height) + Math.floor((delegate.x + dragCenter.x) / delegate.width) + favoriteStrip.count;
plasmoid.nativeInterface.applicationListModel.setLocation(index, ApplicationListModel.Grid);
}
launcherDragManager.showSpacer(delegate, dragCenter.x, dragCenter.y);
plasmoid.nativeInterface.applicationListModel.setDesktopItem(index, false);
plasmoid.nativeInterface.applicationListModel.moveItem(modelData.index, newRow);
} }
contentItem: MouseArea { contentItem: MouseArea {

View file

@ -21,6 +21,8 @@ import QtQuick 2.4
import org.kde.plasma.private.containmentlayoutmanager 1.0 as ContainmentLayoutManager import org.kde.plasma.private.containmentlayoutmanager 1.0 as ContainmentLayoutManager
import org.kde.phone.homescreen 1.0
QtObject { QtObject {
id: root id: root
@ -33,152 +35,203 @@ QtObject {
height: width height: width
} }
function raiseContainer(container) { function startDrag(item) {
container.z = 1; internal.showSpacer(item, 0, 0);
if (container == appletsLayout) {
launcherGrid.z = 0;
favoriteStrip.z = 0;
} else if (container == favoriteStrip) {
appletsLayout.z = 0;
launcherGrid.z = 0;
} else {
appletsLayout.z = 0;
favoriteStrip.z = 0;
}
} }
function containerForItem(item, dragCenterX, dragCenterY) { function dragItem(delegate, index, dragCenterX, dragCenterY) {
if (favoriteStrip.contains(favoriteStrip.mapFromItem(item, dragCenterX, dragCenterY))) { // newPosition
return favoriteStrip; var newRow = 0;
} else if (appletsLayout.contains(appletsLayout.mapFromItem(item, dragCenterX, dragCenterY))) {
return appletsLayout;
} else {
return launcherGrid;
}
}
function changeContainer(item, container) { var newContainer = internal.containerForItem(delegate, dragCenterX, dragCenterY);
var pos;
print("$$$$$$$$"+container)
if (container == appletsLayout) {
pos = container.mapFromItem(item, 0, 0);
item.parent = container;
} else {
pos = container.contentItem.mapFromItem(item, 0, 0);
item.parent = container.contentItem;
}
item.x = pos.x; // Put it in the favorites strip
item.y = pos.y; if (newContainer == favoriteStrip) {
} var pos = favoriteStrip.mapFromItem(delegate, 0, 0);
newRow = Math.floor((pos.x + dragCenterX) / delegate.width);
function putInContainerLayout(item, container) { plasmoid.nativeInterface.applicationListModel.setLocation(index, ApplicationListModel.Favorites);
var pos = container.contentItem.mapFromItem(item, 0, 0);
print("££££££££££££££££"+container)
if (container == appletsLayout) {
item.parent = container;
} else {
item.parent = container.flow;
}
item.x = pos.x; internal.showSpacer(delegate, dragCenterX, dragCenterY);
item.y = pos.y; plasmoid.nativeInterface.applicationListModel.moveItem(modelData.index, newRow);
}
function nearestChild (item, dragCenterX, dragCenterY, container) { // Put it on desktop
var distance = Number.POSITIVE_INFINITY; } else if (newContainer == appletsLayout) {
var child; var pos = appletsLayout.mapFromItem(delegate, 0, 0);
plasmoid.nativeInterface.applicationListModel.setLocation(index, ApplicationListModel.Desktop);
// Search Left
for (var i = 0; i < item.width * 2; i += item.width/2) { internal.showSpacer(delegate, dragCenterX, dragCenterY);
var candidate = container.flow.childAt(
Math.min(container.flow.width, Math.max(0, item.x + dragCenterX + i)),
Math.min(container.flow.height-1, Math.max(0, item.y + dragCenterY)));
if (candidate && i < distance) {
child = candidate;
break;
}
}
// Search Right
for (var i = 0; i < item.width * 2; i += item.width/2) {
var candidate = container.flow.childAt(Math.min(container.flow.width, Math.max(0, item.x + dragCenterX - i)), Math.min(container.flow.height-1, Math.max(0, item.y + dragCenterY)));
if (candidate && i < distance) {
child = candidate;
break;
}
}
if (!child) {
if (item.y < container.flow.height/2) {
child = container.flow.children[0];
} else {
child = container.flow.children[container.flow.children.length - 1];
}
}
return child;
}
function showSpacer(item, dragCenterX, dragCenterY) {
var container = containerForItem(item, dragCenterX, dragCenterY);
raiseContainer(container);
print("&&&&&&&&&&&"+container)
if (container == appletsLayout) {
spacer.visible = false;
changeContainer(item, container);
return; return;
// Put it in the general view
} else {
newRow = Math.round(newContainer.flow.width / delegate.width) * Math.floor((delegate.y + dragCenterY) / delegate.height) + Math.floor((delegate.x + dragCenterX) / delegate.width) + favoriteStrip.count;
plasmoid.nativeInterface.applicationListModel.setLocation(index, ApplicationListModel.Grid);
internal.showSpacer(delegate, dragCenterX, dragCenterY);
plasmoid.nativeInterface.applicationListModel.moveItem(modelData.index, newRow);
} }
var child = nearestChild(item, dragCenterX, dragCenterY, container); internal.showSpacer(delegate, dragCenterX, dragCenterY);
plasmoid.nativeInterface.applicationListModel.moveItem(modelData.index, newRow);
}
function dropItem(item, dragCenterX, dragCenterY) {
internal.positionItem(item, dragCenterX, dragCenterY);
}
// Those should never be accessed from outside
property QtObject __internal: QtObject {
id: internal
function raiseContainer(container) {
container.z = 1;
if (container == appletsLayout) {
launcherGrid.z = 0;
favoriteStrip.z = 0;
} else if (container == favoriteStrip) {
appletsLayout.z = 0;
launcherGrid.z = 0;
} else {
appletsLayout.z = 0;
favoriteStrip.z = 0;
}
}
function containerForItem(item, dragCenterX, dragCenterY) {
if (favoriteStrip.contains(favoriteStrip.mapFromItem(item, dragCenterX, dragCenterY))) {
return favoriteStrip;
} else if (appletsLayout.contains(appletsLayout.mapFromItem(item, dragCenterX, dragCenterY))) {
return appletsLayout;
} else {
return launcherGrid;
}
}
function changeContainer(item, container) {
var pos;
if (container == appletsLayout) {
pos = container.mapFromItem(item, 0, 0);
item.parent = container;
} else {
pos = container.contentItem.mapFromItem(item, 0, 0);
item.parent = container.contentItem;
}
item.x = pos.x;
item.y = pos.y;
}
function putInContainerLayout(item, container) {
var pos = container.contentItem.mapFromItem(item, 0, 0);
if (container == appletsLayout) {
item.parent = container;
} else {
item.parent = container.flow;
}
item.x = pos.x;
item.y = pos.y;
}
function nearestChild (item, dragCenterX, dragCenterY, container) {
var distance = Number.POSITIVE_INFINITY;
var child;
// Search Left
for (var i = 0; i < item.width * 2; i += item.width/2) {
var candidate = container.flow.childAt(
Math.min(container.flow.width, Math.max(0, item.x + dragCenterX + i)),
Math.min(container.flow.height-1, Math.max(0, item.y + dragCenterY)));
if (candidate && i < distance) {
child = candidate;
break;
}
}
// Search Right
for (var i = 0; i < item.width * 2; i += item.width/2) {
var candidate = container.flow.childAt(Math.min(container.flow.width, Math.max(0, item.x + dragCenterX - i)), Math.min(container.flow.height-1, Math.max(0, item.y + dragCenterY)));
if (candidate && i < distance) {
child = candidate;
break;
}
}
if (!child) {
if (item.y < container.flow.height/2) {
child = container.flow.children[0];
} else {
child = container.flow.children[container.flow.children.length - 1];
}
}
return child;
}
function showSpacer(item, dragCenterX, dragCenterY) {
var container = containerForItem(item, dragCenterX, dragCenterY);
raiseContainer(container);
if (container == appletsLayout) {
spacer.visible = false;
changeContainer(item, container);
return;
}
var child = nearestChild(item, dragCenterX, dragCenterY, container);
if (!child) {
spacer.visible = false;
spacer.parent = container.flow
return;
}
if (!child) {
spacer.visible = false; spacer.visible = false;
spacer.parent = container.flow spacer.parent = container.flow
return;
if (item.x + dragCenterX < child.x + child.width / 2) {
plasmoid.nativeInterface.stackBefore(spacer, child);
} else {
plasmoid.nativeInterface.stackAfter(spacer, child);
}
changeContainer(item, container);
spacer.visible = true;
} }
spacer.visible = false; function positionItem(item, dragCenterX, dragCenterY) {
spacer.parent = container.flow var container = containerForItem(item, dragCenterX, dragCenterY);
if (item.x + dragCenterX < child.x + child.width / 2) { raiseContainer(container);
plasmoid.nativeInterface.stackBefore(spacer, child);
} else {
plasmoid.nativeInterface.stackAfter(spacer, child);
}
changeContainer(item, container); if (container == appletsLayout) {
appletsLayout.positionItem(item);
return;
}
spacer.visible = true; spacer.visible = false;
} spacer.parent = container.contentItem;
function positionItem(item, dragCenterX, dragCenterY) { var child = nearestChild(item, dragCenterX, dragCenterY, container);
var container = containerForItem(item, dragCenterX, dragCenterY);
raiseContainer(container); if (!child) {
putInContainerLayout(item, container);
return;
}
if (container == appletsLayout) { if (item.x + dragCenterX < child.x + child.width / 2) {
return; putInContainerLayout(item, container);
} plasmoid.nativeInterface.stackBefore(item, child);
} else {
spacer.visible = false; putInContainerLayout(item, container);
spacer.parent = container.contentItem; plasmoid.nativeInterface.stackAfter(item, child);
}
var child = nearestChild(item, dragCenterX, dragCenterY, container);
if (!child) {
putInContainerLayout(item, container);
return;
}
if (item.x + dragCenterX < child.x + child.width / 2) {
putInContainerLayout(item, container);
plasmoid.nativeInterface.stackBefore(item, child);
} else {
putInContainerLayout(item, container);
plasmoid.nativeInterface.stackAfter(item, child);
} }
} }
} }