💥 ags(scripts/wallpapers): fix writeChanges and a lot of stuff

This commit is contained in:
retrozinndev
2025-05-11 20:51:35 -03:00
parent 0bb1589df5
commit ae1d29bc89
+87 -47
View File
@@ -1,4 +1,4 @@
import { execAsync, Gio, GLib, GObject, monitorFile, property, register } from "astal"; import { AstalIO, execAsync, Gio, GLib, GObject, monitorFile, property, register, timeout } from "astal";
export { Wallpaper }; export { Wallpaper };
@@ -10,14 +10,13 @@ class Wallpaper extends GObject.Object {
#monitor: Gio.FileMonitor; #monitor: Gio.FileMonitor;
#hyprpaperFile: Gio.File; #hyprpaperFile: Gio.File;
#wallpapersPath: string; #wallpapersPath: string;
#ignoreWatch: boolean = false;
@property(Boolean) @property(Boolean)
public get splash() { return this.#splash; } public get splash() { return this.#splash; }
public set splash(showSplash: boolean) { public set splash(showSplash: boolean) {
this.#splash = showSplash; this.#splash = showSplash;
this.notify("splash"); this.notify("splash");
this.writeChanges();
} }
@property(String) @property(String)
@@ -28,31 +27,59 @@ class Wallpaper extends GObject.Object {
constructor() { constructor() {
super(); super();
this.#wallpapersPath = GLib.getenv("WALLPAPERS") ?? `${GLib.get_home_dir()}/wallpapers`; this.#wallpapersPath = GLib.getenv("WALLPAPERS") ?? `${GLib.get_home_dir()}/wallpapers`;
this.#hyprpaperFile = Gio.File.new_for_path(`${GLib.get_user_config_dir()}/hypr/hyprpaper.conf`); this.#hyprpaperFile = Gio.File.new_for_path(`${GLib.get_user_config_dir()}/hypr/hyprpaper.conf`);
this.getWallpaper().then((wall) => this.#wallpaper = wall).catch(r => { this.getWallpaper().then((wall) => {
throw new Error(`Wallpaper: Couldn't get wallpaper. Stderr: ${r}`) if(wall?.trim()) this.#wallpaper = wall.trim();
}); });
this.#monitor = monitorFile(this.#hyprpaperFile.get_path()!, async (_, event) => { let tmeout: (AstalIO.Time|undefined) = undefined;
if(event == Gio.FileMonitorEvent.CHANGED) {
const [loaded, , content ] = this.#hyprpaperFile.load_contents(null); this.#monitor = monitorFile(this.#hyprpaperFile.get_path()!, (_, event) => {
if(loaded) if(event !== Gio.FileMonitorEvent.CHANGED && event !== Gio.FileMonitorEvent.CREATED &&
event !== Gio.FileMonitorEvent.MOVED_IN)
return;
if(tmeout) return;
else tmeout = timeout(1500, () => tmeout = undefined);
if(this.#ignoreWatch) {
this.#ignoreWatch = false;
return;
}
const [ loaded, text ] = this.#hyprpaperFile.load_contents(null);
if(!loaded)
console.error("Wallpaper: Couldn't read changes inside the hyprpaper file!");
const content = new TextDecoder().decode(text);
if(content) {
let setWall: boolean = true;
for(const line of content.split('\n')) { for(const line of content.split('\n')) {
const [ key, value ] = line.split('='); if(line.trim().startsWith('#'))
if(key.trim().startsWith('#'))
continue; continue;
const lineSplit = line.split('=');
const key = lineSplit[0].trim(),
value = lineSplit.filter((_, i) => i !== 0).join('=').trim();
switch(key) { switch(key) {
case "splash": case "splash":
this.#splash = /(yes|true|on)/.test(value.trim()) ? true : false; this.splash = /(yes|true|on|enable|enabled)/.test(value) ? true : false;
break; break;
case "wallpaper": case "wallpaper":
this.#wallpaper = value; if(this.#wallpaper !== value && setWall) {
this.setWallpaper(value, false);
setWall = false; // wallpaper already set
}
break; break;
} }
}; }
} }
}); });
} }
@@ -68,70 +95,83 @@ class Wallpaper extends GObject.Object {
return this.instance; return this.instance;
} }
private async writeChanges(): Promise<void> { private writeChanges(): void {
if(!this.#wallpaper) return; this.#ignoreWatch = true; // tell monitor to ignore file replace
this.#hyprpaperFile.replace_async(null, false,
Gio.FileCreateFlags.REPLACE_DESTINATION,
GLib.PRIORITY_DEFAULT, null, (_, result) => {
const res = this.#hyprpaperFile.replace_finish(result);
if(res) {
// success
this.#ignoreWatch = true; // tell monitor to ignore this change
res.write_bytes_async(new TextEncoder().encode(`# This file was automatically generated by color-shell
const hyprpaperFile = Gio.File.new_for_path(`${GLib.get_user_config_dir()}/hypr/hyprpaper.conf`);
hyprpaperFile.create(Gio.FileCreateFlags.REPLACE_DESTINATION, null);
return hyprpaperFile.replace_contents_async(`# This file is automatically generated when choosing a new wallpaper in colorshell
preload = ${this.#wallpaper} preload = ${this.#wallpaper}
splash = ${this.#splash} splash = ${this.#splash}
wallpaper = , ${this.#wallpaper}`.split('\n').map(str => str.trimStart()).join('\n'), null, false, wallpaper = , ${this.#wallpaper}`.split('\n').map(str => str.trimStart()).join('\n')),
Gio.FileCreateFlags.REPLACE_DESTINATION, null GLib.PRIORITY_DEFAULT, null, (_, asyncRes) => {
).then(() => {}).catch(r => { if(_!.write_finish(asyncRes)) res.flush(null);
throw new Error(`Wallpaper: Couldn't write hyprpaper file. Stderr: ${r}`); res.close(null);
}); }
);
return;
}
console.error(`Wallpaper: an error occurred when trying to replace the hyprpaper file`);
}
);
} }
public async getWallpaper(): Promise<string|undefined> { public async getWallpaper(): Promise<string|undefined> {
return await execAsync("hyprctl hyprpaper listactive | tail -n 1").then(stdout => { return await execAsync("sh -c \"hyprctl hyprpaper listactive | tail -n 1\"").then(stdout => {
const loaded: (string|undefined) = stdout.split('=')[1]?.trim(); const loaded: (string|undefined) = stdout.split('=')[1]?.trim();
if(!loaded) if(!loaded)
throw new Error(`Wallpaper: Couldn't get wallpaper. There are no loaded wallpaper(s)`); console.warn(`Wallpaper: Couldn't get wallpaper. There is(are) no loaded wallpaper(s)`);
return loaded; return loaded;
}).catch(stderr => { }).catch((err: Gio.IOErrorEnum) => {
throw new Error(`Wallpaper: Couldn't get wallpaper. Stderr: ${stderr}`); console.error(`Wallpaper: Couldn't get wallpaper. Stderr: \n${err.message ? `${err.message} /` : ""} Stack: \n ${err.stack}`);
return undefined;
}); });
} }
public async reloadColors(): Promise<void> { public reloadColors(): void {
return await execAsync(`wal -t --cols16 darken -i "${this.#wallpaper}"`).then(() => { execAsync(`wal -t --cols16 darken -i "${this.#wallpaper}"`).then(() => {
console.log("Wallpaper: reloaded shell colors");
}).catch(r => { }).catch(r => {
throw new Error(`Wallpaper: Couldn't update shell colors. Stderr: ${r}`); console.error(`Wallpaper: Couldn't update shell colors. Stderr: ${r}`);
}); });
} }
public async setWallpaper(path: string|Gio.File): Promise<void> { public setWallpaper(path: string|Gio.File, write: boolean = true): void {
return await execAsync("hyprctl hyprpaper unload all").then(async () => execAsync("hyprctl hyprpaper unload all").then(() =>
await execAsync(`hyprctl hyprpaper preload ${path}`).then(async () => execAsync(`hyprctl hyprpaper preload ${path}`).then(() =>
await execAsync(`hyprctl hyprpaper wallpaper ${path}`).then(async () => { execAsync(`hyprctl hyprpaper wallpaper ${path}`).then(() => {
this.#wallpaper = (typeof path === "string") ? path : path.get_path()!; this.#wallpaper = (typeof path === "string") ? path : path.get_path()!;
this.reloadColors(); this.reloadColors();
this.writeChanges().catch(r => { write && this.writeChanges();
throw new Error(`Wallpaper: Couldn't write changes to hyprpaper config file. Stderr: ${r}`);
});
}).catch(r => { }).catch(r => {
throw new Error(`Wallpaper: Couldn't set wallpaper. Stderr: ${r}`); console.error(`Wallpaper: Couldn't set wallpaper. Stderr: ${r}`);
}) })
).catch(r => { ).catch(r => {
throw new Error(`Wallpaper: Couldn't preload image. Stderr: ${r}`); console.error(`Wallpaper: Couldn't preload image. Stderr: ${r}`);
}) })
).catch(r => { ).catch(r => {
throw new Error(`Wallpaper: Couldn't unload images from memory. Stderr: ${r}`); console.error(`Wallpaper: Couldn't unload images from memory. Stderr: ${r}`);
}); });
} }
public async pickWallpaper(): Promise<string|undefined> { public async pickWallpaper(): Promise<string|undefined> {
return execAsync(`zenity --file-selection`).then(wall => { return (await execAsync(`zenity --file-selection`).then(wall => {
if(!wall.trim()) return undefined; if(!wall.trim()) return undefined;
this.setWallpaper(wall).catch(r => { this.setWallpaper(wall);
throw new Error(`Wallpaper: An error occurred in \`setWallpaper()\`. Stderr: ${r}`);
});
return wall; return wall;
}).catch(r => { }).catch(r => {
throw new Error(`Wallpaper: Couldn't pick wallpaper, is zenity installed? Stderr: ${r}`); console.error(`Wallpaper: Couldn't pick wallpaper, is \`zenity\` installed? Stderr: ${r}`);
}); return undefined;
}));
} }
} }