如何从awful.spawn.easy_async中添加小部件?

我想添加一个电源状态小部件,但只有在系统支持它的情况下才添加(我在台式机和笔记本电脑上运行相同的配置文件)。 显然我可以检查主机名,但我宁愿只检查 acpi 是否可用。 目前我有这个:

mybattery = nil
awful.spawn.easy_async("acpi",
                           function(stdout, stderr, reason, exit_code)
                               if string.find(stdout, "^Battery") then
                                   mybattery = batteryarc_widget{
                                       show_current_level = true,
                                       arc_thickness = 1
                                   }
                               end
                           end
    )

然后 mybattery 作为一个小部件添加。这失败了,可能是因为以后更新 mybattery 不会更新 wibar。如何事后添加小部件?或者有没有更好的方法? 我不想同步调用 acpi

后续: 由于此小部件定期轮询 acpi 并在无法找到电池时弹出警告,因此我不想创建小部件,除非它可以工作。 所以采取 Emmanuel 的建议后,我最终得到了以下结果。 vert_sep 只是一个分隔小部件,mybattery稍后在配置文件中添加到 wibar 中。

mybattery = wibox.container.background()
awful.spawn.easy_async("acpi",
                       function(stdout, stderr, reason, exit_code)
                           if string.find(stdout, "^Battery") then
                               local batteryarc_widget =
                                   require("awesome-wm-widgets.batteryarc-widget.batteryarc")
                               local w = wibox.layout.fixed.horizontal()
                               w:add(batteryarc_widget{
                                         show_current_level = true,
                                         arc_thickness = 1})
                               w:add(vert_sep)
                               mybattery.widget = w
                           end
                       end
)
点赞
用户1672598
用户1672598

有多种方法可以实现这个目的。你可以使用布局中的 :add():insert() 方法 [1]。即使 Widget 不会被使用,也可以创建它。使用 visible = falseforced_width = 0 来确保 Widget 不会被显示出来。虽然这种方法会微量降低性能,但比使用命令式布局方法更简单。第三种方法是添加一个占位符容器,稍后再设置它的 .widget。然而此时,使用 visible 的技巧似乎更简单。

[1] https://awesomewm.org/apidoc/widget_layouts/wibox.layout.fixed.html#insert

2020-09-20 04:04:52