【问题标题】:How to set focus on a client under mouse cursor when a tag is changed?更改标签时如何在鼠标光标下将焦点设置在客户端上?
【发布时间】:2015-06-06 14:29:44
【问题描述】:

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

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

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

【问题讨论】:

    标签: awesome-wm


    【解决方案1】:

    这段代码起到了作用,但是应该有比设置一个巨大的 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
    )
    

    tagthis global object,因此您应该将此代码放在您的 rc.lua 中的任何位置。

    【讨论】:

    • 哇。我一直在努力做到这一点,但直到我添加了延迟,我的才起作用。如果您使用小至 1 毫秒的超时,我的工作。
    • 我在客户端对象上使用了tag::history::update,而不是property::selected。这意味着它会在标签发生变化时运行(例如切换标签等)。
    【解决方案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 在没有其他事件挂起时获取回调。这应该意味着“其他一切”都已处理。

    【讨论】:

      【解决方案3】:

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

      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 )
      

      【讨论】:

        猜你喜欢
        • 2012-09-22
        • 1970-01-01
        • 1970-01-01
        • 2010-09-27
        • 1970-01-01
        • 2011-02-28
        • 2011-03-04
        • 2015-06-12
        • 1970-01-01
        相关资源
        最近更新 更多