From 2fd5f51e8d31094a26180b43d452ecc0c80dacbc Mon Sep 17 00:00:00 2001
From: Michail Vourlakos <mvourlakos@gmail.com>
Date: Thu, 31 Dec 2020 11:09:32 +0200
Subject: [PATCH] fixes for justify alignment arrangement

---
 app/view/containmentinterface.cpp             | 13 +++--
 .../package/contents/code/LayoutManager.js    |  2 +-
 .../package/contents/ui/DragDropArea.qml      |  2 +-
 .../contents/ui/editmode/ConfigOverlay.qml    |  2 +-
 containment/package/contents/ui/main.qml      | 52 ++-----------------
 5 files changed, 14 insertions(+), 57 deletions(-)

diff --git a/app/view/containmentinterface.cpp b/app/view/containmentinterface.cpp
index 6b4a907d7..71eaafa6a 100644
--- a/app/view/containmentinterface.cpp
+++ b/app/view/containmentinterface.cpp
@@ -665,6 +665,7 @@ void ContainmentInterface::moveAppletsInJustifyAlignment(QQuickItem *start, QQui
             if (issplitter) {
                 secondSplitterFound = true;
                 splitter2 = i;
+                appletlist[i]->setParentItem(end);
             } else {
                 appletlist[i]->setParentItem(main);
             }
@@ -673,11 +674,13 @@ void ContainmentInterface::moveAppletsInJustifyAlignment(QQuickItem *start, QQui
         }
     }
 
-    if (splitter1>0) {
-        appletlist[splitter1]->stackAfter(appletlist[splitter1 - 1]);
-    }
-    if (splitter2>0 && splitter2<appletlist.count()-1) {
-        appletlist[splitter2]->stackBefore(appletlist[splitter2 + 1]);
+    for(int i=0; i<appletlist.count()-2; ++i) {
+        QQuickItem *before = appletlist[i];
+        QQuickItem *after = appletlist[i+1];
+
+        if (before->parentItem() == after->parentItem()) {
+            before->stackBefore(after);
+        }
     }
 }
 
diff --git a/containment/package/contents/code/LayoutManager.js b/containment/package/contents/code/LayoutManager.js
index 296ff3a31..e87097fb6 100644
--- a/containment/package/contents/code/LayoutManager.js
+++ b/containment/package/contents/code/LayoutManager.js
@@ -112,7 +112,7 @@ function restore() {
     inRestore = false;
 
     if (plasmoid.configuration.alignment === 10/*Justify*/) {
-        root.splitMainLayoutToLayouts();
+        root.moveAppletsBasedOnJustifyAlignment();
         root.updateIndexes();
     }
 }
diff --git a/containment/package/contents/ui/DragDropArea.qml b/containment/package/contents/ui/DragDropArea.qml
index 452910314..3440c6336 100644
--- a/containment/package/contents/ui/DragDropArea.qml
+++ b/containment/package/contents/ui/DragDropArea.qml
@@ -219,7 +219,7 @@ DragDrop.DropArea {
         dndSpacer.opacity = 0;
 
         if (dragInfo.isPlasmoid && root.panelAlignment === LatteCore.Types.Justify) {
-            root.layoutManagerMoveAppletsBasedOnJustifyAlignment();
+            root.moveAppletsBasedOnJustifyAlignment();
         }
     }
 }
diff --git a/containment/package/contents/ui/editmode/ConfigOverlay.qml b/containment/package/contents/ui/editmode/ConfigOverlay.qml
index d5adac6b5..75dffece8 100644
--- a/containment/package/contents/ui/editmode/ConfigOverlay.qml
+++ b/containment/package/contents/ui/editmode/ConfigOverlay.qml
@@ -249,7 +249,7 @@ MouseArea {
         //     handle.width = currentApplet.width;
         //    handle.height = currentApplet.height;
         root.layoutManagerSave();
-        root.layoutManagerMoveAppletsBasedOnJustifyAlignment();
+        root.moveAppletsBasedOnJustifyAlignment();
         root.layouter.appletsInParentChange = false;
         layouter.updateSizeForAppletsInFill();
     }
diff --git a/containment/package/contents/ui/main.qml b/containment/package/contents/ui/main.qml
index 30883f7af..02f0626ec 100644
--- a/containment/package/contents/ui/main.qml
+++ b/containment/package/contents/ui/main.qml
@@ -515,7 +515,7 @@ Item {
         if (root.editMode){
             if (panelAlignment===LatteCore.Types.Justify) {
                 addInternalViewSplittersInMainLayout();
-                splitMainLayoutToLayouts();
+                moveAppletsBasedOnJustifyAlignment();
             } else {
                 joinLayoutsToMainLayout();
                 root.destroyInternalViewSplitters();
@@ -975,10 +975,8 @@ Item {
         }
     }
 
-    function layoutManagerMoveAppletsBasedOnJustifyAlignment() {
-        if (plasmoid.configuration.alignment !== 10) {
-            return;
-        }
+    function moveAppletsBasedOnJustifyAlignment() {
+        layouter.appletsInParentChange = true;
 
         if (latteView) {
             latteView.extendedInterface.moveAppletsInJustifyAlignment(layoutsContainer.startLayout,
@@ -989,50 +987,6 @@ Item {
         layouter.appletsInParentChange = false;
     }
 
-    function splitMainLayoutToLayouts() {
-        if (internalViewSplittersCount() === 2) {
-            layouter.appletsInParentChange = true;
-
-            console.log("LAYOUTS: Moving applets from MAIN to THREE Layouts mode...");
-            var splitter = -1;
-            var splitter2 = -1;
-
-            var totalChildren = layoutsContainer.mainLayout.children.length;
-            for (var i=0; i<totalChildren; ++i) {
-                var item = layoutsContainer.mainLayout.children[i];
-
-                if(item.isInternalViewSplitter && splitter === -1) {
-                    splitter = i;
-                } else if (item.isInternalViewSplitter && splitter>=0 && splitter2 === -1) {
-                    splitter2 = i;
-                }
-            }
-
-            // console.log("update layouts 1:"+splitter + " - "+splitter2);
-
-            if (splitter > 0) {
-                for (var i=0; i<=splitter; ++i){
-                    var item = layoutsContainer.mainLayout.children[0];
-                    item.parent = layoutsContainer.startLayout;
-                }
-            }
-
-            if (splitter2 > 0) {
-                splitter2 = splitter2 - splitter - 1;
-                // console.log("update layouts 2:"+splitter + " - "+splitter2);
-
-                totalChildren = layoutsContainer.mainLayout.children.length;
-
-                for (var i=totalChildren-1; i>=splitter2; --i){
-                    var item = layoutsContainer.mainLayout.children[i];
-                    LayoutManager.insertAtIndex(layoutsContainer.endLayout, item, 0);
-                }
-            }
-
-            layouter.appletsInParentChange = false;
-        }
-    }
-
     function joinLayoutsToMainLayout() {
         layouter.appletsInParentChange = true;