[gnome-shell/T27795: 133/138] appDisplay: Add moveItem()



commit a1f41d4f572d87cb9354ac54e77c39845c8cd76a
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Tue Jul 2 17:41:15 2019 -0300

    appDisplay: Add moveItem()
    
    This is a handy function to implement changing an icon's position
    in the grid.
    
    WIP: better commit message
    
    https://gitlab.gnome.org/GNOME/gnome-shell/merge_requests/603

 js/ui/appDisplay.js | 22 ++++++++++++++++++++++
 js/ui/iconGrid.js   | 16 ++++++++++++++++
 2 files changed, 38 insertions(+)
---
diff --git a/js/ui/appDisplay.js b/js/ui/appDisplay.js
index 20e07659e9..953bd4851b 100644
--- a/js/ui/appDisplay.js
+++ b/js/ui/appDisplay.js
@@ -223,6 +223,28 @@ class BaseAppView {
         return a.name.localeCompare(b.name);
     }
 
+    moveItem(item, newPosition) {
+        let itemIndex = this._allItems.indexOf(item);
+
+        if (itemIndex == -1) {
+            log('Trying to move item %s that is not in this app view'.format(item.id));
+            return;
+        }
+
+        let visibleItems = this._allItems.filter(item => item.actor.visible);
+        let visibleIndex = visibleItems.indexOf(item);
+        if (newPosition > visibleIndex)
+            newPosition -= 1;
+
+        // Remove from the old position
+        this._allItems.splice(itemIndex, 1);
+
+        let realPosition = this._grid.moveItem(item, newPosition);
+        this._allItems.splice(realPosition, 0, item);
+
+        return realPosition;
+    }
+
     _selectAppInternal(id) {
         if (this._items[id])
             this._items[id].actor.navigate_focus(null, St.DirectionType.TAB_FORWARD, false);
diff --git a/js/ui/iconGrid.js b/js/ui/iconGrid.js
index 885d43e282..427e43ea00 100644
--- a/js/ui/iconGrid.js
+++ b/js/ui/iconGrid.js
@@ -678,6 +678,22 @@ var IconGrid = GObject.registerClass({
             this.add_actor(item.actor);
     }
 
+    moveItem(item, newPosition) {
+        if (!this.contains(item.actor)) {
+            log('Cannot move item not contained by the IconGrid');
+            return;
+        }
+
+        let children = this.get_children();
+        let visibleChildren = children.filter(c => c.is_visible());
+        let visibleChildAtPosition = visibleChildren[newPosition];
+        let realPosition = children.indexOf(visibleChildAtPosition);
+
+        this.set_child_at_index(item.actor, realPosition);
+
+        return realPosition;
+    }
+
     removeItem(item) {
         this.remove_child(item.actor);
     }


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