Search code examples
awesome-wm

How to set focus on a client under mouse cursor when a tag is changed?


When I switch to another tag, a new client gets selected, but it is sometimes not a client that I have my mouse cursor over. To get a client under my mouse pointer focused, I have to either click somewhere on it, or switch to it with Mod4+j / k, or move mouse cursor out and back on that client.

I want awesome to give focus to a client that is under the mouse cursor whenever a tag is changed. How do I do that?

I found a function mouse.object_under_pointer() that finds the client I need, but I don't know when to call that function. Should I connect a handler to some particular signal? I tried connecting to various signals from Signals page on the wiki and checking with naughty.notify() if that is the right signal, but none of them were triggered when I was switching between tags.


Solution

  • This code did the trick, however there should be a better way to do this than setting up a huge 200 ms timer (smaller timeouts didn't properly focus some clients for me, but you can try setting a smaller one).

    tag.connect_signal(
      "property::selected",
      function (t)
        local selected = tostring(t.selected) == "false"
        if selected then
          local focus_timer = timer({ timeout = 0.2 })
          focus_timer:connect_signal("timeout", function()
            local c = awful.mouse.client_under_pointer()
            if not (c == nil) then
              client.focus = c
              c:raise()
            end
            focus_timer:stop()
          end)
          focus_timer:start()
        end
      end
    )
    

    tag is this global object, so you should just place this code anywhere in your rc.lua.