Skip to content

Commit

Permalink
Fixed crash when some features unavailable. Bumped version to v4.
Browse files Browse the repository at this point in the history
  • Loading branch information
egormanga committed Mar 12, 2022
1 parent a17e743 commit c01e892
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
43 changes: 36 additions & 7 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ class HuaweiWmiIndicator extends PanelMenu.Button { // TODO: move to system batt
power_unlock.connect('toggled', (item, state) => this._set_power_unlock(state));
}; this.menu.addMenuItem(power_unlock);

this.menu.connect('open-state-changed', () => this._bpm.activate());
this.connect('enter-event', () => this._update());
this.connect('button-press-event', () => this._update());
this.connect('key-press-event', () => this._update());
this.menu.connect('open-state-changed', () => {
if (this._bpm.sensitive) this._bpm.activate();
});

this._bind_keys();

Expand Down Expand Up @@ -94,6 +99,8 @@ class HuaweiWmiIndicator extends PanelMenu.Button { // TODO: move to system batt
let old_state = this._power_unlock.state;
this._set_power_unlock();

if (!this._power_unlock.visible) return;

if (this._power_unlock.state === old_state) {
icon = 'battery-caution-symbolic';
text = _("Power Unlock\nunavailable on battery");
Expand All @@ -116,6 +123,12 @@ class HuaweiWmiIndicator extends PanelMenu.Button { // TODO: move to system batt
Main.osdWindowManager.show(-1, Gio.icon_new_for_string(icon), text);
}

_update() {
this._set_bpm();
this._set_fn_lock();
this._set_power_unlock();
}

_set_bpm(low, high) {
let file = Gio.File.new_for_path("/sys/devices/platform/huawei-wmi/charge_control_thresholds");

Expand All @@ -124,8 +137,14 @@ class HuaweiWmiIndicator extends PanelMenu.Button { // TODO: move to system batt
file.replace_contents(`${low} ${high}`, null, false, 0, null);
} catch (e) {}

[low, high] = ByteArray.toString(file.load_contents(null)[1]).split(' ').map(Number);

try {
[low, high] = ByteArray.toString(file.load_contents(null)[1]).split(' ').map(Number);
} catch (e) {
this._bpm.setSensitive(false);
this._bpm.label.set_text(this._BPM);
return;
}
this._bpm.setSensitive(true);
this._bpm.label.set_text(this._BPM + `: ${low}%-${high}%`);
}

Expand All @@ -137,8 +156,13 @@ class HuaweiWmiIndicator extends PanelMenu.Button { // TODO: move to system batt
file.replace_contents(Number(state).toString(), null, false, 0, null);
} catch (e) {}

state = Boolean(Number(ByteArray.toString(file.load_contents(null)[1])));

try {
state = Boolean(Number(ByteArray.toString(file.load_contents(null)[1])));
} catch (e) {
this._fn_lock.setSensitive(false);
return;
}
this._fn_lock.setSensitive(true);
this._fn_lock.setToggleState(state);
}

Expand All @@ -150,8 +174,13 @@ class HuaweiWmiIndicator extends PanelMenu.Button { // TODO: move to system batt
file.replace_contents(Number(state).toString(), null, false, 0, null);
} catch (e) {}

state = Boolean(Number(ByteArray.toString(file.load_contents(null)[1])));

try {
state = Boolean(Number(ByteArray.toString(file.load_contents(null)[1])));
} catch (e) {
this._power_unlock.setSensitive(false);
return;
}
this._power_unlock.setSensitive(true);
this._power_unlock.setToggleState(state);
}
});
Expand Down
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"uuid": "huawei-wmi@apps.sdore.me",
"name": "Huawei WMI controls",
"description": "Control various Huawei and Honor laptops WMI functions, such as battery protection, Fn-lock and keyboard backlight.",
"version": 3,
"version": 4,
"shell-version": ["40", "41"],
"url": "https://apps.sdore.me/gnome-extension-huawei-wmi"
}

0 comments on commit c01e892

Please sign in to comment.