Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua
http://www.hammerspoon.org
MIT License
11.7k stars 580 forks source link

hs.window:focus() and hs.window:focusWindow{North,East,South,West}() can focus wrong app window #2558

Open eraserhd opened 3 years ago

eraserhd commented 3 years ago

On 0.9.81 (5594).

I have windows, from west to east:

If I call focusWindowWest() on Kitty B, Chrome A is sometimes focused. In fact, testing just now, it always focuses the topmost Chrome window, regardless of where it is, so it is somehow focusing the correct app and the wrong window.

I'm calling this function with 'West':

local function focus_window(what)
  if type(what) == 'string' then
    local window = hs.window.focusedWindow()
    window['focusWindow' .. what](window, nil, true, true)
  else
    window_number(what):focus()
  end
end
eraserhd commented 3 years ago

This appears to be a more general issue, actually. I've just found that, in my new config, focusWindow() will focus the wrong window from the right app. If I try to focus a window from an app that has windows on both screens, and the app's front-most window is on the wrong monitor, it will focus the frontmost window (which is on the wrong monitor).

I have "Displays have separate Spaces" on, if that matters.

eraserhd commented 3 years ago

This terrible-looking work-around works:

  window:focus()
  if hs.window.focusedWindow() ~= window then
    window:focus()
  end
mikaraunio commented 1 year ago

I'm still seeing this with 0.9.100.

@eraserhd can't seem to grasp how to use your work-around. The code seems to re-focus the window I wanted to focus away from, and indeed that's what I see when I use it.