fix(awful: hotkeys_popup: widget: create_column): correct max label width detection
This commit is contained in:
parent
901bb3d88e
commit
20a79ed448
|
@ -484,15 +484,12 @@ function widget.new(args)
|
||||||
|
|
||||||
local function insert_keys(_keys, _add_new_column)
|
local function insert_keys(_keys, _add_new_column)
|
||||||
local max_label_width = 0
|
local max_label_width = 0
|
||||||
local max_label_content = ""
|
|
||||||
local joined_labels = ""
|
local joined_labels = ""
|
||||||
for i, key in ipairs(_keys) do
|
for i, key in ipairs(_keys) do
|
||||||
local length = string.len(key.key or '') + string.len(key.description or '')
|
|
||||||
local modifiers = key.mod
|
local modifiers = key.mod
|
||||||
if not modifiers or modifiers == "none" then
|
if not modifiers or modifiers == "none" then
|
||||||
modifiers = ""
|
modifiers = ""
|
||||||
else
|
else
|
||||||
length = length + string.len(modifiers) + 1 -- +1 for "+" character
|
|
||||||
modifiers = markup.fg(self.modifiers_fg, modifiers.."+")
|
modifiers = markup.fg(self.modifiers_fg, modifiers.."+")
|
||||||
end
|
end
|
||||||
local rendered_hotkey = markup.font(self.font,
|
local rendered_hotkey = markup.font(self.font,
|
||||||
|
@ -500,16 +497,15 @@ function widget.new(args)
|
||||||
) .. markup.font(self.description_font,
|
) .. markup.font(self.description_font,
|
||||||
key.description or ""
|
key.description or ""
|
||||||
)
|
)
|
||||||
if length > max_label_width then
|
local label_width = wibox.widget.textbox.get_markup_geometry(rendered_hotkey, s).width
|
||||||
max_label_width = length
|
if label_width > max_label_width then
|
||||||
max_label_content = rendered_hotkey
|
max_label_width = label_width
|
||||||
end
|
end
|
||||||
joined_labels = joined_labels .. rendered_hotkey .. (i~=#_keys and "\n" or "")
|
joined_labels = joined_labels .. rendered_hotkey .. (i~=#_keys and "\n" or "")
|
||||||
end
|
end
|
||||||
current_column.layout:add(wibox.widget.textbox(joined_labels))
|
current_column.layout:add(wibox.widget.textbox(joined_labels))
|
||||||
local max_width, _ = wibox.widget.textbox(max_label_content):get_preferred_size(s)
|
local max_width = max_label_width + self.group_margin
|
||||||
max_width = max_width + self.group_margin
|
if not current_column.max_width or (max_width > current_column.max_width) then
|
||||||
if not current_column.max_width or max_width > current_column.max_width then
|
|
||||||
current_column.max_width = max_width
|
current_column.max_width = max_width
|
||||||
end
|
end
|
||||||
-- +1 for group label:
|
-- +1 for group label:
|
||||||
|
|
|
@ -373,6 +373,26 @@ function textbox.mt.__call(_, ...)
|
||||||
return new(...)
|
return new(...)
|
||||||
end
|
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_widget_COMMON@
|
||||||
|
|
||||||
--@DOC_object_COMMON@
|
--@DOC_object_COMMON@
|
||||||
|
|
|
@ -5,6 +5,17 @@
|
||||||
|
|
||||||
local textbox = require("wibox.widget.textbox")
|
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()
|
describe("wibox.widget.textbox", function()
|
||||||
local widget
|
local widget
|
||||||
before_each(function()
|
before_each(function()
|
||||||
|
@ -113,6 +124,29 @@ describe("wibox.widget.textbox", function()
|
||||||
assert.is.equal(2, layout_changed)
|
assert.is.equal(2, layout_changed)
|
||||||
end)
|
end)
|
||||||
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)
|
end)
|
||||||
|
|
||||||
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|
||||||
|
|
Loading…
Reference in New Issue