2024-10-15 20:01:20 -04:00
|
|
|
import { Gtk, Widget } from 'astal/gtk3';
|
|
|
|
import { timeout } from 'astal';
|
2024-09-27 21:14:58 -04:00
|
|
|
|
2024-10-22 13:09:39 -04:00
|
|
|
import AstalHyprland from 'gi://AstalHyprland';
|
2024-09-27 21:14:58 -04:00
|
|
|
|
2024-10-16 12:32:06 -04:00
|
|
|
import { hyprMessage } from '../../../lib';
|
|
|
|
|
2024-09-27 21:14:58 -04:00
|
|
|
|
|
|
|
const URGENT_DURATION = 1000;
|
|
|
|
|
2024-11-20 23:38:41 -05:00
|
|
|
const Workspace = ({ id = 0 }) => {
|
|
|
|
const hyprland = AstalHyprland.get_default();
|
2024-09-27 21:14:58 -04:00
|
|
|
|
2024-11-20 23:38:41 -05:00
|
|
|
return (
|
|
|
|
<revealer
|
|
|
|
name={id.toString()}
|
|
|
|
transitionType={Gtk.RevealerTransitionType.SLIDE_RIGHT}
|
|
|
|
>
|
|
|
|
<eventbox
|
|
|
|
cursor="pointer"
|
|
|
|
tooltip_text={id.toString()}
|
2024-09-27 21:14:58 -04:00
|
|
|
|
2024-11-20 23:38:41 -05:00
|
|
|
onClickRelease={() => {
|
|
|
|
hyprMessage(`dispatch workspace ${id}`).catch(console.log);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<box
|
|
|
|
valign={Gtk.Align.CENTER}
|
|
|
|
className="button"
|
|
|
|
|
|
|
|
setup={(self) => {
|
|
|
|
const update = (
|
|
|
|
_: Widget.Box,
|
|
|
|
client?: AstalHyprland.Client,
|
|
|
|
) => {
|
|
|
|
const workspace = hyprland.get_workspace(id);
|
|
|
|
const occupied = workspace && workspace.get_clients().length > 0;
|
|
|
|
|
|
|
|
self.toggleClassName('occupied', occupied);
|
|
|
|
|
|
|
|
if (!client) {
|
|
|
|
return;
|
|
|
|
}
|
2024-09-27 21:14:58 -04:00
|
|
|
|
2024-11-20 23:38:41 -05:00
|
|
|
const isUrgent = client &&
|
2024-10-11 14:43:42 -04:00
|
|
|
client.get_workspace().get_id() === id;
|
2024-09-27 21:14:58 -04:00
|
|
|
|
2024-11-20 23:38:41 -05:00
|
|
|
if (isUrgent) {
|
|
|
|
self.toggleClassName('urgent', true);
|
2024-09-27 21:14:58 -04:00
|
|
|
|
|
|
|
// Only show for a sec when urgent is current workspace
|
2024-11-20 23:38:41 -05:00
|
|
|
if (hyprland.get_focused_workspace().get_id() === id) {
|
|
|
|
timeout(URGENT_DURATION, () => {
|
|
|
|
self.toggleClassName('urgent', false);
|
|
|
|
});
|
|
|
|
}
|
2024-09-27 21:14:58 -04:00
|
|
|
}
|
2024-11-20 23:38:41 -05:00
|
|
|
};
|
2024-09-27 21:14:58 -04:00
|
|
|
|
2024-11-20 23:38:41 -05:00
|
|
|
update(self);
|
|
|
|
self
|
|
|
|
.hook(hyprland, 'event', () => update(self))
|
2024-09-27 21:14:58 -04:00
|
|
|
|
|
|
|
// Deal with urgent windows
|
2024-11-20 23:38:41 -05:00
|
|
|
.hook(hyprland, 'urgent', update)
|
2024-09-27 21:14:58 -04:00
|
|
|
|
2024-11-20 23:38:41 -05:00
|
|
|
.hook(hyprland, 'notify::focused-workspace', () => {
|
|
|
|
if (hyprland.get_focused_workspace().get_id() === id) {
|
|
|
|
self.toggleClassName('urgent', false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</eventbox>
|
|
|
|
</revealer>
|
|
|
|
);
|
|
|
|
};
|
2024-09-27 21:14:58 -04:00
|
|
|
|
|
|
|
export default () => {
|
2024-11-20 23:38:41 -05:00
|
|
|
const Hyprland = AstalHyprland.get_default();
|
|
|
|
|
2024-09-27 21:14:58 -04:00
|
|
|
const L_PADDING = 2;
|
|
|
|
const WS_WIDTH = 30;
|
|
|
|
|
|
|
|
const updateHighlight = (self: Widget.Box) => {
|
|
|
|
const currentId = Hyprland.get_focused_workspace().get_id().toString();
|
|
|
|
|
|
|
|
const indicators = ((self.get_parent() as Widget.Overlay)
|
|
|
|
.child as Widget.Box)
|
|
|
|
.children as Widget.Revealer[];
|
|
|
|
|
|
|
|
const currentIndex = indicators.findIndex((w) => w.name === currentId);
|
|
|
|
|
|
|
|
if (currentIndex >= 0) {
|
|
|
|
self.css = `margin-left: ${L_PADDING + (currentIndex * WS_WIDTH)}px`;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const highlight = (
|
|
|
|
<box
|
|
|
|
className="button active"
|
|
|
|
|
|
|
|
valign={Gtk.Align.CENTER}
|
|
|
|
halign={Gtk.Align.START}
|
|
|
|
|
|
|
|
setup={(self) => {
|
|
|
|
self.hook(Hyprland, 'notify::focused-workspace', updateHighlight);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
) as Widget.Box;
|
|
|
|
|
|
|
|
let workspaces: Widget.Revealer[] = [];
|
|
|
|
|
|
|
|
return (
|
|
|
|
<box
|
|
|
|
className="bar-item"
|
|
|
|
>
|
|
|
|
<overlay
|
|
|
|
className="workspaces"
|
|
|
|
passThrough
|
|
|
|
overlay={highlight}
|
|
|
|
>
|
|
|
|
<box
|
|
|
|
setup={(self) => {
|
|
|
|
const refresh = () => {
|
|
|
|
(self.children as Widget.Revealer[]).forEach((rev) => {
|
|
|
|
rev.reveal_child = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
workspaces.forEach((ws) => {
|
|
|
|
ws.reveal_child = true;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateWorkspaces = () => {
|
|
|
|
Hyprland.get_workspaces().forEach((ws) => {
|
|
|
|
const currentWs = (self.children as Widget.Revealer[])
|
|
|
|
.find((ch) => ch.name === ws.id.toString());
|
|
|
|
|
|
|
|
if (!currentWs && ws.id > 0) {
|
|
|
|
self.add(Workspace({ id: ws.id }));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make sure the order is correct
|
|
|
|
workspaces.forEach((workspace, i) => {
|
|
|
|
(workspace.get_parent() as Widget.Box)
|
|
|
|
.reorder_child(workspace, i);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-10-03 11:50:42 -04:00
|
|
|
const updateAll = () => {
|
2024-09-27 21:14:58 -04:00
|
|
|
workspaces = (self.children as Widget.Revealer[])
|
|
|
|
.filter((ch) => {
|
|
|
|
return Hyprland.get_workspaces().find((ws) => {
|
|
|
|
return ws.id.toString() === ch.name;
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.sort((a, b) => parseInt(a.name ?? '0') - parseInt(b.name ?? '0'));
|
|
|
|
|
|
|
|
updateWorkspaces();
|
|
|
|
refresh();
|
|
|
|
|
|
|
|
// Make sure the highlight doesn't go too far
|
|
|
|
const TEMP_TIMEOUT = 100;
|
|
|
|
|
|
|
|
timeout(TEMP_TIMEOUT, () => updateHighlight(highlight));
|
2024-10-03 11:50:42 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
updateAll();
|
|
|
|
self.hook(Hyprland, 'event', updateAll);
|
2024-09-27 21:14:58 -04:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</overlay>
|
|
|
|
</box>
|
|
|
|
);
|
|
|
|
};
|