dots/ags/lib/utils.ts
2025-05-03 17:06:04 +02:00

63 lines
1.7 KiB
TypeScript

import { Gtk, Gdk } from "astal/gtk4";
export type If<Condition, Then, Else> = Condition extends true ? Then : Else;
export type Belongs<T, U> = {
[K in keyof T]: T[K] extends U ? K : never;
}[keyof T];
export const generateModmaskMap = () => {
const MODIFIERS: Record<number, string> = {
1: "SHIFT",
4: "CTRL",
8: "ALT",
64: "SUPER"
};
const modmaskMap: Record<number, string> = {};
const modifierValues = Object.keys(MODIFIERS).map(Number);
const totalCombinations = 1 << modifierValues.length;
for (let i = 0; i < totalCombinations; i++) {
let modmask = 0;
const labels: string[] = [];
modifierValues.forEach((value, index) => {
if (i & (1 << index)) {
modmask |= value;
labels.push(MODIFIERS[value]);
}
});
modmaskMap[modmask] = labels.reverse().join(" + ");
}
return modmaskMap;
};
export const hideWindow = (self: Gtk.Window, keyval: number) => {
const keys = ["quick_settings"].includes(self.name)
? [Gdk.KEY_Escape]
: [
Gdk.KEY_Escape,
Gdk.KEY_Super_L,
Gdk.KEY_Super_R
];
if (keys.some(key => key === keyval)) self.hide();
}
export const openOnButton = (event: Gdk.ButtonEvent, keyval: number) => (action: () => void) => {
if (event.get_button() !== keyval) return;
action();
}
export const limit = <T>(list: T[], max: number) => list.filter((_, i) => i <= (max - 1));
export const skip = <T>(list: T[], items: number) => list.filter((_, i) => {
if (items < 0) items = 0;
if (items > (list.length - 1)) items = list.length;
return i > (items - 1);
});