Created
July 15, 2023 15:30
-
-
Save jamesb93/3e19cc4769b43fc97968140c423edd0f to your computer and use it in GitHub Desktop.
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
local math = require('hs.math') | |
local hyper = {"cmd"} | |
local function getWindows() | |
local win = hs.window.focusedWindow() | |
local screen = win:screen() | |
return win, screen | |
end | |
local function getFrames(win, screen) | |
return win:frame(), screen:frame() | |
end | |
local function tableHas(set, key) | |
return set[key] ~= nil | |
end | |
local function roundToThree(x) | |
return math.floor(x * 1000 + 0.5) / 1000 | |
end | |
local function calcNextRatio(win, screen, command) | |
local state = { | |
[0.5] = 0.333, | |
[0.333] = 0.666, | |
[0.666] = 0.5 | |
} | |
-- Is it on the left and you're asking to go on the right? i.e opposite side? | |
if command == 'right' then | |
if win.x == 0 then return 0.5 end -- if it was on the left | |
end | |
if command == 'left' then | |
if win.x == screen.w * 0.5 or | |
win.x == screen.w * 0.333 or | |
win.x == screen.w * 0.666 then | |
return 0.5 | |
end | |
end | |
-- calculates the ratio that the screen takes up currently | |
local ratio = roundToThree(win.w / screen.w) | |
if tableHas(state, ratio) then | |
return state[ratio] | |
end | |
return 0.5 | |
end | |
hs.hotkey.bind(hyper, "left", function() | |
local win, screen = getWindows() | |
local winframe, screenframe = getFrames(win, screen) | |
local nextRatio = calcNextRatio(winframe, screenframe, "left") | |
winframe.x = screenframe.x | |
winframe.y = screenframe.y | |
winframe.w = screenframe.w * nextRatio | |
winframe.h = screenframe.h | |
win:setFrame(winframe, 0) | |
end) | |
hs.hotkey.bind(hyper, "right", function() | |
local win, screen = getWindows() | |
local winframe, screenframe = getFrames(win, screen) | |
local nextRatio = calcNextRatio(winframe, screenframe, "right") | |
winframe.x = screenframe.x + (screenframe.w * (1 - nextRatio)) | |
winframe.y = screenframe.y | |
winframe.w = screenframe.w * nextRatio | |
winframe.h = screenframe.h | |
win:setFrame(winframe, 0) | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment