Skip to content

Instantly share code, notes, and snippets.

@winguse
Created March 10, 2020 14:31
Show Gist options
  • Save winguse/1548c9e94f4cf65b7bb55ee13279dd59 to your computer and use it in GitHub Desktop.
Save winguse/1548c9e94f4cf65b7bb55ee13279dd59 to your computer and use it in GitHub Desktop.
local timer=require'hs.timer'
local checkMods=require'hs.eventtap'.checkKeyboardModifiers
local function modsPressed() return checkMods(true)._raw>0 end
local frontWindows = {}
local frontFocusIdx = 0
local modes = {
full = { 1 },
half = { 0.5, 0.5 },
third = { 0.34, 0.33, 0.33 },
}
local winIndex = 0
local windowList = nil
local timerHandle = nil
function stopTimer()
if timerHandle == nil then
return
end
timerHandle:stop()
timerHandle = nil
end
function hasValue(tab, val)
for index, value in pairs(tab) do
if value == val then
return true
end
end
return false
end
function renderFront(mode)
stopTimer()
local count = #mode
local filter = hs.window.filter.new():setDefaultFilter{}
local allWindows = filter:getWindows(hs.window.filter.sortByFocusedLast)
if #allWindows < 1 then
return
end
local screen = allWindows[1]:screen()
local max = screen:frame()
frontWindows = {}
frontFocusIdx = 0
local preSum = 0
for idx, win in pairs(allWindows) do
table.insert(frontWindows, win)
local f = win:frame()
f.x = max.x + preSum
f.y = max.y
f.w = max.w * mode[idx]
f.h = max.h
preSum = preSum + f.w
win:setFrame(f)
if idx == #mode then
break
end
end
end
function focus(dir)
local win = windowList[winIndex + 1]
win:setFrame(frontWindows[frontFocusIdx + 1]:frame())
win:focus()
frontWindows[frontFocusIdx + 1] = win
winIndex = (winIndex + dir + #windowList) % #windowList
end
function buildFocus(appName, title)
return function()
local filter = hs.window.filter.new(false):setAppFilter(appName,{allowTitles=title})
local windows = filter:getWindows()
if #windows == 0 then
return
end
windows[1]:focus()
windows[1]:setFrame(frontWindows[frontFocusIdx + 1]:frame())
frontWindows[frontFocusIdx + 1] = windows[1]
end
end
function buildSwitcher(dir)
return function()
if timerHandle ~= nil then
focus(dir)
return
end
local filter = hs.window.filter.new():setDefaultFilter{}
local allWindows = filter:getWindows(hs.window.filter.sortByFocusedLast)
local currentWin = hs.window.focusedWindow()
windowList = {}
table.insert(windowList, currentWin)
for idx, win in pairs(allWindows) do
if not hasValue(frontWindows, win) then
table.insert(windowList, win)
end
end
winIndex = 1
focus(dir)
timerHandle = timer.waitWhile(modsPressed, stopTimer, 0.25)
end
end
function buildFrontSwitcher(dir)
return function()
if #frontWindows == 0 then
return
end
stopTimer()
frontFocusIdx = (frontFocusIdx + dir + #frontWindows) % #frontWindows
frontWindows[frontFocusIdx + 1]:focus()
end
end
function buildSwitchFront(dir)
return function()
if #frontWindows < 2 then
return
end
stopTimer()
local aIdx = frontFocusIdx
frontFocusIdx = (frontFocusIdx + dir + #frontWindows) % #frontWindows
local bIdx = frontFocusIdx
local aFrame = frontWindows[aIdx + 1]:frame()
local bFrame = frontWindows[bIdx + 1]:frame()
frontWindows[aIdx + 1]:setFrame(bFrame)
frontWindows[bIdx + 1]:setFrame(aFrame)
local tmp = frontWindows[aIdx + 1]
frontWindows[aIdx + 1] = frontWindows[bIdx + 1]
frontWindows[bIdx + 1] = tmp
frontWindows[aIdx + 1]:focus() -- bring render attention
frontWindows[bIdx + 1]:focus()
end
end
return function (mash, shiftMash)
hs.hotkey.bind(mash, "f", "Full Screen Mode", function()
renderFront(modes.full)
end)
hs.hotkey.bind(mash, "d", "Half Screen Mode", function()
renderFront(modes.half)
end)
hs.hotkey.bind(mash, "s", "1/3 Screen Mode", function()
renderFront(modes.third)
end)
hs.hotkey.bind(mash, "j", "Next Window", buildSwitcher(1))
hs.hotkey.bind(mash, "k", "Previous Window", buildSwitcher(-1))
hs.hotkey.bind(mash, "h", "Focus Left", buildFrontSwitcher(-1))
hs.hotkey.bind(mash, "l", "Focus Right", buildFrontSwitcher(1))
hs.hotkey.bind(shiftMash, "h", "Move Left", buildSwitchFront(-1))
hs.hotkey.bind(shiftMash, "l", "Move Right", buildSwitchFront(1))
hs.hotkey.bind(mash, "t", "Tubi Chrome", buildFocus("Google Chrome", "(@tubi)"))
hs.hotkey.bind(mash, "c", "Personal Chrome", buildFocus("Google Chrome", "(winguse)"))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment