perf(wallpaper): use hyprpaper reload instead of unloading, preloading and then setting wallpaper

this is much fastergit add src/modules src/config.ts
This commit is contained in:
retrozinndev
2025-11-11 18:33:58 -03:00
parent 2f106a527e
commit cd8a39fc9f
3 changed files with 32 additions and 33 deletions
+1 -1
View File
@@ -23,7 +23,7 @@ const generalConfigDefaults = {
position_v: "top",
/** dismisses notification popup when unhovered after hovering
* @default false */
dismiss_after_unhover: false
dismiss_on_unhover: false
},
night_light: {
+3 -6
View File
@@ -133,8 +133,7 @@ class Config<K extends string, V = any> extends GObject.Object {
public bindProperty(path: string, expectType: "number"): Accessor<number>;
public bindProperty(path: string, expectType: "string"): Accessor<string>;
public bindProperty(path: string, expectType: "object"): Accessor<object>;
public bindProperty(path: string, expectType: "any"): Accessor<any>;
public bindProperty(path: string, expectType: undefined): Accessor<any>;
public bindProperty(path: string, expectType?: "any"): Accessor<any>;
public bindProperty(propertyPath: string, expectType?: ValueTypes): Accessor<boolean|number|string|object|any> {
return new Accessor(() => this.getProperty(propertyPath, expectType as never), (callback: () => void) => {
@@ -147,8 +146,7 @@ class Config<K extends string, V = any> extends GObject.Object {
public getProperty(path: string, expectType: "number"): number;
public getProperty(path: string, expectType: "string"): string;
public getProperty(path: string, expectType: "object"): object;
public getProperty(path: string, expectType: "any"): any;
public getProperty(path: string, expectType: undefined): any;
public getProperty(path: string, expectType?: "any"): any;
public getProperty(path: string, expectType?: ValueTypes): boolean|number|string|object|any {
return this._getProperty(path, this.#entries, expectType);
@@ -158,8 +156,7 @@ class Config<K extends string, V = any> extends GObject.Object {
public getPropertyDefault(path: string, expectType: "number"): number;
public getPropertyDefault(path: string, expectType: "string"): string;
public getPropertyDefault(path: string, expectType: "object"): object;
public getPropertyDefault(path: string, expectType: "any"): any;
public getPropertyDefault(path: string, expectType: undefined): any;
public getPropertyDefault(path: string, expectType?: "any"): any;
public getPropertyDefault(path: string, expectType?: ValueTypes): boolean|number|string|object|any {
return this._getProperty(path, this.defaults, expectType);
+28 -26
View File
@@ -70,7 +70,7 @@ class Wallpaper extends GObject.Object {
@setter(String)
set wallpaper(newValue: string) { this.setWallpaper(newValue); }
public get wallpapersPath() { return this.#wallpapersPath; }
get wallpapersPath() { return this.#wallpapersPath; }
@property(gtype<WallpaperPositioning>(String))
positioning: WallpaperPositioning = "cover";
@@ -98,6 +98,10 @@ class Wallpaper extends GObject.Object {
generalConfig.bindProperty("wallpaper.color_mode", "string"),
() => {
const mode = generalConfig.getProperty("wallpaper.color_mode", "string");
if(this.colorMode === mode)
return;
if(!mode || (mode !== "darken" && mode !== "lighten")) {
Notifications.getDefault().sendNotification({
appName: "colorshell",
@@ -118,6 +122,9 @@ class Wallpaper extends GObject.Object {
const positioning = generalConfig
.getProperty("wallpaper.positioning", "string") as WallpaperPositioning;
if(this.positioning === positioning)
return;
if(!positioning || (positioning !== "contain" &&
positioning !== "cover" &&
positioning !== "tile")) {
@@ -125,7 +132,7 @@ class Wallpaper extends GObject.Object {
Notifications.getDefault().sendNotification({
appName: "colorshell",
summary: "Couldn't update wallpaper position",
body: "Invalid position value. Possible values are: \"cover\", \"contain\" or \"tile\""
body: "Invalid position value. Possible values are: \"cover\"(default), \"contain\" or \"tile\""
});
return;
}
@@ -165,11 +172,13 @@ class Wallpaper extends GObject.Object {
}
// success
res.write_bytes_async(encoder.encode(`# This file was automatically generated by color-shell
res.write_bytes_async(encoder.encode(`\
# This file was automatically generated by colorshell
preload = ${this.#wallpaper}
splash = ${this.#splash}
wallpaper = , ${this.#wallpaper}`.split('\n').map(str => str.trimStart()).join('\n')),
wallpaper = , ${this.positioning === "cover" ? "" : `${this.positioning}:`}${
this.#wallpaper}`.split('\n').map(str => str.trimStart()).join('\n')),
GLib.PRIORITY_DEFAULT, null, (_, asyncRes) => {
if(_!.write_finish(asyncRes)) res.flush(null);
res.close(null);
@@ -187,18 +196,15 @@ class Wallpaper extends GObject.Object {
}
public async getWallpaper(): Promise<string|undefined> {
return await execAsync("hyprctl hyprpaper listactive").then(stdout => {
const lineSplit = stdout.split('\n');
stdout = lineSplit[lineSplit.length - 1];
return await execAsync("sh -c \"hyprctl hyprpaper listactive | tail -n 1\"").then(stdout => {
const loaded = stdout.split('=')[1]?.trim();
if(!loaded)
console.warn(`Wallpaper: Couldn't get wallpaper. There is(are) no loaded wallpaper(s)`);
return loaded;
}).catch((err: Error) => {
console.error(`Wallpaper: Couldn't get wallpaper. Stderr: \n${err.message}`);
}).catch((e: Error) => {
console.error(`Wallpaper: Couldn't get wallpaper. Stderr: \n${e.message}`);
return undefined;
});
}
@@ -215,29 +221,25 @@ class Wallpaper extends GObject.Object {
if(this.wallpaper.trim() === "")
return;
await execAsync(`hyprctl hyprpaper wallpaper \", ${this.positioning}:${this.wallpaper}\"`);
this.reloadColors();
await execAsync(`hyprctl hyprpaper reload \", ${
this.positioning === "cover" ? "" : `${this.positioning}:`
}${this.wallpaper}\"`);
write && this.writeChanges();
}
public setWallpaper(path: string|Gio.File, write: boolean = true): void {
path = typeof path === "string" ? path : path.peek_path()!;
execAsync("hyprctl hyprpaper unload all").then(() =>
execAsync(`hyprctl hyprpaper preload ${path}`).then(() =>
execAsync(`hyprctl hyprpaper wallpaper \", ${this.positioning}:${path}\"`).then(() => {
this.#wallpaper = path;
this.reloadColors();
write && this.writeChanges();
}).catch((e: Error) => {
console.error(`Wallpaper: Couldn't set wallpaper. Stderr: ${e.message}`);
})
).catch((e: Error) => {
console.error(`Wallpaper: Couldn't preload image. Stderr: ${e.message}`);
})
).catch((e: Error) => {
console.error(`Wallpaper: Couldn't unload images from memory. Stderr: ${e.message}`);
if(!GLib.file_test(path, GLib.FileTest.EXISTS)) {
console.error("Wallpaper: file does not exist, skipped");
return;
}
this.#wallpaper = path;
this.reloadWallpaper(write).catch((e: Error) => {
console.error(`Wallpaper: Couldn't set wallpaper. Stderr: ${e.message}`);
});
this.reloadColors();
}
public async pickWallpaper(): Promise<string|undefined> {