hs: refactor config

This commit is contained in:
Henrik Lissner 2015-12-27 02:29:38 -05:00
parent 1f83e90a73
commit 25626ec072
6 changed files with 142 additions and 144 deletions

View file

@ -3,29 +3,29 @@ commandMode = hs.hotkey.modal.new('cmd', 'escape')
-- Screen overlay
local f = hs.screen.primaryScreen():frame()
local r = hs.drawing.rectangle(hs.geometry(0, 0, f.w, f.h + 30))
r:setAlpha(0.5)
r:setFillColor(hs.drawing.color.black)
r:bringToFront()
local overlay = hs.drawing.rectangle(hs.geometry(0, 0, f.w, f.h + 30))
overlay:setAlpha(0.5)
overlay:setFillColor(hs.drawing.color.black)
overlay:bringToFront()
--
function commandMode:entered() hs.alert("Command mode!"); r:show() end
function commandMode:exited() r:hide(); hs.alert("Bye bye") end
function commandMode:entered() overlay:show() end
function commandMode:exited() overlay:hide() end
-- Command mode keybindings
hs.fnutils.each({
{ '', 'escape', util.call(commandMode, 'exit') },
{ '', 'r', util.reload },
{ 'shift', 'r', hs.openConsole },
{ 'shift', 'r', function() hs.openConsole(); commandMode:exit() end },
}, function (item)
commandMode:bind(item[1], item[2], item[3])
end)
-- Global keybindings
hs.fnutils.each({
-- { {'cmd', 'ctrl'}, 'up', function() print("UP") end },
-- { {'cmd', 'ctrl'}, 'left', function() print("LEFT") end },
-- { {'cmd', 'ctrl'}, 'right', function() print("RIGHT") end },
-- { {'cmd', 'ctrl'}, 'up', function() print("UP"); modes.position:enter() end },
-- { {'cmd', 'ctrl'}, 'left', function() print("LEFT"); resizeBRMode:enter() end },
-- { {'cmd', 'ctrl'}, 'right', function() print("RIGHT"); resizeTLMode:enter() end },
}, function (item)
hs.hotkey.bind(item[1], item[2], item[3])
end)

View file

@ -3,13 +3,17 @@ util = require('util')
-- Settings
hs.window.animationDuration = 0
hs.urlevent.bind("reload", util.reload)
-- Modules
-- require('apps')
-- require('urls')
-- require('locations')
util.autoimport("./apps")
--
require('modules/apps')
-- require('modules/windows')
util.autoimport("./apps")
require('bindings')

View file

@ -0,0 +1,125 @@
local apps = {}
-- App-specific code, runs when you launch or switch to another app
App = {}
App.new = function(name)
local self = {}
self.name = name
self.bindings = {}
self.onInitFn = nil
self.onActivateFn = nil
self.onDeactivateFn = nil
self.onLaunchedFn = nil
self.onTerminatedFn = nil
self.bind = function(mods, key, action, predicate)
table.insert(self.bindings,
{key = hs.hotkey.new(mods, key, action), predicate = predicate})
return self
end
self.onInit = function(fn)
fn(self)
return self
end
self.onActivate = function(fn)
self.onActivateFn = fn
return self
end
self.onDeactivate = function(fn)
self.onDeactivateFn = fn
return self
end
self.onLaunched = function(fn)
self.onLaunchedFn = fn
return self
end
self.onTerminated = function(fn)
self.onTerminatedFn = fn
return self
end
self.activate = function(appObject)
if type(self.onActivateFn) == 'function' then
self:onActivateFn(appObject)
end
self:enableBinds()
end
self.deactivate = function(appObject)
if type(self.onDeactivateFn) == 'function' then
self:onDeactivateFn(appObject)
end
self:disableBinds()
end
self.launch = function(appObject)
if type(self.onLaunchedFn) == 'function' then
self:onLaunchedFn(appObject)
end
end
self.terminate = function(appObject)
if type(self.onTerminatedFn) == 'function' then
self:onTerminatedFn(appObject)
end
self:clearBinds()
end
self.enableBinds = function(self)
hs.fnutils.each(self.bindings, function(b)
if b.predicate and not b.predicate() then
b.key:disable()
else
b.key:enable()
end
end)
end
self.disableBinds = function(self)
hs.fnutils.each(self.bindings, function(b) b.key:disable() end)
end
self.clearBinds = function(self)
hs.fnutils.each(self.bindings, function(b) b.key:delete() end)
self.bindings = {}
end
apps[name] = self
return self
end
function invoke(keys, predicate)
return function ()
if predicate and not predicate() then
return
end
if type(keys) == 'table' then
hs.fnutils.each(keys, function (key)
if type(key) ~= 'table' then
key = {{}, key}
end
hs.eventtap.keyStroke(key[1], key[2])
end)
elseif type(keys) == 'string' then
os.execute(keys)
elseif type(keys) == 'function' then
return keys()
end
end
end
--
function appGlobalWatcher(appName, eventType, appObject)
if apps[appName] then
if (eventType == hs.application.watcher.activated) then
apps[appName].activate(appObject)
elseif (eventType == hs.application.watcher.deactivated) then
apps[appName].deactivate(appObject)
elseif (eventType == hs.application.watcher.launched) then
apps[appName].launch(appObject)
elseif (eventType == hs.application.watcher.terminated) then
apps[appName].terminate(appObject)
end
end
end
hs.application.watcher.new(appGlobalWatcher):start()

