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 { 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: '󰣐',
});

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 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,28 +17,12 @@ 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);
Persist({
name: 'bluetooth',
gobject: Bluetooth,
prop: 'enabled',
signal: 'notify::enabled',
});
};
// On launch
readFileAsync(bluetoothFile).then((content) => {
Bluetooth.enabled = JSON.parse(content);
timeout(1000, () => {
monitorState();
});
}).catch(() => {
execAsync(stateCmd()).then(() => {
monitorState();
}).catch(print);
});
TouchGestures.addGesture({
name: 'openAppLauncher',