// SimpleDwindle
// KWin 6.8 - Minimal chronological Dwindle
-const VERSION = "0.6.2";
+const VERSION = "0.7.0";
// ------------------------------------------------------------
// Configuration
const suspended = [];
const watched = [];
const interactiveMoves = new Map();
+const multiDesktop = [];
+const quarantinedLayouts = new Set();
+const SYNC_DELAY_MS = 50;
+let syncSerial = 0;
+let pendingSync = null;
+let syncTimer = null;
+
// ------------------------------------------------------------
// Helpers
return matchesClass(w, IGNORED_CLASSES);
}
+function isMultiDesktop(w) {
+ // KWin uses an empty desktop list for "on all desktops".
+ return !w.desktops || w.desktops.length !== 1;
+}
+
function isForcedFloating(w) {
return matchesClass(w, FLOATING_CLASSES);
}
// ------------------------------------------------------------
function layoutFor(w) {
- const desktop = w.desktops.length
- ? w.desktops[0]
- : workspace.currentDesktop;
+ const desktop = w.desktops[0];
let layout = layouts.find(l =>
l.output === w.output &&
}
function arrange(layout, skipWindow = null) {
- if (!layout || !layout.root)
+ if (!layout || quarantinedLayouts.has(layout) || !layout.root)
return;
const area = workspace.clientArea(
if (!isCandidate(w) ||
isIgnored(w) ||
contains(managed, w) ||
- w.minimized)
+ w.minimized ||
+ isMultiDesktop(w) ||
+ contains(multiDesktop, w))
return;
removeFromList(floating, w);
managed.push(w);
const layout = layoutFor(w);
+ if (quarantinedLayouts.has(layout)) {
+ removeFromList(managed, w);
+ if (!contains(floating, w)) floating.push(w);
+ w.keepBelow = false;
+ log("DEGRADED/FLOATING: quarantined layout " + w.caption);
+ return;
+ }
insertWindow(layout, w);
function toggleFloating() {
const w = workspace.activeWindow;
- if (!isCandidate(w) || isIgnored(w))
+ if (!isCandidate(w) || isIgnored(w) || isMultiDesktop(w) ||
+ contains(multiDesktop, w))
return;
if (contains(managed, w)) {
removeFromList(suspended, w);
- tileWindow(w);
+ if (isMultiDesktop(w)) enterMultiDesktop(w);
+ else tileWindow(w);
log("RESTORED: " + w.caption);
}
});
w.desktopsChanged.connect(function() {
- handleWindowLocationChanged(w);
+ handleWindowLocationChanged(w, "desktopsChanged");
});
w.outputChanged.connect(function() {
- handleWindowLocationChanged(w);
+ handleWindowLocationChanged(w, "outputChanged");
});
w.interactiveMoveResizeStarted.connect(function() {
watchWindow(w);
+ if (isMultiDesktop(w)) {
+ enterMultiDesktop(w);
+ return;
+ }
+
if (isForcedFloating(w)) {
floatWindow(w);
return;
tileWindow(w);
}
-// Reconcile a tiled window after KWin moves it to another desktop/output.
-// The old tree is identified by membership, not by the window's new desktop.
-function handleWindowLocationChanged(w) {
- if (!contains(managed, w))
- return;
-
- const oldLayout = findWindowLayout(w);
- if (!oldLayout)
- return;
+// KWin can emit both signals for one operation. Reconcile once after
+// the 50 ms quiet period and give all related log entries one sync ID.
+function scheduleSync(eventName, w) {
+ if (!pendingSync) {
+ pendingSync = { id: ++syncSerial, events: new Set() };
+ }
+ const sync = pendingSync;
+ sync.events.add(w);
+ log(`[SYNC:${sync.id}] EVENT ${eventName} window=${w.caption}`);
+
+ if (syncTimer !== null && typeof clearTimeout === "function")
+ clearTimeout(syncTimer);
+
+ if (typeof setTimeout === "function") {
+ syncTimer = setTimeout(function() {
+ if (pendingSync !== sync) return;
+ pendingSync = null;
+ syncTimer = null;
+ reconcileLayouts(sync);
+ }, SYNC_DELAY_MS);
+ } else {
+ // Some KWin scripting engines do not expose browser timers.
+ // Stay functional rather than failing to load the entire script.
+ pendingSync = null;
+ syncTimer = null;
+ reconcileLayouts(sync);
+ }
+}
- const desktop = w.desktops.length
- ? w.desktops[0]
- : workspace.currentDesktop;
+function handleWindowLocationChanged(w, eventName) {
+ if (isCandidate(w) && !isIgnored(w))
+ scheduleSync(eventName, w);
+}
- if (oldLayout.desktop === desktop && oldLayout.output === w.output)
- return;
+function countLeaves(node, found, seenNodes = new Set()) {
+ if (!node) return;
+ if (seenNodes.has(node)) throw new Error("cycle in layout tree");
+ seenNodes.add(node);
+ if (node.window) {
+ found.push(node.window);
+ } else {
+ if (!node.first || !node.second)
+ throw new Error("incomplete split");
+ countLeaves(node.first, found, seenNodes);
+ countLeaves(node.second, found, seenNodes);
+ }
+}
- removeNode(oldLayout, w);
- normalizeSplits(oldLayout.root);
- arrange(oldLayout);
+function centerFloating(w) {
+ if (w.fullScreen) return;
+ try {
+ const area = workspace.clientArea(KWin.MaximizeArea, w.output, workspace.currentDesktop);
+ const original = w.frameGeometry;
+ // One size request only. KWin/application may clamp or ignore it.
+ const requested = {
+ x: original.x, y: original.y,
+ width: Math.min(1980, area.width),
+ height: Math.min(1080, area.height)
+ };
+ w.frameGeometry = requested;
+ // Read back the geometry KWin actually accepted.
+ const actual = w.frameGeometry;
+ w.frameGeometry = {
+ x: Math.round(area.x + (area.width - actual.width) / 2),
+ y: Math.round(area.y + (area.height - actual.height) / 2),
+ width: actual.width, height: actual.height
+ };
+ } catch (e) {
+ log("CENTER WARNING: " + w.caption + " " + e);
+ }
+}
- // Only join a destination that already has a tiled tree.
- const target = layouts.find(l =>
- l.desktop === desktop &&
- l.output === w.output &&
- l.root !== null
- );
+function enterMultiDesktop(w) {
+ const wasTiled = contains(managed, w) || contains(suspended, w);
+ if (contains(managed, w)) detachWindow(w);
+ removeFromList(suspended, w);
+ removeFromList(floating, w);
+ if (!contains(multiDesktop, w)) multiDesktop.push(w);
+ if (wasTiled) {
+ w.keepBelow = false;
+ if (!w.fullScreen) centerFloating(w);
+ log("MULTI/FLOATING: " + w.caption);
+ }
+}
- if (target) {
- insertWindow(target, w);
- normalizeSplits(target.root);
- arrange(target);
- log("MOVED/TILED: " + w.caption);
- } else {
- removeFromList(managed, w);
- removeFromList(suspended, w);
- if (!contains(floating, w))
- floating.push(w);
+function reconcileWindow(w) {
+ if (!isCandidate(w) || isIgnored(w)) return 0;
+ if (isMultiDesktop(w)) {
+ if (!contains(multiDesktop, w)) {
+ enterMultiDesktop(w);
+ return 1;
+ }
+ return 0;
+ }
+ if (contains(multiDesktop, w)) {
+ removeFromList(multiDesktop, w);
+ if (!contains(floating, w)) floating.push(w);
+ log("MULTI EXIT/FLOATING: " + w.caption);
+ return 1;
+ }
+ if (!contains(managed, w)) return 0;
+ const oldLayout = findWindowLayout(w);
+ const desktop = w.desktops[0];
+ if (oldLayout && oldLayout.desktop === desktop && oldLayout.output === w.output)
+ return 0;
+ if (oldLayout) {
+ removeNode(oldLayout, w);
+ normalizeSplits(oldLayout.root);
+ arrange(oldLayout);
+ }
+ const target = layoutFor(w);
+ insertWindow(target, w);
+ normalizeSplits(target.root);
+ arrange(target);
+ log("MOVED/TILED: " + w.caption);
+ return 1;
+}
- w.keepBelow = false;
- log("MOVED/FLOATING: " + w.caption);
+function reconcileLayouts(sync) {
+ let corrections = 0;
+ let status = "OK";
+ try {
+ for (const w of sync.events)
+ corrections += reconcileWindow(w);
+
+ // Validate the entire forest without rebuilding unaffected trees.
+ const owners = new Map();
+ for (const layout of layouts) {
+ if (quarantinedLayouts.has(layout)) continue;
+ const windows = [];
+ try {
+ countLeaves(layout.root, windows);
+ } catch (e) {
+ quarantinedLayouts.add(layout);
+ log(`[SYNC:${sync.id}] ERROR invalid tree: ${e}; layout quarantined`);
+ status = "DEGRADED";
+ continue;
+ }
+ for (const w of windows) {
+ if (!owners.has(w)) owners.set(w, []);
+ owners.get(w).push(layout);
+ }
+ }
+ for (const [w, locations] of owners) {
+ if (locations.length === 1 && contains(managed, w) &&
+ !isMultiDesktop(w) && locations[0].desktop === w.desktops[0] &&
+ locations[0].output === w.output)
+ continue;
+ log(`[SYNC:${sync.id}] WARN inconsistent membership window=${w.caption} count=${locations.length}`);
+ try {
+ for (const layout of locations) {
+ removeNode(layout, w);
+ normalizeSplits(layout.root);
+ arrange(layout);
+ }
+ removeFromList(managed, w);
+ removeFromList(suspended, w);
+ if (isMultiDesktop(w)) {
+ if (!contains(multiDesktop, w)) multiDesktop.push(w);
+ } else if (!contains(floating, w)) floating.push(w);
+ w.keepBelow = false;
+ corrections++;
+ log(`[SYNC:${sync.id}] REPAIR fallback floating window=${w.caption}`);
+ } catch (e) {
+ for (const layout of locations) quarantinedLayouts.add(layout);
+ status = "DEGRADED";
+ log(`[SYNC:${sync.id}] ERROR fallback failed window=${w.caption}: ${e}`);
+ }
+ }
+ if (status !== "DEGRADED" && corrections) status = "REPAIRED";
+ } catch (e) {
+ status = "DEGRADED";
+ log(`[SYNC:${sync.id}] ERROR ${e}`);
}
+ log(`[SYNC:${sync.id}] DONE status=${status} corrections=${corrections} trees=${layouts.length}`);
}
function handleWindowRemoved(w) {
removeFromList(floating, w);
removeFromList(suspended, w);
removeFromList(watched, w);
+ removeFromList(multiDesktop, w);
}
// ------------------------------------------------------------
}
);
+function initialReconstruction() {
+ const id = ++syncSerial;
+ log(`[SYNC:${id}] START initial reconstruction`);
+ const windows = workspace.windowList().slice().sort(function(a, b) {
+ const ar = a.frameGeometry, br = b.frameGeometry;
+ return ar.x - br.x || ar.y - br.y || String(a.internalId).localeCompare(String(b.internalId));
+ });
+ for (const w of windows) handleWindowAdded(w);
+ reconcileLayouts({id: id, events: new Set(windows)});
+}
+
log("Version " + VERSION + " initialized");
+initialReconstruction();