nixos-configs/devices/wim/config/ags/ts/bar/items/workspaces.ts

173 lines
5.7 KiB
TypeScript
Raw Normal View History

import Hyprland from 'resource:///com/github/Aylur/ags/service/hyprland.js';
import { timeout } from 'resource:///com/github/Aylur/ags/utils.js';
import { Box, Overlay, Revealer } from 'resource:///com/github/Aylur/ags/widget.js';
2023-09-04 00:41:14 -04:00
import CursorBox from '../../misc/cursorbox.js';
const URGENT_DURATION = 1000;
2024-01-13 11:15:08 -05:00
// Types
import AgsBox from 'types/widgets/box.js';
import AgsRevealer from 'types/widgets/revealer.js';
import AgsOverlay from 'types/widgets/overlay.js';
import AgsEventBox from 'types/widgets/eventbox.js';
2023-12-20 03:45:05 -05:00
2023-12-18 18:00:30 -05:00
2024-01-13 11:15:08 -05:00
const Workspace = ({ id }: { id: number }) => {
return Revealer({
transition: 'slide_right',
2023-12-18 18:00:30 -05:00
attribute: { id },
child: CursorBox({
tooltip_text: `${id}`,
on_primary_click_release: () => {
2023-12-18 18:00:30 -05:00
Hyprland.sendMessage(`dispatch workspace ${id}`);
},
child: Box({
2023-11-06 18:37:23 -05:00
vpack: 'center',
2023-12-18 18:00:30 -05:00
class_name: 'button',
setup: (self) => {
2024-01-13 11:15:08 -05:00
const update = (_: AgsBox, addr: string | undefined) => {
2023-12-18 18:00:30 -05:00
const workspace = Hyprland.getWorkspace(id);
2023-12-20 03:45:05 -05:00
const occupied = workspace && workspace.windows > 0;
self.toggleClassName('occupied', occupied);
2023-12-18 18:00:30 -05:00
if (!addr) {
return;
}
// Deal with urgent windows
2023-12-18 18:00:30 -05:00
const client = Hyprland.getClient(addr);
const isThisUrgent = client &&
2023-12-20 03:45:05 -05:00
client.workspace.id === id;
2023-12-18 18:00:30 -05:00
if (isThisUrgent) {
self.toggleClassName('urgent', true);
// Only show for a sec when urgent is current workspace
2023-12-18 18:00:30 -05:00
if (Hyprland.active.workspace.id === id) {
timeout(URGENT_DURATION, () => {
self.toggleClassName('urgent', false);
});
}
}
};
self
2023-12-18 18:00:30 -05:00
.hook(Hyprland, update)
// Deal with urgent windows
2023-12-18 18:00:30 -05:00
.hook(Hyprland, update, 'urgent-window')
.hook(Hyprland.active.workspace, () => {
2023-12-18 18:00:30 -05:00
if (Hyprland.active.workspace.id === id) {
self.toggleClassName('urgent', false);
}
});
},
}),
}),
});
};
2023-09-04 00:41:14 -04:00
export default () => {
const L_PADDING = 16;
const WS_WIDTH = 30;
2024-01-13 11:15:08 -05:00
const updateHighlight = (self: AgsBox) => {
const currentId = Hyprland.active.workspace.id;
2023-12-20 03:45:05 -05:00
2024-01-13 11:15:08 -05:00
const indicators = (((self.get_parent() as AgsOverlay)
.child as AgsEventBox)
.child as AgsBox)
.children as Array<AgsRevealer>;
2023-12-20 03:45:05 -05:00
const currentIndex = indicators
2023-12-18 18:00:30 -05:00
.findIndex((w) => w.attribute.id === currentId);
if (currentIndex < 0) {
return;
}
self.setCss(`margin-left: ${L_PADDING + (currentIndex * WS_WIDTH)}px`);
};
const highlight = Box({
2023-11-06 18:37:23 -05:00
vpack: 'center',
hpack: 'start',
2023-12-18 18:00:30 -05:00
class_name: 'button active',
2023-12-18 18:00:30 -05:00
}).hook(Hyprland.active.workspace, updateHighlight);
const widget = Overlay({
pass_through: true,
overlays: [highlight],
child: CursorBox({
child: Box({
2023-12-18 18:00:30 -05:00
class_name: 'workspaces',
2024-01-13 11:15:08 -05:00
attribute: { workspaces: [] },
2023-12-18 18:00:30 -05:00
setup: (self) => {
2024-01-13 11:15:08 -05:00
const workspaces = (): Array<AgsRevealer> =>
self.attribute.workspaces;
2023-12-20 03:45:05 -05:00
2023-12-18 18:00:30 -05:00
const refresh = () => {
2024-01-13 11:15:08 -05:00
(self.children as Array<AgsRevealer>).forEach((rev) => {
rev.reveal_child = false;
});
2023-12-20 03:45:05 -05:00
workspaces().forEach((ws) => {
ws.reveal_child = true;
});
2023-12-18 18:00:30 -05:00
};
2023-12-18 18:00:30 -05:00
const updateWorkspaces = () => {
Hyprland.workspaces.forEach((ws) => {
2024-01-13 11:15:08 -05:00
const currentWs = (self.children as Array<AgsBox>)
.find((ch) => ch.attribute.id === ws.id);
2023-12-20 03:45:05 -05:00
if (!currentWs && ws.id > 0) {
self.add(Workspace({ id: ws.id }));
}
});
self.show_all();
// Make sure the order is correct
2023-12-20 03:45:05 -05:00
workspaces().forEach((workspace, i) => {
2024-01-13 11:15:08 -05:00
(<AgsBox> workspace.get_parent()).reorder_child(
2023-12-20 03:45:05 -05:00
workspace,
i,
);
});
2023-12-18 18:00:30 -05:00
};
self.hook(Hyprland, () => {
2023-12-18 18:00:30 -05:00
self.attribute.workspaces =
2024-01-13 11:15:08 -05:00
(self.children as Array<AgsBox>).filter((ch) => {
2023-12-18 18:00:30 -05:00
return Hyprland.workspaces.find((ws) => {
2023-12-20 03:45:05 -05:00
return ws.id === ch.attribute.id;
2023-12-18 18:00:30 -05:00
});
}).sort((a, b) => a.attribute.id - b.attribute.id);
2023-09-04 00:41:14 -04:00
2023-12-18 18:00:30 -05:00
updateWorkspaces();
refresh();
// Make sure the highlight doesn't go too far
const TEMP_TIMEOUT = 10;
timeout(TEMP_TIMEOUT, () => updateHighlight(highlight));
});
},
}),
}),
});
return widget;
};