From 2249dc3c81001761671153fc5402251d05ac3f90 Mon Sep 17 00:00:00 2001 From: actionless Date: Fri, 18 Dec 2020 15:40:40 +0100 Subject: [PATCH] refactor(menubar, textbox): replace menubar.utils.compute_text_width() with textbox.get_markup_geometry() feat(menubar): allow setting beautiful.menubar_font fix(menubar: init: get_current_page): label return value Update lib/wibox/widget/textbox.lua Co-authored-by: Lucas Schwiderski <4508454+sclu1034@users.noreply.github.com> Update lib/wibox/widget/textbox.lua Co-authored-by: Lucas Schwiderski <4508454+sclu1034@users.noreply.github.com> revert textbox changes fix(menubar: init: show): fix font height detection Update lib/menubar/init.lua fix(menubar: init: get_current_page): list_spacing depends on presence of icon; also take cursor block width into consideration fix(menubar: init: label): force icons to be square fix(menubar: init: get_current_page): also take border_width into consideration when computing available space merge conflict --- lib/menubar/init.lua | 38 ++++++++++++++++++++++++------------ lib/menubar/utils.lua | 12 +++--------- lib/wibox/widget/textbox.lua | 1 + 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/lib/menubar/init.lua b/lib/menubar/init.lua index d84585d7..88c8fa5a 100644 --- a/lib/menubar/init.lua +++ b/lib/menubar/init.lua @@ -70,12 +70,15 @@ end -- @beautiful beautiful.menubar_bg_focus -- @param color +--- Menubar font. +-- @beautiful beautiful.menubar_font +-- @param[opt=beautiful.font] font + -- menubar local menubar = { menu_entries = {} } menubar.menu_gen = require("menubar.menu_gen") menubar.utils = require("menubar.utils") -local compute_text_width = menubar.utils.compute_text_width -- Options section @@ -119,10 +122,9 @@ menubar.right_label = "▶▶" -- @tfield[opt="◀◀"] string left_label menubar.left_label = "◀◀" --- awful.widget.common.list_update adds three times a margin of dpi(4) --- for each item: --- @tfield number list_interspace -local list_interspace = theme.xresources.apply_dpi(4) * 3 +-- awful.widget.common.list_update adds spacing of dpi(4) between items. +-- @tfield number list_spacing +local list_spacing = theme.xresources.apply_dpi(4) --- Allows user to specify custom parameters for prompt.run function -- (like colors). This will merge with the default parameters, overriding affected values. @@ -149,7 +151,7 @@ end --- Get how the menu item should be displayed. -- @param o The menu item. --- @return item name, item background color, background image, item icon. +-- @return item name, item background color, background image, item icon, item args. local function label(o) local fg_color = theme.menubar_fg_normal or theme.menu_fg_normal or theme.fg_normal local bg_color = theme.menubar_bg_normal or theme.menu_bg_normal or theme.bg_normal @@ -160,7 +162,8 @@ local function label(o) return colortext(gstring.xml_escape(o.name), fg_color), bg_color, nil, - o.icon + o.icon, + o.icon and {icon_size=instance.geometry.height} end local function load_count_table() @@ -225,6 +228,11 @@ end -- @tparam number|screen scr Screen -- @return table List of items for current page. local function get_current_page(all_items, query, scr) + + local compute_text_width = function(text, s) + return wibox.widget.textbox.get_markup_geometry(text, s, instance.font)['width'] + end + scr = get_screen(scr) if not instance.prompt.width then instance.prompt.width = compute_text_width(instance.prompt.prompt, scr) @@ -235,16 +243,19 @@ local function get_current_page(all_items, query, scr) if not menubar.right_label_width then menubar.right_label_width = compute_text_width(menubar.right_label, scr) end + local border_width = theme.menubar_border_width or theme.menu_border_width or 0 local available_space = instance.geometry.width - menubar.right_margin - menubar.right_label_width - menubar.left_label_width - - compute_text_width(query, scr) - instance.prompt.width + compute_text_width(query..' ', scr) - instance.prompt.width - border_width * 2 + -- space character is added as input cursor placeholder local width_sum = 0 local current_page = {} for i, item in ipairs(all_items) do - item.width = item.width or - compute_text_width(item.name, scr) + - (item.icon and instance.geometry.height or 0) + list_interspace + item.width = item.width or ( + compute_text_width(label(item), scr) + + (item.icon and (instance.geometry.height + list_spacing) or 0) + list_spacing * 2 + ) if width_sum + item.width > available_space then if current_item < i then table.insert(current_page, { name = menubar.right_label, icon = nil }) @@ -449,6 +460,7 @@ function menubar.show(scr) local bg_color = theme.menubar_bg_normal or theme.menu_bg_normal or theme.bg_normal local border_width = theme.menubar_border_width or theme.menu_border_width or 0 local border_color = theme.menubar_border_color or theme.menu_border_color + local font = theme.menubar_font or theme.font or "Monospace 10" if not instance then -- Add to each category the name of its key in all_categories @@ -467,11 +479,13 @@ function menubar.show(scr) fg = fg_color, border_width = border_width, border_color = border_color, + font = font, }, widget = common_args.w, prompt = awful.widget.prompt(), query = nil, count_table = nil, + font = font, } local layout = wibox.layout.fixed.horizontal() layout:add(instance.prompt) @@ -490,7 +504,7 @@ function menubar.show(scr) local geometry = menubar.geometry instance.geometry = {x = geometry.x or scrgeom.x, y = geometry.y or scrgeom.y, - height = geometry.height or gmath.round(theme.get_font_height() * 1.5), + height = geometry.height or gmath.round(theme.get_font_height(font) * 1.5), width = (geometry.width or scrgeom.width) - border_width * 2} instance.wibox:geometry(instance.geometry) diff --git a/lib/menubar/utils.lua b/lib/menubar/utils.lua index b112397a..119ca2b4 100644 --- a/lib/menubar/utils.lua +++ b/lib/menubar/utils.lua @@ -19,7 +19,6 @@ local glib = lgi.GLib local w_textbox = require("wibox.widget.textbox") local gdebug = require("gears.debug") local protected_call = require("gears.protected_call") -local gstring = require("gears.string") local unpack = unpack or table.unpack -- luacheck: globals unpack (compatibility with Lua 5.1) local utils = {} @@ -417,14 +416,9 @@ function utils.compute_textbox_width(textbox, s) return w end ---- Compute text width. --- @tparam str text Text. --- @tparam number|screen s Screen --- @treturn int Text width. --- @staticfct menubar.utils.compute_text_width -function utils.compute_text_width(text, s) - local w, _ = w_textbox(gstring.xml_escape(text)):get_preferred_size(s) - return w +function utils.compute_text_width(text, s, font) + gdebug.deprecate("Use 'width = textbox.get_markup_geometry(text, s, font)['width']'.", {deprecated_in=4}) + return w_textbox.get_markup_geometry(text, s, font)['width'] end return utils diff --git a/lib/wibox/widget/textbox.lua b/lib/wibox/widget/textbox.lua index 8a9347f0..b4019381 100644 --- a/lib/wibox/widget/textbox.lua +++ b/lib/wibox/widget/textbox.lua @@ -381,6 +381,7 @@ end -- @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. +-- @staticfct wibox.widget.textbox.get_markup_geometry function textbox.get_markup_geometry(text, s, font) font = font or beautiful.font local pctx = PangoCairo.font_map_get_default():create_context()