在 awesome-wm 中自动生成小部件

Auto-generating widgets in awesome-wm

所以我目前想做的就是在 awesome 中实现 rofi。

我想这样做而不只是使用 rofi 的原因是因为我想学习如何 'auto-generate' widgets in awesome。 稍后当我将实现诸如网络小部件之类的东西时,这会派上用场,点击时会显示一个面板,显示可用的 wifi 热点作为行等。所以我只是为了了解 awesome 如何更好地工作。而且,我想要一个程序启动器。

而且,在有人建议之前,我已经知道 awesome 中有一个内置的启动器,而且我也知道有 this。这不是我要找的。我想要 rofi 和 dmenu 拥有相同的东西:我想要在你按键时弹出建议。我希望能够点击建议等。 我想要的是这样的:uhhhh

所以我遇到的问题是:如何自动生成行?我希望能够只在一个地方指定我想要的行数,剩下的就交给真棒了。

我浏览了 Elv 的 github 并找到了 radical 尽管他制作的是一个菜单系统,但我认为我可以使用他的一些代码来做我想做的事.但我不能为了上帝的爱弄清楚它是如何工作的。没有冒犯他,但它并没有得到很好的记录,即使对于用户来说也是如此,而且对于实际解释代码如何工作,没有文档。

所以我的问题是:我怎样才能完成这项工作?我将如何着手制作自动充当行的小部件?

长话短说:

编辑:

我想要的是能够自动创建我的启动器的行。我知道我可以自己对行进行硬编码,让每一行都有不同的 ID,然后我可以编写一个函数,在每次按键时,将使用最相关的匹配更新每个小部件。所以它会是这样的(根本没有测试):


local wibox = require("wibox")
local awful = require("awful")

local num_rows = 10
local row_height = 40

-- set the height of the background in accordance to how many rows there are,
-- and how high each row should be
local prompt_height = row_height * num_rows
local prompt_width = 300

-- make a widget in the middle of the screen
local background = wibox({
    x = awful.screen.focused().geometry.width / 2 - prompt_width / 2,
    y = awful.screen.focused().geometry.height / 2 - prompt_height / 2,
    width = prompt_width,
    height = prompt_height,
    bg = "#111111",
    visible = false,
    ontop = false
})
local rofi_launcher = wibox.widget({
    widget = background,
    {
        -- get a flexible layout so the searchbox and the suggestion boxes get 
        -- scaled to take up all the space of the background
        layout = wibox.layout.flex.vertical,
        { -- the prompt you actually type in
            -- set id here so we can adjust its ratio later, so the magnifying
            -- glass will end up on the right, and the texbox will take up the left side
            id = "searchbox_and_mangifying_glass",
            layout = wibox.layout.ratio.horizontal,
            {
                -- set id so we can use it as a prompt later
                id = "searchbox",
                widget = wibox.widget.textbox,
            },
            {
                widget = wibox.widget.imagebox,
                icon = '~/path/to/magnifying_glass_icon.svg',
                
            },
        },
        { -- this is where I actually create the rows that will display suggestions
            { -- row number 1
                -- make a background for the textbox to sit in, so you can change 
                -- background color later for the selected widget, etc etc. 
                widget = wibox.widget.background,
                    {
                        -- give it an id so we can change what's displayed in the
                        -- textbox when we press keys in the prompt
                        id = "suggestion_1",
                        widget = wibox.widget.textbox,
                    },
            },
            { -- row number 2
                -- background, again
                widget = wibox.widget.background,
                    {
                        -- id and textbox again
                        id = "suggestion_2",
                        widget = wibox.widget.textbox,
                    },
            },
            -- and another 8 (according to the `num_rows` variable) of the same two 
            -- textboxes above. This is exactly my problem. How can I make these 
            -- textboxes automatically and still be able to interact with them to 
            -- display suggestions on the fly, as the user types keys into the prompt?

        },

    },
})

如果这还不够清楚,请告诉我您不明白的地方,我会更新我的问题。

与您的代码一样未经测试,但这会创建一个文本框表,而不是使用声明性布局来创建所有这些文本框:

[SNIP; For shorter code I removed some stuff at the beginning]

local textboxes = {}
local widgets = {}

for i = 1, num_rows do
    local tb = wibox.widget.textbox()
    local bg = wibox.widget.background(tb)
    bg:set_bg("#ff0000") -- The original code did not set a bg color, but that would make the bg widget useless...?

    tb.id = "suggestion_" .. tostring(i) -- This is likely unnecessary, but the original code set these IDs, too

    table.insert(textboxes, tb)
    table.insert(widgets, bg)
end

local rofi_launcher = wibox.widget({
    widget = background,
    {
        -- get a flexible layout so the searchbox and the suggestion boxes get 
        -- scaled to take up all the space of the background
        layout = wibox.layout.flex.vertical,
        { -- the prompt you actually type in
        [SNIP - I did not change anything here; I only removed this part to make the code shorter]
        },
        widgets
    },
})

-- Now make the textboxes display something
textboxes[3].text = "I am the third row"
textboxes[5].text = "I am not"