nix-configuration/home-manager/hammerspoon/C3CWorkspace.spoon/init.lua
2025-02-06 13:00:44 +01:00

123 lines
3.6 KiB
Lua

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";
author = "Arnie";
license = "MIT";
}
-- DELL S2722DGM: 0F6BDB5B-840D-40BE-AAC9-B467A78E057A
-- DELL S2721DGF: D3142823-261D-46EF-B9C2-5181C7FE2CA5
-- AV Receiver: B5A65BB6-E73E-4C3D-977C-33C86798AA5A
local appScreenMap = {
Slack = {
desktop = 1,
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 = grid.get(win, screen)
cell.x = 0
cell.y = 0
cell.w = 24
cell.h = 24
grid.set(win, cell, screen)
end
function spoon:restoreAppsToScreens()
local screens = {}
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 = application.get(appName)
if app then
local appFilter = wf.new(appName)
if appFilter then
-- TODO: Really bad performance
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
function spoon:bindHotKeys(mapping)
local spec = {
restoreAppsToScreens = hs.fnutils.partial(self.restoreAppsToScreens, self)
}
hs.spoons.bindHotkeysToSpec(spec, mapping)
return self
end
return spoon
end)()