nixos-configs/devices/wim/config/ags/js/bar/buttons/workspaces.js

183 lines
6.1 KiB
JavaScript
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;
2023-12-20 03:45:05 -05:00
/** @typedef {import('types/widgets/revealer.js').default} Revealer */
2023-12-18 18:00:30 -05:00
/** @property {number} id */
const Workspace = ({ id }) => {
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) => {
2023-12-18 18:00:30 -05:00
/**
* @param {import('types/widgets/box').default} _
2023-12-18 18:00:30 -05:00
* @param {string|undefined} addr
*/
const update = (_, addr) => {
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;
/** @param {import('types/widgets/box').default} self */
const updateHighlight = (self) => {
const currentId = Hyprland.active.workspace.id;
2023-12-20 03:45:05 -05:00
/** @type Array<Revealer> */
2023-12-18 18:00:30 -05:00
// @ts-expect-error
const indicators = self?.get_parent()?.child.child.child.children;
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',
2023-12-20 03:45:05 -05:00
attribute: {
/** @type Array<Revealer> */
workspaces: [],
},
2023-12-18 18:00:30 -05:00
setup: (self) => {
2023-12-20 03:45:05 -05:00
/** @type function(void):Array<Revealer> */
const workspaces = () => self.attribute.workspaces;
2023-12-18 18:00:30 -05:00
const refresh = () => {
2023-12-20 03:45:05 -05:00
self.children.forEach((rev) => {
// @ts-expect-error they are in fact revealers
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) => {
2023-12-20 03:45:05 -05:00
const currentWs = self.children.find((ch) => {
2023-12-20 17:14:07 -05:00
// @ts-expect-error
2023-12-20 03:45:05 -05:00
return 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) => {
// @ts-expect-error
workspace?.get_parent()?.reorder_child(
workspace,
i,
);
});
2023-12-18 18:00:30 -05:00
};
self.hook(Hyprland, () => {
2023-12-18 18:00:30 -05:00
self.attribute.workspaces =
self.children.filter((ch) => {
return Hyprland.workspaces.find((ws) => {
// @ts-expect-error
2023-12-20 03:45:05 -05:00
return ws.id === ch.attribute.id;
2023-12-18 18:00:30 -05:00
});
// @ts-expect-error
}).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;
};