From 8f85c4658adfdf7a01c591afd79baa9eed8b79dd Mon Sep 17 00:00:00 2001
From: Bharadwaj Raju <bharadwaj.raju777@protonmail.com>
Date: Tue, 12 Apr 2022 15:01:15 +0000
Subject: [PATCH] Folder View: save desktop containment icon positions on a
 per-resolution basis

BUG: 360478
BUG: 354802
FIXED-IN: 5.24.5
(cherry picked from commit 2dca17060c06f85abc365bab9484ee4446d78772)
---
 .../package/contents/ui/FolderView.qml        | 15 +--------
 .../package/contents/ui/FolderViewLayer.qml   | 32 +++++++++++++++++--
 .../desktop/plugins/folder/positioner.cpp     |  1 +
 3 files changed, 31 insertions(+), 17 deletions(-)

diff --git a/containments/desktop/package/contents/ui/FolderView.qml b/containments/desktop/package/contents/ui/FolderView.qml
index 218a3da77..72ae4903a 100644
--- a/containments/desktop/package/contents/ui/FolderView.qml
+++ b/containments/desktop/package/contents/ui/FolderView.qml
@@ -30,6 +30,7 @@ FocusScope {
     property alias currentIndex: gridView.currentIndex
     property alias url: dir.url
     property alias status: dir.status
+    property alias perStripe: positioner.perStripe
     property alias positions: positioner.positions
     property alias errorString: dir.errorString
     property alias dragging: dir.dragging
@@ -828,20 +829,6 @@ FocusScope {
                     }
                 }
 
-                onFlowChanged: {
-                    // FIXME TODO: Preserve positions.
-                    if (positioner.enabled) {
-                        positioner.reset();
-                    }
-                }
-
-                onLayoutDirectionChanged: {
-                    // FIXME TODO: Preserve positions.
-                    if (positioner.enabled) {
-                        positioner.reset();
-                    }
-                }
-
                 onCurrentIndexChanged: {
                     positionViewAtIndex(currentIndex, GridView.Contain);
                 }
diff --git a/containments/desktop/package/contents/ui/FolderViewLayer.qml b/containments/desktop/package/contents/ui/FolderViewLayer.qml
index 3f9b29937..f2044e1b0 100644
--- a/containments/desktop/package/contents/ui/FolderViewLayer.qml
+++ b/containments/desktop/package/contents/ui/FolderViewLayer.qml
@@ -29,6 +29,8 @@ FocusScope {
     property alias overflowing: folderView.overflowing
     property alias flow: folderView.flow
 
+    property string resolution: Math.round(plasmoid.screenGeometry.width) + "x" + Math.round(plasmoid.screenGeometry.height)
+
     readonly property bool lockedByKiosk: !KAuthorized.authorize("editable_desktop_icons")
 
     focus: true
@@ -178,6 +180,26 @@ FocusScope {
         }
     }
 
+    function getPositions() {
+        try {
+            var allPositions = JSON.parse(plasmoid.configuration.positions);
+        } catch (err) {
+            var allPositions = {};
+            allPositions[resolution] = plasmoid.configuration.positions;
+        }
+        return allPositions[resolution] || "";
+    }
+
+    function savePositions(positions) {
+        try {
+            var allPositions = JSON.parse(plasmoid.configuration.positions);
+        } catch (err) {
+            var allPositions = {};
+        }
+        allPositions[resolution] = positions;
+        plasmoid.configuration.positions = JSON.stringify(allPositions, Object.keys(allPositions).sort());
+    }
+
     Connections {
         target: plasmoid.configuration
 
@@ -211,7 +233,7 @@ FocusScope {
         }
 
         function onPositionsChanged() {
-            folderView.positions = plasmoid.configuration.positions;
+            folderView.positions = getPositions();
         }
     }
 
@@ -241,12 +263,16 @@ FocusScope {
         }
 
         onPositionsChanged: {
-            plasmoid.configuration.positions = folderView.positions;
+            savePositions(folderView.positions);
+        }
+
+        onPerStripeChanged: {
+            folderView.positions = getPositions();
         }
 
         Component.onCompleted: {
             folderView.sortMode = plasmoid.configuration.sortMode;
-            folderView.positions = plasmoid.configuration.positions;
+            folderView.positions = getPositions();
         }
     }
 
diff --git a/containments/desktop/plugins/folder/positioner.cpp b/containments/desktop/plugins/folder/positioner.cpp
index 859c16754..3b2df8c5a 100644
--- a/containments/desktop/plugins/folder/positioner.cpp
+++ b/containments/desktop/plugins/folder/positioner.cpp
@@ -854,6 +854,7 @@ void Positioner::applyPositions()
         }
 
         if (pos > m_perStripe) {
+            qDebug() << "Some items did not fit, rearranging";
             name = positions.at(offset);
 
             if (!sourceIndices.contains(name)) {
-- 
GitLab