View file

@ -1,4 +0,0 @@
-- hammerspoon://reload
hs.urlevent.bind("reload", core.reload)
-- TODO: hammerspoon://org?search=keywords

View file

@ -1,6 +1,5 @@
-- TODO: Turn into App class
local module = {
return {
apps = {},
bindings = {},
@ -33,129 +32,3 @@ local module = {
end
}
-- App-specific code, runs when you launch or switch to another app
local apps = {}
App = {}
App.new = function(name)
local self = {}
self.name = name
self.bindings = {}
self.onInitFn = nil
self.onActivateFn = nil
self.onDeactivateFn = nil
self.onLaunchedFn = nil
self.onTerminatedFn = nil
self.bind = function(mods, key, action, predicate)
table.insert(self.bindings,
{key = hs.hotkey.new(mods, key, action), predicate = predicate})
return self
end
self.onInit = function(fn)
fn(self)
return self
end
self.onActivate = function(fn)
self.onActivateFn = fn
return self
end
self.onDeactivate = function(fn)
self.onDeactivateFn = fn
return self
end
self.onLaunched = function(fn)
self.onLaunchedFn = fn
return self
end
self.onTerminated = function(fn)
self.onTerminatedFn = fn
return self
end
self.activate = function(appObject)
if type(self.onActivateFn) == 'function' then
self:onActivateFn(appObject)
end
self:enableBinds()
end
self.deactivate = function(appObject)
if type(self.onDeactivateFn) == 'function' then
self:onDeactivateFn(appObject)
end
self:disableBinds()
end
self.launch = function(appObject)
if type(self.onLaunchedFn) == 'function' then
self:onLaunchedFn(appObject)
end
end
self.terminate = function(appObject)
if type(self.onTerminatedFn) == 'function' then
self:onTerminatedFn(appObject)
end
self:clearBinds()
end
self.enableBinds = function(self)
hs.fnutils.each(self.bindings, function(b)
if b.predicate and not b.predicate() then
b.key:disable()
else
b.key:enable()
end
end)
end
self.disableBinds = function(self)
hs.fnutils.each(self.bindings, function(b) b.key:disable() end)
end
self.clearBinds = function(self)
hs.fnutils.each(self.bindings, function(b) b.key:delete() end)
self.bindings = {}
end
apps[name] = self
return self
end
function invoke(keys, predicate)
return function ()
if predicate and not predicate() then
return
end
if type(keys) == 'table' then
hs.fnutils.each(keys, function (key)
if type(key) ~= 'table' then
key = {{}, key}
end
hs.eventtap.keyStroke(key[1], key[2])
end)
elseif type(keys) == 'string' then
os.execute(keys)
elseif type(keys) == 'function' then
return keys()
end
end
end
--
function appGlobalWatcher(appName, eventType, appObject)
if apps[appName] then
if (eventType == hs.application.watcher.activated) then
apps[appName].activate(appObject)
elseif (eventType == hs.application.watcher.deactivated) then
apps[appName].deactivate(appObject)
elseif (eventType == hs.application.watcher.launched) then
apps[appName].launch(appObject)
elseif (eventType == hs.application.watcher.terminated) then
apps[appName].terminate(appObject)
end
end
end
hs.application.watcher.new(appGlobalWatcher):start()
--
return module