scheme: fix dynamic preview

Also catch read errors
This commit is contained in:
2 * r + 2 * t 2025-04-22 18:13:14 +10:00
parent d4cb4b3c3e
commit ac525a07ea
2 changed files with 13 additions and 21 deletions

View file

@ -40,21 +40,20 @@ export default class Schemes extends GObject.Object {
return this.#map; return this.#map;
} }
async parseMode(path: string): Promise<IPalette> { async parseMode(path: string): Promise<IPalette | undefined> {
const schemeColours = (await readFileAsync(path)).split("\n").map(l => l.split(" ")); const schemeColours = (await readFileAsync(path).catch(() => undefined))?.split("\n").map(l => l.split(" "));
return schemeColours.reduce((acc, [name, hex]) => ({ ...acc, [name]: `#${hex}` }), {} as IPalette); 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> { async parseFlavour(scheme: string, name: string): Promise<Flavour> {
const path = `${this.#schemeDir}/${scheme}/${name}`; const path = `${this.#schemeDir}/${scheme}/${name}`;
return { name, scheme, colours: await this.parseColours(path) };
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 } };
} }
async parseScheme(name: string): Promise<Scheme> { 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 }), ).reduce((acc, f) => ({ ...acc, [f.name]: f }), {} as { [k: string]: Flavour }),
}; };
let light = undefined; return { name, colours: await this.parseColours(path) };
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 } };
} }
async update() { async update() {
@ -105,8 +99,6 @@ export default class Schemes extends GObject.Object {
super(); super();
this.update().catch(console.error); this.update().catch(console.error);
monitorDirectory(this.#schemeDir, (_m, file, _f, type) => { monitorDirectory(this.#schemeDir, (_, file) => this.updateFile(file).catch(console.error));
if (type !== Gio.FileMonitorEvent.DELETED) this.updateFile(file).catch(console.error);
});
} }
} }

View file

@ -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); monitors.add(monitor);
monitor.connect("notify::cancelled", () => monitor.cancelled && monitors.delete(monitor)); monitor.connect("notify::cancelled", () => monitor.cancelled && monitors.delete(monitor));