0

So this is the scenario: Let's say I have a textbox that I want to place exactly at the right-hand side of the screen, but STILL letting the textbox retain its own dimensions. In other words, I want to just specify the font, let the textbox pick the right dimensions and then place it at the edge of the screen. It'd be really easy if I knew the dimensions, since then I can do

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

local text_widget = wibox.widget({
    widget = wibox.widget.textbox,
    font = "Roboto Medium 15",
    text = "hello world",
})

local rightmost_place = wibox.widget({
    layout = wibox.layout.manual,
    {
        text_widget,
        point = {
            y = 0, 
            -- You can't actually do the `text_widget.geometry.width`
            x = awful.screen.focused().geometry.width
                - text_widget.geometry.width
        },
        -- We're not setting these so the textbox
        -- will get its size automatically
        -- forced_width = 
        -- forced_height = 
    },
})

So this way, whenever I were to type something in, the textbox will get bigger, but it would automatically reposition itself according to the x coordinate.

Also, this is not just about textboxes. I've had the same problem with other widgets, so my question is: is there some way to let the widgets get the dimensions that they prefer, but still get their geometry to reposition them where you'd like?

Dylan KAS
  • 4,840
  • 2
  • 15
  • 33
  • This is specific to a textbox, so I will not give it as an answer, but: wibox.widget.textbox has a `get_preferred_size_at_dpi()` function. This also highlights why it is so hard to get the size of a textbox: Different screens can have different DPIs, so the same textbox can have different sizes at the same time. Other widgets have other complications. Emmanuel once tried to add an API to get the preferred size for all widgets and I seem to have managed to talk him out of it... – Uli Schlachter Apr 25 '19 at 06:37

1 Answers1

0

The issue is that widgets can be placed in multiple location (with multiple size) at the same time. This is useful for some case like the textclock to have a single instance (with a single timer) for all screens. This makes getting the size a bit tricky. If you don't plan to use the widget in multiple location, you can cheat and add the width/height properties.

-- in the declarative syntax, add an `id`
id = `id_of_the_widget_with_size`,

-- Get the widget (somewhere outside of the declarative section)
local w = rightmost_place:get_children_by_id("id_of_the_widget_with_size")[1]
w._real_draw = w.draw
w.draw = function(self, context, cr, width, height)
    self.width, self.height = width, height
    w._real_draw(self, context, cr, width, height)
end

Now, in theory, the widget will have a width and height property.

  • Thank you, but would this not work for multiple screens? I'm customizing my wibar, so for all I know, this is a case where it could appear on multiple screens. So if this is for one instance, is there a way to do this for multiple instances of this widget? – LawsDontApplyToPigs Apr 24 '19 at 17:28
  • 1
    Why not use the `align` layout for this? The `manual` layout isn't really being helpful here. You could also use `awful.placement` with the `manual` layout. There is also the `place` container which has the ability to anchor widgets to a corner. If resizing is the issue, I could add a `grow` property to it so it always increase its base size to the "new maximum" size of the contained widget. The would address the use case where, for example, a network traffic widget can eventually display values with a different number of digits. I don't know if it's the issue you are facing. – Emmanuel Lepage Vallee Apr 24 '19 at 17:55
  • Thank you, I ended up using the `wibox.container.place` container. – LawsDontApplyToPigs Apr 24 '19 at 19:49