refactor(ags): add persist function to simplify code

This commit is contained in:
matt1432 2023-12-07 16:48:34 -05:00
parent 2562e90ac3
commit 32b1130779
3 changed files with 56 additions and 45 deletions

View file

@ -1,31 +1,18 @@
import { Box, Label } from 'resource:///com/github/Aylur/ags/widget.js'; 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'; import Variable from 'resource:///com/github/Aylur/ags/variable.js';
const { get_home_dir } = imports.gi.GLib;
import EventBox from '../../misc/cursorbox.js'; import EventBox from '../../misc/cursorbox.js';
import Persist from '../../misc/persist.js';
const HeartState = Variable(''); const HeartState = Variable();
const heartFile = `${get_home_dir()}/.cache/ags/.heart`;
const stateCmd = () => ['bash', '-c', Persist({
`echo ${HeartState.value === ''} > ${heartFile}`]; name: 'heart',
const monitorState = () => { gobject: HeartState,
HeartState.connect('changed', () => { prop: 'value',
execAsync(stateCmd()).catch(print); condition: '',
}); whenFalse: '󰣐',
};
// On launch
readFileAsync(heartFile).then((content) => {
HeartState.value = JSON.parse(content) ? '' : '󰣐';
monitorState();
}).catch(() => {
execAsync(stateCmd()).then(() => {
monitorState();
}).catch(print);
}); });

View file

@ -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);
});
};

View file

@ -1,15 +1,14 @@
import App from 'resource:///com/github/Aylur/ags/app.js'; import App from 'resource:///com/github/Aylur/ags/app.js';
import Hyprland from 'resource:///com/github/Aylur/ags/service/hyprland.js'; import Hyprland from 'resource:///com/github/Aylur/ags/service/hyprland.js';
import Bluetooth from 'resource:///com/github/Aylur/ags/service/bluetooth.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 Brightness from '../services/brightness.js';
import Pointers from '../services/pointers.js'; import Pointers from '../services/pointers.js';
import Tablet from '../services/tablet.js'; import Tablet from '../services/tablet.js';
import TouchGestures from '../services/touch-gestures.js'; import TouchGestures from '../services/touch-gestures.js';
import closeAll from './misc/closer.js'; import closeAll from './misc/closer.js';
import Persist from './misc/persist.js';
export default () => { export default () => {
@ -18,28 +17,12 @@ export default () => {
globalThis.Tablet = Tablet; globalThis.Tablet = Tablet;
globalThis.closeAll = closeAll; globalThis.closeAll = closeAll;
// Persist Bluetooth state Persist({
const bluetoothFile = `${get_home_dir()}/.cache/ags/.bluetooth`; name: 'bluetooth',
const stateCmd = () => ['bash', '-c', gobject: Bluetooth,
`echo ${Bluetooth.enabled} > ${bluetoothFile}`]; prop: 'enabled',
const monitorState = () => { signal: 'notify::enabled',
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);
});
TouchGestures.addGesture({ TouchGestures.addGesture({
name: 'openAppLauncher', name: 'openAppLauncher',