2021-07-07 20:05:25 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
/* exported Indicator */
|
|
|
|
|
2022-07-29 16:50:57 +00:00
|
|
|
const {Gio, GObject} = imports.gi;
|
|
|
|
|
|
|
|
const {QuickMenuToggle, SystemIndicator} = imports.ui.quickSettings;
|
2021-07-07 20:05:25 +00:00
|
|
|
|
|
|
|
const PopupMenu = imports.ui.popupMenu;
|
|
|
|
|
2022-07-29 16:50:57 +00:00
|
|
|
const {loadInterfaceXML} = imports.misc.fileUtils;
|
2021-07-07 20:05:25 +00:00
|
|
|
|
|
|
|
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 15:13:22 +00: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 20:05:25 +00:00
|
|
|
};
|
|
|
|
|
2022-07-29 18:00:18 +00:00
|
|
|
const LAST_PROFILE_KEY = 'last-selected-power-profile';
|
|
|
|
|
2022-07-29 16:50:57 +00:00
|
|
|
const PowerProfilesToggle = GObject.registerClass(
|
|
|
|
class PowerProfilesToggle extends QuickMenuToggle {
|
2021-07-07 20:05:25 +00:00
|
|
|
_init() {
|
|
|
|
super._init();
|
|
|
|
|
|
|
|
this._profileItems = new Map();
|
|
|
|
|
2022-07-29 16:50:57 +00:00
|
|
|
this.connect('clicked', () => {
|
|
|
|
this._proxy.ActiveProfile = this.checked
|
|
|
|
? 'balanced'
|
|
|
|
: global.settings.get_string(LAST_PROFILE_KEY);
|
|
|
|
});
|
|
|
|
|
2021-07-07 20:05:25 +00:00
|
|
|
this._proxy = new PowerProfilesProxy(Gio.DBus.system, BUS_NAME, OBJECT_PATH,
|
|
|
|
(proxy, error) => {
|
|
|
|
if (error) {
|
|
|
|
log(error.message);
|
|
|
|
} else {
|
2022-08-10 09:44:42 +00:00
|
|
|
this._proxy.connect('g-properties-changed', (p, properties) => {
|
|
|
|
const profilesChanged = !!properties.lookup_value('Profiles', null);
|
|
|
|
if (profilesChanged)
|
|
|
|
this._syncProfiles();
|
|
|
|
this._sync();
|
|
|
|
});
|
2022-10-20 11:04:18 +00:00
|
|
|
|
|
|
|
if (this._proxy.g_name_owner)
|
|
|
|
this._syncProfiles();
|
2021-07-07 20:05:25 +00:00
|
|
|
}
|
|
|
|
this._sync();
|
|
|
|
});
|
|
|
|
|
|
|
|
this._profileSection = new PopupMenu.PopupMenuSection();
|
2022-07-29 16:50:57 +00:00
|
|
|
this.menu.addMenuItem(this._profileSection);
|
|
|
|
this.menu.setHeader('power-profile-balanced-symbolic', _('Power Profiles'));
|
2023-01-13 22:56:44 +00:00
|
|
|
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
|
2023-01-13 16:09:25 +00:00
|
|
|
this.menu.addSettingsAction(_('Power Settings'),
|
|
|
|
'gnome-power-panel.desktop');
|
2021-07-07 20:05:25 +00:00
|
|
|
|
2022-07-29 16:50:57 +00:00
|
|
|
this._sync();
|
2021-07-07 20:05:25 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 16:46:09 +00: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 15:13:22 +00:00
|
|
|
const {label, iconName} = PROFILE_PARAMS[profile];
|
2022-07-22 16:46:09 +00:00
|
|
|
if (!label)
|
|
|
|
continue;
|
|
|
|
|
2022-07-23 15:13:22 +00:00
|
|
|
const item = new PopupMenu.PopupImageMenuItem(label, iconName);
|
2022-07-22 16:46:09 +00:00
|
|
|
item.connect('activate',
|
|
|
|
() => (this._proxy.ActiveProfile = profile));
|
|
|
|
this._profileItems.set(profile, item);
|
|
|
|
this._profileSection.addMenuItem(item);
|
|
|
|
}
|
2022-07-29 16:50:57 +00:00
|
|
|
|
|
|
|
this.menuEnabled = this._profileItems.size > 2;
|
2022-07-22 16:46:09 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 20:05:25 +00:00
|
|
|
_sync() {
|
2022-07-29 16:50:57 +00:00
|
|
|
this.visible = this._proxy.g_name_owner !== null;
|
2021-07-07 20:05:25 +00:00
|
|
|
|
2022-07-29 16:50:57 +00:00
|
|
|
if (!this.visible)
|
2021-07-07 20:05:25 +00:00
|
|
|
return;
|
|
|
|
|
2022-07-29 18:00:18 +00:00
|
|
|
const {ActiveProfile: activeProfile} = this._proxy;
|
|
|
|
|
2021-07-07 20:05:25 +00:00
|
|
|
for (const [profile, item] of this._profileItems) {
|
2022-07-29 18:00:18 +00:00
|
|
|
item.setOrnament(profile === activeProfile
|
2022-07-23 15:14:02 +00:00
|
|
|
? PopupMenu.Ornament.CHECK
|
2021-07-07 20:05:25 +00:00
|
|
|
: PopupMenu.Ornament.NONE);
|
|
|
|
}
|
|
|
|
|
2022-07-29 16:50:57 +00:00
|
|
|
this.set(PROFILE_PARAMS[activeProfile]);
|
|
|
|
this.checked = activeProfile !== 'balanced';
|
2022-07-29 18:00:18 +00:00
|
|
|
|
2022-07-29 16:50:57 +00:00
|
|
|
if (this.checked)
|
2022-07-29 18:00:18 +00:00
|
|
|
global.settings.set_string(LAST_PROFILE_KEY, activeProfile);
|
2021-07-07 20:05:25 +00:00
|
|
|
}
|
|
|
|
});
|
2022-07-29 16:50:57 +00:00
|
|
|
|
|
|
|
var Indicator = GObject.registerClass(
|
|
|
|
class Indicator extends SystemIndicator {
|
|
|
|
_init() {
|
|
|
|
super._init();
|
|
|
|
|
|
|
|
this.quickSettingsItems.push(new PowerProfilesToggle());
|
|
|
|
}
|
|
|
|
});
|