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
|
|
|
|
2022-07-30 14:52:39 +00:00
|
|
|
const {Atk, Clutter, Gio, GObject, Shell, 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;
|
2022-07-30 14:52:39 +00:00
|
|
|
const {QuickToggle, SystemIndicator} = imports.ui.quickSettings;
|
2010-11-15 21:45:17 +00:00
|
|
|
|
2022-07-30 14:52:39 +00:00
|
|
|
const {loadInterfaceXML} = imports.misc.fileUtils;
|
2018-09-06 00:55:20 +00:00
|
|
|
|
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
|
|
|
|
2022-07-30 14:52:39 +00:00
|
|
|
const SHOW_BATTERY_PERCENTAGE = 'show-battery-percentage';
|
2015-11-09 13:02:02 +00:00
|
|
|
|
2022-07-30 14:52:39 +00:00
|
|
|
const PowerToggle = GObject.registerClass({
|
|
|
|
Properties: {
|
|
|
|
'fallback-icon-name': GObject.ParamSpec.string('fallback-icon-name', '', '',
|
|
|
|
GObject.ParamFlags.READWRITE,
|
|
|
|
''),
|
|
|
|
},
|
|
|
|
}, class PowerToggle extends QuickToggle {
|
2019-07-16 09:24:13 +00:00
|
|
|
_init() {
|
2022-07-30 14:52:39 +00:00
|
|
|
super._init({
|
|
|
|
accessible_role: Atk.Role.PUSH_BUTTON,
|
2021-06-28 16:29:23 +00:00
|
|
|
});
|
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,
|
2021-06-28 16:29:23 +00:00
|
|
|
(proxy, error) => {
|
2022-07-30 14:52:39 +00:00
|
|
|
if (error)
|
|
|
|
console.error(error.message);
|
|
|
|
else
|
|
|
|
this._proxy.connect('g-properties-changed', () => this._sync());
|
2021-06-28 16:29:23 +00:00
|
|
|
this._sync();
|
|
|
|
});
|
|
|
|
|
2022-07-30 14:52:39 +00:00
|
|
|
this.bind_property('fallback-icon-name',
|
|
|
|
this._icon, 'fallback-icon-name',
|
|
|
|
GObject.BindingFlags.SYNC_CREATE);
|
|
|
|
|
|
|
|
this.connect('clicked', () => {
|
|
|
|
const app = Shell.AppSystem.get_default().lookup_app('gnome-power-panel.desktop');
|
|
|
|
Main.overview.hide();
|
|
|
|
Main.panel.closeQuickSettings();
|
|
|
|
app.activate();
|
|
|
|
});
|
2013-05-22 18:19:00 +00:00
|
|
|
|
2022-07-30 14:52:39 +00:00
|
|
|
Main.sessionMode.connect('updated', () => this._sessionUpdated());
|
2013-05-22 18:19:00 +00:00
|
|
|
this._sessionUpdated();
|
2022-07-30 14:52:39 +00:00
|
|
|
this._sync();
|
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() {
|
2022-07-30 14:52:39 +00:00
|
|
|
this.reactive = Main.sessionMode.allowSettings;
|
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?
|
2022-07-30 14:52:39 +00:00
|
|
|
this.visible = this._proxy.IsPresent;
|
|
|
|
if (!this.visible)
|
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
|
2021-06-28 16:29:23 +00:00
|
|
|
let chargingState = this._proxy.State === UPower.DeviceState.CHARGING
|
2019-05-28 17:56:36 +00:00
|
|
|
? '-charging' : '';
|
|
|
|
let fillLevel = 10 * Math.floor(this._proxy.Percentage / 10);
|
2020-05-26 19:26:22 +00:00
|
|
|
const charged =
|
|
|
|
this._proxy.State === UPower.DeviceState.FULLY_CHARGED ||
|
|
|
|
(this._proxy.State === UPower.DeviceState.CHARGING && fillLevel === 100);
|
|
|
|
const icon = charged
|
|
|
|
? 'battery-level-100-charged-symbolic'
|
2022-02-07 14:14:06 +00:00
|
|
|
: `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
|
2022-07-30 14:52:39 +00:00
|
|
|
const gicon = new Gio.ThemedIcon({
|
2019-07-15 22:37:13 +00:00
|
|
|
name: icon,
|
2019-08-20 21:43:54 +00:00
|
|
|
use_default_fallbacks: false,
|
2019-07-15 22:37:13 +00:00
|
|
|
});
|
|
|
|
|
2022-07-30 14:52:39 +00:00
|
|
|
this.set({
|
|
|
|
label: _('%d\u2009%%').format(this._proxy.Percentage),
|
|
|
|
fallback_icon_name: this._proxy.IconName,
|
|
|
|
gicon,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var Indicator = GObject.registerClass(
|
|
|
|
class Indicator extends SystemIndicator {
|
|
|
|
_init() {
|
|
|
|
super._init();
|
|
|
|
|
|
|
|
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();
|
2013-10-17 14:45:17 +00:00
|
|
|
|
2022-07-30 14:52:39 +00:00
|
|
|
this._powerToggle.bind_property('label',
|
|
|
|
this._percentageLabel, 'text',
|
|
|
|
GObject.BindingFlags.SYNC_CREATE);
|
2019-05-28 17:56:36 +00:00
|
|
|
|
2022-07-30 14:52:39 +00:00
|
|
|
this._powerToggle.connectObject(
|
|
|
|
'notify::visible', () => this._sync(),
|
|
|
|
'notify::gicon', () => this._sync(),
|
|
|
|
'notify::fallback-icon-name', () => this._sync(),
|
|
|
|
this);
|
2015-11-09 13:02:02 +00:00
|
|
|
|
2022-07-30 14:52:39 +00:00
|
|
|
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();
|
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2019-07-16 09:24:13 +00:00
|
|
|
});
|