helm/hammerspoon/modules/apps.lua

132 lines
3.2 KiB
Lua
Raw Normal View History

2015-12-26 23:29:38 -08:00
local apps = {}
-- App-specific code, runs when you launch or switch to another app
App = {}
App.new = function(name)
2016-01-02 12:40:13 -08:00
local self = {}
self.name = name
self.bindings = {}
2015-12-26 23:29:38 -08:00
2016-01-02 12:40:13 -08:00
self.onActivateFn = nil
self.onDeactivateFn = nil
self.onLaunchedFn = nil
self.onTerminatedFn = nil
2015-12-26 23:29:38 -08:00
2016-01-02 12:40:13 -08:00
self.bind = function(mods, key, action, predicate)
2016-01-06 21:55:50 -08:00
table.insert(self.bindings, {key = hs.hotkey.new(mods, key, action),
predicate = predicate})
2016-01-02 12:40:13 -08:00
return self
end
2015-12-26 23:29:38 -08:00
2016-01-02 12:40:13 -08:00
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
2016-01-06 21:56:20 -08:00
local app = hs.application.find(self.name)
if app then
self.launch(app)
if app:isFrontmost() then
self.activate(app)
end
end
2016-01-02 12:40:13 -08:00
return self
end
self.onTerminated = function(fn)
self.onTerminatedFn = fn
return self
end
2015-12-26 23:29:38 -08:00
2016-01-02 12:40:13 -08:00
self.activate = function(appObject)
if type(self.onActivateFn) == 'function' then
self:onActivateFn(appObject)
2015-12-26 23:29:38 -08:00
end
2016-01-02 12:40:13 -08:00
self:enableBinds()
end
self.deactivate = function(appObject)
if type(self.onDeactivateFn) == 'function' then
self:onDeactivateFn(appObject)
2015-12-26 23:29:38 -08:00
end
2016-01-02 12:40:13 -08:00
self:disableBinds()
end
self.launch = function(appObject)
if type(self.onLaunchedFn) == 'function' then
self:onLaunchedFn(appObject)
2015-12-26 23:29:38 -08:00
end
2016-01-02 12:40:13 -08:00
end
self.terminate = function(appObject)
if type(self.onTerminatedFn) == 'function' then
self:onTerminatedFn(appObject)
2015-12-26 23:29:38 -08:00
end
2016-01-02 12:40:13 -08:00
self:clearBinds()
end
2015-12-26 23:29:38 -08:00
2016-01-02 12:40:13 -08:00
self.enableBinds = function(self)
hs.fnutils.each(self.bindings, function(b)
2016-01-06 21:55:50 -08:00
if b.predicate and not b.predicate() then
b.key:disable()
else
b.key:enable()
end
2016-01-02 12:40:13 -08:00
end)
end
2015-12-26 23:29:38 -08:00
2016-01-02 12:40:13 -08:00
self.disableBinds = function(self)
2016-01-06 21:56:53 -08:00
hs.fnutils.each(self.bindings, function(b)
if b and b.key then
b.key:disable()
end
end)
2016-01-02 12:40:13 -08:00
end
2015-12-26 23:29:38 -08:00
2016-01-02 12:40:13 -08:00
self.clearBinds = function(self)
2016-01-06 21:55:50 -08:00
self:disableBinds()
2016-01-02 12:40:13 -08:00
self.bindings = {}
end
2015-12-26 23:29:38 -08:00
2016-01-02 12:40:13 -08:00
apps[name] = self
return self
2015-12-26 23:29:38 -08:00
end
function invoke(keys, predicate)
2016-01-02 12:40:13 -08:00
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()
2015-12-26 23:29:38 -08:00
end
2016-01-02 12:40:13 -08:00
end
2015-12-26 23:29:38 -08:00
end
--
function appGlobalWatcher(appName, eventType, appObject)
2016-01-02 12:40:13 -08:00
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)
2015-12-26 23:29:38 -08:00
end
2016-01-02 12:40:13 -08:00
end
2015-12-26 23:29:38 -08:00
end
hs.application.watcher.new(appGlobalWatcher):start()