diff --git a/contents/code/main.js b/contents/code/main.js index bed285b..a016d17 100644 --- a/contents/code/main.js +++ b/contents/code/main.js @@ -5,8 +5,6 @@ // Desktop numbers are from zero (unlike how it worked in plasma 5) - -const MIN_DESKTOPS = 2; const LOG_LEVEL = 2; // 0 trace, 1 debug, 2 info @@ -98,6 +96,11 @@ const compat = isKde6 /***** Logic definition *****/ +// Wrappers for reading config values +// This keeps default values in one place, +// which are also needed for readConfig to correctly derive the type +function getMinDesktops() {return readConfig("minDesktops", 2);} +function getKeepEmptyMiddleDesktops() {return readConfig("keepEmptyMiddleDesktops", false);} // shifts a window to the left if it's more to the right than number function shiftRighterThan(client, number) @@ -146,7 +149,7 @@ function removeDesktop(number) debug("Not removing desktop at end"); return false; } - if (desktopsLength <= MIN_DESKTOPS) + if (desktopsLength <= getMinDesktops()) { debug("Not removing desktop, too few left"); return false; @@ -238,7 +241,6 @@ function onDesktopSwitch(oldDesktop) const oldDesktopIndex = compat.findDesktop(allDesktops, compat.toDesktop(oldDesktop)); const currentDesktopIndex = compat.findDesktop(allDesktops, compat.toDesktop(workspace.currentDesktop)); const getDesktopsLength = () => compat.workspaceDesktops().length; - const keepEmptyMiddleDesktops = readConfig("keepEmptyMiddleDesktops", false); if (oldDesktopIndex <= currentDesktopIndex) { @@ -246,19 +248,28 @@ function onDesktopSwitch(oldDesktop) return; } + // Calculate the index of the last desktop we want to preserve when cleaning up. + // We need to preserve: + // 1. The current desktop (currentDesktopIndex) + // 2. At least MIN_DESKTOPS. Note we actually subtract two because: + // - We always have the dynamic empty desktop at the end + // - MIN_DESKTOPS is a count, but we need an index + const preserveUpToIndex = Math.max(currentDesktopIndex, getMinDesktops() - 2); + // Loop through desktops right-to-left and delete empty ones: // - Starts from second-to-last desktop (preserving always one empty desktop at the end) // - Stops before reaching current desktop (preserves what user is viewing) - // - Extra check (desktopIdx > 0) prevents an infinite loop caused by abnormal conditions - // In case of interference with other plugins, we'll only examine as many desktops as we initially detect - for (let desktopIdx = getDesktopsLength() - 2; desktopIdx > currentDesktopIndex && desktopIdx > 0; --desktopIdx) + // - Stops before reaching minimum number of desktops + // To prevent an infinite loop caused by abnormal conditions (e.g. interference with other plugins), + // we only examine as many desktops as we initially detect. + for (let desktopIdx = getDesktopsLength() - 2; desktopIdx > preserveUpToIndex; --desktopIdx) { debug(`Examine desktop ${desktopIdx}`); if (isEmptyDesktop(desktopIdx)) { removeDesktop(desktopIdx); } - else if (keepEmptyMiddleDesktops) + else if (getKeepEmptyMiddleDesktops()) { debug("Found non-empty desktop, stopping purge"); break; diff --git a/contents/config/main.xml b/contents/config/main.xml index 6adec06..2198f11 100644 --- a/contents/config/main.xml +++ b/contents/config/main.xml @@ -9,6 +9,11 @@ false + + 2 + 2 + 20 + \ No newline at end of file diff --git a/contents/ui/config.ui b/contents/ui/config.ui index e586aa9..57c299b 100644 --- a/contents/ui/config.ui +++ b/contents/ui/config.ui @@ -21,6 +21,24 @@ + + + + + + Minimal number of desktops: + + + kcfg_minDesktops + + + + + + + + +