parent
345d51b6ab
commit
a1caad618b
7 changed files with 398 additions and 0 deletions
modules/ags/gtk4/lib
94
modules/ags/gtk4/lib/hypr.ts
Normal file
94
modules/ags/gtk4/lib/hypr.ts
Normal file
|
@ -0,0 +1,94 @@
|
|||
import { Gdk } from 'astal/gtk4';
|
||||
|
||||
import AstalHyprland from 'gi://AstalHyprland';
|
||||
|
||||
|
||||
export const get_hyprland_monitor = (monitor: Gdk.Monitor): AstalHyprland.Monitor | undefined => {
|
||||
const hyprland = AstalHyprland.get_default();
|
||||
|
||||
const manufacturer = monitor.get_manufacturer()?.replace(',', '');
|
||||
const model = monitor.get_model()?.replace(',', '');
|
||||
const start = `${manufacturer} ${model}`;
|
||||
|
||||
return hyprland.get_monitors().find((m) => m.get_description()?.startsWith(start));
|
||||
};
|
||||
|
||||
export const get_hyprland_monitor_desc = (monitor: Gdk.Monitor): string => {
|
||||
const hyprland = AstalHyprland.get_default();
|
||||
|
||||
const manufacturer = monitor.get_manufacturer()?.replace(',', '');
|
||||
const model = monitor.get_model()?.replace(',', '');
|
||||
const start = `${manufacturer} ${model}`;
|
||||
|
||||
return `desc:${hyprland
|
||||
.get_monitors()
|
||||
.find((m) => m.get_description()?.startsWith(start))?.get_description()}`;
|
||||
};
|
||||
|
||||
export const get_gdkmonitor_from_desc = (desc: string): Gdk.Monitor => {
|
||||
const display = Gdk.Display.get_default();
|
||||
|
||||
for (let m = 0; m < (display?.get_monitors().get_n_items() ?? 0); m++) {
|
||||
const monitor = display?.get_monitors().get_item(m) as Gdk.Monitor;
|
||||
|
||||
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.get_description()}`;
|
||||
};
|
||||
|
||||
export const hyprMessage = (message: string) => new Promise<string>((
|
||||
resolution = () => { /**/ },
|
||||
rejection = () => { /**/ },
|
||||
) => {
|
||||
const hyprland = AstalHyprland.get_default();
|
||||
|
||||
try {
|
||||
hyprland.message_async(message, (_, asyncResult) => {
|
||||
const result = hyprland.message_finish(asyncResult);
|
||||
|
||||
resolution(result);
|
||||
});
|
||||
}
|
||||
catch (e) {
|
||||
rejection(e);
|
||||
}
|
||||
});
|
||||
|
||||
export const centerCursor = (): void => {
|
||||
const hyprland = AstalHyprland.get_default();
|
||||
|
||||
let x: number;
|
||||
let y: number;
|
||||
const monitor = hyprland.get_focused_monitor();
|
||||
|
||||
switch (monitor.get_transform()) {
|
||||
case 1:
|
||||
x = monitor.get_x() - (monitor.get_height() / 2);
|
||||
y = monitor.get_y() - (monitor.get_width() / 2);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
x = monitor.get_x() - (monitor.get_width() / 2);
|
||||
y = monitor.get_y() - (monitor.get_height() / 2);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
x = monitor.get_x() + (monitor.get_height() / 2);
|
||||
y = monitor.get_y() + (monitor.get_width() / 2);
|
||||
break;
|
||||
|
||||
default:
|
||||
x = monitor.get_x() + (monitor.get_width() / 2);
|
||||
y = monitor.get_y() + (monitor.get_height() / 2);
|
||||
break;
|
||||
}
|
||||
|
||||
hyprMessage(`dispatch movecursor ${x} ${y}`);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue