Created
September 27, 2024 09:19
-
-
Save kenlim/04a9ce425c7218929ccbea3b004161e5 to your computer and use it in GitHub Desktop.
My Hammerspoon init file that provides window resizing to thirds of the screen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "Left", function() | |
local win = hs.window.focusedWindow() | |
local currentWindow = win:frame() | |
local screen = win:screen() | |
local screenFrame = screen:frame() | |
local half = screenFrame.w / 2 | |
local third = screenFrame.w / 3 | |
local twothirds = screenFrame.w - third | |
if currentWindow.x == screenFrame.x and currentWindow.w == half then | |
-- is in left half, shrink to left third | |
currentWindow.w = third | |
elseif currentWindow.x == screenFrame.x and currentWindow.w < half then | |
-- is in left third, expand to left twothirds | |
currentWindow.w = twothirds | |
else | |
-- default is to be half screen | |
currentWindow.w = half | |
end | |
currentWindow.x = screenFrame.x -- push to the left fo the screen | |
currentWindow.y = screenFrame.y | |
currentWindow.h = screenFrame.h | |
win:setFrame(currentWindow) | |
end) | |
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "Right", function() | |
local win = hs.window.focusedWindow() | |
local currentWindow = win:frame() | |
local screen = win:screen() | |
local screenFrame = screen:frame() | |
local half = screenFrame.w / 2 | |
local third = screenFrame.w / 3 | |
local twothirds = screenFrame.w - third | |
if currentWindow.x - screenFrame.x == half and currentWindow.w == half then | |
-- is in right half mode, shrink to right third | |
currentWindow.x = screenFrame.x + twothirds -- screenFrame could be another monitor. | |
currentWindow.w = third | |
elseif currentWindow.x - screenFrame.x > half and currentWindow.w < half then | |
-- is to the right of the halfway line, and is smaller than a half | |
currentWindow.x = screenFrame.x + third | |
currentWindow.w = twothirds | |
else | |
currentWindow.x = screenFrame.x + half | |
currentWindow.w = half | |
end | |
currentWindow.y = screenFrame.y | |
currentWindow.h = screenFrame.h | |
win:setFrame(currentWindow) | |
end) | |
hs.hotkey.bind({'alt', 'ctrl', 'cmd'}, 'Up', function() | |
local win = hs.window.focusedWindow() | |
local currentWindow = win:frame() | |
local screen = win:screen() | |
local screenFrame = screen:frame() | |
local thirdHeight = screenFrame.h / 3 | |
local twothirdsHeight = screenFrame.h - thirdHeight | |
currentWindow.x = screenFrame.x -- push to the left fo the screen | |
currentWindow.w = screenFrame.w / 2 | |
currentWindow.y = screenFrame.y -- top edge | |
currentWindow.h = twothirdsHeight | |
win:setFrame(currentWindow) | |
end) | |
hs.hotkey.bind({'alt', 'ctrl', 'cmd'}, 'Down', function() | |
local win = hs.window.focusedWindow() | |
local currentWindow = win:frame() | |
local screen = win:screen() | |
local screenFrame = screen:frame() | |
local thirdHeight = screenFrame.h / 3 | |
local twothirdsHeight = screenFrame.h - thirdHeight | |
currentWindow.x = screenFrame.x | |
currentWindow.w = screenFrame.w / 2 | |
currentWindow.y = screenFrame.y + twothirdsHeight | |
currentWindow.h = thirdHeight | |
win:setFrame(currentWindow) | |
end) | |
hs.hotkey.bind({'alt', 'ctrl', 'cmd'}, 'n', function() | |
-- get the focused window | |
local win = hs.window.focusedWindow() | |
-- get the screen where the focused window is displayed, a.k.a. current screen | |
local screen = win:screen() | |
-- compute the unitRect of the focused window relative to the current screen | |
-- and move the window to the next screen setting the same unitRect | |
win:move(win:frame():toUnitRect(screen:frame()), screen:next(), true, 0) | |
end) | |
hs.hotkey.bind({'alt', 'ctrl', 'cmd'}, 'm', function() | |
-- maximise to screen | |
local win = hs.window.focusedWindow() | |
local f = win:frame() | |
local screen = win:screen() | |
local max = screen:frame() | |
f.x = max.x | |
f.y = max.y | |
f.w = max.w | |
f.h = max.h | |
win:setFrame(f) | |
end) | |
hs.hotkey.bind({'alt', 'ctrl', 'cmd'}, 'c', function() | |
-- resize to 60% of screen, and position it in the center | |
local win = hs.window.focusedWindow() | |
local f = win:frame() | |
local screen = win:screen() | |
local max = screen:frame() | |
f.x = max.x | |
f.y = max.y | |
f.w = max.w * 0.6 | |
f.h = max.h * 0.6 | |
win:setFrame(f) | |
win:centerOnScreen() | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment