Merge pull request #3185 from actionless/hotkeys-popup-better-split-key-labels
Hotkeys popup: better split key labels for multiple keys per one action
This commit is contained in:
commit
35e0acbccb
|
@ -438,7 +438,10 @@ function widget.new(args)
|
||||||
end
|
end
|
||||||
|
|
||||||
function widget_instance:_create_group_columns(column_layouts, group, keys, s, wibox_height)
|
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
|
local group_label_height = line_height + self.group_margin
|
||||||
-- -1 for possible pagination:
|
-- -1 for possible pagination:
|
||||||
local max_height_px = wibox_height - group_label_height
|
local max_height_px = wibox_height - group_label_height
|
||||||
|
@ -472,7 +475,7 @@ function widget.new(args)
|
||||||
table.insert(((i<available_height_items) and new_keys or overlap_leftovers), keys[i])
|
table.insert(((i<available_height_items) and new_keys or overlap_leftovers), keys[i])
|
||||||
end
|
end
|
||||||
keys = new_keys
|
keys = new_keys
|
||||||
table.insert(keys, {key=markup.fg(self.modifiers_fg, "▽"), description=""})
|
table.insert(keys, {key="▽", description=""})
|
||||||
end
|
end
|
||||||
if not current_column then
|
if not current_column then
|
||||||
current_column = {layout=wibox.layout.fixed.vertical()}
|
current_column = {layout=wibox.layout.fixed.vertical()}
|
||||||
|
@ -481,32 +484,39 @@ 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 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 .. 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 = gstring.xml_escape(key.key)
|
||||||
|
end
|
||||||
local rendered_hotkey = markup.font(self.font,
|
local rendered_hotkey = markup.font(self.font,
|
||||||
modifiers .. (key.key or "") .. " "
|
modifiers .. key_label .. " "
|
||||||
) .. 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:
|
||||||
|
|
|
@ -375,6 +375,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
|
||||||
|
|
||||||
return setmetatable(textbox, textbox.mt)
|
return setmetatable(textbox, textbox.mt)
|
||||||
|
|
||||||
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|
||||||
|
|
|
@ -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