status/power: Port to quick settings
The power indicator should not be a regular quick toggle, but instead be part of a "system area" row at the top of the menu. But as in the end it is still a simple button, we can do the port to quick settings now, and move it later when the system row is implemented. Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2392>
This commit is contained in:
parent
2ebdb6e318
commit
80d1f68c77
@ -378,7 +378,6 @@ class AggregateMenu extends PanelMenu.Button {
|
|||||||
else
|
else
|
||||||
this._network = null;
|
this._network = null;
|
||||||
|
|
||||||
this._power = new imports.ui.status.power.Indicator();
|
|
||||||
this._powerProfiles = new imports.ui.status.powerProfiles.Indicator();
|
this._powerProfiles = new imports.ui.status.powerProfiles.Indicator();
|
||||||
this._volume = new imports.ui.status.volume.Indicator();
|
this._volume = new imports.ui.status.volume.Indicator();
|
||||||
this._brightness = new imports.ui.status.brightness.Indicator();
|
this._brightness = new imports.ui.status.brightness.Indicator();
|
||||||
@ -387,7 +386,6 @@ class AggregateMenu extends PanelMenu.Button {
|
|||||||
if (this._network)
|
if (this._network)
|
||||||
this._indicators.add_child(this._network);
|
this._indicators.add_child(this._network);
|
||||||
this._indicators.add_child(this._volume);
|
this._indicators.add_child(this._volume);
|
||||||
this._indicators.add_child(this._power);
|
|
||||||
this._indicators.add_child(this._powerProfiles);
|
this._indicators.add_child(this._powerProfiles);
|
||||||
|
|
||||||
this.menu.addMenuItem(this._volume.menu);
|
this.menu.addMenuItem(this._volume.menu);
|
||||||
@ -396,12 +394,10 @@ class AggregateMenu extends PanelMenu.Button {
|
|||||||
if (this._network)
|
if (this._network)
|
||||||
this.menu.addMenuItem(this._network.menu);
|
this.menu.addMenuItem(this._network.menu);
|
||||||
|
|
||||||
this.menu.addMenuItem(this._power.menu);
|
|
||||||
this.menu.addMenuItem(this._powerProfiles.menu);
|
this.menu.addMenuItem(this._powerProfiles.menu);
|
||||||
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
|
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
|
||||||
this.menu.addMenuItem(this._system.menu);
|
this.menu.addMenuItem(this._system.menu);
|
||||||
|
|
||||||
menuLayout.addSizeChild(this._power.menu.actor);
|
|
||||||
menuLayout.addSizeChild(this._powerProfiles.menu.actor);
|
menuLayout.addSizeChild(this._powerProfiles.menu.actor);
|
||||||
menuLayout.addSizeChild(this._system.menu.actor);
|
menuLayout.addSizeChild(this._system.menu.actor);
|
||||||
}
|
}
|
||||||
@ -430,6 +426,7 @@ class QuickSettings extends PanelMenu.Button {
|
|||||||
this._nightLight = new imports.ui.status.nightLight.Indicator();
|
this._nightLight = new imports.ui.status.nightLight.Indicator();
|
||||||
this._rfkill = new imports.ui.status.rfkill.Indicator();
|
this._rfkill = new imports.ui.status.rfkill.Indicator();
|
||||||
this._unsafeMode = new UnsafeModeIndicator();
|
this._unsafeMode = new UnsafeModeIndicator();
|
||||||
|
this._power = new imports.ui.status.power.Indicator();
|
||||||
|
|
||||||
this._indicators.add_child(this._remoteAccess);
|
this._indicators.add_child(this._remoteAccess);
|
||||||
this._indicators.add_child(this._thunderbolt);
|
this._indicators.add_child(this._thunderbolt);
|
||||||
@ -439,6 +436,7 @@ class QuickSettings extends PanelMenu.Button {
|
|||||||
this._indicators.add_child(this._bluetooth);
|
this._indicators.add_child(this._bluetooth);
|
||||||
this._indicators.add_child(this._rfkill);
|
this._indicators.add_child(this._rfkill);
|
||||||
this._indicators.add_child(this._unsafeMode);
|
this._indicators.add_child(this._unsafeMode);
|
||||||
|
this._indicators.add_child(this._power);
|
||||||
|
|
||||||
this._addItems(this._remoteAccess.quickSettingsItems);
|
this._addItems(this._remoteAccess.quickSettingsItems);
|
||||||
this._addItems(this._thunderbolt.quickSettingsItems);
|
this._addItems(this._thunderbolt.quickSettingsItems);
|
||||||
@ -448,6 +446,7 @@ class QuickSettings extends PanelMenu.Button {
|
|||||||
this._addItems(this._nightLight.quickSettingsItems);
|
this._addItems(this._nightLight.quickSettingsItems);
|
||||||
this._addItems(this._rfkill.quickSettingsItems);
|
this._addItems(this._rfkill.quickSettingsItems);
|
||||||
this._addItems(this._unsafeMode.quickSettingsItems);
|
this._addItems(this._unsafeMode.quickSettingsItems);
|
||||||
|
this._addItems(this._power.quickSettingsItems);
|
||||||
}
|
}
|
||||||
|
|
||||||
_addItems(items, colSpan = 1) {
|
_addItems(items, colSpan = 1) {
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
||||||
/* exported Indicator */
|
/* exported Indicator */
|
||||||
|
|
||||||
const { Clutter, Gio, GObject, St, UPowerGlib: UPower } = imports.gi;
|
const {Atk, Clutter, Gio, GObject, Shell, St, UPowerGlib: UPower} = imports.gi;
|
||||||
|
|
||||||
const Main = imports.ui.main;
|
const Main = imports.ui.main;
|
||||||
const PanelMenu = imports.ui.panelMenu;
|
const {QuickToggle, SystemIndicator} = imports.ui.quickSettings;
|
||||||
const PopupMenu = imports.ui.popupMenu;
|
|
||||||
|
|
||||||
const { loadInterfaceXML } = imports.misc.fileUtils;
|
const {loadInterfaceXML} = imports.misc.fileUtils;
|
||||||
|
|
||||||
const BUS_NAME = 'org.freedesktop.UPower';
|
const BUS_NAME = 'org.freedesktop.UPower';
|
||||||
const OBJECT_PATH = '/org/freedesktop/UPower/devices/DisplayDevice';
|
const OBJECT_PATH = '/org/freedesktop/UPower/devices/DisplayDevice';
|
||||||
@ -15,106 +14,54 @@ const OBJECT_PATH = '/org/freedesktop/UPower/devices/DisplayDevice';
|
|||||||
const DisplayDeviceInterface = loadInterfaceXML('org.freedesktop.UPower.Device');
|
const DisplayDeviceInterface = loadInterfaceXML('org.freedesktop.UPower.Device');
|
||||||
const PowerManagerProxy = Gio.DBusProxy.makeProxyWrapper(DisplayDeviceInterface);
|
const PowerManagerProxy = Gio.DBusProxy.makeProxyWrapper(DisplayDeviceInterface);
|
||||||
|
|
||||||
const SHOW_BATTERY_PERCENTAGE = 'show-battery-percentage';
|
const SHOW_BATTERY_PERCENTAGE = 'show-battery-percentage';
|
||||||
|
|
||||||
var Indicator = GObject.registerClass(
|
const PowerToggle = GObject.registerClass({
|
||||||
class Indicator extends PanelMenu.SystemIndicator {
|
Properties: {
|
||||||
|
'fallback-icon-name': GObject.ParamSpec.string('fallback-icon-name', '', '',
|
||||||
|
GObject.ParamFlags.READWRITE,
|
||||||
|
''),
|
||||||
|
},
|
||||||
|
}, class PowerToggle extends QuickToggle {
|
||||||
_init() {
|
_init() {
|
||||||
super._init();
|
super._init({
|
||||||
|
accessible_role: Atk.Role.PUSH_BUTTON,
|
||||||
this._desktopSettings = new Gio.Settings({
|
|
||||||
schema_id: 'org.gnome.desktop.interface',
|
|
||||||
});
|
});
|
||||||
this._desktopSettings.connect(
|
|
||||||
`changed::${SHOW_BATTERY_PERCENTAGE}`, this._sync.bind(this));
|
|
||||||
|
|
||||||
this._indicator = this._addIndicator();
|
|
||||||
this._percentageLabel = new St.Label({
|
|
||||||
y_expand: true,
|
|
||||||
y_align: Clutter.ActorAlign.CENTER,
|
|
||||||
});
|
|
||||||
this.add_child(this._percentageLabel);
|
|
||||||
this.add_style_class_name('power-status');
|
|
||||||
|
|
||||||
this._proxy = new PowerManagerProxy(Gio.DBus.system, BUS_NAME, OBJECT_PATH,
|
this._proxy = new PowerManagerProxy(Gio.DBus.system, BUS_NAME, OBJECT_PATH,
|
||||||
(proxy, error) => {
|
(proxy, error) => {
|
||||||
if (error) {
|
if (error)
|
||||||
log(error.message);
|
console.error(error.message);
|
||||||
} else {
|
else
|
||||||
this._proxy.connect('g-properties-changed',
|
this._proxy.connect('g-properties-changed', () => this._sync());
|
||||||
this._sync.bind(this));
|
|
||||||
}
|
|
||||||
this._sync();
|
this._sync();
|
||||||
});
|
});
|
||||||
|
|
||||||
this._item = new PopupMenu.PopupSubMenuMenuItem('', true);
|
this.bind_property('fallback-icon-name',
|
||||||
this._item.menu.addSettingsAction(_('Power Settings'),
|
this._icon, 'fallback-icon-name',
|
||||||
'gnome-power-panel.desktop');
|
GObject.BindingFlags.SYNC_CREATE);
|
||||||
this.menu.addMenuItem(this._item);
|
|
||||||
|
|
||||||
Main.sessionMode.connect('updated', this._sessionUpdated.bind(this));
|
this.connect('clicked', () => {
|
||||||
|
const app = Shell.AppSystem.get_default().lookup_app('gnome-power-panel.desktop');
|
||||||
|
Main.overview.hide();
|
||||||
|
Main.panel.closeQuickSettings();
|
||||||
|
app.activate();
|
||||||
|
});
|
||||||
|
|
||||||
|
Main.sessionMode.connect('updated', () => this._sessionUpdated());
|
||||||
this._sessionUpdated();
|
this._sessionUpdated();
|
||||||
|
this._sync();
|
||||||
}
|
}
|
||||||
|
|
||||||
_sessionUpdated() {
|
_sessionUpdated() {
|
||||||
let sensitive = !Main.sessionMode.isLocked && !Main.sessionMode.isGreeter;
|
this.reactive = Main.sessionMode.allowSettings;
|
||||||
this.menu.setSensitive(sensitive);
|
|
||||||
}
|
|
||||||
|
|
||||||
_getStatus() {
|
|
||||||
let seconds = 0;
|
|
||||||
|
|
||||||
if (this._proxy.State === UPower.DeviceState.FULLY_CHARGED)
|
|
||||||
return _('Fully Charged');
|
|
||||||
else if (this._proxy.State === UPower.DeviceState.CHARGING)
|
|
||||||
seconds = this._proxy.TimeToFull;
|
|
||||||
else if (this._proxy.State === UPower.DeviceState.DISCHARGING)
|
|
||||||
seconds = this._proxy.TimeToEmpty;
|
|
||||||
else if (this._proxy.State === UPower.DeviceState.PENDING_CHARGE)
|
|
||||||
return _('Not Charging');
|
|
||||||
// state is PENDING_DISCHARGE
|
|
||||||
else
|
|
||||||
return _('Estimating…');
|
|
||||||
|
|
||||||
let time = Math.round(seconds / 60);
|
|
||||||
if (time === 0) {
|
|
||||||
// 0 is reported when UPower does not have enough data
|
|
||||||
// to estimate battery life
|
|
||||||
return _('Estimating…');
|
|
||||||
}
|
|
||||||
|
|
||||||
let minutes = time % 60;
|
|
||||||
let hours = Math.floor(time / 60);
|
|
||||||
|
|
||||||
if (this._proxy.State === UPower.DeviceState.DISCHARGING) {
|
|
||||||
// Translators: this is <hours>:<minutes> Remaining (<percentage>)
|
|
||||||
return _('%d\u2236%02d Remaining (%d\u2009%%)').format(
|
|
||||||
hours, minutes, this._proxy.Percentage);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this._proxy.State === UPower.DeviceState.CHARGING) {
|
|
||||||
// Translators: this is <hours>:<minutes> Until Full (<percentage>)
|
|
||||||
return _('%d\u2236%02d Until Full (%d\u2009%%)').format(
|
|
||||||
hours, minutes, this._proxy.Percentage);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_sync() {
|
_sync() {
|
||||||
// Do we have batteries or a UPS?
|
// Do we have batteries or a UPS?
|
||||||
let visible = this._proxy.IsPresent;
|
this.visible = this._proxy.IsPresent;
|
||||||
if (visible) {
|
if (!this.visible)
|
||||||
this._item.show();
|
|
||||||
this._percentageLabel.visible =
|
|
||||||
this._desktopSettings.get_boolean(SHOW_BATTERY_PERCENTAGE);
|
|
||||||
} else {
|
|
||||||
// If there's no battery, then we use the power icon.
|
|
||||||
this._item.hide();
|
|
||||||
this._indicator.icon_name = 'system-shutdown-symbolic';
|
|
||||||
this._percentageLabel.hide();
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
// The icons
|
// The icons
|
||||||
let chargingState = this._proxy.State === UPower.DeviceState.CHARGING
|
let chargingState = this._proxy.State === UPower.DeviceState.CHARGING
|
||||||
@ -129,23 +76,67 @@ class Indicator extends PanelMenu.SystemIndicator {
|
|||||||
|
|
||||||
// Make sure we fall back to fallback-icon-name and not GThemedIcon's
|
// Make sure we fall back to fallback-icon-name and not GThemedIcon's
|
||||||
// default fallbacks
|
// default fallbacks
|
||||||
let gicon = new Gio.ThemedIcon({
|
const gicon = new Gio.ThemedIcon({
|
||||||
name: icon,
|
name: icon,
|
||||||
use_default_fallbacks: false,
|
use_default_fallbacks: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
this._indicator.gicon = gicon;
|
this.set({
|
||||||
this._item.icon.gicon = gicon;
|
label: _('%d\u2009%%').format(this._proxy.Percentage),
|
||||||
|
fallback_icon_name: this._proxy.IconName,
|
||||||
let fallbackIcon = this._proxy.IconName;
|
gicon,
|
||||||
this._indicator.fallback_icon_name = fallbackIcon;
|
});
|
||||||
this._item.icon.fallback_icon_name = fallbackIcon;
|
}
|
||||||
|
});
|
||||||
// The icon label
|
|
||||||
const label = _('%d\u2009%%').format(this._proxy.Percentage);
|
var Indicator = GObject.registerClass(
|
||||||
this._percentageLabel.text = label;
|
class Indicator extends SystemIndicator {
|
||||||
|
_init() {
|
||||||
// The status label
|
super._init();
|
||||||
this._item.label.text = this._getStatus();
|
|
||||||
|
this._desktopSettings = new Gio.Settings({
|
||||||
|
schema_id: 'org.gnome.desktop.interface',
|
||||||
|
});
|
||||||
|
this._desktopSettings.connect(
|
||||||
|
`changed::${SHOW_BATTERY_PERCENTAGE}`, () => this._sync());
|
||||||
|
|
||||||
|
this._indicator = this._addIndicator();
|
||||||
|
this._percentageLabel = new St.Label({
|
||||||
|
y_expand: true,
|
||||||
|
y_align: Clutter.ActorAlign.CENTER,
|
||||||
|
});
|
||||||
|
this.add_child(this._percentageLabel);
|
||||||
|
this.add_style_class_name('power-status');
|
||||||
|
|
||||||
|
this._powerToggle = new PowerToggle();
|
||||||
|
|
||||||
|
this._powerToggle.bind_property('label',
|
||||||
|
this._percentageLabel, 'text',
|
||||||
|
GObject.BindingFlags.SYNC_CREATE);
|
||||||
|
|
||||||
|
this._powerToggle.connectObject(
|
||||||
|
'notify::visible', () => this._sync(),
|
||||||
|
'notify::gicon', () => this._sync(),
|
||||||
|
'notify::fallback-icon-name', () => this._sync(),
|
||||||
|
this);
|
||||||
|
|
||||||
|
this.quickSettingsItems.push(this._powerToggle);
|
||||||
|
|
||||||
|
this._sync();
|
||||||
|
}
|
||||||
|
|
||||||
|
_sync() {
|
||||||
|
if (this._powerToggle.visible) {
|
||||||
|
this._indicator.set({
|
||||||
|
gicon: this._powerToggle.gicon,
|
||||||
|
fallback_icon_name: this._powerToggle.fallback_icon_name,
|
||||||
|
});
|
||||||
|
this._percentageLabel.visible =
|
||||||
|
this._desktopSettings.get_boolean(SHOW_BATTERY_PERCENTAGE);
|
||||||
|
} else {
|
||||||
|
// If there's no battery, then we use the power icon.
|
||||||
|
this._indicator.icon_name = 'system-shutdown-symbolic';
|
||||||
|
this._percentageLabel.hide();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user