[gnome-shell-extensions] all: port to the new class framework



commit 9b6374881d3a30a654009c80a38704555f1eeca4
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Wed Feb 8 17:24:23 2012 +0100

    all: port to the new class framework
    
    A long due update, since this was merged in gjs and core shell.
    We no longer need to mess with __proto__ and prototype, and can
    use decent syntax for true object oriented programming.
    (This affects all except xrandr-indicator, since I want to port
    that to GDBus too, using the new bindings right from the start)

 extensions/apps-menu/extension.js               |   48 +++---
 extensions/auto-move-windows/extension.js       |    8 +-
 extensions/dock/extension.js                    |   14 +-
 extensions/drive-menu/extension.js              |   37 ++---
 extensions/native-window-placement/extension.js |   13 +-
 extensions/places-menu/extension.js             |   28 ++--
 extensions/systemMonitor/extension.js           |   39 ++---
 extensions/user-theme/extension.js              |    9 +-
 extensions/workspace-indicator/extension.js     |  198 ++++++++++++-----------
 9 files changed, 201 insertions(+), 193 deletions(-)
---
diff --git a/extensions/apps-menu/extension.js b/extensions/apps-menu/extension.js
index a5de2c2..6081f4d 100644
--- a/extensions/apps-menu/extension.js
+++ b/extensions/apps-menu/extension.js
@@ -10,17 +10,13 @@ const PanelMenu = imports.ui.panelMenu;
 const PopupMenu = imports.ui.popupMenu;
 
 const ICON_SIZE = 28;
-let appsys = Shell.AppSystem.get_default();
 
