2021-07-07 16:05:25 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
/* exported Indicator */
|
|
|
|
|
|
|
|
const { Gio, GObject } = imports.gi;
|
|
|
|
|
|
|
|
const Main = imports.ui.main;
|
|
|
|
const PanelMenu = imports.ui.panelMenu;
|
|
|
|
const PopupMenu = imports.ui.popupMenu;
|
|
|
|
|
|
|
|
const { loadInterfaceXML } = imports.misc.fileUtils;
|
|
|
|
|
|
|
|
const BUS_NAME = 'net.hadess.PowerProfiles';
|
|
|
|
const OBJECT_PATH = '/net/hadess/PowerProfiles';
|
|
|
|
|
|
|
|
const PowerProfilesIface = loadInterfaceXML('net.hadess.PowerProfiles');
|
|
|
|
const PowerProfilesProxy = Gio.DBusProxy.makeProxyWrapper(PowerProfilesIface);
|
|
|
|
|
2022-07-23 11:13:22 -04:00
|
|
|
const PROFILE_PARAMS = {
|
|
|
|
'performance': {
|
|
|
|
label: C_('Power profile', 'Performance'),
|
|
|
|
iconName: 'power-profile-performance-symbolic',
|
|
|
|
},
|
|
|
|
|
|
|
|
'balanced': {
|
|
|
|
label: C_('Power profile', 'Balanced'),
|
|
|
|
iconName: 'power-profile-balanced-symbolic',
|
|
|
|
},
|
|
|
|
|
|
|
|
'power-saver': {
|
|
|
|
label: C_('Power profile', 'Power Saver'),
|
|
|
|
iconName: 'power-profile-power-saver-symbolic',
|
|
|
|
},
|
2021-07-07 16:05:25 -04:00
|
|
|
};
|
|
|
|
|
2022-07-29 14:00:18 -04:00
|
|
|
const LAST_PROFILE_KEY = 'last-selected-power-profile';
|
|
|
|
|
2021-07-07 16:05:25 -04:00
|
|
|
var Indicator = GObject.registerClass(
|
|
|
|
class Indicator extends PanelMenu.SystemIndicator {
|
|
|
|
_init() {
|
|
|
|
super._init();
|
|
|
|
|
|
|
|
this._profileItems = new Map();
|
|
|
|
|
|
|
|
this._proxy = new PowerProfilesProxy(Gio.DBus.system, BUS_NAME, OBJECT_PATH,
|
|
|
|
(proxy, error) => {
|
|
|
|
if (error) {
|
|
|
|
log(error.message);
|
|
|
|
} else {
|
|
|
|
this._proxy.connect('g-properties-changed',
|
|
|
|
(p, properties) => {
|
|
|
|
const propertyNames = properties.deep_unpack();
|
2022-07-22 12:46:09 -04:00
|
|
|
if ('Profiles' in propertyNames)
|
|
|
|
this._syncProfiles();
|
2021-07-07 16:05:25 -04:00
|
|
|
this._sync();
|
|
|
|
});
|
2022-07-22 12:46:09 -04:00
|
|
|
this._syncProfiles();
|
2021-07-07 16:05:25 -04:00
|
|
|
}
|
|
|
|
this._sync();
|
|
|
|
});
|
|
|
|
|
|
|
|
this._item = new PopupMenu.PopupSubMenuMenuItem('', true);
|
|
|
|
|
|
|
|
this._profileSection = new PopupMenu.PopupMenuSection();
|
|
|
|
this._item.menu.addMenuItem(this._profileSection);
|
|
|
|
this._item.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
|
|
|
|
this._item.menu.addSettingsAction(_('Power Settings'),
|
|
|
|
'gnome-power-panel.desktop');
|
|
|
|
this.menu.addMenuItem(this._item);
|
|
|
|
|
|
|
|
Main.sessionMode.connect('updated', this._sessionUpdated.bind(this));
|
|
|
|
this._sessionUpdated();
|
|
|
|
this._sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
_sessionUpdated() {
|
|
|
|
const sensitive = !Main.sessionMode.isLocked && !Main.sessionMode.isGreeter;
|
|
|
|
this.menu.setSensitive(sensitive);
|
|
|
|
}
|
|
|
|
|
2022-07-22 12:46:09 -04:00
|
|
|
_syncProfiles() {
|
|
|
|
this._profileSection.removeAll();
|
|
|
|
this._profileItems.clear();
|
|
|
|
|
|
|
|
const profiles = this._proxy.Profiles
|
|
|
|
.map(p => p.Profile.unpack())
|
|
|
|
.reverse();
|
|
|
|
for (const profile of profiles) {
|
2022-07-23 11:13:22 -04:00
|
|
|
const {label, iconName} = PROFILE_PARAMS[profile];
|
2022-07-22 12:46:09 -04:00
|
|
|
if (!label)
|
|
|
|
continue;
|
|
|
|
|
2022-07-23 11:13:22 -04:00
|
|
|
const item = new PopupMenu.PopupImageMenuItem(label, iconName);
|
2022-07-22 12:46:09 -04:00
|
|
|
item.connect('activate',
|
|
|
|
() => (this._proxy.ActiveProfile = profile));
|
|
|
|
this._profileItems.set(profile, item);
|
|
|
|
this._profileSection.addMenuItem(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 16:05:25 -04:00
|
|
|
_sync() {
|
|
|
|
this._item.visible = this._proxy.g_name_owner !== null;
|
|
|
|
|
|
|
|
if (!this._item.visible)
|
|
|
|
return;
|
|
|
|
|
2022-07-29 14:00:18 -04:00
|
|
|
const {ActiveProfile: activeProfile} = this._proxy;
|
|
|
|
|
2021-07-07 16:05:25 -04:00
|
|
|
for (const [profile, item] of this._profileItems) {
|
2022-07-29 14:00:18 -04:00
|
|
|
item.setOrnament(profile === activeProfile
|
2022-07-23 11:14:02 -04:00
|
|
|
? PopupMenu.Ornament.CHECK
|
2021-07-07 16:05:25 -04:00
|
|
|
: PopupMenu.Ornament.NONE);
|
|
|
|
}
|
|
|
|
|
2022-07-29 14:00:18 -04:00
|
|
|
const {label, iconName} = PROFILE_PARAMS[activeProfile];
|
2022-07-23 11:13:22 -04:00
|
|
|
this._item.label.text = label;
|
|
|
|
this._item.icon.icon_name = iconName;
|
2022-07-29 14:00:18 -04:00
|
|
|
|
|
|
|
if (activeProfile !== 'balanced')
|
|
|
|
global.settings.set_string(LAST_PROFILE_KEY, activeProfile);
|
2021-07-07 16:05:25 -04:00
|
|
|
}
|
|
|
|
});
|