🔧 chore(modules/backlight, modules/config): fix property name not found, add support for setting properties in config

This commit is contained in:
retrozinndev
2025-08-28 18:33:39 -03:00
parent 09568ac738
commit f213d994aa
4 changed files with 113 additions and 61 deletions
+10 -18
View File
@@ -1,15 +1,12 @@
import { monitorFile, readFile } from "ags/file";
import { exec } from "ags/process";
import GObject, { getter, ParamSpec, setter, signal } from "ags/gobject";
import GObject, { getter, ParamSpec, register, setter, signal } from "ags/gobject";
import Gio from "gi://Gio?version=2.0";
export namespace Backlights {
const BacklightsParamSpec = (name: string, flags: GObject.ParamFlags) =>
GObject.ParamSpec.object(name, null, null, flags) as ParamSpec<Backlights>;
const BacklightParamSpec = (name: string, flags: GObject.ParamFlags) =>
GObject.ParamSpec.object(name, null, null, flags) as ParamSpec<Backlight>;
@@ -22,13 +19,8 @@ export namespace Backlights {
return instance;
}
export class Backlights extends GObject.Object {
static {
GObject.registerClass({
GTypeName: "Backlights"
}, this);
}
@register({ GTypeName: "Backlights" })
class _Backlights extends GObject.Object {
#backlights: Array<Backlight> = [];
#default: Backlight|null = null;
@@ -101,13 +93,8 @@ export namespace Backlights {
}
}
export class Backlight extends GObject.Object {
static {
GObject.registerClass({
GTypeName: "Backlight"
}, this);
}
@register({ GTypeName: "Backlight" })
class _Backlight extends GObject.Object {
declare $signals: GObject.Object.SignalSignatures & {
"brightness-changed": (value: number) => void
@@ -207,4 +194,9 @@ export namespace Backlights {
super.emit(signal, ...args);
}
}
export const Backlights = _Backlights;
export const Backlight = _Backlight;
export type Backlight = InstanceType<typeof Backlight>;
export type Backlights = InstanceType<typeof Backlights>;
}
+40 -26
View File
@@ -1,11 +1,9 @@
import { timeout } from "ags/time";
import { monitorFile, readFileAsync } from "ags/file";
import { monitorFile, readFileAsync, writeFileAsync } from "ags/file";
import { Notifications } from "./notifications";
import { encoder } from "./utils";
import { Accessor } from "ags";
import GObject, { getter, register } from "ags/gobject";
import GObject, { getter, ParamSpec, register } from "ags/gobject";
import GLib from "gi://GLib?version=2.0";
import Gio from "gi://Gio?version=2.0";
import AstalIO from "gi://AstalIO";
import AstalNotifd from "gi://AstalNotifd";
@@ -24,8 +22,8 @@ class Config<K extends NonNullable<string|number|symbol>, V extends string|objec
* in the `entries` field */
public readonly defaults: Record<K, V>;
@getter(Object)
public get entries(): object { return this.#entries; }
@getter(Object as unknown as ParamSpec<Record<K, V>>)
public get entries() { return this.#entries; }
#file: Gio.File;
#entries: Record<K, V>;
@@ -47,26 +45,12 @@ class Config<K extends NonNullable<string|number|symbol>, V extends string|objec
this.#file.make_directory_with_parents(null);
this.#file.delete(null);
this.#file.create_readwrite_async(
Gio.FileCreateFlags.NONE, GLib.PRIORITY_DEFAULT,
null, (_, asyncRes) => {
const ioStream = this.#file.create_readwrite_finish(asyncRes);
ioStream.outputStream.write_bytes_async(
GLib.Bytes.new(encoder.encode(JSON.stringify(this.entries, undefined, 4))),
GLib.PRIORITY_DEFAULT, null,
(_, asyncRes) => {
const writtenBytes = ioStream.outputStream.write_bytes_finish(asyncRes);
if(!writtenBytes)
Notifications.getDefault().sendNotification({
appName: "colorshell",
summary: "Write error",
body: `Couldn't write default configuration file to "${this.#file.get_path()!}"`
});
}
);
});
this.writeFile().catch(e => Notifications.getDefault().sendNotification({
appName: "colorshell",
summary: "Write error",
body: `Couldn't write default configuration file to "${this.#file.get_path()!
}".\nStderr: ${e}`
}));
}
monitorFile(this.#file.get_path()!,
@@ -97,6 +81,13 @@ class Config<K extends NonNullable<string|number|symbol>, V extends string|objec
));
}
private async writeFile(): Promise<void> {
this.timeout = true;
await writeFileAsync(
this.#file.get_path()!, JSON.stringify(this.entries, undefined, 4)
).finally(() => this.timeout = false);
}
private async readFile(): Promise<void> {
await readFileAsync(this.#file.get_path()!).then((content) => {
let config: (Record<K, V>|undefined);
@@ -174,6 +165,29 @@ class Config<K extends NonNullable<string|number|symbol>, V extends string|objec
return this._getProperty(path, this.defaults, expectType);
}
public setProperty(path: string, value: any, write?: boolean): void {
let property: any = this.#entries,
obj: typeof this.entries = property;
const pathArray = path.split('.').filter(str => str);
for(let i = 0; i < pathArray.length; i++) {
const currentPath = pathArray[i];
property = property[currentPath as keyof typeof property];
if(typeof property === "object") {
obj = property;
} else {
obj[pathArray[pathArray.length - 1] as keyof typeof obj] = value;
break;
}
}
write && this.writeFile().catch(e => console.error(
`Config: Couldn't save file. Stderr: ${e}`
));
}
private _getProperty(path: string, entries: Record<K, V>, expectType?: ValueTypes): (any|undefined) {
let property: any = entries;
const pathArray = path.split('.').filter(str => str);