diff --git a/devices/wim/config/ags/js/bar/buttons/heart.js b/devices/wim/config/ags/js/bar/buttons/heart.js index 898b57a..0571a7f 100644 --- a/devices/wim/config/ags/js/bar/buttons/heart.js +++ b/devices/wim/config/ags/js/bar/buttons/heart.js @@ -1,31 +1,18 @@ import { Box, Label } from 'resource:///com/github/Aylur/ags/widget.js'; -import { execAsync, readFileAsync } from 'resource:///com/github/Aylur/ags/utils.js'; import Variable from 'resource:///com/github/Aylur/ags/variable.js'; -const { get_home_dir } = imports.gi.GLib; - import EventBox from '../../misc/cursorbox.js'; +import Persist from '../../misc/persist.js'; -const HeartState = Variable(''); -const heartFile = `${get_home_dir()}/.cache/ags/.heart`; +const HeartState = Variable(); -const stateCmd = () => ['bash', '-c', - `echo ${HeartState.value === ''} > ${heartFile}`]; -const monitorState = () => { - HeartState.connect('changed', () => { - execAsync(stateCmd()).catch(print); - }); -}; - -// On launch -readFileAsync(heartFile).then((content) => { - HeartState.value = JSON.parse(content) ? '' : '󰣐'; - monitorState(); -}).catch(() => { - execAsync(stateCmd()).then(() => { - monitorState(); - }).catch(print); +Persist({ + name: 'heart', + gobject: HeartState, + prop: 'value', + condition: '', + whenFalse: '󰣐', }); diff --git a/devices/wim/config/ags/js/misc/persist.js b/devices/wim/config/ags/js/misc/persist.js new file mode 100644 index 0000000..d1b7961 --- /dev/null +++ b/devices/wim/config/ags/js/misc/persist.js @@ -0,0 +1,41 @@ +import { execAsync, readFileAsync, timeout } from 'resource:///com/github/Aylur/ags/utils.js'; +const { get_home_dir } = imports.gi.GLib; + +export default ({ + name, + gobject, + prop, + condition = true, + whenTrue = condition, + whenFalse = false, + signal = 'changed', +} = {}) => { + 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); + }); +}; diff --git a/devices/wim/config/ags/js/setup.js b/devices/wim/config/ags/js/setup.js index c63553a..0ab6384 100644 --- a/devices/wim/config/ags/js/setup.js +++ b/devices/wim/config/ags/js/setup.js @@ -1,15 +1,14 @@ import App from 'resource:///com/github/Aylur/ags/app.js'; import Hyprland from 'resource:///com/github/Aylur/ags/service/hyprland.js'; import Bluetooth from 'resource:///com/github/Aylur/ags/service/bluetooth.js'; -import { execAsync, readFileAsync, timeout } from 'resource:///com/github/Aylur/ags/utils.js'; - -const { get_home_dir } = imports.gi.GLib; import Brightness from '../services/brightness.js'; import Pointers from '../services/pointers.js'; import Tablet from '../services/tablet.js'; import TouchGestures from '../services/touch-gestures.js'; + import closeAll from './misc/closer.js'; +import Persist from './misc/persist.js'; export default () => { @@ -18,29 +17,13 @@ export default () => { globalThis.Tablet = Tablet; globalThis.closeAll = closeAll; - // Persist Bluetooth state - const bluetoothFile = `${get_home_dir()}/.cache/ags/.bluetooth`; - const stateCmd = () => ['bash', '-c', - `echo ${Bluetooth.enabled} > ${bluetoothFile}`]; - const monitorState = () => { - Bluetooth.connect('notify::enabled', () => { - execAsync(stateCmd()).catch(print); - }); - }; - - // On launch - readFileAsync(bluetoothFile).then((content) => { - Bluetooth.enabled = JSON.parse(content); - timeout(1000, () => { - monitorState(); - }); - }).catch(() => { - execAsync(stateCmd()).then(() => { - monitorState(); - }).catch(print); + Persist({ + name: 'bluetooth', + gobject: Bluetooth, + prop: 'enabled', + signal: 'notify::enabled', }); - TouchGestures.addGesture({ name: 'openAppLauncher', gesture: 'UD',