From 7ff227a13f06781ef1260ca334e8fa55e57bbb21 Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Thu, 15 Aug 2019 13:19:16 +0200 Subject: [PATCH] try a different approach to dragging --- containments/homescreen2/CMakeLists.txt | 1 + containments/homescreen2/homescreen.cpp | 11 ++++ containments/homescreen2/homescreen.h | 3 + .../package/contents/ui/launcher/Delegate.qml | 38 +++++++----- .../contents/ui/launcher/FavoriteStrip.qml | 45 ++++++++++---- .../contents/ui/launcher/LauncherGrid.qml | 58 +++++++++++++------ 6 files changed, 113 insertions(+), 43 deletions(-) diff --git a/containments/homescreen2/CMakeLists.txt b/containments/homescreen2/CMakeLists.txt index 487849d6..2e8dce79 100644 --- a/containments/homescreen2/CMakeLists.txt +++ b/containments/homescreen2/CMakeLists.txt @@ -11,6 +11,7 @@ target_link_libraries(plasma_containment_phone_homescreen2 Qt5::Gui KF5::Plasma Qt5::Qml + Qt5::Quick KF5::I18n KF5::Service KF5::KIOWidgets diff --git a/containments/homescreen2/homescreen.cpp b/containments/homescreen2/homescreen.cpp index 452667e4..34bfc764 100644 --- a/containments/homescreen2/homescreen.cpp +++ b/containments/homescreen2/homescreen.cpp @@ -22,6 +22,7 @@ #include #include +#include HomeScreen::HomeScreen(QObject *parent, const QVariantList &args) : Plasma::Containment(parent, args) @@ -39,6 +40,16 @@ ApplicationListModel *HomeScreen::applicationListModel() return m_applicationListModel; } +void HomeScreen::orderItems(QQuickItem *item1, QQuickItem *item2) +{ + if (!item1 || !item2 || item1->parentItem() != item2->parentItem()) { + return; + } + + item1->stackBefore(item2); +} + + K_EXPORT_PLASMA_APPLET_WITH_JSON(homescreen, HomeScreen, "metadata.json") #include "homescreen.moc" diff --git a/containments/homescreen2/homescreen.h b/containments/homescreen2/homescreen.h index 6a7bbf18..fc366ae0 100644 --- a/containments/homescreen2/homescreen.h +++ b/containments/homescreen2/homescreen.h @@ -24,6 +24,7 @@ #include +class QQuickItem; class ApplicationListModel; class HomeScreen : public Plasma::Containment @@ -37,6 +38,8 @@ public: ApplicationListModel *applicationListModel(); + Q_INVOKABLE void orderItems(QQuickItem *item1, QQuickItem *item2); + private: ApplicationListModel *m_applicationListModel; diff --git a/containments/homescreen2/package/contents/ui/launcher/Delegate.qml b/containments/homescreen2/package/contents/ui/launcher/Delegate.qml index 385d02f0..04415f08 100644 --- a/containments/homescreen2/package/contents/ui/launcher/Delegate.qml +++ b/containments/homescreen2/package/contents/ui/launcher/Delegate.qml @@ -33,8 +33,9 @@ ContainmentLayoutManager.ItemContainer { z: dragging ? 1 : 0 property var modelData: typeof model !== "undefined" ? model : null - property bool dragging - property ContainmentLayoutManager.ItemContainer dragDelegate + property ContainmentLayoutManager.ItemContainer beforeItem + property Item container + property ContainmentLayoutManager.ItemContainer before leftPadding: units.smallSpacing * 2 topPadding: units.smallSpacing * 2 @@ -46,29 +47,29 @@ ContainmentLayoutManager.ItemContainer { editModeCondition: ContainmentLayoutManager.ItemContainer.AfterPressAndHold//model.ApplicationOnDesktopRole ? ContainmentLayoutManager.ItemContainer.AfterPressAndHold: ContainmentLayoutManager.ItemContainer.Manual onEditModeChanged: {//FIXME: remove plasmoid.editMode = editMode + if (!editMode) { + root.forceLayout(); + } } onDragActiveChanged: { if (dragActive) { + if (container) { + container.showSpacerBefore(delegate); + } return; } plasmoid.editMode = false; editMode = false; - launcher.forceLayout(); - favoriteStrip.forceLayout(); + if (container) { + container.forceLayout(); + container.hideSpacer(); + } } - onDraggingChanged: { - if (dragging) { - var pos = dragDelegate.parent.mapFromItem(delegate, 0, 0); - dragDelegate.parent = delegate.parent.parent; - dragDelegate.x = delegate.x - dragDelegate.y = delegate.y - dragDelegate.modelData = model; - root.reorderingApps = true; - } else { - dragDelegate.modelData = null; - root.reorderingApps = false; + onParentChanged: { + if (container) { + plasmoid.nativeInterface.orderItems(delegate, before); } } @@ -80,6 +81,7 @@ ContainmentLayoutManager.ItemContainer { if (favoriteStrip.contains(favoriteStrip.mapFromItem(delegate, dragCenter.x, dragCenter.y))) { var pos = favoriteStrip.mapFromItem(delegate, 0, 0); newRow = Math.floor((pos.x + dragCenter.x) / delegate.width); + before = favoriteStrip.flow.childAt(delegate.x + dragCenter.x, delegate.y + dragCenter.y); // Put it on desktop } else if (appletsLayout.contains(appletsLayout.mapFromItem(delegate, dragCenter.x, dragCenter.y))) { @@ -87,17 +89,23 @@ ContainmentLayoutManager.ItemContainer { plasmoid.nativeInterface.applicationListModel.setDesktopItem(index, true); delegate.x = pos.x delegate.y = pos.y + before = null; return; // Put it in the general view } else { newRow = Math.round(applicationsFlow.width / delegate.width) * Math.floor((delegate.y + dragCenter.y) / delegate.height) + Math.floor((delegate.x + dragCenter.x) / delegate.width) + favoriteStrip.count; + before = applicationsFlow.childAt(delegate.x + dragCenter.x, delegate.y + dragCenter.y); } plasmoid.nativeInterface.applicationListModel.setDesktopItem(index, false); plasmoid.nativeInterface.applicationListModel.moveItem(modelData.index, newRow); + if (container) { + container.showSpacerBefore(before); + } + //delegate.x = newPosition.x; } diff --git a/containments/homescreen2/package/contents/ui/launcher/FavoriteStrip.qml b/containments/homescreen2/package/contents/ui/launcher/FavoriteStrip.qml index d928b51d..7b5bf205 100644 --- a/containments/homescreen2/package/contents/ui/launcher/FavoriteStrip.qml +++ b/containments/homescreen2/package/contents/ui/launcher/FavoriteStrip.qml @@ -29,12 +29,23 @@ import org.kde.kquickcontrolsaddons 2.0 Controls.Control { id: root + property alias flow: applicationsFlow + + property Controls.Control launcherGrid + readonly property int count: applicationsFlow.width / launcherGrid.cellWidth + function forceLayout() { applicationsFlow.forceLayout(); } - property Controls.Control launcherGrid - readonly property int count: applicationsFlow.width / launcherGrid.cellWidth + function showSpacerBefore(item) { + spacer.parent = applicationsFlow + plasmoid.nativeInterface.orderItems(spacer, item); + } + + function hideSpacer() { + spacer.parent = flowParent; + } implicitHeight: applicationsFlow.implicitHeight + frame.margins.top + frame.margins.bottom @@ -49,16 +60,30 @@ Controls.Control { anchors.fill: parent } - contentItem: Flow { - id: applicationsFlow + contentItem: Item { + id: flowParent - spacing: 0 + implicitWidth: applicationsFlow.implicitWidth + implicitHeight: applicationsFlow.implicitHeight - move: Transition { - NumberAnimation { - duration: units.longDuration - easing.type: Easing.InOutQuad - properties: "x,y" + Item { + id: spacer + width: units.gridUnit * 4 + height: width + } + + Flow { + id: applicationsFlow + anchors.fill: parent + + spacing: 0 + + move: Transition { + NumberAnimation { + duration: units.longDuration + easing.type: Easing.InOutQuad + properties: "x,y" + } } } } diff --git a/containments/homescreen2/package/contents/ui/launcher/LauncherGrid.qml b/containments/homescreen2/package/contents/ui/launcher/LauncherGrid.qml index 9d6c8f6c..44f529ec 100644 --- a/containments/homescreen2/package/contents/ui/launcher/LauncherGrid.qml +++ b/containments/homescreen2/package/contents/ui/launcher/LauncherGrid.qml @@ -30,9 +30,8 @@ import org.kde.plasma.private.containmentlayoutmanager 1.0 as ContainmentLayoutM Controls.Control { id: root - function forceLayout() { - applicationsFlow.forceLayout(); - } + property alias flow: applicationsFlow + readonly property bool dragging: applicationsFlow.dragData property bool reorderingApps: false @@ -49,6 +48,19 @@ Controls.Control { signal externalDragStarted signal dragPositionChanged(point pos) + function forceLayout() { + applicationsFlow.forceLayout(); + } + + function showSpacerBefore(item) { + spacer.parent = applicationsFlow + plasmoid.nativeInterface.orderItems(spacer, item); + } + + function hideSpacer() { + spacer.parent = flowParent; + } + implicitHeight: applicationsFlow.implicitHeight + frame.margins.top + frame.margins.bottom leftPadding: frame.margins.left @@ -63,6 +75,7 @@ Controls.Control { } contentItem: Item { + id: flowParent //NOTE: TextMetrics can't handle multi line Controls.Label { id: metrics @@ -70,19 +83,12 @@ Controls.Control { visible: false } - //This Delegate is the placeholder for the "drag" - //delegate (that is not actual drag and drop - Delegate { - id: dragDelegateItem - z: 999 - width: root.cellWidth - height: root.cellHeight - onYChanged: dragPositionChanged(Qt.point(x, y)) - opacity: 1 - - visible: modelData !== null + Item { + id: spacer + width: units.gridUnit * 4 + height: width + visible:parent == applicationsFlow } - Flow { id: applicationsFlow anchors.fill: parent @@ -114,15 +120,31 @@ Controls.Control { delegate: Delegate { width: root.cellWidth height: root.cellHeight - dragDelegate: dragDelegateItem + container: { + if (model.ApplicationOnDesktopRole) { + return null; + } + if (index < favoriteStrip.count) { + return favoriteStrip; + } + return root; + } parent: { if (model.ApplicationOnDesktopRole) { return appletsLayout; } if (index < favoriteStrip.count) { - return favoriteStrip.contentItem; + if (editMode) { + return favoriteStrip.contentItem; + } else { + return favoriteStrip.flow; + } + } + if (editMode) { + return flowParent; + } else { + return applicationsFlow; } - return applicationsFlow; } } }