fix(awful: hotkeys_popup: widget: create_column): correct max label width detection

This commit is contained in:
actionless 2020-09-03 04:07:53 +02:00
parent 901bb3d88e
commit 20a79ed448
3 changed files with 59 additions and 9 deletions

View File

@ -484,15 +484,12 @@ function widget.new(args)
local function insert_keys(_keys, _add_new_column)
local max_label_width = 0
local max_label_content = ""
local joined_labels = ""
for i, key in ipairs(_keys) do
local length = string.len(key.key or '') + string.len(key.description or '')
local modifiers = key.mod
if not modifiers or modifiers == "none" then
modifiers = ""
else
length = length + string.len(modifiers) + 1 -- +1 for "+" character
modifiers = markup.fg(self.modifiers_fg, modifiers.."+")
end
local rendered_hotkey = markup.font(self.font,
@ -500,16 +497,15 @@ function widget.new(args)
) .. markup.font(self.description_font,
key.description or ""
)
if length > max_label_width then
max_label_width = length
max_label_content = rendered_hotkey
local label_width = wibox.widget.textbox.get_markup_geometry(rendered_hotkey, s).width
if label_width > max_label_width then
max_label_width = label_width
end
joined_labels = joined_labels .. rendered_hotkey .. (i~=#_keys and "\n" or "")
end
current_column.layout:add(wibox.widget.textbox(joined_labels))
local max_width, _ = wibox.widget.textbox(max_label_content):get_preferred_size(s)
max_width = max_width + self.group_margin
if not current_column.max_width or max_width > current_column.max_width then
local max_width = max_label_width + self.group_margin
if not current_column.max_width or (max_width > current_column.max_width) then
current_column.max_width = max_width
end
-- +1 for group label:

View File

@ -373,6 +373,26 @@ function textbox.mt.__call(_, ...)
return new(...)
end
--- Get geometry of text label, as if textbox would be created for it on the screen.
--
-- @tparam string text The text content, pango markup supported.
-- @tparam[opt=nil] integer|screen s The screen on which the textbox would be displayed.
-- @tparam[opt=beautiful.font] string font The font description as string.
-- @treturn table Geometry (width, height) hashtable.
function textbox.get_markup_geometry(text, s, font)
font = font or beautiful.font
local pctx = PangoCairo.font_map_get_default():create_context()
local playout = Pango.Layout.new(pctx)
playout:set_font_description(beautiful.get_font(font))
local dpi_scale = beautiful.xresources.get_dpi(s)
pctx:set_resolution(dpi_scale)
playout:context_changed()
local attr, parsed = Pango.parse_markup(text, -1, 0)
playout.attributes, playout.text = attr, parsed
local _, logical = playout:get_pixel_extents()
return logical
end
--@DOC_widget_COMMON@
--@DOC_object_COMMON@

View File

@ -5,6 +5,17 @@
local textbox = require("wibox.widget.textbox")
local test_dpi_value = 192
_G.screen = {
{ geometry = { x = 0, y = 0, width = 1920, height = 1080 }, index = 1, dpi=test_dpi_value },
}
local beautiful = require("beautiful")
local xresources = require("beautiful.xresources")
beautiful.font = 'Monospace 10'
xresources.get_dpi = function(_) return test_dpi_value end
package.loaded["beautiful"] = beautiful
package.loaded["beautiful.xresources"] = xresources
describe("wibox.widget.textbox", function()
local widget
before_each(function()
@ -113,6 +124,29 @@ describe("wibox.widget.textbox", function()
assert.is.equal(2, layout_changed)
end)
end)
describe("auxiliary", function()
it("can compute text geometry w/default font", function()
local text = "<b><i>test</i></b>"
local s = 1
local pango_geometry = textbox.get_markup_geometry(text, s)
local actual_textbox_width, actual_textbox_height = textbox(text):get_preferred_size(s)
assert.is.equal(pango_geometry.width, actual_textbox_width)
assert.is.equal(pango_geometry.height, actual_textbox_height)
end)
it("can compute text geometry w/hardcoded font", function()
local text = "<span font='Monospace 16'><b><i>test</i></b></span>"
local s = 1
local pango_geometry = textbox.get_markup_geometry(text, s)
local actual_textbox_width, actual_textbox_height = textbox(text):get_preferred_size(s)
assert.is.equal(pango_geometry.width, actual_textbox_width)
assert.is.equal(pango_geometry.height, actual_textbox_height)
end)
end)
end)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80