Skip to content

Commit 903026d

Browse files
authored
Merge pull request #24 from oscfdezdz/esm
Port to ESM for 45
2 parents 4917510 + 0315c48 commit 903026d

File tree

5 files changed

+133
-137
lines changed

5 files changed

+133
-137
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Clone the repo, pack and install the extension.
3939
```
4040
git clone https://github.com/eliapasquali/power-profile-switcher
4141
cd power-profile-switcher
42-
gnome-extensions pack
42+
gnome-extensions pack --extra-source=ui
4343
gnome-extensions install power-profile-switcher@eliapasquali.github.io.shell-extension.zip
4444
```
4545
After this, the extensions is installed. In order to enable it run the following command or use the Extensions app.

extension.js

+104-101
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
const ExtensionUtils = imports.misc.extensionUtils;
2-
const Me = ExtensionUtils.getCurrentExtension();
3-
const { loadInterfaceXML } = imports.misc.fileUtils;
1+
import Gio from 'gi://Gio';
2+
import GLib from 'gi://GLib';
3+
import UPower from 'gi://UPowerGlib';
44

5-
const Main = imports.ui.main;
6-
const { Gio, GLib, UPowerGlib:UPower } = imports.gi;
5+
import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js';
6+
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
7+
import * as FileUtils from 'resource:///org/gnome/shell/misc/fileUtils.js';
78

89
let settings, client, device;
910

@@ -36,7 +37,7 @@ const PowerManagerProxy = Gio.DBusProxy.makeProxyWrapper(DisplayDeviceInterface)
3637
const POWER_PROFILES_BUS_NAME = 'net.hadess.PowerProfiles';
3738
const POWER_PROFILES_OBJECT_PATH = '/net/hadess/PowerProfiles';
3839

39-
const PowerProfilesIface = loadInterfaceXML('net.hadess.PowerProfiles');
40+
const PowerProfilesIface = FileUtils.loadInterfaceXML('net.hadess.PowerProfiles');
4041
const PowerProfilesProxy = Gio.DBusProxy.makeProxyWrapper(PowerProfilesIface);
4142

4243

@@ -102,109 +103,111 @@ const getDefaults = () => {
102103
batteryThreshold = settings.get_int("threshold");
103104
}
104105

