// SimpleDwindle
// KWin 6.8 - Minimal chronological Dwindle
-const VERSION = "0.5.2";
+const VERSION = "0.6.2";
// ------------------------------------------------------------
// Configuration
// 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 = [
parent: parent,
first: null,
second: null,
- vertical: false
+ vertical: false,
+ ratio: 0.5
};
}
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);
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);
}
// Geometry
// ------------------------------------------------------------
-function applyNode(node, rect, rootWide = false, isRoot = true) {
+function applyNode(node, rect, rootWide = false, isRoot = true, skipWindow = null) {
if (!node)
return;
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,
}
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;
layout.desktop
);
- applyNode(layout.root, area, layout.rootWide);
+ applyNode(layout.root, area, layout.rootWide, true, skipWindow);
}
// ------------------------------------------------------------
}
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
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
}
}
+// ------------------------------------------------------------
+// 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
// ------------------------------------------------------------
handleInteractiveStart(w);
});
+ w.interactiveMoveResizeStepped.connect(function() {
+ handleInteractiveStep(w);
+ });
+
w.interactiveMoveResizeFinished.connect(function() {
handleInteractiveFinish(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);
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);
}
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"); });