From 901bb3d88ef7e822354995f9ab2674df5bb5b042 Mon Sep 17 00:00:00 2001 From: actionless Date: Thu, 3 Sep 2020 04:20:05 +0200 Subject: [PATCH 1/5] fix(awful: hotkeys_popup: widget: create_group_columns): correct max label height detection --- lib/awful/hotkeys_popup/widget.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/awful/hotkeys_popup/widget.lua b/lib/awful/hotkeys_popup/widget.lua index 0157cd33f..81cc24696 100644 --- a/lib/awful/hotkeys_popup/widget.lua +++ b/lib/awful/hotkeys_popup/widget.lua @@ -438,7 +438,10 @@ function widget.new(args) end function widget_instance:_create_group_columns(column_layouts, group, keys, s, wibox_height) - local line_height = beautiful.get_font_height(self.font) + local line_height = math.max( + beautiful.get_font_height(self.font), + beautiful.get_font_height(self.description_font) + ) local group_label_height = line_height + self.group_margin -- -1 for possible pagination: local max_height_px = wibox_height - group_label_height From 20a79ed448c3b350959e806aebf6bbfa67420302 Mon Sep 17 00:00:00 2001 From: actionless Date: Thu, 3 Sep 2020 04:07:53 +0200 Subject: [PATCH 2/5] 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 81cc24696..61bd6664b 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 2c09358ab..e76dd73ab 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 10f5028a5..160c401b4 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 From a2674c2d14ff5ac6a015c75a5b57c83f2db417d9 Mon Sep 17 00:00:00 2001 From: actionless Date: Sat, 26 Sep 2020 04:43:12 +0200 Subject: [PATCH 3/5] feat(awful: hotkeys_poup): better visually split multiple keys --- lib/awful/hotkeys_popup/widget.lua | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/awful/hotkeys_popup/widget.lua b/lib/awful/hotkeys_popup/widget.lua index 61bd6664b..4de1bafe6 100644 --- a/lib/awful/hotkeys_popup/widget.lua +++ b/lib/awful/hotkeys_popup/widget.lua @@ -492,8 +492,19 @@ function widget.new(args) else modifiers = markup.fg(self.modifiers_fg, modifiers.."+") end + local key_label = "" + if key.keylist and #key.keylist > 1 then + for each_key_idx, each_key in ipairs(key.keylist) do + key_label = key_label .. each_key + if each_key_idx ~= #key.keylist then + key_label = key_label .. markup.fg(self.modifiers_fg, '/') + end + end + elseif key.key then + key_label = key.key + end local rendered_hotkey = markup.font(self.font, - modifiers .. (key.key or "") .. " " + modifiers .. key_label .. " " ) .. markup.font(self.description_font, key.description or "" ) From 6b93661048f1b290dea6cce5c1bf89c4de8f3da0 Mon Sep 17 00:00:00 2001 From: actionless Date: Fri, 23 Apr 2021 07:33:22 +0200 Subject: [PATCH 4/5] fix(awful: hotkeys_popup: insert_keys): handle case when user actually binded some key like `<` or `>` which require xml escape for pango --- lib/awful/hotkeys_popup/widget.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/awful/hotkeys_popup/widget.lua b/lib/awful/hotkeys_popup/widget.lua index 3b68b70ce..2dfba1cff 100644 --- a/lib/awful/hotkeys_popup/widget.lua +++ b/lib/awful/hotkeys_popup/widget.lua @@ -475,7 +475,7 @@ function widget.new(args) table.insert(((i 1 then for each_key_idx, each_key in ipairs(key.keylist) do - key_label = key_label .. each_key + key_label = key_label .. gstring.xml_escape(each_key) if each_key_idx ~= #key.keylist then key_label = key_label .. markup.fg(self.modifiers_fg, '/') end end elseif key.key then - key_label = key.key + key_label = gstring.xml_escape(key.key) end local rendered_hotkey = markup.font(self.font, modifiers .. key_label .. " " From 5bd0ff82a6e03ace27ece33b784923661fa147df Mon Sep 17 00:00:00 2001 From: actionless Date: Sun, 25 Apr 2021 20:55:44 +0200 Subject: [PATCH 5/5] fix(wibox: textbox): remove ldoc tags added by merge mistake --- lib/wibox/widget/textbox.lua | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/wibox/widget/textbox.lua b/lib/wibox/widget/textbox.lua index 8982cd231..8a9347f0c 100644 --- a/lib/wibox/widget/textbox.lua +++ b/lib/wibox/widget/textbox.lua @@ -395,10 +395,6 @@ function textbox.get_markup_geometry(text, s, font) return logical end ---@DOC_widget_COMMON@ - ---@DOC_object_COMMON@ - return setmetatable(textbox, textbox.mt) -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80