105-
function init() {
106-
ExtensionUtils.initTranslations(Me.metadata.uuid);
107-
}
106+
export default class PowerProfileSwitcher extends Extension {
107+
constructor(metadata) {
108+
super(metadata);
109+
}
108110

109-
function enable() {
110-
client = UPower.Client.new();
111-
device = client.get_display_device();
112-
113-
settings = ExtensionUtils.getSettings(
114-
"org.gnome.shell.extensions.power-profile-switcher"
115-
);
116-
117-
batteryPercentageWatcher = settings.connect(
118-
"changed::threshold",
119-
checkProfile
120-
);
121-
122-
ACDefaultWatcher = settings.connect(
123-
"changed::ac",
124-
checkProfile
125-
);
126-
127-
batteryDefaultWatcher = settings.connect(
128-
"changed::bat",
129-
checkProfile
130-
);
131-
132-
powerManagerCancellable = new Gio.Cancellable();
133-
powerManagerProxy = new PowerManagerProxy(Gio.DBus.system, UPOWER_BUS_NAME, UPOWER_OBJECT_PATH,
134-
(proxy, error) => {
135-
if (error) {
136-
logError(error.message);
137-
return;
138-
}
139-
batteryThresholdWatcher = powerManagerProxy.connect('g-properties-changed', checkProfile);
140-
checkProfile();
141-
}, powerManagerCancellable);
142-
143-
144-
powerProfilesCancellable = new Gio.Cancellable();
145-
powerProfilesProxy = new PowerProfilesProxy(Gio.DBus.system, POWER_PROFILES_BUS_NAME, POWER_PROFILES_OBJECT_PATH,
146-
(proxy, error) => {
147-
if (error) {
148-
logError(error.message);
149-
} else {
150-
powerProfileWatcher = powerProfilesProxy.connect('g-properties-changed', (p, properties) => {
151-
const payload = properties?.deep_unpack();
152-
153-
if (payload?.ActiveProfile) {
154-
activeProfile = payload?.ActiveProfile?.unpack();
155-
if (perfDebounceTimerId) {
156-
GLib.source_remove(perfDebounceTimerId);
157-
perfDebounceTimerId = null;
158-
}
159-
}
160-
161-
const isOnPowerSupply = device?.power_supply ||
162-
device?.state !== UPower.DeviceState.PENDING_DISCHARGE ||
163-
device?.state !== UPower.DeviceState.DISCHARGING;
111+
enable() {
112+
client = UPower.Client.new();
113+
device = client.get_display_device();
114+
115+
settings = this.getSettings(
116+
"org.gnome.shell.extensions.power-profile-switcher"
117+
);
118+
119+
batteryPercentageWatcher = settings.connect(
120+
"changed::threshold",
121+
checkProfile
122+
);
164123

165-
if (isOnPowerSupply && payload?.PerformanceDegraded) {
166-
try {
167-
const reason = payload?.PerformanceDegraded?.unpack();
168-
169-
if (reason === 'lap-detected') {
170-
perfDebounceTimerId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 2000, () => {
171-
checkProfile();
172-
perfDebounceTimerId = null;
173-
return GLib.SOURCE_REMOVE;
174-
});
175-
}
176-
else if (reason) {
177-
log(`ActiveProfile: ${activeProfile}, PerformanceDegraded: ${reason}`);
124+
ACDefaultWatcher = settings.connect(
125+
"changed::ac",
126+
checkProfile
127+
);
128+
129+
batteryDefaultWatcher = settings.connect(
130+
"changed::bat",
131+
checkProfile
132+
);
133+
134+
powerManagerCancellable = new Gio.Cancellable();
135+
powerManagerProxy = new PowerManagerProxy(Gio.DBus.system, UPOWER_BUS_NAME, UPOWER_OBJECT_PATH,
136+
(proxy, error) => {
137+
if (error) {
138+
logError(error.message);
139+
return;
140+
}
141+
batteryThresholdWatcher = powerManagerProxy.connect('g-properties-changed', checkProfile);
142+
checkProfile();
143+
}, powerManagerCancellable);
144+
145+
146+
powerProfilesCancellable = new Gio.Cancellable();
147+
powerProfilesProxy = new PowerProfilesProxy(Gio.DBus.system, POWER_PROFILES_BUS_NAME, POWER_PROFILES_OBJECT_PATH,
148+
(proxy, error) => {
149+
if (error) {
150+
logError(error.message);
151+
} else {
152+
powerProfileWatcher = powerProfilesProxy.connect('g-properties-changed', (p, properties) => {
153+
const payload = properties?.deep_unpack();
154+
155+
if (payload?.ActiveProfile) {
156+
activeProfile = payload?.ActiveProfile?.unpack();
157+
if (perfDebounceTimerId) {
158+
GLib.source_remove(perfDebounceTimerId);
159+
perfDebounceTimerId = null;
178160
}
179161
}
180-
catch (e) {
181-
logError(e)
162+
163+
const isOnPowerSupply = device?.power_supply ||
164+
device?.state !== UPower.DeviceState.PENDING_DISCHARGE ||
165+
device?.state !== UPower.DeviceState.DISCHARGING;
166+
167+
if (isOnPowerSupply && payload?.PerformanceDegraded) {
168+
try {
169+
const reason = payload?.PerformanceDegraded?.unpack();
170+
171+
if (reason === 'lap-detected') {
172+
perfDebounceTimerId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 2000, () => {
173+
checkProfile();
174+
perfDebounceTimerId = null;
175+
return GLib.SOURCE_REMOVE;
176+
});
177+
}
178+
else if (reason) {
179+
log(`ActiveProfile: ${activeProfile}, PerformanceDegraded: ${reason}`);
180+
}
181+
}
182+
catch (e) {
183+
logError(e)
184+
}
182185
}
183-
}
184-
});
185-
}
186-
}, powerProfilesCancellable);
187-
}
186+
});
187+
}
188+
}, powerProfilesCancellable);
189+
}
188190

189-
function disable() {
190-
settings.disconnect(batteryPercentageWatcher);
191-
settings.disconnect(ACDefaultWatcher);
192-
settings.disconnect(batteryDefaultWatcher);
191+
disable() {
192+
settings.disconnect(batteryPercentageWatcher);
193+
settings.disconnect(ACDefaultWatcher);
194+
settings.disconnect(batteryDefaultWatcher);
193195

194-
powerManagerProxy.disconnect(batteryThresholdWatcher);
195-
powerManagerCancellable.cancel();
196+
powerManagerProxy.disconnect(batteryThresholdWatcher);
197+
powerManagerCancellable.cancel();
196198

197-
powerProfilesProxy.disconnect(powerProfileWatcher);
198-
powerProfilesCancellable.cancel();
199+
powerProfilesProxy.disconnect(powerProfileWatcher);
200+
powerProfilesCancellable.cancel();
199201

200-
if (perfDebounceTimerId) {
201-
GLib.source_remove(perfDebounceTimerId);
202-
perfDebounceTimerId = null;
203-
}
202+
if (perfDebounceTimerId) {
203+
GLib.source_remove(perfDebounceTimerId);
204+
perfDebounceTimerId = null;
205+
}
204206

205-
settings = null;
206-
client = null;
207-
device = null;
208-
activeProfile = null;
209-
switchProfile("balanced");
207+
settings = null;
208+
client = null;
209+
device = null;
210+
activeProfile = null;
211+
switchProfile("balanced");
212+
}
210213
}

metadata.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"uuid": "[email protected]",
33
"name": "Power Profile Switcher",
44
"description": "Automatically switch between power profiles based on power supply and percentage.",
5-
"version": 3,
6-
"shell-version": ["42", "43", "44"],
7-
"url": "https://github.com/eliapasquali/power-profile-switcher"
5+
"version": 12,
6+
"shell-version": ["45"],
7+
"url": "https://github.com/eliapasquali/power-profile-switcher",
8+
"gettext-domain": "org.gnome.shell.extensions.power-profile-switcher",
9+
"settings-schema": "org.gnome.shell.extensions.power-profile-switcher"
810
}

prefs.js

+17-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
const { Adw, GLib, GObject, Gio } = imports.gi;
1+
import Adw from 'gi://Adw';
2+
import GLib from 'gi://GLib';
3+
import GObject from 'gi://GObject';
4+
import Gio from 'gi://Gio';
25

3-
const ExtensionUtils = imports.misc.extensionUtils;
4-
const Me = ExtensionUtils.getCurrentExtension();
6+
import {ExtensionPreferences} from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';
57

68
const PROFILE_CHOICES = [
79
'performance',
@@ -25,38 +27,33 @@ function bindAdwComboRow(comboRow, settings, key, map_) {
2527
});
2628
}
2729

