diff options
| author | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-04-22 18:13:14 +1000 |
|---|---|---|
| committer | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-04-22 18:13:14 +1000 |
| commit | ac525a07ea8df6ce5719689ce2b3d39762cc7305 (patch) | |
| tree | a821a6412c7875aafc7d233ea00423e15d9f6d8a | |
| parent | mediadisplay: fix visualiser symmetry (diff) | |
| download | caelestia-shell-ac525a07ea8df6ce5719689ce2b3d39762cc7305.tar.gz caelestia-shell-ac525a07ea8df6ce5719689ce2b3d39762cc7305.tar.bz2 caelestia-shell-ac525a07ea8df6ce5719689ce2b3d39762cc7305.zip | |
scheme: fix dynamic preview
Also catch read errors
| -rw-r--r-- | src/services/schemes.ts | 32 | ||||
| -rw-r--r-- | src/utils/system.ts | 2 |
2 files changed, 13 insertions, 21 deletions
diff --git a/src/services/schemes.ts b/src/services/schemes.ts index 2808b55..f8a71f9 100644 --- a/src/services/schemes.ts +++ b/src/services/schemes.ts @@ -40,21 +40,20 @@ export default class Schemes extends GObject.Object { return this.#map; } - async parseMode(path: string): Promise<IPalette> { - const schemeColours = (await readFileAsync(path)).split("\n").map(l => l.split(" ")); - return schemeColours.reduce((acc, [name, hex]) => ({ ...acc, [name]: `#${hex}` }), {} as IPalette); + async parseMode(path: string): Promise<IPalette | undefined> { + const schemeColours = (await readFileAsync(path).catch(() => undefined))?.split("\n").map(l => l.split(" ")); + return schemeColours?.reduce((acc, [name, hex]) => ({ ...acc, [name]: `#${hex}` }), {} as IPalette); + } + + async parseColours(path: string): Promise<Colours> { + const light = await this.parseMode(`${path}/light.txt`); + const dark = await this.parseMode(`${path}/dark.txt`); + return { light, dark }; } async parseFlavour(scheme: string, name: string): Promise<Flavour> { const path = `${this.#schemeDir}/${scheme}/${name}`; - - let light = undefined; - let dark = undefined; - if (GLib.file_test(`${path}/light.txt`, GLib.FileTest.EXISTS)) - light = await this.parseMode(`${path}/light.txt`); - if (GLib.file_test(`${path}/dark.txt`, GLib.FileTest.EXISTS)) dark = await this.parseMode(`${path}/dark.txt`); - - return { name, scheme, colours: { light, dark } }; + return { name, scheme, colours: await this.parseColours(path) }; } async parseScheme(name: string): Promise<Scheme> { @@ -69,12 +68,7 @@ export default class Schemes extends GObject.Object { ).reduce((acc, f) => ({ ...acc, [f.name]: f }), {} as { [k: string]: Flavour }), }; - let light = undefined; - let dark = undefined; - if (GLib.file_test(`${path}/light.txt`, GLib.FileTest.EXISTS)) - light = await this.parseMode(`${path}/light.txt`); - if (GLib.file_test(`${path}/dark.txt`, GLib.FileTest.EXISTS)) dark = await this.parseMode(`${path}/dark.txt`); - return { name, colours: { light, dark } }; + return { name, colours: await this.parseColours(path) }; } async update() { @@ -105,8 +99,6 @@ export default class Schemes extends GObject.Object { super(); this.update().catch(console.error); - monitorDirectory(this.#schemeDir, (_m, file, _f, type) => { - if (type !== Gio.FileMonitorEvent.DELETED) this.updateFile(file).catch(console.error); - }); + monitorDirectory(this.#schemeDir, (_, file) => this.updateFile(file).catch(console.error)); } } diff --git a/src/utils/system.ts b/src/utils/system.ts index 360a261..3a9caa6 100644 --- a/src/utils/system.ts +++ b/src/utils/system.ts @@ -103,7 +103,7 @@ export const monitorDirectory = ( } } - // Keep ref to monitor so it doesn't get GCed + // Keep ref to monitor so it doesn't get GC'd monitors.add(monitor); monitor.connect("notify::cancelled", () => monitor.cancelled && monitors.delete(monitor)); |