From 51433e6aca6139ef0f35556876833c5efae64e87 Mon Sep 17 00:00:00 2001 From: Lukas Cech Date: Thu, 6 Feb 2025 13:00:44 +0100 Subject: [PATCH] Update hammerspoon window management --- .../hammerspoon/C3CWorkspace.spoon/init.lua | 78 +++++++++++++++---- home-manager/hammerspoon/window-move.lua | 55 +++++++------ home-manager/hammerspoon/window-tiling.lua | 39 +++++----- 3 files changed, 119 insertions(+), 53 deletions(-) diff --git a/home-manager/hammerspoon/C3CWorkspace.spoon/init.lua b/home-manager/hammerspoon/C3CWorkspace.spoon/init.lua index bac846d..9823893 100644 --- a/home-manager/hammerspoon/C3CWorkspace.spoon/init.lua +++ b/home-manager/hammerspoon/C3CWorkspace.spoon/init.lua @@ -1,4 +1,11 @@ return (function() + local application = hs.application + local grid = hs.grid + local screen = hs.screen + local spaces = hs.spaces + local timer = hs.timer + local wf = hs.window.filter + spoon = { name = "C3C Workspace"; version = "0.0.1"; @@ -15,45 +22,90 @@ return (function() screen = "0F6BDB5B-840D-40BE-AAC9-B467A78E057A", fullscreen = true, }, + Cursor = { + desktop = 1, + screen = "B5A65BB6-E73E-4C3D-977C-33C86798AA5A", + fullscreen = true, + }, + Spotify = { + desktop = 3, + screen = "B5A65BB6-E73E-4C3D-977C-33C86798AA5A", + fullscreen = true, + }, + ["zoom.us"] = { + desktop = 2, + screen = "D3142823-261D-46EF-B9C2-5181C7FE2CA5", + fullscreen = true, + }, + ["Microsoft Outlook"] = { + desktop = 3, + screen = "D3142823-261D-46EF-B9C2-5181C7FE2CA5", + fullscreen = true, + }, } local fullscreen = function(win) local screen = win:screen() - local cell = hs.grid.get(win, screen) + local cell = grid.get(win, screen) cell.x = 0 cell.y = 0 cell.w = 24 cell.h = 24 - hs.grid.set(win, cell, screen) + grid.set(win, cell, screen) end function spoon:restoreAppsToScreens() local screens = {} - for _, screen in ipairs(hs.screen.allScreens()) do - screens[screen:getUUID()] = screen + for _, scr in ipairs(screen.allScreens()) do + screens[scr:getUUID()] = scr end + local currentSpaces = spaces.allSpaces() + + local foundWindows = {} + for appName, def in pairs(appScreenMap) do local scr = screens[def.screen] if scr ~= nil then - local app = hs.application.get(appName) + local app = application.get(appName) if app then - local wf = hs.window.filter.new(appName) - if wf then + local appFilter = wf.new(appName) + if appFilter then -- TODO: Really bad performance - for _, win in pairs(wf:getWindows()) do - win:moveToScreen(scr) - if def.fullscreen then - fullscreen(win) - end - end + foundWindows[appName] = appFilter:getWindows() end end end end + + for appName, windows in pairs(foundWindows) do + local def = appScreenMap[appName] + local scr = screens[def.screen] + + for _, win in pairs(windows) do + if currentSpaces[def.screen] ~= nil then + print(appName .. " moving window into an index " .. def.desktop .. " which is space " .. currentSpaces[def.screen][def.desktop]) + spaces.moveWindowToSpace(win, currentSpaces[def.screen][def.desktop]) + end + + print(appName .. " scheduling timer to move window into a screen") + timer.doAfter(1, function() + print(appName .. " moving window into a screen " .. def.screen) + + win:moveToScreen(scr) + if def.fullscreen then + timer.doAfter(1, function() + print(appName .. " fullscreening window") + + fullscreen(win) + end) + end + end) + end + end end -- https://github.com/Hammerspoon/hammerspoon/blob/master/SPOONS.md#hotkeys diff --git a/home-manager/hammerspoon/window-move.lua b/home-manager/hammerspoon/window-move.lua index 3086253..a0a484e 100644 --- a/home-manager/hammerspoon/window-move.lua +++ b/home-manager/hammerspoon/window-move.lua @@ -1,27 +1,32 @@ windowMove = function() + local grid = hs.grid + local screen = hs.screen + local timer = hs.timer + local window = hs.window + -- move window to next screen hs.hotkey.bind({ "ctrl", "alt", "cmd" }, "N", function() - local win = hs.window.focusedWindow() + local win = window.focusedWindow() win:moveToScreen(win:screen():next()) end) local fullscreen = function(win) - local screen = win:screen() + local scr = win:screen() - local cell = hs.grid.get(win, screen) + local cell = grid.get(win, scr) cell.x = 0 cell.y = 0 cell.w = 24 cell.h = 24 - hs.grid.set(win, cell, screen) + grid.set(win, cell, scr) end local getScreenById = function(id) - for _, screen in ipairs(hs.screen.allScreens()) do - if screen:getUUID() == id then - return screen + for _, scr in ipairs(screen.allScreens()) do + if scr:getUUID() == id then + return scr end end @@ -30,38 +35,44 @@ windowMove = function() hs.hotkey.bind({ "ctrl", "alt", "cmd" }, "pad1", function() -- DELL S2722DGM - local screen = getScreenById("0F6BDB5B-840D-40BE-AAC9-B467A78E057A") - if screen == nil then + local scr = getScreenById("0F6BDB5B-840D-40BE-AAC9-B467A78E057A") + if scr == nil then return end - local win = hs.window.focusedWindow() - win:moveToScreen(screen) - fullscreen(win) + local win = window.focusedWindow() + win:moveToScreen(scr) + timer.doAfter(1, function() + fullscreen(win) + end) end) hs.hotkey.bind({ "ctrl", "alt", "cmd" }, "pad2", function() -- DELL S2721DGF - local screen = getScreenById("D3142823-261D-46EF-B9C2-5181C7FE2CA5") - if screen == nil then + local scr = getScreenById("D3142823-261D-46EF-B9C2-5181C7FE2CA5") + if scr == nil then return end - local win = hs.window.focusedWindow() - win:moveToScreen(screen) - fullscreen(win) + local win = window.focusedWindow() + win:moveToScreen(scr) + timer.doAfter(1, function() + fullscreen(win) + end) end) hs.hotkey.bind({ "ctrl", "alt", "cmd" }, "pad3", function() -- AV Receiver - local screen = getScreenById("B5A65BB6-E73E-4C3D-977C-33C86798AA5A") - if screen == nil then + local scr = getScreenById("B5A65BB6-E73E-4C3D-977C-33C86798AA5A") + if scr == nil then return end - local win = hs.window.focusedWindow() - win:moveToScreen(screen) - fullscreen(win) + local win = window.focusedWindow() + win:moveToScreen(scr) + timer.doAfter(1, function() + fullscreen(win) + end) end) end diff --git a/home-manager/hammerspoon/window-tiling.lua b/home-manager/hammerspoon/window-tiling.lua index 213836b..8aee9c9 100644 --- a/home-manager/hammerspoon/window-tiling.lua +++ b/home-manager/hammerspoon/window-tiling.lua @@ -1,4 +1,8 @@ windowTiling =function() + local window = hs.window + local grid = hs.grid + local screen = hs.screen + hs.window.animationDuration = 0 hs.window.setShadows(false) local hyper = { "ctrl", "alt", "cmd" } @@ -8,14 +12,14 @@ windowTiling =function() } function wm:_nextStep(dim, offs, cb) - if hs.window.focusedWindow() then + if window.focusedWindow() then local axis = dim == "w" and "x" or "y" local oppDim = dim == "w" and "h" or "w" local oppAxis = dim == "w" and "y" or "x" - local win = hs.window.frontmostWindow() - local screen = win:screen() + local win = window.frontmostWindow() + local scr = win:screen() - local cell = hs.grid.get(win, screen) + local cell = grid.get(win, scr) local nextSize = self.sizes[1] for i = 1, #self.sizes do @@ -34,32 +38,31 @@ windowTiling =function() cell[oppAxis] = 0 end - hs.grid.set(win, cell, screen) + grid.set(win, cell, scr) end end function wm:_fullscreen() if hs.window.focusedWindow() then - local win = hs.window.frontmostWindow() - local screen = win:screen() + local win = window.frontmostWindow() + local scr = win:screen() - local cell = hs.grid.get(win, screen) + local cell = grid.get(win, scr) cell.x = 0 cell.y = 0 cell.w = self.GRID.w cell.h = self.GRID.h - hs.grid.set(win, cell, screen) + grid.set(win, cell, scr) end end function wm:_fullDimension(dim) - if hs.window.focusedWindow() then - local win = hs.window.frontmostWindow() - local id = win:id() - local screen = win:screen() - local cell = hs.grid.get(win, screen) + if window.focusedWindow() then + local win = window.frontmostWindow() + local scr = win:screen() + local cell = grid.get(win, scr) if dim == "x" then cell = "0,0 " .. self.GRID.w .. "x" .. self.GRID.h @@ -68,7 +71,7 @@ windowTiling =function() cell[dim == "w" and "x" or "y"] = 0 end - hs.grid.set(win, cell, screen) + grid.set(win, cell, scr) end end @@ -81,9 +84,9 @@ windowTiling =function() } self.GRID = { w = 24, h = 24 } - hs.grid.setGrid(self.GRID.w .. "x" .. self.GRID.h) - hs.grid.MARGINX = 0 - hs.grid.MARGINY = 0 + grid.setGrid(self.GRID.w .. "x" .. self.GRID.h) + grid.MARGINX = 0 + grid.MARGINY = 0 hs.hotkey.bind(hyper, "down", function() self._pressed.down = true