From 20a79ed448c3b350959e806aebf6bbfa67420302 Mon Sep 17 00:00:00 2001 From: actionless Date: Thu, 3 Sep 2020 04:07:53 +0200 Subject: [PATCH] fix(awful: hotkeys_popup: widget: create_column): correct max label width detection --- lib/awful/hotkeys_popup/widget.lua | 14 +++++------- lib/wibox/widget/textbox.lua | 20 ++++++++++++++++++ spec/wibox/widget/textbox_spec.lua | 34 ++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/lib/awful/hotkeys_popup/widget.lua b/lib/awful/hotkeys_popup/widget.lua index 81cc2469..61bd6664 100644 --- a/lib/awful/hotkeys_popup/widget.lua +++ b/lib/awful/hotkeys_popup/widget.lua @@ -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: diff --git a/lib/wibox/widget/textbox.lua b/lib/wibox/widget/textbox.lua index 2c09358a..e76dd73a 100644 --- a/lib/wibox/widget/textbox.lua +++ b/lib/wibox/widget/textbox.lua @@ -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@ diff --git a/spec/wibox/widget/textbox_spec.lua b/spec/wibox/widget/textbox_spec.lua index 10f5028a..160c401b 100644 --- a/spec/wibox/widget/textbox_spec.lua +++ b/spec/wibox/widget/textbox_spec.lua @@ -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 = "test" + 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 = "test" + 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