From 851ff2bd05543b55fe0211e8c979f93113ed60e8 Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Fri, 16 Aug 2019 19:14:42 +0200 Subject: [PATCH] a new central LauncherDragManager.qml a central place to manage the dragging around of launchers and reparent between their 3 possible parents --- .../package/contents/ui/launcher/Delegate.qml | 41 ++---- .../contents/ui/launcher/FavoriteStrip.qml | 2 - .../ui/launcher/LauncherContainer.qml | 18 +-- .../ui/launcher/LauncherDragManager.qml | 139 ++++++++++++++++++ .../contents/ui/launcher/LauncherGrid.qml | 18 --- .../homescreen2/package/contents/ui/main.qml | 9 ++ 6 files changed, 163 insertions(+), 64 deletions(-) create mode 100644 containments/homescreen2/package/contents/ui/launcher/LauncherDragManager.qml diff --git a/containments/homescreen2/package/contents/ui/launcher/Delegate.qml b/containments/homescreen2/package/contents/ui/launcher/Delegate.qml index 31ff42cb..e0e008bb 100644 --- a/containments/homescreen2/package/contents/ui/launcher/Delegate.qml +++ b/containments/homescreen2/package/contents/ui/launcher/Delegate.qml @@ -33,9 +33,7 @@ ContainmentLayoutManager.ItemContainer { z: dragging ? 1 : 0 property var modelData: typeof model !== "undefined" ? model : null - property ContainmentLayoutManager.ItemContainer beforeItem property Item container - property ContainmentLayoutManager.ItemContainer before leftPadding: units.smallSpacing * 2 topPadding: units.smallSpacing * 2 @@ -47,72 +45,53 @@ 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); - } + launcherDragManager.showSpacer(delegate, 0, 0); +print("AAAH, BENGALAAAAH") return; } plasmoid.editMode = false; editMode = false; - if (container) { - container.forceLayout(); - container.hideSpacer(); - } - } - - onParentChanged: { - if (container) { - plasmoid.nativeInterface.orderItems(delegate, before); - } } onUserDrag: { // newPosition var newRow = 0; + var newContainer = launcherDragManager.containerForItem(delegate, dragCenter.x, dragCenter.y); + // Put it in the favorites strip - if (favoriteStrip.contains(favoriteStrip.mapFromItem(delegate, dragCenter.x, dragCenter.y))) { + if (newContainer == favoriteStrip) { 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); plasmoid.nativeInterface.applicationListModel.setFavoriteItem(index, true); // Put it on desktop - } else if (appletsLayout.contains(appletsLayout.mapFromItem(delegate, dragCenter.x, dragCenter.y))) { + } else if (newContainer == appletsLayout) { var pos = appletsLayout.mapFromItem(delegate, 0, 0); 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(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.setFavoriteItem(index, false); plasmoid.nativeInterface.applicationListModel.setDesktopItem(index, false); - - 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); } + launcherDragManager.showSpacer(delegate, dragCenter.x, dragCenter.y); + plasmoid.nativeInterface.applicationListModel.setDesktopItem(index, false); plasmoid.nativeInterface.applicationListModel.moveItem(modelData.index, newRow); - - if (container) { - container.showSpacerBefore(before); - } - - //delegate.x = newPosition.x; } contentItem: MouseArea { diff --git a/containments/homescreen2/package/contents/ui/launcher/FavoriteStrip.qml b/containments/homescreen2/package/contents/ui/launcher/FavoriteStrip.qml index 74813cd0..fe0daba1 100644 --- a/containments/homescreen2/package/contents/ui/launcher/FavoriteStrip.qml +++ b/containments/homescreen2/package/contents/ui/launcher/FavoriteStrip.qml @@ -29,8 +29,6 @@ import org.kde.kquickcontrolsaddons 2.0 LauncherContainer { id: root - property Controls.Control launcherGrid - readonly property int count: flow.width / launcherGrid.cellWidth flow.flow: Flow.TopToBottom diff --git a/containments/homescreen2/package/contents/ui/launcher/LauncherContainer.qml b/containments/homescreen2/package/contents/ui/launcher/LauncherContainer.qml index 9df3cc1b..46bb87d4 100644 --- a/containments/homescreen2/package/contents/ui/launcher/LauncherContainer.qml +++ b/containments/homescreen2/package/contents/ui/launcher/LauncherContainer.qml @@ -25,6 +25,7 @@ import org.kde.plasma.core 2.0 as PlasmaCore import org.kde.plasma.components 2.0 as PlasmaComponents import org.kde.kquickcontrolsaddons 2.0 +import org.kde.plasma.private.containmentlayoutmanager 1.0 as ContainmentLayoutManager Controls.Control { id: root @@ -32,21 +33,12 @@ Controls.Control { readonly property int reservedSpaceForLabel: metrics.height property int availableCellHeight: units.iconSizes.huge + reservedSpaceForLabel + property ContainmentLayoutManager.AppletsLayout appletsLayout + property Controls.Control launcherGrid + property Controls.Control favoriteStrip + property alias flow: applicationsFlow - function forceLayout() { - applicationsFlow.forceLayout(); - } - - function showSpacerBefore(item) { - spacer.parent = applicationsFlow - plasmoid.nativeInterface.orderItems(spacer, item); - } - - function hideSpacer() { - spacer.parent = flowParent; - } - implicitWidth: contentItem.implicitWidth + frame.margins.top + frame.margins.bottom implicitHeight: contentItem.implicitHeight + frame.margins.top + frame.margins.bottom diff --git a/containments/homescreen2/package/contents/ui/launcher/LauncherDragManager.qml b/containments/homescreen2/package/contents/ui/launcher/LauncherDragManager.qml new file mode 100644 index 00000000..77190748 --- /dev/null +++ b/containments/homescreen2/package/contents/ui/launcher/LauncherDragManager.qml @@ -0,0 +1,139 @@ +/* + * Copyright 2019 Marco Martin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +import QtQuick 2.4 + +import org.kde.plasma.private.containmentlayoutmanager 1.0 as ContainmentLayoutManager + +QtObject { + id: root + + property ContainmentLayoutManager.AppletsLayout appletsLayout + property LauncherGrid launcherGrid + property FavoriteStrip favoriteStrip + + readonly property Item spacer: Item { + width: units.gridUnit * 4 + height: width + } + + 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 = container.contentItem.mapFromItem(item, 0, 0); + + if (container == appletsLayout) { + item.parent = container; + } else { + 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 showSpacer(item, dragCenterX, dragCenterY) { + var container = containerForItem(item, dragCenterX, dragCenterY); + + raiseContainer(container); + + var child = container.flow.childAt(item.x + dragCenterX, item.y + dragCenterX); + if (!child) { + return; + } + + changeContainer(item, container); + if (container == appletsLayout) { + return; + } + + spacer.visible = false; + spacer.parent = container.flow + + if (item.x + dragCenterX < child.x + child.width / 2) { + plasmoid.nativeInterface.orderItems(spacer, child); + } else { + plasmoid.nativeInterface.orderItems(child, spacer); + } + spacer.visible = true; + } + + function positionItem(item, dragCenterX, dragCenterY) { + var container = containerForItem(item, dragCenterX, dragCenterY); + + raiseContainer(container); + + if (container == appletsLayout) { + return; + } + + spacer.visible = false; + spacer.parent = container.contentItem; + + var child = container.flow.childAt(item.x + dragCenterX, item.y + dragCenterX); + if (!child) { + return; + } + + putInContainerLayout(item, container); + if (item.x + dragCenterX < child.x + child.width / 2) { + plasmoid.nativeInterface.orderItems(item, child); + } else { + plasmoid.nativeInterface.orderItems(child, item); + } + } +} + + diff --git a/containments/homescreen2/package/contents/ui/launcher/LauncherGrid.qml b/containments/homescreen2/package/contents/ui/launcher/LauncherGrid.qml index de11fde2..7e57ca6b 100644 --- a/containments/homescreen2/package/contents/ui/launcher/LauncherGrid.qml +++ b/containments/homescreen2/package/contents/ui/launcher/LauncherGrid.qml @@ -37,9 +37,6 @@ LauncherContainer { readonly property int cellWidth: root.flow.width / Math.floor(root.flow.width / ((availableCellHeight - reservedSpaceForLabel) + units.smallSpacing*4)) readonly property int cellHeight: availableCellHeight - topPadding - property ContainmentLayoutManager.AppletsLayout appletsLayout - property FavoriteStrip favoriteStrip - Repeater { model: plasmoid.nativeInterface.applicationListModel @@ -58,33 +55,18 @@ LauncherContainer { } parent: { if (model.ApplicationOnDesktopRole) { - var pos = appletsLayout.mapFromItem(delegate, 0, 0); - x = pos.x; - y = pos.y; return appletsLayout; } if (model.ApplicationFavoriteRole) { if (editMode) { - var pos = favoriteStrip.contentItem.mapFromItem(delegate, 0, 0); - x = pos.x; - y = pos.y; return favoriteStrip.contentItem; } else { - var pos = favoriteStrip.flow.mapFromItem(delegate, 0, 0); - x = pos.x; - y = pos.y; return favoriteStrip.flow; } } if (editMode) { - var pos = flowParent.mapFromItem(delegate, 0, 0); - x = pos.x; - y = pos.y; return flowParent; } else { - var pos = root.flow.mapFromItem(delegate, 0, 0); - x = pos.x; - y = pos.y; return root.flow; } } diff --git a/containments/homescreen2/package/contents/ui/main.qml b/containments/homescreen2/package/contents/ui/main.qml index 395d4eb9..d73a0a84 100644 --- a/containments/homescreen2/package/contents/ui/main.qml +++ b/containments/homescreen2/package/contents/ui/main.qml @@ -49,6 +49,13 @@ Text { } } + Launcher.LauncherDragManager { + id: launcherDragManager + appletsLayout: appletsLayout + launcherGrid: launcher + favoriteStrip: favoriteStrip + } + Flickable { id: mainFlickable anchors { @@ -152,6 +159,7 @@ Text { placeHolder: ContainmentLayoutManager.PlaceHolder {} } } + Launcher.LauncherGrid { id: launcher Layout.fillWidth: true @@ -161,6 +169,7 @@ Text { } } } + Launcher.FavoriteStrip { id: favoriteStrip anchors.horizontalCenter: parent.horizontalCenter