From a9f26cfd6bb64d3cc6a63da831f51521d0043087 Mon Sep 17 00:00:00 2001 From: arnie Date: Wed, 30 Apr 2025 09:21:52 +0200 Subject: [PATCH] Prepare window switching for all spaces --- .../hammerspoon/C3CWorkspace.spoon/init.lua | 67 +++++++++++++------ 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/home-manager/hammerspoon/C3CWorkspace.spoon/init.lua b/home-manager/hammerspoon/C3CWorkspace.spoon/init.lua index 71b89b3..da8ce3e 100644 --- a/home-manager/hammerspoon/C3CWorkspace.spoon/init.lua +++ b/home-manager/hammerspoon/C3CWorkspace.spoon/init.lua @@ -26,6 +26,10 @@ return (function() screen = "B5A65BB6-E73E-4C3D-977C-33C86798AA5A", fullscreen = true, }, + Notes = { + desktop = 2, + screen = "B5A65BB6-E73E-4C3D-977C-33C86798AA5A" + }, Spotify = { desktop = 3, screen = "B5A65BB6-E73E-4C3D-977C-33C86798AA5A", @@ -63,29 +67,52 @@ return (function() local currentSpaces = spaces.allSpaces() - for _, v in pairs(window.list()) do - local def = appScreenMap[v.kCGWindowOwnerName] - local win = window.get(v.kCGWindowNumber) - if def ~= nil and win ~= nil then - if def.desktop ~= nil and currentSpaces[def.screen] ~= nil then - print(v.kCGWindowOwnerName .. " moving window into an index " .. def.desktop .. " which is space " .. currentSpaces[def.screen][def.desktop]) - spaces.moveWindowToSpace(win, currentSpaces[def.screen][def.desktop]) - end + local spaceMap = {} + for _, screenSpaces in pairs(currentSpaces) do + for _, space in ipairs(screenSpaces) do + spaceMap[space] = true + end + end - local scr = screens[def.screen] - if scr ~= nil then - timer.doAfter(1, function() - print(v.kCGWindowOwnerName .. " moving window into a screen " .. def.screen) + local spaceIds = {} + for spaceId, _ in pairs(spaceMap) do + table.insert(spaceIds, spaceId) + end - win:moveToScreen(scr) - if def.fullscreen then - timer.doAfter(1, function() - print(v.kCGWindowOwnerName .. " fullscreening window") + local winMap = {} + for _, spaceId in pairs(spaceIds) do + for _, winId in ipairs(spaces.windowsForSpace(spaceId)) do + winMap[winId] = true + end + end - fullscreen(win) - end) - end - end) + for winId, _ in pairs(winMap) do + -- Cannot get windows for non-active spaces, window.filter would have to be used, but performance is crap + local win = window.get(winId) + if win ~= nil then + local name = win:application():name() + local def = appScreenMap[name] + if def ~= nil then + if def.desktop ~= nil and currentSpaces[def.screen] ~= nil then + print(name .. " moving window into an index " .. def.desktop .. " which is space " .. currentSpaces[def.screen][def.desktop]) + spaces.moveWindowToSpace(win, currentSpaces[def.screen][def.desktop]) + end + + local scr = screens[def.screen] + if scr ~= nil then + timer.doAfter(1, function() + print(name .. " moving window into a screen " .. def.screen) + + win:moveToScreen(scr) + if def.fullscreen then + timer.doAfter(1, function() + print(name .. " fullscreening window") + + fullscreen(win) + end) + end + end) + end end end end