nixos-configs/devices/wim/config/ags/services/pointers.ts

196 lines
5.6 KiB
TypeScript
Raw Normal View History

import App from 'resource:///com/github/Aylur/ags/app.js';
import Hyprland from 'resource:///com/github/Aylur/ags/service/hyprland.js';
import Service from 'resource:///com/github/Aylur/ags/service.js';
import { subprocess } from 'resource:///com/github/Aylur/ags/utils.js';
const ON_RELEASE_TRIGGERS = [
'released',
'TOUCH_UP',
'HOLD_END',
];
const ON_CLICK_TRIGGERS = [
'pressed',
'TOUCH_DOWN',
];
2024-01-13 11:15:08 -05:00
// Types
import AgsWindow from 'types/widgets/window';
type Subprocess = typeof imports.gi.Gio.Subprocess;
type Layer = {
address: string;
x: number;
y: number;
w: number;
h: number;
namespace: string;
};
type Levels = {
0?: Array<Layer> | null;
1?: Array<Layer> | null;
2?: Array<Layer> | null;
3?: Array<Layer> | null;
};
type Layers = {
levels: Levels;
};
type CursorPos = {
x: number;
y: number;
};
class Pointers extends Service {
static {
Service.register(this, {
'proc-started': ['boolean'],
'proc-destroyed': ['boolean'],
'device-fetched': ['boolean'],
'new-line': ['string'],
'released': ['string'],
'clicked': ['string'],
});
}
2024-01-13 11:15:08 -05:00
#process: Subprocess;
#lastLine = '';
2024-01-13 11:15:08 -05:00
#pointers = [] as Array<String>;
get process() {
return this.#process;
}
get lastLine() {
return this.#lastLine;
}
get pointers() {
return this.#pointers;
}
constructor() {
super();
this.#initAppConnection();
}
startProc() {
if (this.#process) {
return;
}
this.#process = subprocess(
2024-01-13 11:15:08 -05:00
['libinput', 'debug-events'],
(output) => {
if (output.includes('cancelled')) {
return;
}
if (ON_RELEASE_TRIGGERS.some((p) => output.includes(p))) {
this.#lastLine = output;
Pointers.detectClickedOutside('released');
this.emit('released', output);
this.emit('new-line', output);
}
if (ON_CLICK_TRIGGERS.some((p) => output.includes(p))) {
this.#lastLine = output;
Pointers.detectClickedOutside('clicked');
this.emit('clicked', output);
this.emit('new-line', output);
}
},
);
this.emit('proc-started', true);
}
killProc() {
if (this.#process) {
this.#process.force_exit();
this.#process = null;
this.emit('proc-destroyed', true);
}
}
#initAppConnection() {
App.connect('window-toggled', () => {
2024-01-13 11:15:08 -05:00
const anyVisibleAndClosable =
(Array.from(App.windows) as Array<[string, AgsWindow]>)
.some((w) => {
const closable = w[1].attribute?.close_on_unfocus &&
!(w[1].attribute?.close_on_unfocus === 'none' ||
w[1].attribute?.close_on_unfocus === 'stay');
2024-01-13 11:15:08 -05:00
return w[1].visible && closable;
});
if (anyVisibleAndClosable) {
this.startProc();
}
else {
this.killProc();
}
});
}
2024-01-13 11:15:08 -05:00
static detectClickedOutside(clickStage: string) {
const toClose = (Array.from(App.windows) as Array<[string, AgsWindow]>)
.some((w) => {
const closable = (w[1].attribute?.close_on_unfocus &&
w[1].attribute?.close_on_unfocus === clickStage);
2024-01-13 11:15:08 -05:00
return w[1].visible && closable;
});
if (!toClose) {
return;
}
Hyprland.sendMessage('j/layers').then((response) => {
2024-01-13 11:15:08 -05:00
const layers = JSON.parse(response) as { Layers: Layers };
Hyprland.sendMessage('j/cursorpos').then((res) => {
2024-01-13 11:15:08 -05:00
const pos = JSON.parse(res) as CursorPos;
Object.values(layers).forEach((key) => {
2024-01-13 11:15:08 -05:00
const bar = key.levels['3']?.find(
(n) => n.namespace === 'bar',
2024-01-13 11:15:08 -05:00
) ||
// Return an empty Layer if bar doesn't exist
{ address: '', x: 0, y: 0, w: 0, h: 0, namespace: '' };
2024-01-13 11:15:08 -05:00
const widgets = key.levels['3']?.filter(
(n) => {
2024-01-13 11:15:08 -05:00
const window =
(App.getWindow(n.namespace) as AgsWindow);
return window?.attribute?.close_on_unfocus &&
window?.attribute
?.close_on_unfocus === clickStage;
},
);
if (pos.x > bar?.x && pos.x < bar?.x + bar?.w &&
pos.y > bar?.y && pos.y < bar?.y + bar?.h) {
// Don't handle clicks when on bar
// TODO: make this configurable
}
else {
2024-01-13 11:15:08 -05:00
widgets?.forEach(
(w) => {
if (!(pos.x > w.x && pos.x < w.x + w.w &&
pos.y > w.y && pos.y < w.y + w.h)) {
App.closeWindow(w.namespace);
}
},
);
}
});
}).catch(print);
}).catch(print);
}
}
export default new Pointers();