Replies: 3 comments 1 reply
-
The most inefficient thing in this is the destruction and recreation of every ws button on every change. What I have done, is connecting to the workspace-added/removed signals. When a new Hyprland workspace is created, I create a button for it and insert it at the correct position. When Hyperland destroys a workspace I remove the button. This prevents unnecessary widget creation/destruction. So here is what i wrote. const WorkspaceButton = (name) => Widget.EventBox({
class_name: "ws-button",
attribute: {name: name},
on_primary_click_release: () => Hyprland.sendMessage(`dispatch workspace name:${name}`),
child: Widget.Label({
label: name,
class_name: "ws-button-label"
})
})
.hook(Hyprland.active.workspace, (button) => {
button.toggleClassName("active", Hyprland.active.workspace.name === name);
});
const Workspaces = () => Widget.Box({
class_name: "ws-container",
attribute: {
ws: new Map(),
onAdded: (box, ws) => {
if(!ws) return;
const wsButton = WorkspaceButton(ws);
box.attribute.ws.set(ws, wsButton);
const pos = box.children.filter(button => button.attribute.name <= ws ).length;
box.add(wsButton);
box.reorder_child(wsButton, pos);
box.show_all()
},
onRemoved: (box, ws) => {
if(!ws) return;
const wsButton = box.attribute.ws.get(ws);
box.remove(wsButton);
wsButton.destroy();
box.attribute.ws.delete(ws);
}
},
setup: (box) => {
const connID = Hyprland.connect("notify::workspaces", () => {
Hyprland.workspaces.forEach(ws => box.attribute.onAdded(box, ws.name));
Hyprland.disconnect(connID)
})
}
})
.hook(Hyprland, (box, ws) => box.attribute.onAdded(box, ws),"workspace-added")
.hook(Hyprland, (box, ws) => box.attribute.onRemoved(box, ws),"workspace-removed") This is quite the change. So here are a few smaller improvements that could be done on your approach:
connections: [[Hyprland, self => {
// generates an array of buttons, indicating the ws
self.children = Hyprland.workspaces.map(ws => {
const button = Widget.Button({
properties: [["id", ws.id]],
onPrimaryClick: () => execAsync(`hyprctl dispatch workspace ${button._id}`),
className: Hyprland.active.workspace.id == button._id ? 'bar-wsp-foc' : 'bar-wsp',
});
return button;
});
}, "notify::workspaces"]], Note: i didn't try this though. also you should move over to the new syntax using .hook and attribute. |
Beta Was this translation helpful? Give feedback.
-
I just sorted the workspaces array. Isn't that a simpler solution? |
Beta Was this translation helpful? Give feedback.
-
@rafaeljacov There is working solution in Ayalur personal repo |
Beta Was this translation helpful? Give feedback.
-
Hi!
I've been using AGS for a while (btw, really cool project :)). I use Hyprland, and, coming from EWW, i initially found it difficult to manage the dynamic workspaces that you can have in Hyprland.
The main problem is that Hyprland generates those workspaces on demand, and still calls them by number. So, if i initially have workspaces (1, 2, 3), and delete 1, i'll have (2, 3). Now, the workspace in position 1 is 2, and 2 is 3. This makes it more difficult to assign the button's link to the workspace
If i then recreate 1, i'll have (2, 3, 1). The relationship between the workspace and its position is still different.
Now it's been a while since i wrote this code, but i solved it this way:
(I hope it's not that bad, i'm not very familiar with JS)
I was wondering if some of you has a better solution to this, because i think it's very inefficient.
Thanks in advance
Beta Was this translation helpful? Give feedback.
All reactions