Made unoccupied workspaces setting more compaitble with # of workspaces assigned. (#192)

* Made unoccupied workspaces setting more compaitble with # of workspaces assigned.

* Updated monitor listings

* Fix scenario where workspace rules aren't defined.

* Refactor for clean up and use workspace names instead of loop index.
This commit is contained in:
Jas Singh
2024-08-25 22:32:00 -07:00
committed by GitHub
parent cedf94ff41
commit fc2b781673
2 changed files with 69 additions and 19 deletions

View File

@@ -16,7 +16,10 @@ export const getWorkspacesForMonitor = (curWs: number, wsRules: WorkspaceMap, mo
} }
const monitorMap = {}; const monitorMap = {};
hyprland.monitors.forEach((m) => (monitorMap[m.id] = m.name)); const workspaceMonitorList = hyprland?.workspaces?.map(m => ({ id: m.monitorID, name: m.monitor }));
const monitors = [...new Map([...workspaceMonitorList, ...hyprland.monitors].map(item => [item.id, item])).values()];
monitors.forEach((m) => (monitorMap[m.id] = m.name));
const currentMonitorName = monitorMap[monitor]; const currentMonitorName = monitorMap[monitor];
const monitorWSRules = wsRules[currentMonitorName]; const monitorWSRules = wsRules[currentMonitorName];
@@ -35,7 +38,10 @@ export const getWorkspaceRules = (): WorkspaceMap => {
JSON.parse(rules).forEach((rule: WorkspaceRule, index: number) => { JSON.parse(rules).forEach((rule: WorkspaceRule, index: number) => {
if (Object.hasOwnProperty.call(workspaceRules, rule.monitor)) { if (Object.hasOwnProperty.call(workspaceRules, rule.monitor)) {
workspaceRules[rule.monitor].push(index + 1); const workspaceNum = parseInt(rule.workspaceString, 10);
if (!isNaN(workspaceNum)) {
workspaceRules[rule.monitor].push(workspaceNum);
}
} else { } else {
workspaceRules[rule.monitor] = [index + 1]; workspaceRules[rule.monitor] = [index + 1];
} }

View File

@@ -1,6 +1,7 @@
const hyprland = await Service.import("hyprland"); const hyprland = await Service.import("hyprland");
import options from "options"; import options from "options";
import { createThrottledScrollHandlers, getCurrentMonitorWorkspaces, getWorkspaceRules, getWorkspacesForMonitor } from "./helpers"; import { createThrottledScrollHandlers, getCurrentMonitorWorkspaces, getWorkspaceRules, getWorkspacesForMonitor } from "./helpers";
import { Workspace } from "types/service/hyprland";
const { const {
workspaces, workspaces,
@@ -56,8 +57,8 @@ const Workspaces = (monitor = -1) => {
const defaultWses = () => { const defaultWses = () => {
return Widget.Box({ return Widget.Box({
children: Utils.merge( children: Utils.merge(
[workspaces.bind(), monitorSpecific.bind()], [workspaces.bind("value"), monitorSpecific.bind()],
(workspaces, monitorSpecific) => { (workspaces: number, monitorSpecific: boolean) => {
return range(workspaces || 8) return range(workspaces || 8)
.filter((i) => { .filter((i) => {
if (!monitorSpecific) { if (!monitorSpecific) {
@@ -66,6 +67,9 @@ const Workspaces = (monitor = -1) => {
const workspaceRules = getWorkspaceRules(); const workspaceRules = getWorkspaceRules();
return getWorkspacesForMonitor(i, workspaceRules, monitor); return getWorkspacesForMonitor(i, workspaceRules, monitor);
}) })
.sort((a, b) => {
return a - b;
})
.map((i, index) => { .map((i, index) => {
return Widget.Button({ return Widget.Button({
class_name: "workspace-button", class_name: "workspace-button",
@@ -87,16 +91,16 @@ const Workspaces = (monitor = -1) => {
options.bar.workspaces.icons.occupied.bind("value"), options.bar.workspaces.icons.occupied.bind("value"),
hyprland.active.workspace.bind("id") hyprland.active.workspace.bind("id")
], ],
(show_icons, show_numbered, numbered_active_indicator) => { (showIcons: boolean, showNumbered: boolean, numberedActiveIndicator: string) => {
if (show_icons) { if (showIcons) {
return `workspace-icon txt-icon bar`; return `workspace-icon txt-icon bar`;
} }
if (show_numbered) { if (showNumbered) {
const numActiveInd = hyprland.active.workspace.id === i const numActiveInd = hyprland.active.workspace.id === i
? numbered_active_indicator ? numberedActiveIndicator
: ""; : "";
return `workspace-number can_${numbered_active_indicator} ${numActiveInd}`; return `workspace-number can_${numberedActiveIndicator} ${numActiveInd}`;
} }
return "default"; return "default";
}, },
@@ -110,7 +114,7 @@ const Workspaces = (monitor = -1) => {
workspaceMask.bind("value"), workspaceMask.bind("value"),
hyprland.active.workspace.bind("id") hyprland.active.workspace.bind("id")
], ],
(showIcons, available, active, occupied, workspaceMask, _) => { (showIcons: boolean, available: string, active: string, occupied: string, workspaceMask: boolean, _: number) => {
if (showIcons) { if (showIcons) {
if (hyprland.active.workspace.id === i) { if (hyprland.active.workspace.id === i) {
return active; return active;
@@ -155,6 +159,7 @@ const Workspaces = (monitor = -1) => {
monitorSpecific.bind("value"), monitorSpecific.bind("value"),
hyprland.bind("workspaces"), hyprland.bind("workspaces"),
workspaceMask.bind("value"), workspaceMask.bind("value"),
workspaces.bind("value"),
options.bar.workspaces.show_icons.bind("value"), options.bar.workspaces.show_icons.bind("value"),
options.bar.workspaces.icons.available.bind("value"), options.bar.workspaces.icons.available.bind("value"),
options.bar.workspaces.icons.active.bind("value"), options.bar.workspaces.icons.active.bind("value"),
@@ -164,16 +169,55 @@ const Workspaces = (monitor = -1) => {
spacing.bind("value"), spacing.bind("value"),
hyprland.active.workspace.bind("id"), hyprland.active.workspace.bind("id"),
], ],
(monitorSpecific, wkSpaces, workspaceMask, showIcons, available, active, occupied, showNumbered, numberedActiveIndicator, spacing, activeId) => { (
const activeWorkspaces = wkSpaces.map(w => w.id); monitorSpecific: boolean,
return activeWorkspaces wkSpaces: Workspace[],
.filter((i) => { workspaceMask: boolean,
if (monitorSpecific === false) { totalWkspcs: number,
return true; showIcons: boolean,
} available: string,
active: string,
occupied: string,
showNumbered: boolean,
numberedActiveIndicator: string,
spacing: number,
activeId: number,
) => {
let allWkspcs = range(totalWkspcs || 8);
const isOnMonitor = hyprland.workspaces.find(w => w.id === i)?.monitorID === monitor; const activeWorkspaces = wkSpaces.map(w => w.id);
return isOnMonitor; const workspaceRules = getWorkspaceRules();
// Sometimes hyprland doesn't have all the monitors in the list
// so we complement it with monitors from the workspace list
const workspaceMonitorList = hyprland?.workspaces?.map(m => ({ id: m.monitorID, name: m.monitor }));
const curMonitor = hyprland.monitors.find(m => m.id === monitor)
|| workspaceMonitorList.find(m => m.id === monitor);
// go through each key in workspaceRules and flatten the array
const workspacesWithRules = Object.keys(workspaceRules).reduce((acc: number[], k: string) => {
return [...acc, ...workspaceRules[k]];
}, [] as number[]);
const activesForMonitor = activeWorkspaces.filter(w => {
if (curMonitor && Object.hasOwnProperty.call(workspaceRules, curMonitor.name) && workspacesWithRules.includes(w)) {
return workspaceRules[curMonitor.name].includes(w);
}
return true;
});
if (monitorSpecific) {
const wrkspcsInRange = range(totalWkspcs).filter(w => {
return getWorkspacesForMonitor(w, workspaceRules, monitor);
});
allWkspcs = [...new Set([...activesForMonitor, ...wrkspcsInRange])];
} else {
allWkspcs = [...new Set([...allWkspcs, ...activeWorkspaces])];
}
return allWkspcs
.sort((a, b) => {
return a - b;
}) })
.map((i, index) => { .map((i, index) => {
return Widget.Button({ return Widget.Button({