2024-01-30 11:29:07 -05:00
|
|
|
const Hyprland = await Service.import('hyprland');
|
|
|
|
const { subprocess } = Utils;
|
2023-12-24 00:26:44 -05:00
|
|
|
|
2023-10-24 17:26:38 -04:00
|
|
|
const ON_RELEASE_TRIGGERS = [
|
2023-10-24 14:21:33 -04:00
|
|
|
'released',
|
|
|
|
'TOUCH_UP',
|
|
|
|
'HOLD_END',
|
|
|
|
];
|
2023-10-24 17:26:38 -04:00
|
|
|
const ON_CLICK_TRIGGERS = [
|
|
|
|
'pressed',
|
|
|
|
'TOUCH_DOWN',
|
|
|
|
];
|
2023-10-16 17:06:19 -04:00
|
|
|
|
2024-01-13 11:15:08 -05:00
|
|
|
// Types
|
2024-01-29 18:54:07 -05:00
|
|
|
import { PopupWindow } from 'global-types';
|
2024-01-22 10:23:32 -05:00
|
|
|
import { Subprocess } from 'types/@girs/gio-2.0/gio-2.0.cjs';
|
2024-01-13 11:15:08 -05:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2023-10-16 17:06:19 -04:00
|
|
|
|
|
|
|
class Pointers extends Service {
|
2023-10-20 23:11:21 -04:00
|
|
|
static {
|
|
|
|
Service.register(this, {
|
|
|
|
'proc-started': ['boolean'],
|
|
|
|
'proc-destroyed': ['boolean'],
|
|
|
|
'device-fetched': ['boolean'],
|
|
|
|
'new-line': ['string'],
|
2023-10-24 17:26:38 -04:00
|
|
|
'released': ['string'],
|
|
|
|
'clicked': ['string'],
|
2023-10-20 23:11:21 -04:00
|
|
|
});
|
|
|
|
}
|
2023-10-16 17:06:19 -04:00
|
|
|
|
2024-01-22 10:23:32 -05:00
|
|
|
#process = null as Subprocess | null;
|
2023-11-21 01:29:46 -05:00
|
|
|
#lastLine = '';
|
2024-01-29 18:54:07 -05:00
|
|
|
#pointers = [] as Array<string>;
|
2023-10-16 17:06:19 -04:00
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
get process() {
|
|
|
|
return this.#process;
|
|
|
|
}
|
|
|
|
|
|
|
|
get lastLine() {
|
|
|
|
return this.#lastLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
get pointers() {
|
|
|
|
return this.#pointers;
|
|
|
|
}
|
2023-10-16 17:06:19 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
constructor() {
|
|
|
|
super();
|
2023-11-21 01:29:46 -05:00
|
|
|
this.#initAppConnection();
|
2023-10-20 23:11:21 -04:00
|
|
|
}
|
2023-10-16 17:06:19 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
startProc() {
|
2023-11-21 01:29:46 -05:00
|
|
|
if (this.#process) {
|
2023-10-20 23:11:21 -04:00
|
|
|
return;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-10-16 18:43:12 -04:00
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
this.#process = subprocess(
|
2024-01-13 11:15:08 -05:00
|
|
|
['libinput', 'debug-events'],
|
2023-11-21 01:29:46 -05:00
|
|
|
(output) => {
|
|
|
|
if (output.includes('cancelled')) {
|
2023-10-24 17:26:38 -04:00
|
|
|
return;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-10-24 17:26:38 -04:00
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
if (ON_RELEASE_TRIGGERS.some((p) => output.includes(p))) {
|
|
|
|
this.#lastLine = output;
|
|
|
|
Pointers.detectClickedOutside('released');
|
2023-10-24 17:26:38 -04:00
|
|
|
this.emit('released', output);
|
|
|
|
this.emit('new-line', output);
|
|
|
|
}
|
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
if (ON_CLICK_TRIGGERS.some((p) => output.includes(p))) {
|
|
|
|
this.#lastLine = output;
|
|
|
|
Pointers.detectClickedOutside('clicked');
|
2023-10-24 17:26:38 -04:00
|
|
|
this.emit('clicked', output);
|
|
|
|
this.emit('new-line', output);
|
2023-10-20 23:11:21 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
this.emit('proc-started', true);
|
|
|
|
}
|
2023-10-16 17:06:19 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
killProc() {
|
2023-11-21 01:29:46 -05:00
|
|
|
if (this.#process) {
|
|
|
|
this.#process.force_exit();
|
|
|
|
this.#process = null;
|
2023-10-20 23:11:21 -04:00
|
|
|
this.emit('proc-destroyed', true);
|
2023-10-16 17:06:19 -04:00
|
|
|
}
|
|
|
|
}
|
2023-10-24 17:26:38 -04:00
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
#initAppConnection() {
|
2023-10-24 17:26:38 -04:00
|
|
|
App.connect('window-toggled', () => {
|
2024-01-13 11:15:08 -05:00
|
|
|
const anyVisibleAndClosable =
|
2024-01-31 15:09:03 -05:00
|
|
|
(App.windows as Array<PopupWindow>).some((w) => {
|
|
|
|
const closable = w.close_on_unfocus &&
|
|
|
|
!(
|
|
|
|
w.close_on_unfocus === 'none' ||
|
|
|
|
w.close_on_unfocus === 'stay'
|
|
|
|
);
|
2023-10-24 17:26:38 -04:00
|
|
|
|
2024-01-31 15:09:03 -05:00
|
|
|
return w.visible && closable;
|
|
|
|
});
|
2023-10-24 17:26:38 -04:00
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
if (anyVisibleAndClosable) {
|
2023-10-24 17:26:38 -04:00
|
|
|
this.startProc();
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-10-24 17:26:38 -04:00
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
else {
|
2023-10-24 17:26:38 -04:00
|
|
|
this.killProc();
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-10-24 17:26:38 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-13 11:15:08 -05:00
|
|
|
static detectClickedOutside(clickStage: string) {
|
2024-01-31 15:09:03 -05:00
|
|
|
const toClose = ((App.windows as Array<PopupWindow>)).some((w) => {
|
2024-01-29 20:56:56 -05:00
|
|
|
const closable = (
|
2024-01-31 15:09:03 -05:00
|
|
|
w.close_on_unfocus &&
|
|
|
|
w.close_on_unfocus === clickStage
|
2024-01-29 20:56:56 -05:00
|
|
|
);
|
|
|
|
|
2024-01-31 15:09:03 -05:00
|
|
|
return w.visible && closable;
|
2024-01-29 20:56:56 -05:00
|
|
|
});
|
2023-11-21 01:29:46 -05:00
|
|
|
|
|
|
|
if (!toClose) {
|
2023-10-24 17:26:38 -04:00
|
|
|
return;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-10-24 17:26:38 -04:00
|
|
|
|
2024-02-11 02:18:59 -05:00
|
|
|
Hyprland.messageAsync('j/layers').then((response) => {
|
2024-01-13 11:15:08 -05:00
|
|
|
const layers = JSON.parse(response) as { Layers: Layers };
|
2023-10-24 17:26:38 -04:00
|
|
|
|
2024-02-11 02:18:59 -05:00
|
|
|
Hyprland.messageAsync('j/cursorpos').then((res) => {
|
2024-01-13 11:15:08 -05:00
|
|
|
const pos = JSON.parse(res) as CursorPos;
|
2023-10-24 17:26:38 -04:00
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
Object.values(layers).forEach((key) => {
|
2024-01-16 11:26:35 -05:00
|
|
|
const overlayLayer = key.levels['3'];
|
|
|
|
|
|
|
|
if (overlayLayer) {
|
2024-03-21 21:45:07 -04:00
|
|
|
const noCloseWidgetsNames = [
|
|
|
|
'bar-0',
|
|
|
|
'bar-1',
|
|
|
|
'bar-2',
|
|
|
|
'bar-3',
|
|
|
|
'osk',
|
|
|
|
];
|
2024-01-16 11:26:35 -05:00
|
|
|
|
|
|
|
const getNoCloseWidgets = (names: Array<string>) => {
|
|
|
|
const arr = [] as Array<Layer>;
|
|
|
|
|
|
|
|
names.forEach((name) => {
|
|
|
|
arr.push(
|
|
|
|
overlayLayer.find(
|
|
|
|
(n) => n.namespace === name,
|
|
|
|
) ||
|
|
|
|
// Return an empty Layer if widget doesn't exist
|
|
|
|
{
|
|
|
|
address: '',
|
2024-01-22 10:23:32 -05:00
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
w: 0,
|
|
|
|
h: 0,
|
2024-01-16 11:26:35 -05:00
|
|
|
namespace: '',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
};
|
|
|
|
const clickIsOnWidget = (w: Layer) => {
|
|
|
|
return pos.x > w.x && pos.x < w.x + w.w &&
|
|
|
|
pos.y > w.y && pos.y < w.y + w.h;
|
|
|
|
};
|
|
|
|
|
|
|
|
const noCloseWidgets =
|
|
|
|
getNoCloseWidgets(noCloseWidgetsNames);
|
|
|
|
|
|
|
|
const widgets = overlayLayer.filter((n) => {
|
2024-03-21 17:47:06 -04:00
|
|
|
let window = null as null | PopupWindow;
|
|
|
|
|
|
|
|
if (App.windows.some((win) =>
|
|
|
|
win.name === n.namespace)) {
|
|
|
|
window = (App
|
|
|
|
.getWindow(n.namespace) as PopupWindow);
|
|
|
|
}
|
2024-01-16 11:26:35 -05:00
|
|
|
|
|
|
|
return window &&
|
2024-01-29 18:54:07 -05:00
|
|
|
window.close_on_unfocus &&
|
|
|
|
window.close_on_unfocus ===
|
2024-01-16 11:26:35 -05:00
|
|
|
clickStage;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (noCloseWidgets.some(clickIsOnWidget)) {
|
|
|
|
// Don't handle clicks when on certain widgets
|
|
|
|
}
|
|
|
|
else {
|
2024-03-21 17:47:06 -04:00
|
|
|
widgets.forEach(
|
2024-01-16 11:26:35 -05:00
|
|
|
(w) => {
|
|
|
|
if (!(pos.x > w.x && pos.x < w.x + w.w &&
|
2024-03-21 17:47:06 -04:00
|
|
|
pos.y > w.y && pos.y < w.y + w.h)) {
|
2024-01-16 11:26:35 -05:00
|
|
|
App.closeWindow(w.namespace);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2023-10-24 17:26:38 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}).catch(print);
|
|
|
|
}).catch(print);
|
|
|
|
}
|
2023-10-16 17:06:19 -04:00
|
|
|
}
|
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
export default new Pointers();
|