2024-01-30 11:29:07 -05:00
|
|
|
const { execAsync, readFileAsync, timeout } = Utils;
|
2023-12-07 16:48:34 -05:00
|
|
|
const { get_home_dir } = imports.gi.GLib;
|
|
|
|
|
2024-01-22 10:23:32 -05:00
|
|
|
import GObject from 'types/@girs/gobject-2.0/gobject-2.0';
|
|
|
|
|
2024-01-13 11:15:08 -05:00
|
|
|
type Persist = {
|
|
|
|
name: string
|
2024-01-22 10:23:32 -05:00
|
|
|
gobject: GObject.Object
|
2024-01-13 11:15:08 -05:00
|
|
|
prop: string
|
|
|
|
condition?: boolean | string // If string, compare following props to this
|
|
|
|
whenTrue?: boolean | string
|
|
|
|
whenFalse?: boolean | string
|
|
|
|
signal?: string
|
|
|
|
};
|
|
|
|
|
2023-12-18 18:00:30 -05:00
|
|
|
|
2023-12-07 16:48:34 -05:00
|
|
|
export default ({
|
|
|
|
name,
|
|
|
|
gobject,
|
|
|
|
prop,
|
|
|
|
condition = true,
|
|
|
|
whenTrue = condition,
|
|
|
|
whenFalse = false,
|
|
|
|
signal = 'changed',
|
2024-01-13 11:15:08 -05:00
|
|
|
}: Persist) => {
|
2023-12-07 16:48:34 -05:00
|
|
|
const cacheFile = `${get_home_dir()}/.cache/ags/.${name}`;
|
|
|
|
|
|
|
|
const stateCmd = () => ['bash', '-c',
|
|
|
|
`echo ${gobject[prop] === condition} > ${cacheFile}`];
|
|
|
|
|
|
|
|
const monitorState = () => {
|
|
|
|
gobject.connect(signal, () => {
|
|
|
|
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
|
|
|
|
gobject[prop] = JSON.parse(content) ? whenTrue : whenFalse;
|
|
|
|
|
|
|
|
timeout(1000, () => {
|
|
|
|
monitorState();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
execAsync(stateCmd())
|
|
|
|
.then(() => {
|
|
|
|
monitorState();
|
|
|
|
})
|
|
|
|
.catch(print);
|
|
|
|
});
|
|
|
|
};
|