forked from maniacx/Battery-Health-Charging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
283 lines (256 loc) · 10.5 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
'use strict';
const GObject = imports.gi.GObject;
const Gio = imports.gi.Gio;
const PopupMenu = imports.ui.popupMenu;
const Main = imports.ui.main;
const MessageTray = imports.ui.messageTray;
const SystemActions = imports.misc.systemActions;
const Util = imports.misc.util;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const QuickSettings = imports.ui.quickSettings;
const QuickSettingsMenu = imports.ui.main.panel.statusArea.quickSettings;
const Driver = Me.imports.driver;
const gettextDomain = Me.metadata['gettext-domain'];
const Gettext = imports.gettext.domain(gettextDomain);
const _ = Gettext.gettext;
const ICONS_FOLDER = Me.dir.get_child('icons').get_path();
/**
* Get icon
*
* @param {string} iconName Icon name
* @returns {Gio.Icon}
*/
function getIcon(iconName) {
return Gio.icon_new_for_string(`${ICONS_FOLDER}/${iconName}.svg`);
}
const SystemMenuToggle = GObject.registerClass(
class SystemMenuToggle extends QuickSettings.QuickMenuToggle {
_init() {
super._init();
this._settings = ExtensionUtils.getSettings();
this._chargeLimitSection = new PopupMenu.PopupMenuSection();
this.menu.addMenuItem(this._chargeLimitSection);
this.label = _('Charger Limit');
this.gicon = getIcon('charging-limit-mix-100-symbolic');
this.toggleMode = false;
this.menu.setHeader(getIcon('charging-limit-mix-100-symbolic'), _('Battery Health Mode'));
this._updatePanelMenu();
this._settings.connectObject(
'changed::charger-limit', () => {
this._updatePanelMenu();
},
'changed::icon-style-type', () => {
this._updatePanelMenu();
},
this
);
}
_updatePanelMenu() {
const iconStyle = this._settings.get_int('icon-style-type');
let iconType = 'mix';
switch (iconStyle) {
case 0:
iconType = 'mix';
break;
case 1:
iconType = 'sym';
break;
case 2:
iconType = 'num';
break;
}
this._chargeLimitSection.removeAll();
let currentLimitValue = Driver.getCurrentLimitValue();
let currentLimitSettings = this._settings.get_int('charger-limit');
let menuItem100 = new PopupMenu.PopupImageMenuItem(_('Full Capacity Mode (100%)'),
getIcon(`charging-limit-${iconType}-100-symbolic`));
let menuItem80 = new PopupMenu.PopupImageMenuItem(_('Balanced Mode (80%)'),
getIcon(`charging-limit-${iconType}-80-symbolic`));
let menuItem60 = new PopupMenu.PopupImageMenuItem(_('Maximum Lifespan Mode (60%)'),
getIcon(`charging-limit-${iconType}-60-symbolic`));
let currentLimitItem = new PopupMenu.PopupMenuItem(_(`Charging Limit is set to ${currentLimitValue}`));
currentLimitItem.sensitive = false;
currentLimitItem.active = false;
menuItem100.connect('activate', () => {
Main.overview.hide();
Main.panel.closeQuickSettings();
Driver.setLimit('100');
this._settings.set_int('charger-limit', 100);
});
menuItem80.connect('activate', () => {
Main.overview.hide();
Main.panel.closeQuickSettings();
Driver.setLimit('80');
this._settings.set_int('charger-limit', 80);
});
menuItem60.connect('activate', () => {
Main.overview.hide();
Main.panel.closeQuickSettings();
Driver.setLimit('60');
this._settings.set_int('charger-limit', 60);
});
this._chargeLimitSection.addMenuItem(menuItem100);
this._chargeLimitSection.addMenuItem(menuItem80);
this._chargeLimitSection.addMenuItem(menuItem60);
this._chargeLimitSection.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
this._chargeLimitSection.addMenuItem(currentLimitItem);
this.gicon = getIcon(`charging-limit-${iconType}-${currentLimitSettings}-symbolic`);
this.menu.setHeader(getIcon(`charging-limit-${iconType}-${currentLimitSettings}-symbolic`), _('Battery Health Mode'));
menuItem100.setOrnament(currentLimitSettings === 100
? PopupMenu.Ornament.DOT
: PopupMenu.Ornament.NONE);
menuItem80.setOrnament(currentLimitSettings === 80
? PopupMenu.Ornament.DOT
: PopupMenu.Ornament.NONE);
menuItem60.setOrnament(currentLimitSettings === 60
? PopupMenu.Ornament.DOT
: PopupMenu.Ornament.NONE);
}
}
);
var SystemMenu = GObject.registerClass(
class SystemMenu extends QuickSettings.SystemIndicator {
_init() {
super._init();
// indicator initialization
this._settings = ExtensionUtils.getSettings();
this._indicator = this._addIndicator();
this._indicator.gicon = getIcon('charging-limit-mix-100-symbolic');
this.quickSettingsItems.push(new SystemMenuToggle());
this.connect('destroy', () => {
this.quickSettingsItems.forEach(item => item.destroy());
});
QuickSettingsMenu._indicators.insert_child_at_index(this, 0);
QuickSettingsMenu._addItems(this.quickSettingsItems);
this._updateIndicator();
this._settings.connectObject(
'changed::charger-limit', () => {
this._updateIndicator();
},
'changed::icon-style-type', () => {
this._updateIndicator();
},
'changed::show-system-indicator', () => {
this._updateIndicator();
},
this
);
}
_updateIndicator() {
// Iconstyle for indicator
const iconStyle = this._settings.get_int('icon-style-type');
let iconType = 'mix';
switch (iconStyle) {
case 0:
iconType = 'mix';
break;
case 1:
iconType = 'sym';
break;
case 2:
iconType = 'num';
break;
}
let currentLimitSettings = this._settings.get_int('charger-limit');
this._indicator.gicon = getIcon(`charging-limit-${iconType}-${currentLimitSettings}-symbolic`);
if (this._settings.get_boolean('show-system-indicator'))
this._indicator.visible = true;
else
this._indicator.visible = false;
}
}
);
class ChargeLimit {
constructor() {
this._indicator = null;
}
enable() {
this._systemActions = new SystemActions.getDefault();
this._settings = ExtensionUtils.getSettings();
let flag = false;
// on installation service completion notify to logout
this._settings.connectObject(
'changed::install-service', () => {
if (flag) {
if (this._settings.get_boolean('install-service'))
this.notify(_('Installation Successfull. Please save your work and logout.'), 'installed');
else
this.notify(_('Uninstallation Successfull.'), 'uninstalled');
}
},
this
);
// Check for incompatibilities and notify errors
switch (Driver.checkInCompatibility()) {
case 0:
flag = true;
break;
case 1:
this.notify(_('Unsupported Gnome version.\nThis extension is compatible only with Gnome version 43 and above.'));
return;
case 2:
this.notify(_('Unsupported device.\nCannot detect sysfs path : charge_control_end_threshold.'));
return;
case 3:
this.notify(_('Unsupported device.\nDetected sysfs path : charge_control_start_threshold.'));
return;
case 4:
this.notify(_('Battery Health service not installed.\n' +
'Please install required service from Battery Health Charging extension settings under Install / Remove Service'), 'show-settings');
flag = true;
return;
}
// On enable restore the previously set charging limit value.
let currentLimitValue = Driver.getCurrentLimitValue();
let currentLimitSettings = this._settings.get_int('charger-limit');
if (currentLimitValue !== currentLimitSettings)
Driver.setLimit(currentLimitSettings);
this._indicator = new SystemMenu();
}
notify(msg, action = '') {
let notifyTitle, notifyIcon;
if (action === 'installed') {
notifyTitle = _('Battery Health Charging');
notifyIcon = 'system-reboot-symbolic';
} else if (action === 'uninstalled') {
notifyTitle = _('Battery Health Charging');
notifyIcon = 'success-symbolic';
} else {
notifyTitle = _('Battery Health Charging Gnome Extension Error');
notifyIcon = 'mail-mark-junk-symbolic';
}
let source = new MessageTray.Source(Me.metadata.name, notifyIcon);
Main.messageTray.add(source);
let notification = new MessageTray.Notification(source, notifyTitle, msg);
notification.setUrgency(3);
notification.setTransient(true);
if (action === 'installed') {
notification.addAction(_('Log Out'), () => {
this._systemActions.activateLogout();
});
} else if (action === 'uninstalled') {
notification.setUrgency(2);
} else if (action === 'show-settings') {
notification.addAction(_('Settings'), () => {
Util.spawn(['gnome-extensions', 'prefs', Me.metadata.uuid]);
});
}
source.showNotification(notification);
}
disable() {
this._settings.disconnectObject(this);
if (this._indicator != null)
this._indicator.destroy();
this._settings = null;
this._systemActions = null;
this._indicator = null;
}
}
/**
* Init
*/
function init() {
ExtensionUtils.initTranslations(Me.metadata.uuid);
return new ChargeLimit();
}