nixos-configs/modules/ags/config/ts/overview/workspaces.ts

202 lines
6.2 KiB
TypeScript
Raw Normal View History

import Hyprland from 'resource:///com/github/Aylur/ags/service/hyprland.js';
2023-11-07 10:56:12 -05:00
import { Revealer, CenterBox, Box, EventBox, Fixed, Label } from 'resource:///com/github/Aylur/ags/widget.js';
import { WorkspaceDrop } from './dragndrop.ts';
import * as VARS from './variables.ts';
const EMPTY_OFFSET = 16;
const DEFAULT_STYLE = `
min-width: ${(VARS.SCREEN.X * VARS.SCALE) + EMPTY_OFFSET}px;
min-height: ${VARS.SCREEN.Y * VARS.SCALE}px;
`;
2024-01-13 11:15:08 -05:00
// Types
import AgsBox from 'types/widgets/box.ts';
import AgsRevealer from 'types/widgets/revealer.ts';
import AgsCenterBox from 'types/widgets/centerbox.ts';
import AgsEventBox from 'types/widgets/eventbox.ts';
2023-12-23 01:14:21 -05:00
2024-01-13 11:15:08 -05:00
export const getWorkspaces = (box: AgsBox) => {
const children = [] as Array<AgsRevealer>;
2024-01-13 11:15:08 -05:00
(box.children as Array<AgsBox>).forEach((type) => {
(type.children as Array<AgsRevealer>).forEach(
2023-12-23 01:14:21 -05:00
(row) => {
2024-01-13 11:15:08 -05:00
((((row.child as AgsCenterBox)
?.center_widget as AgsEventBox)
?.child as AgsBox)
.children as Array<AgsRevealer>)
.forEach((workspace) => {
2023-12-23 01:14:21 -05:00
children.push(workspace);
2024-01-13 11:15:08 -05:00
});
2023-12-23 01:14:21 -05:00
},
);
});
2024-01-13 11:15:08 -05:00
box.attribute.workspaces = children.sort((a, b) => {
return a.attribute.id - b.attribute.id;
});
};
2024-01-13 11:15:08 -05:00
const Workspace = (id: number, name: string, normal = true) => {
2024-01-06 12:24:09 -05:00
const fixed = Fixed({});
const workspace = Revealer({
transition: 'slide_right',
2023-12-23 01:14:21 -05:00
transition_duration: 500,
attribute: {
id,
name,
get_fixed: () => fixed,
},
setup: (self) => {
if (normal) {
self.hook(Hyprland, () => {
const activeId = Hyprland.active.workspace.id;
2023-12-23 01:14:21 -05:00
const active = activeId === self.attribute.id;
const ws = Hyprland.getWorkspace(self.attribute.id);
2023-12-23 01:14:21 -05:00
self.reveal_child =
(ws?.windows && ws.windows > 0) || active;
});
}
},
child: WorkspaceDrop({
child: Box({
2023-12-23 01:14:21 -05:00
class_name: 'workspace',
css: normal ?
DEFAULT_STYLE :
`
min-width: ${(VARS.SCREEN.X * VARS.SCALE / 2) +
EMPTY_OFFSET}px;
min-height: ${VARS.SCREEN.Y * VARS.SCALE}px;
`,
children: normal ?
[fixed] :
[
fixed,
Label({
label: ' +',
2023-11-07 10:56:12 -05:00
css: 'font-size: 40px;',
}),
],
}),
}),
});
return workspace;
};
2024-01-13 11:15:08 -05:00
export const WorkspaceRow = (class_name: string, i: number) => {
const addWorkspace = Workspace(
2023-12-23 01:14:21 -05:00
class_name === 'special' ? -1 : 1000,
class_name === 'special' ? 'special' : '',
false,
);
return Revealer({
transition: 'slide_down',
2023-12-23 01:14:21 -05:00
hpack: class_name === 'special' ? 'fill' : 'start',
setup: (self) => {
self.hook(Hyprland, (rev) => {
const minId = i * VARS.WORKSPACE_PER_ROW;
const activeId = Hyprland.active.workspace.id;
const rowExists = Hyprland.workspaces.some((ws) => {
const isInRow = ws.id > minId;
const hasClients = ws.windows > 0;
const isActive = ws.id === activeId;
return isInRow && (hasClients || isActive);
});
2023-12-23 01:14:21 -05:00
rev.reveal_child = rowExists;
});
},
child: CenterBox({
2023-12-23 01:14:21 -05:00
center_widget: EventBox({
setup: (self) => {
self.hook(Hyprland, () => {
const maxId = (i + 1) * VARS.WORKSPACE_PER_ROW;
const activeId = Hyprland.active.workspace.id;
2023-12-23 01:14:21 -05:00
const isSpecial = class_name === 'special';
const nextRowExists = Hyprland.workspaces.some((ws) => {
const isInNextRow = ws.id > maxId;
const hasClients = ws.windows > 0;
const isActive = ws.id === activeId;
return isInNextRow && (hasClients || isActive);
});
2023-12-23 01:14:21 -05:00
addWorkspace.reveal_child = isSpecial || !nextRowExists;
});
},
child: Box({
2023-12-23 01:14:21 -05:00
class_name,
children: [addWorkspace],
}),
2023-12-23 01:14:21 -05:00
}),
}),
});
};
2024-01-13 11:15:08 -05:00
export const updateWorkspaces = (box: AgsBox) => {
Hyprland.workspaces.forEach((ws) => {
2024-01-13 11:15:08 -05:00
const currentWs = (box.attribute.workspaces as Array<AgsRevealer>).find(
2023-12-23 01:14:21 -05:00
(ch) => ch.attribute.id === ws.id,
);
if (!currentWs) {
let type = 0;
let rowNo = 0;
if (ws.id < 0) {
// This means it's a special workspace
type = 1;
}
else {
rowNo = Math.floor((ws.id - 1) / VARS.WORKSPACE_PER_ROW);
2024-01-13 11:15:08 -05:00
const wsRow = box.children[type] as AgsBox;
const wsQty = wsRow.children.length;
if (rowNo >= wsQty) {
for (let i = wsQty; i <= rowNo; ++i) {
2024-01-13 11:15:08 -05:00
wsRow.add(WorkspaceRow(
type ? 'special' : 'normal', i,
));
}
}
}
2024-01-13 11:15:08 -05:00
const row = ((((box.children[type] as AgsBox)
.children[rowNo] as AgsRevealer)
.child as AgsCenterBox)
.center_widget as AgsEventBox)
.child as AgsBox;
row.add(Workspace(ws.id, type ? ws.name : ''));
}
});
// Make sure the order is correct
2023-12-23 01:14:21 -05:00
box.attribute.workspaces.forEach(
2024-01-13 11:15:08 -05:00
(workspace: AgsRevealer, i: number) => {
(workspace?.get_parent() as AgsBox)
?.reorder_child(workspace, i);
2023-12-23 01:14:21 -05:00
},
);
2023-11-07 10:56:12 -05:00
box.show_all();
};