3

当我切换到另一个标签时,会选择一个新客户端,但有时我将鼠标光标悬停在该客户端上。要使鼠标指针下的客户端聚焦,我必须单击它的某个位置,或者使用Mod4+切换到它j / k,或者将鼠标光标移出并移回该客户端。

每当更改标签时,我都希望将焦点放在鼠标光标下的客户端上。我怎么做?

我找到了一个函数mouse.object_under_pointer()可以找到我需要的客户端,但我不知道何时调用该函数。我应该将处理程序连接到某个特定信号吗?我尝试从wiki 上的 Signals 页面连接到各种信号并检查naughty.notify()这是否是正确的信号,但是当我在标签之间切换时它们都没有被触发。

4

3 回答 3

3

这段代码成功了,但是应该有比设置一个巨大的 200 毫秒计时器更好的方法(较小的超时不能正确地集中一些客户端,但您可以尝试设置一个较小的)。

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这个全局对象,所以你应该把这段代码放在你的rc.lua.

于 2015-06-06T15:27:38.130 回答
2

我知道这已经很老了,但它帮助我想出了这个

function focus_client_under_mouse()
    gears.timer( {  timeout = 0.1,
                    autostart = true,
                    single_shot = true,
                    callback =  function()
                                    local n = mouse.object_under_pointer()
                                    if n ~= nil and n ~= client.focus then
                                        client.focus = n end
                                    end
                  } )
end

screen.connect_signal( "tag::history::update", focus_client_under_mouse )
于 2018-10-14T11:26:10.560 回答
2

应该做两件事:

首先,您应该require("awful.autofocus")从您的配置中删除,以便此模块在您切换标签时不再尝试通过焦点历史来关注某些客户端。

然后,这段代码对我有用:

do
    local pending = false
    local glib = require("lgi").GLib
    tag.connect_signal("property::selected", function()
        if not pending then
            pending = true
            glib.idle_add(glib.PRIORITY_DEFAULT_IDLE, function()
                pending = false
                local c = mouse.current_client
                if c then
                    client.focus = c
                end
                return false
            end)
        end
    end)
end

这直接使用 GLib 在没有其他事件挂起时获取回调。这应该意味着“其他一切”都已处理。

于 2018-10-18T06:27:16.430 回答