2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 14:07:06 +00:00
|
|
|
/* exported Indicator */
|
2010-11-15 21:45:17 +00:00
|
|
|
|
2019-07-16 09:24:13 +00:00
|
|
|
const { Clutter, Gio, GObject, St, UPowerGlib: UPower } = imports.gi;
|
2010-11-15 21:45:17 +00:00
|
|
|
|
2013-05-22 18:19:00 +00:00
|
|
|
const Main = imports.ui.main;
|
2010-11-15 21:45:17 +00:00
|
|
|
const PanelMenu = imports.ui.panelMenu;
|
|
|
|
const PopupMenu = imports.ui.popupMenu;
|
|
|
|
|
2018-09-06 00:55:20 +00:00
|
|
|
const { loadInterfaceXML } = imports.misc.fileUtils;
|
|
|
|
|
2013-10-17 14:45:17 +00:00
|
|
|
const BUS_NAME = 'org.freedesktop.UPower';
|
|
|
|
const OBJECT_PATH = '/org/freedesktop/UPower/devices/DisplayDevice';
|
|
|
|
|
2018-09-06 00:55:20 +00:00
|
|
|
const DisplayDeviceInterface = loadInterfaceXML('org.freedesktop.UPower.Device');
|
2013-10-17 14:45:17 +00:00
|
|
|
const PowerManagerProxy = Gio.DBusProxy.makeProxyWrapper(DisplayDeviceInterface);
|
2010-11-15 21:45:17 +00:00
|
|
|
|
2015-11-09 13:02:02 +00:00
|
|
|
const SHOW_BATTERY_PERCENTAGE = 'show-battery-percentage';
|
|
|
|
|
2019-10-28 18:35:33 +00:00
|
|
|
var Indicator = GObject.registerClass(
|
|
|
|
class Indicator extends PanelMenu.SystemIndicator {
|
2019-07-16 09:24:13 +00:00
|
|
|
_init() {
|
|
|
|
super._init();
|
2013-06-06 21:27:25 +00:00
|
|
|
|
2015-11-09 13:02:02 +00:00
|
|
|
this._desktopSettings = new Gio.Settings({ schema_id: 'org.gnome.desktop.interface' });
|
2019-01-30 00:18:24 +00:00
|
|
|
this._desktopSettings.connect(`changed::${SHOW_BATTERY_PERCENTAGE}`,
|
2017-12-02 00:27:35 +00:00
|
|
|
this._sync.bind(this));
|
2015-11-09 13:02:02 +00:00
|
|
|
|
2013-07-19 10:05:47 +00:00
|
|
|
this._indicator = this._addIndicator();
|
2015-11-09 13:02:02 +00:00
|
|
|
this._percentageLabel = new St.Label({ y_expand: true,
|
|
|
|
y_align: Clutter.ActorAlign.CENTER });
|
2019-10-21 18:44:00 +00:00
|
|
|
this.add_child(this._percentageLabel);
|
2019-07-16 09:24:13 +00:00
|
|
|
this.add_style_class_name('power-status');
|
2011-11-20 14:38:48 +00:00
|
|
|
|
2013-10-17 14:45:17 +00:00
|
|
|
this._proxy = new PowerManagerProxy(Gio.DBus.system, BUS_NAME, OBJECT_PATH,
|
2017-10-31 00:38:18 +00:00
|
|
|
(proxy, error) => {
|
2013-05-22 18:11:34 +00:00
|
|
|
if (error) {
|
|
|
|
log(error.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._proxy.connect('g-properties-changed',
|
2017-12-02 00:27:35 +00:00
|
|
|
this._sync.bind(this));
|
2013-05-22 18:19:00 +00:00
|
|
|
this._sync();
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2013-05-22 18:11:34 +00:00
|
|
|
|
2013-10-17 14:45:17 +00:00
|
|
|
this._item = new PopupMenu.PopupSubMenuMenuItem("", true);
|
2013-05-22 18:19:00 +00:00
|
|
|
this._item.menu.addSettingsAction(_("Power Settings"), 'gnome-power-panel.desktop');
|
|
|
|
this.menu.addMenuItem(this._item);
|
|
|
|
|
2017-12-02 00:27:35 +00:00
|
|
|
Main.sessionMode.connect('updated', this._sessionUpdated.bind(this));
|
2013-05-22 18:19:00 +00:00
|
|
|
this._sessionUpdated();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-05-22 18:19:00 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_sessionUpdated() {
|
2013-05-22 18:19:00 +00:00
|
|
|
let sensitive = !Main.sessionMode.isLocked && !Main.sessionMode.isGreeter;
|
|
|
|
this.menu.setSensitive(sensitive);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-05-22 18:19:00 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_getStatus() {
|
2015-11-17 12:16:45 +00:00
|
|
|
let seconds = 0;
|
|
|
|
|
2013-10-17 14:45:17 +00:00
|
|
|
if (this._proxy.State == UPower.DeviceState.FULLY_CHARGED)
|
2013-05-22 18:19:00 +00:00
|
|
|
return _("Fully Charged");
|
2013-10-17 14:45:17 +00:00
|
|
|
else if (this._proxy.State == UPower.DeviceState.CHARGING)
|
2015-11-17 12:16:45 +00:00
|
|
|
seconds = this._proxy.TimeToFull;
|
|
|
|
else if (this._proxy.State == UPower.DeviceState.DISCHARGING)
|
|
|
|
seconds = this._proxy.TimeToEmpty;
|
2018-11-02 20:51:33 +00:00
|
|
|
else if (this._proxy.State == UPower.DeviceState.PENDING_CHARGE)
|
|
|
|
return _("Not Charging");
|
|
|
|
// state is PENDING_DISCHARGE
|
2015-11-17 12:16:45 +00:00
|
|
|
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;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2010-11-15 21:45:17 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_sync() {
|
2013-10-17 14:45:17 +00:00
|
|
|
// Do we have batteries or a UPS?
|
|
|
|
let visible = this._proxy.IsPresent;
|
|
|
|
if (visible) {
|
2019-04-12 21:00:49 +00:00
|
|
|
this._item.show();
|
2015-11-09 13:02:02 +00:00
|
|
|
this._percentageLabel.visible = this._desktopSettings.get_boolean(SHOW_BATTERY_PERCENTAGE);
|
2013-10-17 14:45:17 +00:00
|
|
|
} else {
|
|
|
|
// If there's no battery, then we use the power icon.
|
2019-04-12 21:00:49 +00:00
|
|
|
this._item.hide();
|
2013-10-17 14:45:17 +00:00
|
|
|
this._indicator.icon_name = 'system-shutdown-symbolic';
|
2015-11-09 13:02:02 +00:00
|
|
|
this._percentageLabel.hide();
|
2013-10-17 14:45:17 +00:00
|
|
|
return;
|
2013-10-10 15:11:24 +00:00
|
|
|
}
|
|
|
|
|
2013-10-17 14:45:17 +00:00
|
|
|
// The icons
|
2019-05-28 17:56:36 +00:00
|
|
|
let chargingState = this._proxy.State == UPower.DeviceState.CHARGING
|
|
|
|
? '-charging' : '';
|
|
|
|
let fillLevel = 10 * Math.floor(this._proxy.Percentage / 10);
|
2019-11-07 20:51:28 +00:00
|
|
|
let icon;
|
|
|
|
if (this._proxy.State == UPower.DeviceState.FULLY_CHARGED ||
|
|
|
|
fillLevel === 100)
|
|
|
|
icon = 'battery-level-100-charged-symbolic';
|
|
|
|
else
|
|
|
|
icon = `battery-level-${fillLevel}${chargingState}-symbolic`;
|
2019-05-28 17:56:36 +00:00
|
|
|
|
2019-07-15 22:37:13 +00:00
|
|
|
// Make sure we fall back to fallback-icon-name and not GThemedIcon's
|
|
|
|
// default fallbacks
|
|
|
|
let gicon = new Gio.ThemedIcon({
|
|
|
|
name: icon,
|
|
|
|
use_default_fallbacks: false
|
|
|
|
});
|
|
|
|
|
|
|
|
this._indicator.gicon = gicon;
|
|
|
|
this._item.icon.gicon = gicon;
|
2013-10-17 14:45:17 +00:00
|
|
|
|
2019-05-28 17:56:36 +00:00
|
|
|
let fallbackIcon = this._proxy.IconName;
|
|
|
|
this._indicator.fallback_icon_name = fallbackIcon;
|
|
|
|
this._item.icon.fallback_icon_name = fallbackIcon;
|
|
|
|
|
2015-11-09 13:02:02 +00:00
|
|
|
// The icon label
|
2019-01-29 01:18:52 +00:00
|
|
|
let label;
|
2015-11-09 13:02:02 +00:00
|
|
|
if (this._proxy.State == UPower.DeviceState.FULLY_CHARGED)
|
2019-01-29 19:36:54 +00:00
|
|
|
label = _("%d\u2009%%").format(100);
|
2015-11-09 13:02:02 +00:00
|
|
|
else
|
2019-01-29 19:36:54 +00:00
|
|
|
label = _("%d\u2009%%").format(this._proxy.Percentage);
|
2015-11-09 13:02:02 +00:00
|
|
|
this._percentageLabel.clutter_text.set_markup('<span size="smaller">' + label + '</span>');
|
|
|
|
|
2013-10-17 14:45:17 +00:00
|
|
|
// The status label
|
2015-08-06 09:19:37 +00:00
|
|
|
this._item.label.text = this._getStatus();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2019-07-16 09:24:13 +00:00
|
|
|
});
|