-function AppMenuItem() {
-    this._init.apply(this, arguments);
-}
-
-AppMenuItem.prototype = {
-    __proto__: PopupMenu.PopupBaseMenuItem.prototype,
+const AppMenuItem = new Lang.Class({
+    Name: 'AppsMenu.AppMenuItem',
+    Extends: PopupMenu.PopupBaseMenuItem,
 
     _init: function (app, params) {
-        PopupMenu.PopupBaseMenuItem.prototype._init.call(this, params);
+        this.parent(params);
 
         this._app = app;
         this.label = new St.Label({ text: app.get_name() });
@@ -32,30 +28,36 @@ AppMenuItem.prototype = {
     activate: function (event) {
         this._app.activate_full(-1, event.get_time());
 
-        PopupMenu.PopupBaseMenuItem.prototype.activate.call(this, event);
+        this.parent(event);
     }
 
-};
+});
 
-function ApplicationsButton() {
-    this._init();
-}
-
-ApplicationsButton.prototype = {
-    __proto__: PanelMenu.SystemStatusButton.prototype,
+const ApplicationsButton = new Lang.Class({
+    Name: 'AppsMenu.ApplicationsButton',
+    Extends: PanelMenu.SystemStatusButton,
 
     _init: function() {
-        PanelMenu.SystemStatusButton.prototype._init.call(this, 'start-here');
+        this.parent('start-here');
+
+        this._appSys = Shell.AppSystem.get_default();
+        this._installedChangedId = this._appSys.connect('installed-changed', Lang.bind(this, this._refresh));
+
         this._display();
-        appsys.connect('installed-changed', Lang.bind(this, this.reDisplay));
     },
 
-    reDisplay : function() {
+    destroy: function() {
+        this._appSys.disconnect(this._installedChangedId);
+
+        this.parent();
+    },
+
+    _refresh: function() {
         this._clearAll();
         this._display();
     },
 
-    _clearAll : function() {
+    _clearAll: function() {
         this.menu.removeAll();
     },
 
@@ -67,7 +69,7 @@ ApplicationsButton.prototype = {
         while ((nextType = iter.next()) != GMenu.TreeItemType.INVALID) {
             if (nextType == GMenu.TreeItemType.ENTRY) {
                 var entry = iter.get_entry();
-                var app = appsys.lookup_app_by_tree_entry(entry);
+                var app = this._appSys.lookup_app_by_tree_entry(entry);
                 if (!entry.get_app_info().get_nodisplay())
                     menu.addMenuItem(new AppMenuItem(app));
             } else if (nextType == GMenu.TreeItemType.DIRECTORY) {
@@ -77,7 +79,7 @@ ApplicationsButton.prototype = {
     },
 
     _display : function() {
-        let tree = appsys.get_tree();
+        let tree = this._appSys.get_tree();
         let root = tree.get_root_directory();
 
         let iter = root.iter();
@@ -91,7 +93,7 @@ ApplicationsButton.prototype = {
             }
         }
     }
-};
+});
 
 let appsMenuButton;
 
diff --git a/extensions/auto-move-windows/extension.js b/extensions/auto-move-windows/extension.js
index 3a05a15..4a1025f 100644
--- a/extensions/auto-move-windows/extension.js
+++ b/extensions/auto-move-windows/extension.js
@@ -19,11 +19,9 @@ const SETTINGS_KEY = 'application-list';
 
 let settings;
 
-function WindowMover() {
-    this._init();
-}
+const WindowMover = new Lang.Class({
+    Name: 'AutoMoveWindows.WindowMover',
 
-WindowMover.prototype = {
     _init: function() {
         this._settings = settings;
         this._windowTracker = Shell.WindowTracker.get_default();
@@ -79,7 +77,7 @@ WindowMover.prototype = {
             }
         }
     }
-}
+});
 
 let prevCheckWorkspaces;
 let winMover;
diff --git a/extensions/dock/extension.js b/extensions/dock/extension.js
index 7da5bff..63bfa8f 100644
--- a/extensions/dock/extension.js
+++ b/extensions/dock/extension.js
@@ -568,6 +568,10 @@ const DockIcon = new Lang.Class({
     Name: 'Dock.DockIcon',
 
     _init : function(app, dock) {
+        this._dock = dock;
+        this._settings = dock._settings;
+
+
         this.app = app;
         this.actor = new St.Button({ style_class: 'app-well-app',
                                      button_mask: St.ButtonMask.ONE | St.ButtonMask.TWO,
@@ -599,9 +603,6 @@ const DockIcon = new Lang.Class({
         this._stateChangedId = this.app.connect('notify::state',
                                                 Lang.bind(this, this._onStateChanged));
         this._onStateChanged();
-
-        this._dock = dock;
-        this._settings = dock._settings;
     },
 
     _onDestroy: function() {
@@ -697,7 +698,8 @@ const DockIcon = new Lang.Class({
             this._menuManager.addMenu(this._menu, true);
         }
 
-        this._menu.popup();
+        this._menu.redisplay();
+        this._menu.open();
 
         return false;
     },
@@ -781,12 +783,12 @@ const DockIconMenu = new Lang.Class({
             if (!source.actor.mapped)
                 this.close();
         }));
-        source.actor.connect('destroy', Lang.bind(this, function () { this.actor.destroy(); }));
+        source.actor.connect('destroy', Lang.bind(this, function () { this.destroy(); }));
 
         Main.layoutManager.addChrome(this.actor);
     },
 
-    _redisplay: function() {
+    redisplay: function() {
         this.removeAll();
 
         let windows = this._source.app.get_windows();
diff --git a/extensions/drive-menu/extension.js b/extensions/drive-menu/extension.js
index a51fca6..4334195 100644
--- a/extensions/drive-menu/extension.js
+++ b/extensions/drive-menu/extension.js
@@ -16,15 +16,12 @@ const ExtensionUtils = imports.misc.extensionUtils;
 const Me = ExtensionUtils.getCurrentExtension();
 const Convenience = Me.imports.convenience;
 
-function DriveMenuItem(place) {
-    this._init(place);
-}
-
-DriveMenuItem.prototype = {
-    __proto__: PopupMenu.PopupBaseMenuItem.prototype,
+const DriveMenuItem = new Lang.Class({
+    Name: 'DriveMenu.DriveMenuItem',
+    Extends: PopupMenu.PopupBaseMenuItem,
 
     _init: function(place) {
-	PopupMenu.PopupBaseMenuItem.prototype._init.call(this);
+	this.parent();
 
 	this.place = place;
 
@@ -46,23 +43,19 @@ DriveMenuItem.prototype = {
     activate: function(event) {
 	this.place.launch({ timestamp: event.get_time() });
 
-	PopupMenu.PopupBaseMenuItem.prototype.activate.call(this, event);
+	this.parent(event);
     }
-};
-
-function DriveMenu() {
-    this._init();
-}
+});
 
-DriveMenu.prototype = {
-    __proto__: PanelMenu.SystemStatusButton.prototype,
+const DriveMenu = new Lang.Class({
+    Name: 'DriveMenu.DriveMenu',
+    Extends: PanelMenu.SystemStatusButton,
 
     _init: function() {
-	// is 'media-eject' better?
-	PanelMenu.SystemStatusButton.prototype._init.call(this, 'media-eject');
+	this.parent('media-eject');
 
 	this._manager = Main.placesManager;
-	this._manager.connect('mounts-updated', Lang.bind(this, this._update));
+	this._updatedId = this._manager.connect('mounts-updated', Lang.bind(this, this._update));
 
 	this._contentSection = new PopupMenu.PopupMenuSection();
 	this.menu.addMenuItem(this._contentSection);
@@ -91,7 +84,13 @@ DriveMenu.prototype = {
 
 	this.actor.visible = any;
     },
-}
+
+    destroy: function() {
+	this._manager.disconnect(this._updatedId);
+
+	this.parent();
+    },
+});
 
 function init() {
     Convenience.initTranslations();
diff --git a/extensions/native-window-placement/extension.js b/extensions/native-window-placement/extension.js
index dbaa677..aa2c021 100644
--- a/extensions/native-window-placement/extension.js
+++ b/extensions/native-window-placement/extension.js
@@ -48,15 +48,16 @@ function injectToFunction(parent, name, func) {
 }
 const WORKSPACE_BORDER_GAP = 10;                                    // gap between the workspace area and the workspace selector
 
-function Rect(x, y, width, height) {
-    [this.x, this.y, this.width, this.height] = arguments;
-}
+const Rect = new Lang.Class({
+    Name: 'NativeWindowPlacement.Rect',
+
+    _init: function(x, y, width, height) {
+        [this.x, this.y, this.width, this.height] = [x, y, width, height];
+    },
 
-Rect.prototype = {
     /**
      * used in _calculateWindowTransformationsNatural to replace Meta.Rectangle that is too slow.
      */
-
     copy: function() {
         return new Rect(this.x, this.y, this.width, this.height);
     },
@@ -105,7 +106,7 @@ Rect.prototype = {
         this.x += dx;
         this.y += dy;
     }
-};
+});
 
 let winInjections, workspaceInjections, connectedSignals;
 
diff --git a/extensions/places-menu/extension.js b/extensions/places-menu/extension.js
index 225c777..418d249 100644
--- a/extensions/places-menu/extension.js
+++ b/extensions/places-menu/extension.js
@@ -20,15 +20,12 @@ const Convenience = Me.imports.convenience;
 
 const PLACE_ICON_SIZE = 22;
 
-function PlacesMenu() {
-    this._init.apply(this, arguments);
-}
-
-PlacesMenu.prototype = {
-    __proto__: PanelMenu.SystemStatusButton.prototype,
+const PlacesMenu = new Lang.Class({
+    Name: 'PlacesMenu.PlacesMenu',
+    Extends: PanelMenu.SystemStatusButton,
 
     _init: function() {
-        PanelMenu.SystemStatusButton.prototype._init.call(this, 'folder');
+        this.parent('folder');
 
         this.defaultItems = [];
         this.bookmarkItems = [];
@@ -40,8 +37,16 @@ PlacesMenu.prototype = {
         this._devicesMenuItem = new PopupMenu.PopupSubMenuMenuItem(_("Removable Devices"));
         this.menu.addMenuItem(this._devicesMenuItem);
         this._createDevices();
-        Main.placesManager.connect('bookmarks-updated',Lang.bind(this,this._redisplayBookmarks));
-        Main.placesManager.connect('mounts-updated',Lang.bind(this,this._redisplayDevices));
+
+        this._bookmarksId = Main.placesManager.connect('bookmarks-updated',Lang.bind(this,this._redisplayBookmarks));
+        this._mountsId = Main.placesManager.connect('mounts-updated',Lang.bind(this,this._redisplayDevices));
+    },
+
+    destroy: function() {
+        Main.placesManager.disconnect(this._bookmarksId);
+        Main.placesManager.disconnect(this._mountsId);
+
+        this.parent();
     },
 
     _redisplayBookmarks: function(){
@@ -112,10 +117,9 @@ PlacesMenu.prototype = {
 
     _clearDevices : function(){
         this._devicesMenuItem.menu.removeAll();
-        this.DeviceItems = [];
+        this.deviceItems = [];
     },
-};
-
+});
 
 function init() {
     Convenience.initTranslations();
diff --git a/extensions/systemMonitor/extension.js b/extensions/systemMonitor/extension.js
index 6aa117a..4a11355 100644
--- a/extensions/systemMonitor/extension.js
+++ b/extensions/systemMonitor/extension.js
@@ -16,11 +16,9 @@ let _cpuIndicator;
 let _memIndicator;
 let _box;
 
-function Indicator() {
-    this._init();
-}
+const Indicator = new Lang.Class({
+    Name: 'SystemMonitor.Indicator',
 
-Indicator.prototype = {
     _init: function() {
         this._initValues();
         this.drawing_area = new St.DrawingArea({ reactive: true });
@@ -44,6 +42,7 @@ Indicator.prototype = {
 
     destroy: function() {
         Mainloop.source_remove(this._timeout);
+
         this.actor.destroy();
     },
 
@@ -131,20 +130,16 @@ Indicator.prototype = {
             cr.setDash([], 0);
             cr.stroke();
         }
-
     }
+});
 
-};
-
-function CpuIndicator() {
-    this._init();
-}
-
-CpuIndicator.prototype = {
-    __proto__: Indicator.prototype,
+const CpuIndicator = new Lang.Class({
+    Name: 'SystemMonitor.CpuIndicator',
+    Extends: Indicator,
 
     _init: function() {
-        Indicator.prototype._init.call(this);
+        this.parent();
+
         this.gridColor = '-grid-color';
         this.renderStats = [ 'cpu-user', 'cpu-sys', 'cpu-iowait' ];
         
@@ -187,17 +182,15 @@ CpuIndicator.prototype = {
         
         this._prev = cpu;
     }
-};
+});
 
-function MemoryIndicator() {
-    this._init();
-}
-
-MemoryIndicator.prototype = {
-    __proto__: Indicator.prototype,
+const MemoryIndicator = new Lang.Class({
+    Name: 'SystemMonitor.MemoryIndicator',
+    Extends: Indicator,
     
     _init: function() {
-        Indicator.prototype._init.call(this);
+        this.parent();
+
         this.gridColor = '-grid-color';
         this.renderStats = [ 'mem-user', 'mem-other', 'mem-cached' ];
         
@@ -227,7 +220,7 @@ MemoryIndicator.prototype = {
         t += this.mem.cached / this.mem.total;
         this.stats['mem-cached'].values.push(t);
     }
-};
+});
 
 function init() {
     // nothing to do here
diff --git a/extensions/user-theme/extension.js b/extensions/user-theme/extension.js
index f7ca72f..7d8f922 100644
--- a/extensions/user-theme/extension.js
+++ b/extensions/user-theme/extension.js
@@ -12,11 +12,9 @@ const ExtensionUtils = imports.misc.extensionUtils;
 const Me = ExtensionUtils.getCurrentExtension();
 const Convenience = Me.imports.convenience;
 
-function ThemeManager() {
-    this._init.apply(this, arguments);
-}
+const ThemeManager = new Lang.Class({
+    Name: 'UserTheme.ThemeManager',
 
-ThemeManager.prototype = {
     _init: function() {
         this._settings = Convenience.getSettings();
     },
@@ -65,8 +63,7 @@ ThemeManager.prototype = {
         Main.setThemeStylesheet(_stylesheet);
         Main.loadTheme();
     }
-}
-
+});
 
 function init() {
     return new ThemeManager();
diff --git a/extensions/workspace-indicator/extension.js b/extensions/workspace-indicator/extension.js
index ef0f5b7..de72577 100644
--- a/extensions/workspace-indicator/extension.js
+++ b/extensions/workspace-indicator/extension.js
@@ -7,103 +7,115 @@ const PanelMenu = imports.ui.panelMenu;
 const PopupMenu = imports.ui.popupMenu;
 const Panel = imports.ui.panel;
 
-const Main = imports.ui.main;
+const Gettext = imports.gettext.domain('gnome-shell-extensions');
+const _ = Gettext.gettext;
 
-function WorkspaceIndicator() {
-	this._init.apply(this, arguments);
-}
+const Main = imports.ui.main;
 
-WorkspaceIndicator.prototype = {
-	__proto__: PanelMenu.SystemStatusButton.prototype,
-
-	_init: function(){
-		PanelMenu.SystemStatusButton.prototype._init.call(this, 'folder');
-
-	        this._currentWorkspace = global.screen.get_active_workspace().index();
-		this.statusLabel = new St.Label({ text: this._labelText() });
-
-	        // destroy all previously created children, and add our statusLabel
- 	        this.actor.get_children().forEach(function(c) { c.destroy() });
-		this.actor.add_actor(this.statusLabel);
-
-		this.workspacesItems = [];
-		this._workspaceSection = new PopupMenu.PopupMenuSection();
-		this.menu.addMenuItem(this._workspaceSection);
-		global.screen.connect_after('workspace-added', Lang.bind(this,this._createWorkspacesSection));
-		global.screen.connect_after('workspace-removed', Lang.bind(this,this._createWorkspacesSection));
-		global.screen.connect_after('workspace-switched', Lang.bind(this,this._updateIndicator));
-		this.actor.connect('scroll-event', Lang.bind(this, this._onScrollEvent));
-		this._createWorkspacesSection();
-
-		//styling
-		this.menu.actor.add_style_class_name('workspace-indicator-shorter');
-		this.statusLabel.add_style_class_name('panel-workspace-indicator');
-	},
-
-	_updateIndicator: function() {
-	    this.workspacesItems[this._currentWorkspace].setShowDot(false);
-	    this._currentWorkspace = global.screen.get_active_workspace().index();
-	    this.workspacesItems[this._currentWorkspace].setShowDot(true);
-
-	    this.statusLabel.set_text(this._labelText());
-	},
-
-	_labelText : function(workspaceIndex) {
-	    if(workspaceIndex == undefined) {
-		workspaceIndex = this._currentWorkspace;
-		return (workspaceIndex + 1).toString();
-	    }
-	    return Meta.prefs_get_workspace_name(workspaceIndex);
-	},
-
-	_createWorkspacesSection : function() {
-	    this._workspaceSection.removeAll();
-	    this.workspacesItems = [];
-	    this._currentWorkspace = global.screen.get_active_workspace().index();
-
-	    let i = 0;
-	    for(; i < global.screen.n_workspaces; i++) {
-		this.workspacesItems[i] = new PopupMenu.PopupMenuItem(this._labelText(i));
-		this._workspaceSection.addMenuItem(this.workspacesItems[i]);
-		this.workspacesItems[i].workspaceId = i;
-		this.workspacesItems[i].label_actor = this.statusLabel;
-		let self = this;
-		this.workspacesItems[i].connect('activate', Lang.bind(this, function(actor, event) {
-		    this._activate(actor.workspaceId);
-		}));
-
-		if (i == this._currentWorkspace)
-		    this.workspacesItems[i].setShowDot(true);
-	    }
-
-	    this.statusLabel.set_text(this._labelText());
-	},
-
-	_activate : function (index) {
-		if(index >= 0 && index <  global.screen.n_workspaces) {
-			let metaWorkspace = global.screen.get_workspace_by_index(index);
-			metaWorkspace.activate(true);
-		}
-	},
-
-	_onScrollEvent : function(actor, event) {
-		let direction = event.get_scroll_direction();
-		let diff = 0;
-		if (direction == Clutter.ScrollDirection.DOWN) {
-			diff = 1;
-		} else if (direction == Clutter.ScrollDirection.UP) {
-			diff = -1;
-		} else {
-			return;
-		}
-
-		let newIndex = global.screen.get_active_workspace().index() + diff;
-		this._activate(newIndex);
-	},
-}
+const ExtensionUtils = imports.misc.extensionUtils;
+const Me = ExtensionUtils.getCurrentExtension();
+const Convenience = Me.imports.convenience;
+
+const WorkspaceIndicator = new Lang.Class({
+    Name: 'WorkspaceIndicator.WorkspaceIndicator',
+    Extends: PanelMenu.Button,
+
+    _init: function(){
+	this.parent(0.0, _("Workspace Indicator"));
+
+	this._currentWorkspace = global.screen.get_active_workspace().index();
+	this.statusLabel = new St.Label({ text: this._labelText() });
+
+	this.actor.add_actor(this.statusLabel);
+
+	this.workspacesItems = [];
+	this._workspaceSection = new PopupMenu.PopupMenuSection();
+	this.menu.addMenuItem(this._workspaceSection);
+
+	this._screenSignals = [];
+	this._screenSignals.push(global.screen.connect_after('workspace-added', Lang.bind(this,this._createWorkspacesSection)));
+	this._screenSignals.push(global.screen.connect_after('workspace-removed', Lang.bind(this,this._createWorkspacesSection)));
+	this._screenSignals.push(global.screen.connect_after('workspace-switched', Lang.bind(this,this._updateIndicator)));
+
+	this.actor.connect('scroll-event', Lang.bind(this, this._onScrollEvent));
+	this._createWorkspacesSection();
+
+	//styling
+	this.menu.actor.add_style_class_name('workspace-indicator-shorter');
+	this.statusLabel.add_style_class_name('panel-workspace-indicator');
+    },
+
+    destroy: function() {
+	for (let i = 0; i < this._screenSignals.length; i++)
+	    global.screen.disconnect(this._screenSignals[i]);
+
+	this.parent();
+    },
+
+    _updateIndicator: function() {
+	this.workspacesItems[this._currentWorkspace].setShowDot(false);
+	this._currentWorkspace = global.screen.get_active_workspace().index();
+	this.workspacesItems[this._currentWorkspace].setShowDot(true);
+
+	this.statusLabel.set_text(this._labelText());
+    },
+
+    _labelText : function(workspaceIndex) {
+	if(workspaceIndex == undefined) {
+	    workspaceIndex = this._currentWorkspace;
+	    return (workspaceIndex + 1).toString();
+	}
+	return Meta.prefs_get_workspace_name(workspaceIndex);
+    },
+
+    _createWorkspacesSection : function() {
+	this._workspaceSection.removeAll();
+	this.workspacesItems = [];
+	this._currentWorkspace = global.screen.get_active_workspace().index();
+
+	let i = 0;
+	for(; i < global.screen.n_workspaces; i++) {
+	    this.workspacesItems[i] = new PopupMenu.PopupMenuItem(this._labelText(i));
+	    this._workspaceSection.addMenuItem(this.workspacesItems[i]);
+	    this.workspacesItems[i].workspaceId = i;
+	    this.workspacesItems[i].label_actor = this.statusLabel;
+	    let self = this;
+	    this.workspacesItems[i].connect('activate', Lang.bind(this, function(actor, event) {
+		this._activate(actor.workspaceId);
+	    }));
+
+	    if (i == this._currentWorkspace)
+		this.workspacesItems[i].setShowDot(true);
+	}
+
+	this.statusLabel.set_text(this._labelText());
+    },
+
+    _activate : function (index) {
+	if(index >= 0 && index <  global.screen.n_workspaces) {
+	    let metaWorkspace = global.screen.get_workspace_by_index(index);
+	    metaWorkspace.activate(true);
+	}
+    },
+
+    _onScrollEvent : function(actor, event) {
+	let direction = event.get_scroll_direction();
+	let diff = 0;
+	if (direction == Clutter.ScrollDirection.DOWN) {
+	    diff = 1;
+	} else if (direction == Clutter.ScrollDirection.UP) {
+	    diff = -1;
+	} else {
+	    return;
+	}
+
+	let newIndex = global.screen.get_active_workspace().index() + diff;
+	this._activate(newIndex);
+    },
+});
 
 function init(meta) {
-    // empty
+    Convenience.initTranslations();
 }
 
 let _indicator;



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