]> Freerunner's - simpledwindle.git/commitdiff
Add the possibility to resize tiled windows
authorAndre Ramnitz <andre.ramnitz@mail.de>
Wed, 21 Oct 2026 16:52:35 +0000 (18:52 +0200)
committerAndre Ramnitz <andre.ramnitz@mail.de>
Thu, 22 Oct 2026 17:39:08 +0000 (19:39 +0200)
Rework the resizing logic

*) resizing with a key-combo:
Keyboard resize follows Dwindle order: prefer the next subtree.
If no forward split exists on the requested axis, use the closest
preceding split. The direction key moves the boundary, not the focus.

*) resizing with the mouse:
windows on both sides of the split react to the drag and adjust
their size simultaneously
During an interactive resize KWin owns the dragged window.
Update its siblings live without fighting the pointer.

add equalize branch function

README
contents/code/main.js
metadata.json

diff --git a/README b/README
index 2f5128e..e20ffa5 100644 (file)
--- a/README
+++ b/README
@@ -10,9 +10,15 @@ A KWin-script to implement a simple tiling algorithm in kwin
 Default Keybinds:
 ===================
 Meta-Shift-R: alternate between 50:50 and 67:33 horiz. ratio
+Meta-Shift-E: equalize the split ratio in the current branch
 
 Meta-Shift-H: swap tile with nearest neighbour to the left
 Meta-Shift-J: swap tile with nearest neighbour on top
 Meta-Shift-K: swap tile with nearest neighbour below
 Meta-Shift-L: swap tile with nearest neighbour to the right
 
+Meta-Ctrl-H: resize focused window and its direct neighbour horizontally
+Meta-Ctrl-J: resize focused window and its direct neighbour vertically
+Meta-Ctrl-K: resize focused window and its direct neighbour vertically
+Meta-Ctrl-L: resize focused window and its direct neighbour horizontally
+
index 01e1811..ce67c88 100644 (file)
@@ -9,7 +9,7 @@
 
 // SimpleDwindle
 // KWin 6.8 - Minimal chronological Dwindle
-const VERSION = "0.5.2";
+const VERSION = "0.6.2";
 
 // ------------------------------------------------------------
 // Configuration
@@ -20,6 +20,8 @@ const VERSION = "0.5.2";
 //       title => caption
 
 const GAP = 8;
+const RESIZE_STEP = 0.05;
+const MIN_RATIO = 0.15;
 
 // Floating by default, manually tileable via Meta+S
 const FLOATING_CLASSES = [
@@ -166,7 +168,8 @@ function leaf(w, parent = null) {
         parent: parent,
         first: null,
         second: null,
-        vertical: false
+        vertical: false,
+        ratio: 0.5
     };
 }
 
@@ -219,6 +222,7 @@ function insertWindow(layout, w) {
 
     target.window = null;
     target.vertical = (d % 2 === 0);
+    target.ratio = d === 0 && layout.rootWide ? 2 / 3 : 0.5;
 
     target.first = leaf(oldWindow, target);
     target.second = leaf(w, target);
@@ -281,6 +285,8 @@ function normalizeSplits(node, depth = 0) {
         return;
 
     node.vertical = (depth % 2 === 0);
+    if (typeof node.ratio !== "number")
+        node.ratio = 0.5;
     normalizeSplits(node.first, depth + 1);
     normalizeSplits(node.second, depth + 1);
 }
@@ -289,7 +295,7 @@ function normalizeSplits(node, depth = 0) {
 // Geometry
 // ------------------------------------------------------------
 
-function applyNode(node, rect, rootWide = false, isRoot = true) {
+function applyNode(node, rect, rootWide = false, isRoot = true, skipWindow = null) {
     if (!node)
         return;
 
@@ -326,6 +332,11 @@ function applyNode(node, rect, rootWide = false, isRoot = true) {
         w.keepAbove = false;
         w.keepBelow = true;
 
+        // During an interactive resize KWin owns the dragged window.
+        // Update its siblings live without fighting the pointer.
+        if (w === skipWindow)
+            return;
+
         w.frameGeometry = {
             x: x,
             y: y,
@@ -337,41 +348,41 @@ function applyNode(node, rect, rootWide = false, isRoot = true) {
     }
 
     if (node.vertical) {
-        const half = rect.width * (isRoot && rootWide ? 2 / 3 : 1 / 2);
+        const half = rect.width * node.ratio;
 
         applyNode(node.first, {
             x: rect.x,
             y: rect.y,
             width: half,
             height: rect.height
-        }, rootWide, false);
+        }, rootWide, false, skipWindow);
 
         applyNode(node.second, {
             x: rect.x + half,
             y: rect.y,
             width: rect.width - half,
             height: rect.height
-        }, rootWide, false);
+        }, rootWide, false, skipWindow);
     } else {
-        const half = rect.height / 2;
+        const half = rect.height * node.ratio;
 
         applyNode(node.first, {
             x: rect.x,
             y: rect.y,
             width: rect.width,
             height: half
-        }, rootWide, false);
+        }, rootWide, false, skipWindow);
 
         applyNode(node.second, {
             x: rect.x,
             y: rect.y + half,
             width: rect.width,
             height: rect.height - half
-        }, rootWide, false);
+        }, rootWide, false, skipWindow);
     }
 }
 
