[gnome-shell/wip/aggregate-menu: 48/59] network: Implement new designs for VPN/modem submenus



commit ec6d1240dc6e9caee3d66cb8c9bdf0198cd153e2
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Tue Jun 11 21:40:27 2013 -0400

    network: Implement new designs for VPN/modem submenus

 js/ui/status/network.js |  205 ++++++++++++++++++++++++-----------------------
 1 files changed, 105 insertions(+), 100 deletions(-)
---
diff --git a/js/ui/status/network.js b/js/ui/status/network.js
index d667178..c3f5a9c 100644
--- a/js/ui/status/network.js
+++ b/js/ui/status/network.js
@@ -80,14 +80,22 @@ const NMConnectionItem = new Lang.Class({
         this._activeConnection = null;
         this._activeConnectionChangedId = 0;
 
-        this.menuItem = new PopupMenu.PopupSwitchMenuItem(connection.get_id(), false);
-        this.menuItem.connect('toggled', Lang.bind(this, this._toggle));
+        this.labelItem = new PopupMenu.PopupMenuItem('');
+        this.labelItem.connect('activate', Lang.bind(this, this._toggle));
+
+        this.switchItem = new PopupMenu.PopupSwitchMenuItem(connection.get_id(), false);
+        this.switchItem.connect('toggled', Lang.bind(this, this._toggle));
 
         this._sync();
     },
 
     destroy: function() {
-        this.menuItem.destroy();
+        this.labelItem.destroy();
+        this.switchItem.destroy();
+    },
+
+    getName: function() {
+        return this.connection.get_id();
     },
 
     isActive: function() {
@@ -98,8 +106,10 @@ const NMConnectionItem = new Lang.Class({
     },
 
     _sync: function() {
-        this.menuItem.setToggleState(this._getIsActive());
-        this.menuItem.setStatus(this._getStatus());
+        let isActive = this.isActive();
+        this.labelItem.label.text = isActive ? _("Turn Off") : _("Connect");
+        this.switchItem.setToggleState(isActive);
+        this.switchItem.setStatus(this._getStatus());
         this.emit('icon-changed');
     },
 
@@ -146,21 +156,58 @@ const NMConnectionSection = new Lang.Class({
 
         this._connectionItems = new Hash.Map();
         this._connections = [];
+
+        this._labelSection = new PopupMenu.PopupMenuSection();
+        this._switchSection = new PopupMenu.PopupMenuSection();
+
+        if (this._activateAutomaticConnection) {
+            this._autoConnectItem = new PopupMenu.PopupMenuItem(_("Connect"));
+            this._autoConnectItem.connect('activate', Lang.bind(this, this._activateAutomaticConnection));
+        }
+
         this.section = new PopupMenu.PopupMenuSection();
 
-        this.statusItem = new PopupMenu.PopupSwitchMenuItem('', this.connected);
-        this._statusChanged = this.statusItem.connect('toggled', Lang.bind(this, function(item, state) {
-            let ok;
-            if (state)
-                ok = this.activate();
-            else
-                ok = this.deactivate();
+        this._submenu = new PopupMenu.PopupSubMenuMenuItem('', true);
+        this.section.addMenuItem(this._submenu);
 
-            if (!ok)
-                item.setToggleState(!state);
-        }));
+        if (this._activateAutomaticConnection)
+            this._submenu.menu.addMenuItem(this._autoConnectItem);
+
+        this._submenu.menu.addMenuItem(this._labelSection);
+        this._submenu.menu.addMenuItem(this._switchSection);
+        this._submenu.menu.addSettingsAction(_("Network Settings"), 'gnome-network-panel.desktop');
+
+        this.connect('icon-changed', Lang.bind(this, this._sync));
+    },
+
+    _getDescription: function() {
+        return '';
+    },
+
+    _getStatus: function() {
+        let values = this._connectionItems.values();
+        for (let i = 0; i < values; i++) {
+            let item = values[i];
+            if (item.isActive())
+                return item.getName();
+        }
+
+        return _("Off");
+    },
+
+    _sync: function() {
+        let nItems = this._connectionItems.size();
+
+        this._switchSection.actor.visible = (nItems > 1);
+        this._labelSection.actor.visible = (nItems == 1);
+
+        if (this._autoConnectItem)
+            this._autoConnectItem.actor.visible = (nItems == 0);
+        else
+            this._submenu.actor.visible = (nItems > 0);
 
-        this._updateStatusItem();
+        this._submenu.status.text = this._getStatus();
+        this._submenu.icon.icon_name = this._getMenuIcon();
     },
 
     _hasConnection: function(connection) {
@@ -179,6 +226,10 @@ const NMConnectionSection = new Lang.Class({
         return new NMConnectionItem(this._client, connection);
     },
 
+    syncDescription: function() {
+        this._submenu.label.text = this._getDescription();
+    },
+
     checkConnection: function(connection) {
         if (!this._connectionValid(connection))
             return;
@@ -202,8 +253,10 @@ const NMConnectionSection = new Lang.Class({
         }));
 
         let pos = Util.insertSorted(this._connections, connection, this._connectionSortFunction);
-        this.section.addMenuItem(item.menuItem, pos);
+        this._labelSection.addMenuItem(item.labelItem, pos);
+        this._switchSection.addMenuItem(item.switchItem, pos);
         this._connectionItems.set(connection.get_uuid(), item);
+        this._sync();
     },
 
     removeConnection: function(connection) {
@@ -212,6 +265,8 @@ const NMConnectionSection = new Lang.Class({
 
         let pos = this._connections.indexOf(connection);
         this._connections.splice(pos, 1);
+
+        this._sync();
     },
 
     addActiveConnection: function(activeConnection) {
@@ -224,6 +279,10 @@ const NMConnectionSection = new Lang.Class({
         item.setActiveConnection(null);
     },
 
+    _getMenuIcon: function() {
+        return this.getIndicatorIcon();
+    },
+
     getIndicatorIcon: function() {
         let items = this._connectionItems.values();
         for (let i = 0; i < items.length; i++) {
@@ -236,7 +295,6 @@ const NMConnectionSection = new Lang.Class({
     },
 
     destroy: function() {
-        this.statusItem.destroy();
         this.section.destroy();
     },
 });
@@ -254,6 +312,13 @@ const NMDevice = new Lang.Class({
         connections.forEach(Lang.bind(this, this.checkConnection));
     },
 
+    _getDescription: function() {
+        if (!this._device)
+            return '';
+
+        return this._device._description || '';
+    },
+
     _connectionValid: function(connection) {
         return this._device.connection_valid(connection);
     },
@@ -296,35 +361,14 @@ const NMDevice = new Lang.Class({
 
             this._device = null;
         }
-    },
-
-    deactivate: function() {
-        this._device.disconnect(null);
-        return true;
-    },
-
-    activate: function() {
-        if (this._activeConnection)
-            // nothing to do
-            return true;
-
-        // If there is only one connection available, use that
-        // Otherwise, if no connection is currently configured,
-        // try automatic configuration (or summon the config dialog)
-        if (this._connections.length == 1) {
-            this._client.activate_connection(this._connections[0].connection, this._device || null, null, 
null);
-            return true;
-        } else if (this._connections.length == 0) {
-            return this._activateAutomaticConnection();
-        }
 
-        return false;
+        this.syncDescription();
+        this._sync();
     },
 
     _activateAutomaticConnection: function() {
         let connection = new NetworkManager.Connection();
         this._client.add_and_activate_connection(connection, this._device, null, null);
-        return true;
     },
 
     _activeConnectionChanged: function() {
@@ -348,7 +392,7 @@ const NMDevice = new Lang.Class({
             GObject.Object.prototype.disconnect.call(this._device, this._firmwareChangedId);
             this._firmwareChangedId = 0;
         }
-        this._substateChanged();
+        this._sync();
     },
 
     _getStatus: function() {
@@ -397,11 +441,6 @@ const NMDevice = new Lang.Class({
         }
     },
 
-    syncDescription: function() {
-        if (this._device && this._device._description)
-            this.statusItem.label.text = this._device._description;
-    },
-
     _deviceStateChanged: function(device, newstate, oldstate, reason) {
         if (newstate == oldstate) {
             log('device emitted state-changed without actually changing state');
@@ -416,45 +455,20 @@ const NMDevice = new Lang.Class({
             this.emit('activation-failed', reason);
         }
 
-        this._updateStatusItem();
-    },
-
-    _updateStatusItem: function() {
-        this.statusItem.setStatus(this.getStatusLabel());
-        this.statusItem.setToggleState(this.connected);
+        this._sync();
     },
-
-    _substateChanged: function() {
-        this.statusItem.setStatus(this.getStatusLabel());
-    }
 });
 Signals.addSignalMethods(NMDevice.prototype);
 
 const NMDeviceSimple = new Lang.Class({
     Name: 'NMDeviceSimple',
     Extends: NMDevice,
-
-    _init: function(client, device, connections) {
-        this.category = NMConnectionCategory.WIRED;
-
-        this.parent(client, device, connections);
-    },
-
-    _createSection: function() {
-        this.parent();
-
-        // if we have only one connection (normal or automatic)
-        // we hide the connection list, and use the switch to control
-        // the device
-        // we can do it here because addConnection and removeConnection
-        // both call _createSection at some point
-        this.section.actor.visible = this._connections.length > 1;
-    }
+    category: NMConnectionCategory.WIRED,
 });
 
 const NMDeviceWired = new Lang.Class({
     Name: 'NMDeviceWired',
-    Extends: NMDeviceSimple,
+    Extends: NMDevice,
 
     _init: function(client, device, connections) {
         device._description = _("Wired");
@@ -528,34 +542,11 @@ const NMDeviceModem = new Lang.Class({
         return 'network-cellular-signal-' + signalToIcon(this.mobileDevice.signal_quality) + '-symbolic';
     },
 
-    _createSection: function() {
-        if (!this._shouldShowConnectionList())
-            return;
-
-        if (this.mobileDevice) {
-            // If operator_name is null, just pass the empty string, as the item is hidden anyway
-            this._operatorItem = new PopupMenu.PopupMenuItem(this.mobileDevice.operator_name || '',
-                                                             { style_class: 'popup-status-menu-item' });
-            if (!this.mobileDevice.operator_name)
-                this._operatorItem.actor.hide();
-            this.section.addMenuItem(this._operatorItem);
-        }
-
-        this.parent();
-    },
-
-    _clearSection: function() {
-        this._operatorItem = null;
-
-        this.parent();
-    },
-
     _activateAutomaticConnection: function() {
         // Mobile wizard is too complex for the shell UI and
         // is handled by the network panel
         Util.spawn(['gnome-control-center', 'network',
                     'connect-3g', this._device.get_path()]);
-        return true;
     },
 
     getIndicatorIcon: function() {
@@ -590,7 +581,7 @@ const NMDeviceBluetooth = new Lang.Class({
         // so we just create an empty connection and hope
         // that this phone supports PAN
 
-        return this.parent();
+        this.parent();
     },
 
     getIndicatorIcon: function() {
@@ -688,6 +679,21 @@ const NMVPNSection = new Lang.Class({
     Extends: NMConnectionSection,
     category: NMConnectionCategory.VPN,
 
+    _init: function(client) {
+        this.parent(client);
+
+        this.syncDescription();
+        this._sync();
+    },
+
+    _getDescription: function() {
+        return _("VPN");
+    },
+
+    _getMenuIcon: function() {
+        return this.parent() || 'network-vpn-symbolic';
+    },
+
     _makeConnectionItem: function(connection) {
         return new NMVPNConnectionItem(this._client, connection);
     },
@@ -873,7 +879,6 @@ const NMApplet = new Lang.Class({
                                                       Lang.bind(this, this._onActivationFailed));
 
         let section = this._devices[wrapper.category].section;
-        section.addMenuItem(wrapper.statusItem);
         section.addMenuItem(wrapper.section);
 
         let devices = this._devices[wrapper.category].devices;


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