overview: fewer destroy/create

This commit is contained in:
end-4
2024-02-08 17:19:52 +07:00
parent 3cce919268
commit 58b66959b7
@@ -49,7 +49,8 @@ function substitute(str) {
if (!iconExists(str)) str = str.toLowerCase().replace(/\s+/g, '-'); // Turn into kebab-case
return str;
}
export default () => {
const clientMap = new Map();
const ContextMenuWorkspaceArray = ({ label, actionFunc, thisWorkspace }) => Widget.MenuItem({
label: `${label}`,
setup: (menuItem) => {
@@ -91,7 +92,7 @@ const Window = ({ address, at: [x, y], size: [w, h], workspace: { id, name }, cl
// title = truncateTitle(title);
return Widget.Button({
attribute: { x, y },
attribute: { address, x, y, ws: id },
className: 'overview-tasks-window',
hpack: 'center',
vpack: 'center',
@@ -188,7 +189,6 @@ const Window = ({ address, at: [x, y], size: [w, h], workspace: { id, name }, cl
const Workspace = (index) => {
const fixed = Gtk.Fixed.new();
// const clientMap = new Map();
const WorkspaceNumber = (index) => Widget.Label({
className: 'overview-tasks-workspace-number',
label: `${index}`,
@@ -222,12 +222,35 @@ const Workspace = (index) => {
})],
});
widget.clear = () => {
fixed.get_children().forEach(ch => ch.destroy());
clientMap.forEach((client, address) => {
if (!client || client.ws !== index) return;
client.destroy();
client = null;
clientMap.delete(address);
});
const offset = Math.floor((Hyprland.active.workspace.id - 1) / NUM_OF_WORKSPACES_SHOWN) * NUM_OF_WORKSPACES_SHOWN;
fixed.put(WorkspaceNumber(offset + index), 0, 0);
}
widget.set = (clientJson, screenCoords) => {
// if(clientMap.get(clientJson.address)) clientMap.get(clientJson.address).destroy();
let c = clientMap.get(clientJson.address);
if (c) {
if (c.ws !== clientJson.workspace.id) {
c.destroy();
c = null;
clientMap.delete(clientJson.address);
}
else {
fixed.move(c,
Math.max(0, (c.attribute.x + clientJson.x) / 2 * OVERVIEW_SCALE),
Math.max(0, (c.attribute.y + clientJson.y) / 2 * OVERVIEW_SCALE)
);
Utils.timeout(1000, () => fixed.move(c,
Math.max(0, clientJson.x * OVERVIEW_SCALE),
Math.max(0, clientJson.y * OVERVIEW_SCALE)
));
return;
}
}
const newWindow = Window(clientJson, screenCoords);
if (newWindow === null) return;
// clientMap.set(clientJson.address, newWindow);
@@ -235,6 +258,7 @@ const Workspace = (index) => {
Math.max(0, newWindow.attribute.x * OVERVIEW_SCALE),
Math.max(0, newWindow.attribute.y * OVERVIEW_SCALE)
);
clientMap.set(clientJson.address, newWindow);
};
// widget.unset = (clientAddress) => {
// if(clientMap.get(clientAddress)) {
@@ -271,7 +295,7 @@ const OverviewRow = ({ startWorkspace, workspaces, windowName = 'overview' }) =>
update: (box) => {
const offset = Math.floor((Hyprland.active.workspace.id - 1) / NUM_OF_WORKSPACES_SHOWN) * NUM_OF_WORKSPACES_SHOWN;
if (!App.getWindow(windowName).visible) return;
execAsync('hyprctl -j clients').then(clients => {
Hyprland.sendMessage('j/clients').then(clients => {
const allClients = JSON.parse(clients);
const kids = box.get_children();
kids.forEach(kid => kid.clear());
@@ -287,7 +311,26 @@ const OverviewRow = ({ startWorkspace, workspaces, windowName = 'overview' }) =>
kids.forEach(kid => kid.show());
}).catch(print);
},
updateWorkspace: (box, id) => {
const offset = Math.floor((Hyprland.active.workspace.id - 1) / NUM_OF_WORKSPACES_SHOWN) * NUM_OF_WORKSPACES_SHOWN;
if (!( // Not in range, ignore
offset + startWorkspace <= id &&
id <= offset + startWorkspace + workspaces
)) return;
if (!App.getWindow(windowName).visible) return;
Hyprland.sendMessage('j/clients').then(clients => {
const allClients = JSON.parse(clients);
const kids = box.get_children();
for (let i = 0; i < allClients.length; i++) {
const client = allClients[i];
if (client.workspace.id != id) continue;
const screenCoords = box.attribute.monitorMap[client.monitor];
kids[id - (offset + startWorkspace)]?.set(client, screenCoords);
}
kids[id - (offset + startWorkspace)]?.show();
}).catch(print);
},
},
setup: (box) => {
box.attribute.getMonitorMap(box);
@@ -295,11 +338,14 @@ const OverviewRow = ({ startWorkspace, workspaces, windowName = 'overview' }) =>
.hook(overviewTick, (box) => box.attribute.update(box))
.hook(Hyprland, (box, clientAddress) => {
const client = Hyprland.getClient(clientAddress);
box.attribute.updateWorkspace(client.workspace.id);
if (!client) return;
box.attribute.updateWorkspace(box, client.workspace.id);
// box.attribute.update(box)
}, 'client-removed')
.hook(Hyprland, (box, clientAddress) => {
box.attribute.update(box);
const client = Hyprland.getClient(clientAddress);
if (!client) return;
box.attribute.updateWorkspace(box, client.workspace.id);
}, 'client-added')
.hook(Hyprland.active.workspace, (box) => box.attribute.update(box))
.hook(App, (box, name, visible) => { // Update on open
@@ -308,8 +354,7 @@ const OverviewRow = ({ startWorkspace, workspaces, windowName = 'overview' }) =>
},
});
export default () => Widget.Revealer({
return Widget.Revealer({
revealChild: true,
transition: 'slide_down',
transitionDuration: 200,
@@ -324,3 +369,4 @@ export default () => Widget.Revealer({
)
}),
});
}