parent
f0704cba04
commit
7d64a1fe25
8 changed files with 177 additions and 264 deletions
modules/ags/config/widgets/misc
49
modules/ags/config/widgets/misc/persist.ts
Normal file
49
modules/ags/config/widgets/misc/persist.ts
Normal file
|
@ -0,0 +1,49 @@
|
|||
import { execAsync, readFileAsync, timeout, GLib, type Variable } from 'astal';
|
||||
|
||||
const { get_home_dir } = GLib;
|
||||
|
||||
|
||||
export default <T>({
|
||||
name,
|
||||
variable,
|
||||
condition = true,
|
||||
whenTrue = condition,
|
||||
whenFalse = false,
|
||||
}: {
|
||||
name: string
|
||||
variable: Variable<T>
|
||||
condition?: boolean | string
|
||||
whenTrue?: boolean | string
|
||||
whenFalse?: boolean | string
|
||||
}) => {
|
||||
const cacheFile = `${get_home_dir()}/.cache/ags/.${name}`;
|
||||
|
||||
const stateCmd = () => ['bash', '-c',
|
||||
`echo ${variable.get() === condition} > ${cacheFile}`];
|
||||
|
||||
const monitorState = () => {
|
||||
variable.subscribe(() => {
|
||||
execAsync(stateCmd()).catch(print);
|
||||
});
|
||||
};
|
||||
|
||||
readFileAsync(cacheFile)
|
||||
.then((content) => {
|
||||
// JSON.parse was the only way I found to reliably
|
||||
// convert a string of 'true' or 'false' into a bool
|
||||
const value = (JSON.parse(content) ? whenTrue : whenFalse) as T;
|
||||
|
||||
variable.set(value);
|
||||
|
||||
timeout(1000, () => {
|
||||
monitorState();
|
||||
});
|
||||
})
|
||||
.catch(() => {
|
||||
execAsync(stateCmd())
|
||||
.then(() => {
|
||||
monitorState();
|
||||
})
|
||||
.catch(print);
|
||||
});
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue