[gnome-shell] status/network: Use binding for updating primary indicator



commit 2231fce157a6fdbaf057bff084009942a18a2f29
Author: Florian Müllner <fmuellner gnome org>
Date:   Wed Aug 3 04:33:48 2022 +0200

    status/network: Use binding for updating primary indicator
    
    The primary indicator is a bit tricker than the VPN one: The source
    of the binding can change depending on the connection, and we still
    want to show it when there is a network connection we don't know
    about (either because we don't handle the device type, or the
    device isn't managed by NetworkManager).
    
    Bindings still make the whole thing a lot nicer though.
    
    Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2408>

 js/ui/status/network.js | 74 ++++++++++++++-----------------------------------
 1 file changed, 21 insertions(+), 53 deletions(-)
---
diff --git a/js/ui/status/network.js b/js/ui/status/network.js
index e9ea5c05e3..39a89e93d2 100644
--- a/js/ui/status/network.js
+++ b/js/ui/status/network.js
@@ -50,17 +50,6 @@ function ssidToLabel(ssid) {
     return label;
 }
 
-function ensureActiveConnectionProps(active) {
-    if (!active._primaryDevice) {
-        let devices = active.get_devices();
-        if (devices.length > 0) {
-            // This list is guaranteed to have at most one device in it.
-            let device = devices[0]._delegate;
-            active._primaryDevice = device;
-        }
-    }
-}
-
 function launchSettingsPanel(panel, ...args) {
     const param = new GLib.Variant('(sav)',
         [panel, args.map(s => new GLib.Variant('s', s))]);
@@ -1764,7 +1753,6 @@ class Indicator extends PanelMenu.SystemIndicator {
         this._allSections.forEach(section => {
             section.connectObject(
                 'activation-failed', () => this._onActivationFailed(),
-                'notify::icon-name', () => this._updateIcon(),
                 this);
             this.menu.addMenuItem(section.menu);
         });
@@ -1772,6 +1760,11 @@ class Indicator extends PanelMenu.SystemIndicator {
         this._primaryIndicator = this._addIndicator();
         this._vpnIndicator = this._addIndicator();
 
+        this._primaryIndicatorBinding = new GObject.BindingGroup();
+        this._primaryIndicatorBinding.bind('icon-name',
+            this._primaryIndicator, 'icon-name',
+            GObject.BindingFlags.DEFAULT);
+
         this._vpnSection.bind_property('checked',
             this._vpnIndicator, 'visible',
             GObject.BindingFlags.SYNC_CREATE);
@@ -1796,7 +1789,6 @@ class Indicator extends PanelMenu.SystemIndicator {
             GObject.BindingFlags.SYNC_CREATE);
 
         this._client.connectObject(
-            'notify::state', () => this._updateIcon(),
             'notify::primary-connection', () => this._syncMainConnection(),
             'notify::activating-connection', () => this._syncMainConnection(),
             'notify::connectivity', () => this._syncConnectivity(),
@@ -1842,33 +1834,14 @@ class Indicator extends PanelMenu.SystemIndicator {
         source.showNotification(this._notification);
     }
 
-    _getMainConnection() {
-        let connection;
-
-        connection = this._client.get_primary_connection();
-        if (connection) {
-            ensureActiveConnectionProps(connection);
-            return connection;
-        }
-
-        connection = this._client.get_activating_connection();
-        if (connection) {
-            ensureActiveConnectionProps(connection);
-            return connection;
-        }
-
-        return null;
-    }
-
     _syncMainConnection() {
-        this._mainConnection?._primaryDevice?.disconnectObject(this);
         this._mainConnection?.disconnectObject(this);
 
-        this._mainConnection = this._getMainConnection();
+        this._mainConnection =
+            this._client.get_primary_connection() ||
+            this._client.get_activating_connection();
 
         if (this._mainConnection) {
-            this._mainConnection._primaryDevice?.connectObject('notify::icon-name',
-                this._updateIcon.bind(this), this);
             this._mainConnection.connectObject('notify::state',
                 this._mainConnectionStateChanged.bind(this), this);
             this._mainConnectionStateChanged();
@@ -1963,24 +1936,19 @@ class Indicator extends PanelMenu.SystemIndicator {
     }
 
     _updateIcon() {
-        if (!this._client.networking_enabled) {
-            this._primaryIndicator.visible = false;
-        } else {
-            let dev = null;
-            if (this._mainConnection)
-                dev = this._mainConnection._primaryDevice;
-
-            let state = this._client.get_state();
-            let connected = state == NM.State.CONNECTED_GLOBAL;
-            this._primaryIndicator.visible = (dev != null) || connected;
-            if (dev) {
-                this._primaryIndicator.icon_name = dev.icon_name;
-            } else if (connected) {
-                if (this._client.connectivity == NM.ConnectivityState.FULL)
-                    this._primaryIndicator.icon_name = 'network-wired-symbolic';
-                else
-                    this._primaryIndicator.icon_name = 'network-wired-no-route-symbolic';
-            }
+        const [dev] = this._mainConnection?.get_devices() ?? [];
+        const primarySection = this._deviceSections.get(dev?.device_type) ?? null;
+        this._primaryIndicatorBinding.source = primarySection;
+
+        if (!primarySection) {
+            if (this._client.connectivity === NM.ConnectivityState.FULL)
+                this._primaryIndicator.icon_name = 'network-wired-symbolic';
+            else
+                this._primaryIndicator.icon_name = 'network-wired-no-route-symbolic';
         }
+
+        const state = this._client.get_state();
+        const connected = state === NM.State.CONNECTED_GLOBAL;
+        this._primaryIndicator.visible = (primarySection != null) || connected;
     }
 });


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