|
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'; |
4 | 4 |
|
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'; |
7 | 8 |
|
8 | 9 | let settings, client, device;
|
9 | 10 |
|
@@ -36,7 +37,7 @@ const PowerManagerProxy = Gio.DBusProxy.makeProxyWrapper(DisplayDeviceInterface)
|
36 | 37 | const POWER_PROFILES_BUS_NAME = 'net.hadess.PowerProfiles';
|
37 | 38 | const POWER_PROFILES_OBJECT_PATH = '/net/hadess/PowerProfiles';
|
38 | 39 |
|
39 |
| -const PowerProfilesIface = loadInterfaceXML('net.hadess.PowerProfiles'); |
| 40 | +const PowerProfilesIface = FileUtils.loadInterfaceXML('net.hadess.PowerProfiles'); |
40 | 41 | const PowerProfilesProxy = Gio.DBusProxy.makeProxyWrapper(PowerProfilesIface);
|
41 | 42 |
|
42 | 43 |
|
@@ -102,109 +103,111 @@ const getDefaults = () => {
|
102 | 103 | batteryThreshold = settings.get_int("threshold");
|
103 | 104 | }
|
104 | 105 |
|
105 |
| -function init() { |
106 |
| - ExtensionUtils.initTranslations(Me.metadata.uuid); |
107 |
| -} |
| 106 | +export default class PowerProfileSwitcher extends Extension { |
| 107 | + constructor(metadata) { |
| 108 | + super(metadata); |
| 109 | + } |
108 | 110 |
|
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 | + ); |
164 | 123 |
|
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; |
178 | 160 | }
|
179 | 161 | }
|
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 | + } |
182 | 185 | }
|
183 |
| - } |
184 |
| - }); |
185 |
| - } |
186 |
| - }, powerProfilesCancellable); |
187 |
| -} |
| 186 | + }); |
| 187 | + } |
| 188 | + }, powerProfilesCancellable); |
| 189 | + } |
188 | 190 |
|
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); |
193 | 195 |
|
194 |
| - powerManagerProxy.disconnect(batteryThresholdWatcher); |
195 |
| - powerManagerCancellable.cancel(); |
| 196 | + powerManagerProxy.disconnect(batteryThresholdWatcher); |
| 197 | + powerManagerCancellable.cancel(); |
196 | 198 |
|
197 |
| - powerProfilesProxy.disconnect(powerProfileWatcher); |
198 |
| - powerProfilesCancellable.cancel(); |
| 199 | + powerProfilesProxy.disconnect(powerProfileWatcher); |
| 200 | + powerProfilesCancellable.cancel(); |
199 | 201 |
|
200 |
| - if (perfDebounceTimerId) { |
201 |
| - GLib.source_remove(perfDebounceTimerId); |
202 |
| - perfDebounceTimerId = null; |
203 |
| - } |
| 202 | + if (perfDebounceTimerId) { |
| 203 | + GLib.source_remove(perfDebounceTimerId); |
| 204 | + perfDebounceTimerId = null; |
| 205 | + } |
204 | 206 |
|
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 | + } |
210 | 213 | }
|
0 commit comments