index all rss twitter github linkedin email

Álvaro Ramírez

02 May 2021 Cycling window layouts with hammerspoon

Back in January, Patrik Collison tweeted about Rectangle's Todo mode. Rectangle looks great. Although I've not yet adopted it, Todo mode really resonates with me. I've been achieving similar functionality with hammerspoon.

cycle.gif

Here's a quick and dirty function to cycle through my window layouts:

function reframeFocusedWindow()
   local win = hs.window.focusedWindow()
   local maximizedFrame = win:screen():frame()
   maximizedFrame.x = maximizedFrame.x + 15
   maximizedFrame.y = maximizedFrame.y + 15
   maximizedFrame.w = maximizedFrame.w - 30
   maximizedFrame.h = maximizedFrame.h - 30

   local leftFrame = win:screen():frame()
   leftFrame.x = leftFrame.x + 15
   leftFrame.y = leftFrame.y + 15
   leftFrame.w = leftFrame.w - 250
   leftFrame.h = leftFrame.h - 30

   local rightFrame = win:screen():frame()
   rightFrame.x = rightFrame.w - 250 + 15
   rightFrame.y = rightFrame.y + 15
   rightFrame.w = 250 - 15 - 15
   rightFrame.h = rightFrame.h - 30

   -- Make space on right
   if win:frame() == maximizedFrame then
     win:setFrame(leftFrame)
     return
   end

   -- Make space on left
   if win:frame() == leftFrame then
     win:setFrame(rightFrame)
     return
   end

   win:setFrame(maximizedFrame)
end

A here's my ⌥-F binding to reframeFocusedWindow:

hs.hotkey.bind({"alt"}, "F", reframeFocusedWindow)