Skip to content

Instantly share code, notes, and snippets.

@lixiaoyan
Last active December 20, 2024 01:58
Show Gist options
  • Save lixiaoyan/800dd8c4c259535681de05c9d02df533 to your computer and use it in GitHub Desktop.
Save lixiaoyan/800dd8c4c259535681de05c9d02df533 to your computer and use it in GitHub Desktop.
-- https://www.hammerspoon.org/
focusWorkaroundWatchers = {}
local lastActivatedApp = nil
local restoreActivationTimer = nil
local lastFocusedWindowBySpace = {}
focusWorkaroundWatchers.application = hs.application.watcher
.new(function(name, event, app)
if event == hs.application.watcher.activated then
if name == "WindowManager" then
restoreActivationTimer = hs.timer.doAfter(0.1, function()
if lastActivatedApp ~= nil then
lastActivatedApp:activate()
end
end)
else
lastActivatedApp = app
end
end
end)
:start()
hs.window.filter.default:subscribe(hs.window.filter.windowFocused, function(window, name)
for _, space in ipairs(hs.spaces.windowSpaces(window)) do
lastFocusedWindowBySpace[space] = window
end
end)
focusWorkaroundWatchers.spaces = hs.spaces.watcher
.new(function()
lastActivatedApp = nil
if restoreActivationTimer ~= nil then
restoreActivationTimer:stop()
restoreActivationTimer = nil
end
if hs.application.frontmostApplication():bundleID() == "com.apple.WindowManager" then
lastFocusedWindow = lastFocusedWindowBySpace[hs.spaces.focusedSpace()]
if lastFocusedWindow ~= nil then
lastFocusedWindow:focus()
end
end
end)
:start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment