根据内容增加 Wibox 的高度

我有一个 Wibox,其中包含垂直布局和两个文本小部件:

local w = wibox{
    width = 300,
    height = 80,
    ontop = true,
    screen = mouse.screen,
}

w:setup {
    {
        id = 'header',
        widget = wibox.widget.textbox
    },
    {
        id = 'body',
        widget = wibox.widget.textbox
    },
    id = 'text',
    layout = wibox.layout.flex.vertical,
}

当 'body' 文本框中的字符串很短时,一切看起来都很好,小部件的外观如下:

widget with short string

但是,如果字符串很长,则 wibox 的大小不足以显示全部内容,因此一部分字符串被剪切掉:

widget with long string

是否有可能根据内容的大小动态更改 wibox 的大小?

点赞
用户436275
用户436275

如果您要调整内容的 wibox 高度:

更改文本框的文本后,请运行类似以下代码的内容:

local t1 = w:get_children_by_id("header")[1]
local t2 = w:get_children_by_id("body")[1]
local h1 = t1:get_height_for_width(w.width, w.screen)
local h2 = t2:get_height_for_width(w.width, w.screen)
w.height = h1 + h2

如果您想要一些额外的空白空间,就像第一个截图中所示的那样,您可以将一些数字添加到高度中。 42 是一个很好的答案。 ;-)

2017-12-07 10:37:20