69 lines
1.9 KiB
TypeScript

import AstalHyprland from "gi://AstalHyprland";
const getAvailableWorkspaces = (): number[] => {
const workspaces: number[] = [];
AstalHyprland.get_default().get_workspaces().forEach( val => {
workspaces.push( val.get_id() );
} )
return workspaces;
}
const getCurrentWorkspaceID = (): number => {
return AstalHyprland.get_default().get_focused_workspace().get_id();
}
const getCurrentWindowTitle = (): string => {
return AstalHyprland.get_default().get_focused_client().get_title();
}
/**
* Get the workspace ID of a window by its address
* @param address - The address of the window
* @returns The workspace ID
*/
const getWorkspaceIDOfWindowByAddress = ( address: string ): number => {
AstalHyprland.get_default().get_clients().forEach( client => {
if ( client.get_address() === address ) {
return client.get_workspace().get_id();
}
} );
return -1;
}
interface HyprlandSubscriptions {
[key: string]: ( data: string ) => void;
}
const hooks: HyprlandSubscriptions = {};
/**
* Add an event listener for Hyprland events.
* @param event - A hyprland IPC event. See https://wiki.hyprland.org/IPC/. Useful events include: urgent, windowtitlev2, workspace, createworkspacev2, destroyworkspacev2, activewindowv2
* @param cb - The callback function
*/
const subscribeToUpdates = ( event: string, cb: ( data: string ) => void ): void => {
hooks[ event ] = cb;
}
/**
* Listen to events. Must be called at some point if events are to be listened for
*/
const listen = () => {
AstalHyprland.get_default().connect( "event", ( name: string, data: string ) => {
if ( hooks[ name ] ) {
hooks[ name ]( data );
}
} );
}
export default {
getAvailableWorkspaces,
getCurrentWorkspaceID,
getCurrentWindowTitle,
getWorkspaceIDOfWindowByAddress,
subscribeToUpdates,
listen
}