Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions contents/code/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -238,27 +241,35 @@ 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)
{
debug("Desktop switched to the right - ignoring");
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;
Expand Down
5 changes: 5 additions & 0 deletions contents/config/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
<entry name="keepEmptyMiddleDesktops" type="Bool">
<default>false</default>
</entry>
<entry name="minDesktops" type="UInt">
<default>2</default>
<min>2</min> <!-- One occupied desktop plus one empty dynamic desktop at the end -->
<max>20</max> <!-- KDE Plasma has a built-in limit of 20 virtual desktops -->
</entry>
</group>

</kcfg>
18 changes: 18 additions & 0 deletions contents/ui/config.ui
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="minDesktopsLabel">
<property name="text">
<string>Minimal number of desktops:</string>
</property>
<property name="buddy">
<cstring>kcfg_minDesktops</cstring>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="kcfg_minDesktops">
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
Expand Down