Skip to content

Commit

Permalink
refactor: integrating hhtwm and custom wm-related module
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Doster <mvdoster@gmail.com>
  • Loading branch information
vladdoster committed Mar 3, 2022
1 parent 721fa97 commit 016e568
Show file tree
Hide file tree
Showing 11 changed files with 1,069 additions and 113 deletions.
22 changes: 22 additions & 0 deletions hammerspoon/.hammerspoon/bindings/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
local cache = {}
local module = { cache = cache }

-- modifiers in use:
-- * cltr+alt: move focus between windows
-- * ctrl+shift: do things to windows
-- * ultra: custom/global bindings

module.start = function()
hs.fnutils.each(bindings.enabled, function(binding)
cache[binding] = require('bindings.' .. binding)
cache[binding].start()
end)
end

module.stop = function()
hs.fnutils.each(cache, function(binding)
binding.stop()
end)
end

return module
129 changes: 129 additions & 0 deletions hammerspoon/.hammerspoon/bindings/tiling.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
local module = {}
local wm = wm.cache.wm
local move = function(dir)
local win = hs.window.frontmostWindow()
if wm.isFloating(win) then
local directions = {
west = 'left',
south = 'down',
north = 'up',
east = 'right'
}
hs.grid['pushWindow' .. capitalize(directions[dir])](win)
else
wm.swapInDirection(win, dir)
end

end
local throw = function(dir)
local win = hs.window.frontmostWindow()
if wm.isFloating(win) then
hs.grid['pushWindow' .. capitalize(dir) .. 'Screen'](win)
else
wm.throwToScreenUsingSpaces(win, dir)
end

end
local resize = function(resize)
local win = hs.window.frontmostWindow()
if wm.isFloating(win) then
hs.grid['resizeWindow' .. capitalize(resize)](win)

else
wm.resizeLayout(resize)
end
end
module.start = function()
local bind = function(key, fn)
hs.hotkey.bind({ 'ctrl', 'shift' }, key, fn, nil, fn)
end
-- move window
hs.fnutils.each({
{ key = 'h', dir = "west" },
{ key = 'j', dir = "south" },
{ key = 'k', dir = "north" },
{ key = 'l', dir = "east" },
}, function(obj)
bind(obj.key, function() move(obj.dir) end)
end)
-- throw between screens
hs.fnutils.each({
{ key = ']', dir = 'prev' },
{ key = '[', dir = 'next' },
}, function(obj)
bind(obj.key, function() throw(obj.dir) end)
end)
-- resize (floating only)
hs.fnutils.each({
{ key = ',', dir = 'thinner' },
{ key = '.', dir = 'wider' },
{ key = '-', dir = 'shorter' },
{ key = '=', dir = 'taller' }
}, function(obj)
bind(obj.key, function() resize(obj.dir) end)
end)
-- toggle [f]loat
bind('f', function()
local win = hs.window.frontmostWindow()
if not win then return end
wm.toggleFloat(win)
if wm.isFloating(win) then
hs.grid.center(win)
end

end)
-- [r]eset
bind('r', wm.reset)
-- re[t]ile
bind('t', wm.tile)
-- [e]qualize
bind('e', wm.equalizeLayout)
-- [c]enter window
bind('c', function()
local win = hs.window.frontmostWindow()
if not wm.isFloating(win) then
wm.toggleFloat(win)
end
-- win:centerOnScreen()
hs.grid.center(win)

end)
-- toggle [z]oom window
bind('z', function()
local win = hs.window.frontmostWindow()
if not wm.isFloating(win) then
wm.toggleFloat(win)
hs.grid.maximizeWindow(win)
else
wm.toggleFloat(win)
end

end)
-- throw window to space (and move)
for n = 0, 9 do
local idx = tostring(n)
-- important: use this with onKeyReleased, not onKeyPressed
hs.hotkey.bind({ 'ctrl', 'shift' }, idx, nil, function()
local win = hs.window.focusedWindow()
-- if there's no focused window, just move to that space
if not win then
hs.eventtap.keyStroke({ 'ctrl' }, idx)
return
end
local isFloating = wm.isFloating(win)
local success = wm.throwToSpace(win, n)
-- if window switched space, then follow it (ctrl + 0..9) and focus
if success then
hs.eventtap.keyStroke({ 'ctrl' }, idx)
-- retile and re-highlight window after we switch space
hs.timer.doAfter(0.05, function()
if not isFloating then wm.tile() end
highlightWindow(win)
end)
end
end)
end
end
module.stop = function()
end
return module
51 changes: 51 additions & 0 deletions hammerspoon/.hammerspoon/console.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
local axuiWindowElement = require('hs.axuielement').windowElement
local reloadHS = require('ext.system').reloadHS
local module = {}
module.init = function()
-- some global functions for console
inspect = hs.inspect
reload = reloadHS
dumpWindows = function()
hs.fnutils.each(hs.window.allWindows(), function(win)
print(hs.inspect({
id = win:id(),
title = win:title(),
app = win:application():name(),
role = win:role(),
subrole = win:subrole(),
frame = win:frame(),
buttonZoom = axuiWindowElement(win):attributeValue('AXZoomButton'),
buttonFullScreen = axuiWindowElement(win):attributeValue('AXFullScreenButton'),
isResizable = axuiWindowElement(win):isAttributeSettable('AXSize')
}))
end)
end
dumpScreens = function()
hs.fnutils.each(hs.screen.allScreens(), function(s)
print(s:id(), s:position(), s:frame(), s:name())
end)
end
timestamp = function(date)
date = date or hs.timer.secondsSinceEpoch()
return os.date("%F %T" .. ((tostring(date):match("(%.%d+)$")) or ""), math.floor(date))
end
-- console styling
local grayColor = {
red = 24 * 4 / 255,
green = 24 * 4 / 255,
blue = 24 * 4 / 255,
alpha = 1
}
local blackColor = {
red = 24 / 255,
green = 24 / 255,
blue = 24 / 255,
alpha = 1
}
hs.console.consoleCommandColor(blackColor)
hs.console.consoleResultColor(grayColor)
hs.console.consolePrintColor(grayColor)
-- no toolbar
hs.console.toolbar(nil)
end
return module
28 changes: 5 additions & 23 deletions hammerspoon/.hammerspoon/desktop.lua
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
local spaces = require("hs._asm.undocumented.spaces")