28-
2930
var General = GObject.registerClass({
30-
GTypeName: 'GeneralPreferences',
31-
Template: `file://${GLib.build_filenamev([Me.path, 'ui', 'general.ui'])}`,
31+
GTypeName: 'PowerProfileSwitcherPreferences',
32+
Template: GLib.Uri.resolve_relative(import.meta.url, './ui/general.ui', GLib.UriFlags.NONE),
3233
InternalChildren: [
3334
'ac_profile',
3435
'bat_profile',
3536
'threshold',
3637
],
3738
}, class General extends Adw.PreferencesPage {
38-
constructor(settings) {
39-
super({});
39+
_init(settings, params = {}) {
40+
super._init(params);
4041

4142
bindAdwComboRow(this._ac_profile, settings, 'ac', PROFILE_CHOICES);
4243
bindAdwComboRow(this._bat_profile, settings, 'bat', PROFILE_CHOICES);
4344
settings.bind(
44-
'threshold',
45-
this._threshold,
46-
'value',
45+
'threshold',
46+
this._threshold,
47+
'value',
4748
Gio.SettingsBindFlags.DEFAULT
4849
);
4950
}
5051
});
5152

53+
export default class PowerProfileSwitcherPreferences extends ExtensionPreferences {
54+
fillPreferencesWindow(window) {
55+
const settings = this.getSettings();
5256

53-
function init() {}
54-
55-
56-
function fillPreferencesWindow(window) {
57-
58-
const settings = ExtensionUtils.getSettings("org.gnome.shell.extensions.power-profile-switcher");
59-
60-
window.add(new General(settings));
61-
57+
window.add(new General(settings));
58+
}
6259
}

ui/general.ui

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<interface domain="org.gnome.shell.extensions.power-profile-switcher">
3-
<template class="GeneralPreferences" parent="AdwPreferencesPage">
3+
<template class="PowerProfileSwitcherPreferences" parent="AdwPreferencesPage">
44
<property name="name">general</property>
55
<property name="title" translatable="yes">General</property>
66
<property name="icon-name">preferences-other-symbolic</property>
@@ -33,19 +33,13 @@
3333
<property name="description" translatable="yes">Configure the power saving options</property>
3434

3535
<child>
36-
<object class="AdwActionRow">
36+
<object class="AdwSpinRow" id="threshold">
3737
<property name="title" translatable="yes">Power saving threshold</property>
3838
<property name="subtitle" translatable="yes">Select battery level to turn on the power saving profile</property>
39-
<property name="activatable-widget">threshold</property>
40-
<child>
41-
<object class="GtkSpinButton" id="threshold">
42-
<property name="valign">center</property>
43-
<property name="adjustment">threshold_adjustment</property>
44-
<property name="numeric">True</property>
45-
<property name="snap-to-ticks">True</property>
46-
<property name="update-policy">if-valid</property>
47-
</object>
48-
</child>
39+
<property name="adjustment">threshold_adjustment</property>
40+
<property name="numeric">True</property>
41+
<property name="snap-to-ticks">True</property>
42+
<property name="update-policy">if-valid</property>
4943
</object>
5044
</child>
5145
</object>

0 commit comments

Comments
 (0)