简体   繁体   中英

Auto-generating widgets in awesome-wm

So what I currently want to do is pretty much implement rofi in awesome.

The reason I want to do this and I don't just use rofi is because I want to learn how to 'auto-generate' widgets in awesome. This will come in handy later when I'll implement things like network widgets that when clicked, shows you a panel, shows you the wifi hotspots available as rows, etc etc. So it's just for me to learn how awesome works better. But also, I want a program launcher.

And also, before someone suggests it, I already know that there's a built-in launcher in awesome, and I also know that there's this . This is not what I'm looking for. I want to have the same thing thing rofi and dmenu have: I want to have suggestions pop up when you press keys. and I want to be able to click on the suggestions, etc. What I want is something like this: uhhhh

So what I'm having problems is this: how do I auto-generate the rows? I want to be able to specify in only one place how many rows I want, and have awesome do the rest.

I've looked through Elv's github and I found radical and even though what he made is a menu system, I thought that I could use some of his code to do what I want. But I can't for the love of god figure out how it works. No offense to him, but it's not all too well docummented, even for users, and for actually explaining how the code works there's no docummentation.

So My question is: How can I make this work? How would I go about making the widgets that act as the rows automatically?

TL;DR:

  • i want to write a program launcher like rofi in awesome
  • i want to be able to specify only in one place the number of rows
  • therefore, (((I think))) I need to automatically generate widgets as rows somehow, how can I do it?

EDIT:

What I want is to be able to create the rows of my launcher automatically. I know I can hardcode the rows myself, have each row have a different id and then I can write a function that on each keypress, will update each widget with the most relevant matches. So it would be something like (not tested at all):


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?

        },

    },
})

If this is not clear enough please do let me know what you don't understand and I will update my question.

Equally untested as your code, but this creates a tables of textboxes instead of using the declarative layout to create all these textboxes:

[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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM