status/network: Add NMSection:label property

This is the last property that will be needed by the future
betwork quick toggles.

It maps to the same item as :icon-name, except when more than one
item is active. In that case, we overrule the binding and return
a generic name and count (like "VPN (2)").

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2408>
This commit is contained in:
Florian Müllner 2022-08-03 07:09:08 +02:00 committed by Marge Bot
parent f96447079a
commit 11283be390

View File

@ -1282,6 +1282,9 @@ const NMSection = GObject.registerClass({
'icon-name': GObject.ParamSpec.string('icon-name', '', '', 'icon-name': GObject.ParamSpec.string('icon-name', '', '',
GObject.ParamFlags.READWRITE, GObject.ParamFlags.READWRITE,
''), ''),
'label': GObject.ParamSpec.string('label', '', '',
GObject.ParamFlags.READWRITE,
''),
}, },
Signals: { Signals: {
'activation-failed': {}, 'activation-failed': {},
@ -1303,6 +1306,10 @@ const NMSection = GObject.registerClass({
this._itemBinding = new GObject.BindingGroup(); this._itemBinding = new GObject.BindingGroup();
this._itemBinding.bind('icon-name', this._itemBinding.bind('icon-name',
this, 'icon-name', GObject.BindingFlags.DEFAULT); this, 'icon-name', GObject.BindingFlags.DEFAULT);
this._itemBinding.bind_full('name',
this, 'label', GObject.BindingFlags.DEFAULT,
(bind, source) => [true, this._transformLabel(source)],
null);
} }
setClient(client) { setClient(client) {
@ -1328,11 +1335,30 @@ const NMSection = GObject.registerClass({
throw new GObject.NotImplementedError(); throw new GObject.NotImplementedError();
} }
// transform function for property binding:
// Ignore the provided label if there are multiple active
// items, and replace it with something like "VPN (2)"
_transformLabel(source) {
const nActive = this.checked
? [...this._getActiveItems()].length
: 0;
if (nActive > 1)
return `${this._getDefaultName()} (${nActive})`;
return source;
}
_updateItemsVisibility() { _updateItemsVisibility() {
[...this._itemSorter.itemsByMru()].forEach( [...this._itemSorter.itemsByMru()].forEach(
(item, i) => (item.visible = i < MAX_VISIBLE_NETWORKS)); (item, i) => (item.visible = i < MAX_VISIBLE_NETWORKS));
} }
_itemActiveChanged() {
// force an update in case we changed
// from or to multiple active items
this._itemBinding.source?.notify('name');
this._sync();
}
_updateChecked() { _updateChecked() {
const [firstActive] = this._getActiveItems(); const [firstActive] = this._getActiveItems();
this.checked = !!firstActive; this.checked = !!firstActive;
@ -1348,7 +1374,7 @@ const NMSection = GObject.registerClass({
`${this} already has an item for ${key}`); `${this} already has an item for ${key}`);
item.connectObject( item.connectObject(
'notify::is-active', () => this._sync(), 'notify::is-active', () => this._itemActiveChanged(),
'notify::name', () => this._resortItem(item), 'notify::name', () => this._resortItem(item),
'destroy', () => this._removeItem(key), 'destroy', () => this._removeItem(key),
this); this);
@ -1428,6 +1454,10 @@ class NMVpnSection extends NMSection {
this); this);
} }
_getDefaultName() {
return _('VPN');
}
_loadInitialItems() { _loadInitialItems() {
const connections = this._client.get_connections(); const connections = this._client.get_connections();
for (const conn of connections) for (const conn of connections)
@ -1533,6 +1563,12 @@ class NMDeviceSection extends NMSection {
}, this); }, this);
} }
_getDefaultName() {
const [dev] = this._nmDevices;
const [name] = NM.Device.disambiguate_names([dev]);
return name;
}
_loadInitialItems() { _loadInitialItems() {
const devices = this._client.get_devices(); const devices = this._client.get_devices();
for (const dev of devices) for (const dev of devices)