You need to enable JavaScript to run this app. 如何为awesomewm的awful.menu监听mouse::enter等鼠标进出信号
优惠活动
大模型
产品
解决方案
定价
更多

如何为awesomewm的awful.menu监听mouse::enter等鼠标进出信号

原因说明

awful.menu 是管理多层菜单结构的上层容器,本身不是可渲染的wibox组件,因此不会直接触发针对单个组件的mouse::enter、mouse::leave信号。你可以通过监听菜单实例关联的渲染wibox的信号实现需求。

实现方案

你需要调用菜单实例的wibox属性获取实际渲染的窗口容器,直接对该对象绑定鼠标事件即可,示例实现如下:

local gears = require("gears")
local awful = require("awful")
local naughty = require("naughty")

-- 封装带自动隐藏功能的菜单创建函数
local function create_auto_hide_menu(args)
    local menu = awful.menu(args)
    
    -- 绑定鼠标离开信号
    menu.wibox:connect_signal("mouse::leave", function()
        -- 延迟0.3秒隐藏,避免移动到子菜单时误触关闭
        local hide_timer = gears.timer {
            timeout = 0.3,
            single_shot = true,
            callback = function()
                local current_wibox = mouse.current_wibox
                -- 校验鼠标是否在当前菜单或激活的子菜单区域内
                local check_menu = menu
                local in_menu_area = false
                while check_menu do
                    if check_menu.wibox == current_wibox then
                        in_menu_area = true
                        break
                    end
                    check_menu = check_menu.active_child
                end
                if not in_menu_area then
                    menu:hide()
                end
            end
        }
        hide_timer:start()
    end)

    -- 鼠标进入信号绑定,对应你原来的通知逻辑
    menu.wibox:connect_signal("mouse::enter", function()
        naughty.notify({
            preset = naughty.config.presets.critical,
            title = "Entered",
            text = "Entered"
        })
    end)

    return menu
end

-- 使用示例
local mymenu = create_auto_hide_menu({
    items = {
        { "终端", "alacritty" },
        { "浏览器", "firefox" },
        { "关机", function() awesome.quit() end }
    }
})

注意事项

  • 多级子菜单需要统一用上述封装函数创建,才能保证子菜单离开时也能触发对应的自动隐藏逻辑
  • 延迟时长可以根据自己的操作习惯调整,避免跨菜单移动时出现频繁闪现关闭的问题

内容的提问来源于stack exchange,提问作者Cobalt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 08:36:00