[gnome-shell/gbsneto/icon-grid-dnd: 22/35] allView: Add support for custom positioning



commit 67a159fe5beba740b1430f091b85468f9c3ee915
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Tue Jul 2 13:38:11 2019 -0300

    allView: Add support for custom positioning
    
    Use the new 'icon-grid-layout' key to sort the applications and folders
    as it is saved. Applications that are not in the key are appended, and
    when compared to each other, fallback to alphabetical order.
    
    Because the 'icon-grid-layout' key is, by default, empty, this means that
    all icons and folders are sorted alphabetically the first time they are
    displayed, preserving the current behavior.
    
    This commit does not add any way to change the order without modifying
    the GSettings key directly.
    
    https://gitlab.gnome.org/GNOME/gnome-shell/merge_requests/603

 js/ui/appDisplay.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
---
diff --git a/js/ui/appDisplay.js b/js/ui/appDisplay.js
index 7eaf35bd3..bcdef15bb 100644
--- a/js/ui/appDisplay.js
+++ b/js/ui/appDisplay.js
@@ -333,6 +333,12 @@ var AllView = class AllView extends BaseAppView {
             Main.queueDeferredWork(this._redisplayWorkId);
         });
 
+        this._gridSettings = new Gio.Settings({ schema_id: 'org.gnome.shell' });
+        this._gridChangedId = this._gridSettings.connect('changed::icon-grid-layout', () => {
+            if (!this._blockGridSettings)
+                Main.queueDeferredWork(this._redisplayWorkId);
+        });
+
         Main.overview.connect('item-drag-begin', this._onDragBegin.bind(this));
         Main.overview.connect('item-drag-end', this._onDragEnd.bind(this));
 
@@ -413,12 +419,53 @@ var AllView = class AllView extends BaseAppView {
             newApps.push(icon);
         });
 
+        let gridLayout = this._gridSettings.get_strv('icon-grid-layout');
+
+        // Sort the list of apps according to the saved layout. The fallback
+        // is sorting alphabetically.
+        newApps.sort((a, b) => {
+            let indexA = gridLayout.indexOf(a.id);
+            let indexB = gridLayout.indexOf(b.id);
+
+            if (indexA == -1 && indexB == -1)
+                return a.name.localeCompare(b.name);
+            else if (indexA == -1)
+                return 1;
+            else if (indexB == -1)
+                return -1;
+            else
+                return indexA - indexB;
+        });
+
         return newApps;
     }
 
+    _saveGridLayout() {
+        let appIds = this._allItems.filter(icon => icon.actor.visible).map(icon => icon.id);
+        let gridLayout = this._gridSettings.get_strv('icon-grid-layout');
+
+        // Only save the new layout if it changed
+        let changed = appIds.length != gridLayout.length;
+        if (!changed) {
+            for (let i = 0; i < appIds.length; i++) {
+                if (appIds[i] != gridLayout[i]) {
+                    changed = true;
+                    break;
+                }
+            }
+        }
+
+        if (changed) {
+            this._blockGridSettings = true;
+            this._gridSettings.set_strv('icon-grid-layout', appIds);
+            this._blockGridSettings = false;
+        }
+    }
+
     _loadGrid() {
         super._loadGrid();
         this._refilterApps();
+        this._saveGridLayout();
     }
 
     // Overridden from BaseAppView


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]