You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Awesome WM任务列表回调中遍历选中任务并修改overline颜色

解决AwesomeWM任务列表选中态修改overline颜色的问题

你不需要遍历objects列表来处理选中任务,每个回调函数里的c参数就是当前任务对应的客户端对象,直接针对它判断选中状态即可。以下是修改后的代码:

s.mytasklist = awful.widget.tasklist {
    screen  = s,
    filter  = awful.widget.tasklist.filter.currenttags,
    buttons = bindings.tasklist_mouse,
    widget_template = {
         {
            layout = wibox.layout.fixed.vertical,
            {
                {
                    left = 100,
                    right = 100,
                    bottom = 5,
                    widget = wibox.container.margin,
                },
                id = "overline",
                bg = beautiful.underline_color,
                shape = gears.shape.rectangle,
                widget = wibox.container.background
            },
            {
                layout = wibox.layout.fixed.horizontal,
                {
                    {
                        id = "icon_role",
                        widget = wibox.widget.imagebox,
                        resize = true,
                        forced_width = 20
                    },
                    margins = 5,
                    widget = wibox.container.margin
                },
                {
                    id = "text_role",
                    widget = wibox.widget.textbox
                },
            },
         },
         id = "background_role",
         widget = wibox.container.background,
         shape = gears.shape.rectangle,
         create_callback = function(self, c, index, objects)
             -- 获取overline元素
             local overline = self:get_children_by_id("overline")[1]
             -- 判断当前任务是否为焦点选中状态
             if c == client.focus then
                 overline.bg = beautiful.selected_underline_color -- 替换成你想要的选中颜色
             else
                 overline.bg = beautiful.underline_color
             end
         end,
         update_callback = function(self, c, index, objects)
             local overline = self:get_children_by_id("overline")[1]
             if c == client.focus then
                 overline.bg = beautiful.selected_underline_color
             else
                 overline.bg = beautiful.underline_color
             end
         end
     },
}

关键说明:

  • create_callback在任务项首次创建时触发,update_callback在任务状态变化(比如焦点切换、标签页切换)时触发,两个回调都需要处理颜色逻辑
  • 通过self:get_children_by_id("overline")[1]获取模板中的overline元素,因为get_children_by_id返回的是匹配id的元素列表
  • 用c == client.focus判断当前任务是否处于选中(获得焦点)状态,你也可以根据需求调整判断条件,比如c.minimized判断是否最小化等

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 14:22:46