2024-12-19 17:46:57 -05:00
|
|
|
import { idle, subprocess } from 'astal';
|
2024-11-18 10:40:40 -05:00
|
|
|
import { App, Gdk, Gtk } from 'astal/gtk3';
|
2024-09-26 11:11:31 -04:00
|
|
|
|
2024-10-22 13:09:39 -04:00
|
|
|
import AstalHyprland from 'gi://AstalHyprland';
|
2024-09-26 11:11:31 -04:00
|
|
|
|
2024-10-16 21:44:45 -04:00
|
|
|
/* Types */
|
2024-10-30 18:53:15 -04:00
|
|
|
import PopupWindow from './widgets/misc/popup-window';
|
|
|
|
|
2024-10-16 21:44:45 -04:00
|
|
|
export interface Layer {
|
|
|
|
address: string
|
|
|
|
x: number
|
|
|
|
y: number
|
|
|
|
w: number
|
|
|
|
h: number
|
|
|
|
namespace: string
|
|
|
|
}
|
|
|
|
export interface Levels {
|
|
|
|
0?: Layer[] | null
|
|
|
|
1?: Layer[] | null
|
|
|
|
2?: Layer[] | null
|
|
|
|
3?: Layer[] | null
|
|
|
|
}
|
|
|
|
export interface Layers {
|
|
|
|
levels: Levels
|
|
|
|
}
|
|
|
|
export type LayerResult = Record<string, Layers>;
|
|
|
|
export interface CursorPos {
|
|
|
|
x: number
|
|
|
|
y: number
|
|
|
|
}
|
|
|
|
|
2024-11-27 15:19:36 -05:00
|
|
|
type PointProps = [number, number] | {
|
|
|
|
x: number
|
|
|
|
y: number
|
|
|
|
} | number;
|
|
|
|
|
|
|
|
export class Point {
|
|
|
|
public x = 0;
|
|
|
|
public y = 0;
|
|
|
|
|
|
|
|
get values(): [number, number] {
|
|
|
|
return [this.x, this.y];
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props?: PointProps, y?: number) {
|
|
|
|
if (typeof props === 'number') {
|
|
|
|
if (y) {
|
|
|
|
this.x = props;
|
|
|
|
this.y = y;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error('Wrong props');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (Array.isArray(props)) {
|
|
|
|
this.x = props[0];
|
|
|
|
this.y = props[1];
|
|
|
|
}
|
|
|
|
else if (props) {
|
|
|
|
this.x = props.x;
|
|
|
|
this.y = props.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type BezierPoints = [number, number, number, number];
|
|
|
|
|
|
|
|
export class Bezier {
|
|
|
|
private _points: BezierPoints;
|
|
|
|
|
|
|
|
get points() {
|
|
|
|
return [...this._points] as BezierPoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(x1: number, y1: number, x2: number, y2: number) {
|
|
|
|
this._points = [x1, y1, x2, y2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-26 11:11:31 -04:00
|
|
|
|
|
|
|
export const get_hyprland_monitor = (monitor: Gdk.Monitor): AstalHyprland.Monitor | undefined => {
|
2024-11-20 23:38:41 -05:00
|
|
|
const hyprland = AstalHyprland.get_default();
|
|
|
|
|
2024-09-26 11:11:31 -04:00
|
|
|
const manufacturer = monitor.manufacturer?.replace(',', '');
|
|
|
|
const model = monitor.model?.replace(',', '');
|
|
|
|
const start = `${manufacturer} ${model}`;
|
|
|
|
|
2024-11-20 23:38:41 -05:00
|
|
|
return hyprland.get_monitors().find((m) => m.description?.startsWith(start));
|
2024-09-26 11:11:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
export const get_hyprland_monitor_desc = (monitor: Gdk.Monitor): string => {
|
2024-11-20 23:38:41 -05:00
|
|
|
const hyprland = AstalHyprland.get_default();
|
|
|
|
|
2024-09-26 11:11:31 -04:00
|
|
|
const manufacturer = monitor.manufacturer?.replace(',', '');
|
|
|
|
const model = monitor.model?.replace(',', '');
|
|
|
|
const start = `${manufacturer} ${model}`;
|
|
|
|
|
2024-11-20 23:38:41 -05:00
|
|
|
return `desc:${hyprland.get_monitors().find((m) => m.description?.startsWith(start))?.description}`;
|
2024-09-26 11:11:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
export const get_gdkmonitor_from_desc = (desc: string): Gdk.Monitor => {
|
|
|
|
const display = Gdk.Display.get_default();
|
|
|
|
|
|
|
|
for (let m = 0; m < (display?.get_n_monitors() ?? 0); m++) {
|
|
|
|
const monitor = display?.get_monitor(m);
|
|
|
|
|
|
|
|
if (monitor && desc === get_hyprland_monitor_desc(monitor)) {
|
|
|
|
return monitor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Error(`Monitor ${desc} not found`);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const get_monitor_desc = (mon: AstalHyprland.Monitor): string => {
|
|
|
|
return `desc:${mon.description}`;
|
|
|
|
};
|
2024-10-16 12:32:06 -04:00
|
|
|
|
2024-10-16 21:44:45 -04:00
|
|
|
export const hyprMessage = (message: string) => new Promise<string>((
|
|
|
|
resolution = () => { /**/ },
|
|
|
|
rejection = () => { /**/ },
|
|
|
|
) => {
|
2024-11-20 23:38:41 -05:00
|
|
|
const hyprland = AstalHyprland.get_default();
|
|
|
|
|
2024-10-16 21:44:45 -04:00
|
|
|
try {
|
2024-11-20 23:38:41 -05:00
|
|
|
hyprland.message_async(message, (_, asyncResult) => {
|
|
|
|
const result = hyprland.message_finish(asyncResult);
|
2024-10-16 12:32:06 -04:00
|
|
|
|
2024-10-16 21:44:45 -04:00
|
|
|
resolution(result);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
rejection(e);
|
|
|
|
}
|
2024-10-16 12:32:06 -04:00
|
|
|
});
|
2024-10-18 00:44:45 -04:00
|
|
|
|
2024-11-13 19:39:01 -05:00
|
|
|
export const centerCursor = (): void => {
|
2024-11-20 23:38:41 -05:00
|
|
|
const hyprland = AstalHyprland.get_default();
|
|
|
|
|
2024-10-18 00:44:45 -04:00
|
|
|
let x: number;
|
|
|
|
let y: number;
|
2024-11-20 23:38:41 -05:00
|
|
|
const monitor = hyprland.get_focused_monitor();
|
2024-10-18 00:44:45 -04:00
|
|
|
|
|
|
|
switch (monitor.transform) {
|
|
|
|
case 1:
|
|
|
|
x = monitor.x - (monitor.height / 2);
|
|
|
|
y = monitor.y - (monitor.width / 2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
x = monitor.x - (monitor.width / 2);
|
|
|
|
y = monitor.y - (monitor.height / 2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
x = monitor.x + (monitor.height / 2);
|
|
|
|
y = monitor.y + (monitor.width / 2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
x = monitor.x + (monitor.width / 2);
|
|
|
|
y = monitor.y + (monitor.height / 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-11-13 19:39:01 -05:00
|
|
|
hyprMessage(`dispatch movecursor ${x} ${y}`);
|
2024-10-18 00:44:45 -04:00
|
|
|
};
|
2024-10-29 00:11:37 -04:00
|
|
|
|
|
|
|
export const closeAll = () => {
|
|
|
|
(App.get_windows() as PopupWindow[])
|
|
|
|
.filter((w) => w &&
|
2024-11-18 22:35:28 -05:00
|
|
|
w.close_on_unfocus &&
|
|
|
|
w.close_on_unfocus !== 'stay')
|
2024-10-29 00:11:37 -04:00
|
|
|
.forEach((w) => {
|
|
|
|
App.get_window(w.name)?.set_visible(false);
|
|
|
|
});
|
|
|
|
};
|
2024-11-18 10:40:40 -05:00
|
|
|
|
2024-11-18 22:35:28 -05:00
|
|
|
export const perMonitor = (window: (monitor: Gdk.Monitor) => Gtk.Widget) => idle(() => {
|
2024-11-18 10:40:40 -05:00
|
|
|
const display = Gdk.Display.get_default();
|
|
|
|
const windows = new Map<Gdk.Monitor, Gtk.Widget>();
|
|
|
|
|
|
|
|
const createWindow = (monitor: Gdk.Monitor) => {
|
|
|
|
windows.set(monitor, window(monitor));
|
|
|
|
};
|
|
|
|
|
|
|
|
for (let m = 0; m < (display?.get_n_monitors() ?? 0); m++) {
|
|
|
|
const monitor = display?.get_monitor(m);
|
|
|
|
|
|
|
|
if (monitor) {
|
|
|
|
createWindow(monitor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
display?.connect('monitor-added', (_, monitor) => {
|
|
|
|
createWindow(monitor);
|
|
|
|
});
|
|
|
|
|
|
|
|
display?.connect('monitor-removed', (_, monitor) => {
|
|
|
|
windows.get(monitor)?.destroy();
|
|
|
|
windows.delete(monitor);
|
|
|
|
});
|
2024-11-18 22:35:28 -05:00
|
|
|
});
|
2024-12-19 17:46:57 -05:00
|
|
|
|
|
|
|
interface NotifyAction {
|
|
|
|
id: string
|
|
|
|
label: string
|
|
|
|
callback: () => void
|
|
|
|
}
|
|
|
|
interface NotifySendProps {
|
|
|
|
actions?: NotifyAction[]
|
|
|
|
appName?: string
|
|
|
|
body?: string
|
|
|
|
category?: string
|
|
|
|
hint?: string
|
|
|
|
iconName: string
|
|
|
|
replaceId?: number
|
|
|
|
title: string
|
|
|
|
urgency?: 'low' | 'normal' | 'critical'
|
|
|
|
}
|
|
|
|
|
|
|
|
const escapeShellArg = (arg: string): string => `'${arg?.replace(/'/g, '\'\\\'\'')}'`;
|
|
|
|
|
|
|
|
export const notifySend = ({
|
|
|
|
actions = [],
|
|
|
|
appName,
|
|
|
|
body,
|
|
|
|
category,
|
|
|
|
hint,
|
|
|
|
iconName,
|
|
|
|
replaceId,
|
|
|
|
title,
|
|
|
|
urgency = 'normal',
|
|
|
|
}: NotifySendProps) => new Promise<number>((resolve) => {
|
|
|
|
let printedId = false;
|
|
|
|
|
|
|
|
const cmd = [
|
|
|
|
'notify-send',
|
|
|
|
'--print-id',
|
|
|
|
`--icon=${escapeShellArg(iconName)}`,
|
|
|
|
escapeShellArg(title),
|
|
|
|
escapeShellArg(body ?? ''),
|
|
|
|
// Optional params
|
|
|
|
appName ? `--app-name=${escapeShellArg(appName)}` : '',
|
|
|
|
category ? `--category=${escapeShellArg(category)}` : '',
|
|
|
|
hint ? `--hint=${escapeShellArg(hint)}` : '',
|
|
|
|
replaceId ? `--replace-id=${replaceId.toString()}` : '',
|
|
|
|
`--urgency=${urgency}`,
|
|
|
|
].concat(
|
|
|
|
actions.map(({ id, label }) => `--action=${escapeShellArg(id)}=${escapeShellArg(label)}`),
|
|
|
|
).join(' ');
|
|
|
|
|
|
|
|
subprocess(
|
|
|
|
cmd,
|
|
|
|
(out) => {
|
|
|
|
if (!printedId) {
|
|
|
|
resolve(parseInt(out));
|
|
|
|
printedId = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
actions.find((action) => action.id === out)?.callback();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
console.error(`[Notify] ${err}`);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|