[gnome-shell/app-picker-refresh: 12/16] appDisplay: Add AlphabeticalView.addFolder() method



commit 559e4b58f94ff04b6daaf803a7d41feb40780a3c
Author: Florian Müllner <fmuellner gnome org>
Date:   Thu Jan 31 15:32:02 2013 +0100

    appDisplay: Add AlphabeticalView.addFolder() method
    
    Adjust AlphabeticalView to be able to hold both apps and folders,
    and add a addFolder() method to insert a folder.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=694192

 js/ui/appDisplay.js |   66 +++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 49 insertions(+), 17 deletions(-)
---
diff --git a/js/ui/appDisplay.js b/js/ui/appDisplay.js
index 616d7cf..5c24404 100644
--- a/js/ui/appDisplay.js
+++ b/js/ui/appDisplay.js
@@ -62,8 +62,8 @@ const AlphabeticalView = new Lang.Class({
         this._grid = new IconGrid.IconGrid({ xAlign: St.Align.MIDDLE,
                                              columnLimit: MAX_COLUMNS });
 
-        this._appIcons = {}; // desktop file id
-        this._allApps = [];
+        this._items = {}; // desktop file id
+        this._allItems = [];
 
         let box = new St.BoxLayout({ vertical: true });
         this._stack = new St.Widget({ layout_manager: new Clutter.BinLayout() });
@@ -95,18 +95,45 @@ const AlphabeticalView = new Lang.Class({
 
     removeAll: function() {
         this._grid.removeAll();
-        this._appIcons = {};
-        this._allApps = [];
+        this._items = {};
+        this._allItems = [];
+    },
+
+    _getItemId: function(item) {
+        if (item instanceof Shell.App)
+            return item.get_id();
+        else if (item instanceof GMenu.TreeDirectory)
+            return item.get_menu_id();
+        else
+            return null;
+    },
+
+    _compareItems: function(itemA, itemB) {
+        // bit of a hack: rely on both ShellApp and GMenuTreeDirectory
+        // having a get_name() method
+        let nameA = GLib.utf8_collate_key(itemA.get_name(), -1);
+        let nameB = GLib.utf8_collate_key(itemB.get_name(), -1);
+        return (nameA > nameB) ? 1 : (nameA < nameB ? -1 : 0);
     },
 
     addApp: function(app) {
-        let id = app.get_id();
-        if (this._appIcons[id] !== undefined)
+        let id = this._getItemId(app);
+        if (this._items[id] !== undefined)
             return;
 
         let appIcon = new AppIcon(app);
-        this._allApps.push(app);
-        this._appIcons[id] = appIcon;
+        this._allItems.push(app);
+        this._items[id] = appIcon;
+    },
+
+    addFolder: function(dir) {
+        let id = this._getItemId(dir);
+        if (this._items[id] !== undefined)
+            return;
+
+        let folderIcon = new FolderIcon(dir, this);
+        this._allItems.push(dir);
+        this._items[id] = folderIcon;
     },
 
     addFolderPopup: function(popup) {
@@ -119,14 +146,14 @@ const AlphabeticalView = new Lang.Class({
     },
 
     loadGrid: function() {
-        this._allApps.sort(function(a, b) { return a.compare_by_name(b); });
+        this._allItems.sort(this._compareItems);
 
-        for (let i = 0; i < this._allApps.length; i++) {
-            let id = this._allApps[i].get_id();
+        for (let i = 0; i < this._allItems.length; i++) {
+            let id = this._getItemId(this._allItems[i]);
             if (!id)
                 continue;
-            this._grid.addItem(this._appIcons[id].actor);
-            this._appIcons[id].actor.connect('key-focus-in', Lang.bind(this, this._ensureIconVisible));
+            this._grid.addItem(this._items[id].actor);
+            this._items[id].actor.connect('key-focus-in', Lang.bind(this, this._ensureIconVisible));
         }
     },
 
@@ -163,13 +190,18 @@ const AlphabeticalView = new Lang.Class({
         let subSize = Math.floor(FOLDER_SUBICON_FRACTION * size);
 
         let aligns = [ Clutter.ActorAlign.START, Clutter.ActorAlign.END ];
-        for (let i = 0; i < Math.min(this._allApps.length, 4); i++) {
-            let texture = this._allApps[i].create_icon_texture(subSize);
+        let n = 0;
+        for (let i = 0; i < this._allItems.length; i++) {
+            if (!this._allItems[i] instanceof Shell.App)
+                continue;
+            let texture = this._allItems[i].create_icon_texture(subSize);
             let bin = new St.Bin({ child: texture,
                                    x_expand: true, y_expand: true });
-            bin.set_x_align(aligns[i % 2]);
-            bin.set_y_align(aligns[Math.floor(i / 2)]);
+            bin.set_x_align(aligns[n % 2]);
+            bin.set_y_align(aligns[Math.floor(n / 2)]);
             icon.add_actor(bin);
+            if (++n == 4)
+                break;
         }
 
         return icon;


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