From 4f05efa85b7ee9e080537e70c427f78dd8df5131 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 5 Dec 2018 08:13:34 -0800 Subject: simplify frontmost application tracking --- hammerspoon/init.lua | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/hammerspoon/init.lua b/hammerspoon/init.lua index 380e540..40e74e4 100644 --- a/hammerspoon/init.lua +++ b/hammerspoon/init.lua @@ -108,7 +108,6 @@ end) -- hs.eventtap.keyStroke({"cmd", "option"}, "i") -- end) -possibly_active_apps = {} current_app_name = nil function enter_bindings(name) @@ -126,23 +125,15 @@ function exit_bindings(name) end -- the application watcher receives notifications about new apps being --- activated before the old apps are deactivated, so we have to hack around --- that a bit +-- activated before the old apps are deactivated, so we can't rely on +-- deactivated events. this should be fine because there should always be an +-- active app (at the very least, Finder) current_application_watcher = hs.application.watcher.new(function(name, event_type, app) if event_type == hs.application.watcher.activated then - possibly_active_apps[app:pid()] = app - elseif event_type == hs.application.watcher.deactivated then - possibly_active_apps[app:pid()] = nil - end - - apps = {} - for _, v in pairs(possibly_active_apps) do - table.insert(apps, v) - end - - if #apps == 1 and apps[1]:name() ~= current_app_name then - exit_bindings(current_app_name) - enter_bindings(apps[1]:name()) + if current_app_name ~= app:name() then + exit_bindings(current_app_name) + enter_bindings(app:name()) + end end end) enter_bindings(hs.application.frontmostApplication():name()) -- cgit v1.2.3-54-g00ecf