gex: Add slider for charge control

This commit is contained in:
Luke D. Jones
2023-07-01 19:17:02 +12:00
parent ed51a7fa14
commit 19b28f202c
4 changed files with 79 additions and 7 deletions

View File

@@ -7,9 +7,9 @@
<key type="b" name="panel-od-enabled">
<default>false</default>
</key>
<key type="b" name="supernotice">
<default>false</default>
<summary>hide the supergfxctl-gex notice</summary>
<key name="charge-level" type="u">
<range min="20" max="100"/>
<default>100</default>
</key>
</schema>
</schemalist>

View File

@@ -13,14 +13,16 @@ import { Platform } from './modules/dbus/platform';
import { QuickPanelOd } from './modules/quick_toggles/panel_od';
import { IndicateMiniLed } from './modules/indicators/mini_led';
import { QuickMiniLed } from './modules/quick_toggles/mini_led';
import { SliderChargeLevel } from './modules/sliders/charge';
class Extension {
private _indicateMiniLed: typeof IndicateMiniLed;
private _quickMiniLed: typeof QuickMiniLed;
private _quickPanelOd: typeof QuickPanelOd;
private _dbus_power!: Power;
private _dbus_anime!: AnimeDbus;
private _sliderCharge: typeof SliderChargeLevel;
public _dbus_power!: Power;
public _dbus_anime!: AnimeDbus;
public dbus_platform!: Platform;
public dbus_supported!: Supported;
@@ -28,6 +30,7 @@ class Extension {
this._indicateMiniLed = null;
this._quickMiniLed = null;
this._quickPanelOd = null;
this._sliderCharge = null;
this.dbus_supported = new Supported();
this.dbus_supported.start();
@@ -56,6 +59,11 @@ class Extension {
this._quickPanelOd = new QuickPanelOd(this.dbus_platform);
}
}
if (this.dbus_supported.supported.charge_ctrl.charge_level_set) {
if (this._sliderCharge == null) {
this._sliderCharge = new SliderChargeLevel(this._dbus_power);
}
}
}
disable() {
@@ -71,6 +79,10 @@ class Extension {
this._quickPanelOd.destroy();
this._quickPanelOd = null;
}
if (this._sliderCharge != null) {
this._sliderCharge.destroy();
this._sliderCharge = null;
}
this._dbus_power.stop();
this.dbus_platform.stop();

View File

@@ -3,9 +3,9 @@ declare const imports: any;
const { QuickToggle } = imports.ui.quickSettings;
const QuickSettingsMenu = imports.ui.main.panel.statusArea.quickSettings;
export function addQuickSettingsItems(items: [typeof QuickToggle]) {
export function addQuickSettingsItems(items: [typeof QuickToggle], width = 1) {
// Add the items with the built-in function
QuickSettingsMenu._addItems(items);
QuickSettingsMenu._addItems(items, width);
// Ensure the tile(s) are above the background apps menu
for (const item of items) {

View File

@@ -0,0 +1,60 @@
import { Power } from "../dbus/power";
import { addQuickSettingsItems } from "../helpers";
declare const imports: any;
const { GObject } = imports.gi;
const ExtensionUtils = imports.misc.extensionUtils;
const QuickSettings = imports.ui.quickSettings;
export const SliderChargeLevel = GObject.registerClass(
class SliderChargeLevel extends QuickSettings.QuickSlider {
private _dbus_power: Power;
constructor(dbus_power: Power) {
super({
iconName: 'selection-mode-symbolic',
});
this._dbus_power = dbus_power;
this._sliderChangedId = this.slider.connect('drag-end',
this._onSliderChanged.bind(this));
// Binding the slider to a GSettings key
this._settings = ExtensionUtils.getSettings();
this._settings.connect('changed::charge-level',
this._onSettingsChanged.bind(this));
// Set an accessible name for the slider
this.slider.accessible_name = 'Charge level';
this._sync();
this._onSettingsChanged();
addQuickSettingsItems([this], 2);
}
_onSettingsChanged() {
// Prevent the slider from emitting a change signal while being updated
this.slider.block_signal_handler(this._sliderChangedId);
this.slider.value = this._settings.get_uint('charge-level') / 100.0;
this.slider.unblock_signal_handler(this._sliderChangedId);
}
_onSliderChanged() {
// Assuming our GSettings holds values between 0..100, adjust for the
// slider taking values between 0..1
const percent = Math.floor(this.slider.value * 100);
const stored = Math.floor(this._settings.get_uint('charge-level') / 100.0);
if (this.slider.value !== stored)
this._dbus_power.setChargingLimit(percent);
this._settings.set_uint('charge-level', percent);
}
_sync() {
const value = this._dbus_power.getChargingLimit();
if (this.slider.value !== value / 100)
this._settings.set_uint('charge-level', value);
}
});