local config = { spacesDelay = 0.3 }

local module = { active = 1, lastMove = 0 }

module.layout = function()
return spaces.layout()[spaces.mainScreenUUID()]
end

module.set = function(active, force)
if active ~= module.active or force then
module.active = active
print("=== desktop: " .. module.active .. " : " .. module.activeSpace())
print("--- desktop: " .. module.active .. " : " .. module.activeSpace())
hs.window.filter.switchedToSpace(active)
for _, fn in ipairs(module._listeners) do
fn(active)
-- hs.timer.doAfter(.0, function() fn(active) end)
end
end
end

module.spaceId = function(desktop)
return module.layout()[desktop]
end
module.activeSpace = function()
return module.spaceId(module.active)
end

module._update = function(force)
local active = spaces.activeSpace()
for i, space in ipairs(module.layout()) do
Expand All @@ -35,9 +29,7 @@ module._update = function(force)
end
end
end

module._trigger = nil

module._triggerUpdate = function()
if module._trigger then
module._trigger:setNextTrigger(config.spacesDelay)
Expand All @@ -52,56 +44,47 @@ module._triggerUpdate = function()
end)
end
end

module.move = function(count)
module.lastMove = hs.timer.secondsSinceEpoch()
module.set((((module.active + count) - 1) % #module.layout()) + 1)
end

module.next = function()
module.next = function() -- move one space right
module.move(1)
end
module.previous = function()
module.previous = function() -- move one space left
module.move(-1)
end

module._listeners = {}
module.onChange = function(fn)
table.insert(module._listeners, fn)
module._update(true)
end

module._watcher = hs.spaces.watcher.new(function()
module._triggerUpdate()
end)
module._watcher:start()

module.changeTo = function(desktop)
print("=== changeto " .. desktop)
print("--- change to " .. desktop)
module.set(desktop)
hs.eventtap.keyStroke({ "ctrl" }, string.format("%d", desktop), 1000)
end

module._tap = hs.eventtap.new({ hs.eventtap.event.types.keyDown }, function(event)
local hasSpaceMods = event:getFlags():containExactly({ "ctrl" })
local hasMods = event:getFlags():containExactly({ "ctrl", "cmd", "fn" })
local isUp = event:getKeyCode() == hs.keycodes.map["up"]
local isDown = event:getKeyCode() == hs.keycodes.map["down"]

for s = 1, 9 do
if hasSpaceMods and event:getKeyCode() == hs.keycodes.map[string.format("%d", s)] then
module.set(s)
end
end

if hasMods and isUp then
if module.active ~= 1 then
-- module.changeTo(module.active)
-- os.execute("/usr/local/bin/yabai -m space --focus prev")
module.previous()
end
end

if hasMods and isDown then
if module.active ~= #module.layout() then
-- os.execute("/usr/local/bin/yabai -m space --focus next")
Expand All @@ -110,7 +93,6 @@ module._tap = hs.eventtap.new({ hs.eventtap.event.types.keyDown }, function(even
end
end)
module._tap:start()

module._update(true)
print("*** Loaded desktop")
print("--- loaded desktop")
return module
Loading

0 comments on commit 016e568

Please sign in to comment.