-function arrange(layout) {
+function arrange(layout, skipWindow = null) {
     if (!layout || !layout.root)
         return;
 
@@ -381,7 +392,7 @@ function arrange(layout) {
         layout.desktop
     );
 
-    applyNode(layout.root, area, layout.rootWide);
+    applyNode(layout.root, area, layout.rootWide, true, skipWindow);
 }
 
 // ------------------------------------------------------------
@@ -399,7 +410,7 @@ function collectLeaves(node, rect, result, rootWide = false, isRoot = true) {
     }
 
     if (node.vertical) {
-        const half = rect.width * (isRoot && rootWide ? 2 / 3 : 1 / 2);
+        const half = rect.width * node.ratio;
         collectLeaves(node.first, {
             x: rect.x, y: rect.y,
             width: half, height: rect.height
@@ -409,7 +420,7 @@ function collectLeaves(node, rect, result, rootWide = false, isRoot = true) {
             width: rect.width - half, height: rect.height
         }, result, rootWide, false);
     } else {
-        const half = rect.height / 2;
+        const half = rect.height * node.ratio;
         collectLeaves(node.first, {
             x: rect.x, y: rect.y,
             width: rect.width, height: half
@@ -646,6 +657,128 @@ function handleMinimized(w) {
     }
 }
 
+// ------------------------------------------------------------
+// Resize: split ratios are stored on internal tree nodes.
+// ------------------------------------------------------------
+
+function clampRatio(value) {
+    return Math.max(MIN_RATIO, Math.min(1 - MIN_RATIO, value));
+}
+
+function nodeRect(layout, wanted) {
+    const area = workspace.clientArea(KWin.MaximizeArea, layout.output, layout.desktop);
+    let result = null;
+    function visit(node, rect) {
+        if (!node || result) return;
+        if (node === wanted) { result = rect; return; }
+        if (node.window) return;
+        const r = node.ratio;
+        if (node.vertical) {
+            const width = rect.width * r;
+            visit(node.first, {x: rect.x, y: rect.y, width: width, height: rect.height});
+            visit(node.second, {x: rect.x + width, y: rect.y, width: rect.width - width, height: rect.height});
+        } else {
+            const height = rect.height * r;
+            visit(node.first, {x: rect.x, y: rect.y, width: rect.width, height: height});
+            visit(node.second, {x: rect.x, y: rect.y + height, width: rect.width, height: rect.height - height});
+        }
+    }
+    visit(layout.root, area);
+    return result;
+}
+
+// Find nearest split with a border of the selected window on that side.
+function borderSplit(w, side) {
+    const layout = findWindowLayout(w);
+    const node = layout && findLeaf(layout.root, w);
+    if (!node) return null;
+    const vertical = side === "left" || side === "right";
+    let child = node;
+    while (child.parent) {
+        const parent = child.parent;
+        if (parent.vertical === vertical &&
+            ((side === "right" || side === "down") ? parent.first === child : parent.second === child))
+            return {layout: layout, split: parent};
+        child = parent;
+    }
+    return null;
+}
+
+// Keyboard resize follows Dwindle order: prefer the next subtree.
+// If no forward split exists on the requested axis, use the closest
+// preceding split. The direction key moves the boundary, not the focus.
+function keyboardSplit(w, side) {
+    const layout = findWindowLayout(w);
+    const node = layout && findLeaf(layout.root, w);
+    if (!node) return null;
+
+    const vertical = side === "left" || side === "right";
+    let child = node;
+    let fallback = null;
+    while (child.parent) {
+        const parent = child.parent;
+        if (parent.vertical === vertical) {
+            if (parent.first === child)
+                return {layout: layout, split: parent};
+            if (!fallback)
+                fallback = {layout: layout, split: parent};
+        }
+        child = parent;
+    }
+    return fallback;
+}
+
+function adjustSplit(hit, pixels, activeResizeWindow = null) {
+    if (!hit) return false;
+    const rect = nodeRect(hit.layout, hit.split);
+    if (!rect) return false;
+    const extent = hit.split.vertical ? rect.width : rect.height;
+    if (extent <= 0) return false;
+    hit.split.ratio = clampRatio(hit.split.ratio + pixels / extent);
+    if (hit.split === hit.layout.root)
+        hit.layout.rootWide = Math.abs(hit.split.ratio - 2 / 3) < 0.001;
+    arrange(hit.layout, activeResizeWindow);
+    return true;
+}
+
+function resizeBorder(w, side, pixels, activeResizeWindow = null) {
+    return adjustSplit(borderSplit(w, side), pixels, activeResizeWindow);
+}
+
+function keyboardResize(side) {
+    const w = workspace.activeWindow;
+    if (!w || !contains(managed, w) || w.fullScreen || w.minimized) return;
+    const hit = keyboardSplit(w, side);
+    if (!hit) return;
+    const rect = nodeRect(hit.layout, hit.split);
+    if (!rect) return;
+    const extent = hit.split.vertical ? rect.width : rect.height;
+    const sign = (side === "left" || side === "up") ? -1 : 1;
+    adjustSplit(hit, sign * extent * RESIZE_STEP);
+}
+
+// Equalize the nearest parent split, regardless of orientation.
+function equalizeFocusedSplit() {
+    const w = workspace.activeWindow;
+    if (!w || !contains(managed, w) || w.fullScreen || w.minimized)
+        return;
+
+    const layout = findWindowLayout(w);
+    const node = layout && findLeaf(layout.root, w);
+    if (!node || !node.parent)
+        return;
+
+    const split = node.parent;
+    split.ratio = 0.5;
+
+    // Keep the root toggle consistent with the actual geometry.
+    if (split === layout.root)
+        layout.rootWide = false;
+
+    arrange(layout);
+    log("EQUALIZED: " + w.caption);
+}
+
 // ------------------------------------------------------------
 // Window lifecycle
 // ------------------------------------------------------------
@@ -676,6 +809,10 @@ function watchWindow(w) {
         handleInteractiveStart(w);
     });
 
+    w.interactiveMoveResizeStepped.connect(function() {
+        handleInteractiveStep(w);
+    });
+
     w.interactiveMoveResizeFinished.connect(function() {
         handleInteractiveFinish(w);
     });
@@ -773,10 +910,43 @@ function handleInteractiveStart(w) {
 
     const r = w.frameGeometry;
     interactiveMoves.set(w, {
-        x: r.x, y: r.y, width: r.width, height: r.height
+        x: r.x, y: r.y, width: r.width, height: r.height,
+        last: {x: r.x, y: r.y, width: r.width, height: r.height},
+        resizedAny: false
     });
 }
 
+// Apply incremental pointer-driven changes to neighboring tiles.
+// Never reassign the geometry of the actively dragged window mid-drag.
+function applyDragDelta(w, before, after, skipWindow) {
+    const changes = [
+        {side: "left", delta: after.x - before.x},
+        {side: "right", delta: after.x + after.width - before.x - before.width},
+        {side: "up", delta: after.y - before.y},
+        {side: "down", delta: after.y + after.height - before.y - before.height}
+    ];
+    let adjusted = false;
+    for (const change of changes) {
+        if (Math.abs(change.delta) <= 1) continue;
+        adjusted = resizeBorder(w, change.side, change.delta, skipWindow) || adjusted;
+    }
+    return adjusted;
+}
+
+function handleInteractiveStep(w) {
+    const state = interactiveMoves.get(w);
+    if (!state || !contains(managed, w)) return;
+
+    const now = w.frameGeometry;
+    const resized = Math.abs(now.width - state.last.width) > 1 ||
+                    Math.abs(now.height - state.last.height) > 1;
+    if (!resized) return;
+
+    if (applyDragDelta(w, state.last, now, w))
+        state.resizedAny = true;
+    state.last = {x: now.x, y: now.y, width: now.width, height: now.height};
+}
+
 function handleInteractiveFinish(w) {
     const before = interactiveMoves.get(w);
     interactiveMoves.delete(w);
@@ -790,8 +960,13 @@ function handleInteractiveFinish(w) {
     const resized = Math.abs(after.width - before.width) > 1 ||
                     Math.abs(after.height - before.height) > 1;
 
-    // Do not interpret an interactive resize as a move-to-floating.
-    if (moved && !resized) {
+    if (resized || before.resizedAny) {
+        // Apply only the remaining delta since the last live update.
+        // Reapplying the full drag would double-count the resize.
+        applyDragDelta(w, before.last, after, w);
+        arrange(findWindowLayout(w));
+        log("RESIZED: " + w.caption);
+    } else if (moved) {
         floatWindow(w);
         log("DRAGGED/FLOATING: " + w.caption);
     }
@@ -831,11 +1006,20 @@ registerShortcut(
             return;
 
         layout.rootWide = !layout.rootWide;
+        if (layout.root && !layout.root.window)
+            layout.root.ratio = layout.rootWide ? 2 / 3 : 0.5;
         arrange(layout);
         log("ROOT RATIO: " + (layout.rootWide ? "66.7:33.3" : "50:50"));
     }
 );
 
+registerShortcut("SimpleDwindleResizeLeft", "SimpleDwindle: Resize Left", "Meta+Ctrl+H", function() { keyboardResize("left"); });
+registerShortcut("SimpleDwindleResizeDown", "SimpleDwindle: Resize Down", "Meta+Ctrl+J", function() { keyboardResize("down"); });
+registerShortcut("SimpleDwindleResizeUp", "SimpleDwindle: Resize Up", "Meta+Ctrl+K", function() { keyboardResize("up"); });
+registerShortcut("SimpleDwindleResizeRight", "SimpleDwindle: Resize Right", "Meta+Ctrl+L", function() { keyboardResize("right"); });
+
+registerShortcut("SimpleDwindleEqualize", "SimpleDwindle: Equalize Split", "Meta+Shift+E", equalizeFocusedSplit);
+
 registerShortcut("SimpleDwindleSwapLeft", "SimpleDwindle: Swap Left", "Meta+Shift+H", function() { swapDirection("left"); });
 registerShortcut("SimpleDwindleSwapDown", "SimpleDwindle: Swap Down", "Meta+Shift+J", function() { swapDirection("down"); });
 registerShortcut("SimpleDwindleSwapUp", "SimpleDwindle: Swap Up", "Meta+Shift+K", function() { swapDirection("up"); });
index 32b7a83..c1aa8fd 100644 (file)
@@ -3,7 +3,7 @@
     "Id": "simpledwindle",
     "Name": "SimpleDwindle",
     "Description": "Minimal tiling and floating window manager",
-    "Version": "0.5.2",
+    "Version": "0.6.2",
     "License": "GPLv3"
   },
   "X-Plasma-API": "javascript",