From d20652c81bfe60bdc909bdd4c0e88fe76dff7ab5 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 27 Dec 2018 13:44:49 +0100 Subject: [PATCH 1/4] Use fixed vertical main layout This works better with different font heights and no icons (no cropping). --- init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init.lua b/init.lua index 92d0c8e..74a97e8 100644 --- a/init.lua +++ b/init.lua @@ -968,7 +968,7 @@ cyclefocus.cycle = function(startdirection_or_args, args) -- constraint = wibox.layout.constraint(layout, "max", w, h/2) -- wbox:set_widget(constraint) wbox:set_widget(container_layout) - layout = wibox.layout.flex.vertical() + layout = wibox.layout.fixed.vertical() container_inner:set_middle(layout) else layout:reset() From acdf13d72d71626ba3afcef66d3f254bd54d8638 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 27 Dec 2018 13:53:03 +0100 Subject: [PATCH 2/4] layout: revisit and use awful.widget.clienticon if available Fixes https://github.com/blueyed/awesome-cyclefocus/issues/20. --- README.md | 11 ++++-- init.lua | 116 ++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 81 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 905612b..57e5ae8 100644 --- a/README.md +++ b/README.md @@ -228,7 +228,7 @@ cyclefocus = { display_next_count = 3, display_prev_count = 3, - -- Default preset to for entries. + -- Default preset to use for entries. -- `preset_for_offset` (below) gets added to it. default_preset = {}, @@ -244,8 +244,6 @@ cyclefocus = { preset.font = 'sans 8' preset.icon_size = 36 preset.text = escape_markup(cyclefocus.get_client_title(args.client, false)) - - preset.icon = cyclefocus.icon_loader(args.client.icon) end, -- Preset for current entry. @@ -276,6 +274,11 @@ cyclefocus = { function(c, source_c) return not c.minimized end, --luacheck: no unused args }, + -- Experimental: Width of icon column ("max_icon_size", used for margin). + -- This could be "margin" etc instead, but currently only the width for the + -- current entry is known. + icon_col_width = 50, + -- EXPERIMENTAL: only add clients to the history that have been focused by -- cyclefocus. -- This allows to switch clients using other methods, but those are then @@ -301,7 +304,7 @@ cyclefocus = { -- 1: enable, 2: verbose, 3: very verbose, 4: much verbose. debug_level = 0, -- Use naughty notifications for debugging (additional to printing)? - debug_use_naughty_notify = 1, + debug_use_naughty_notify = false, } ``` diff --git a/init.lua b/init.lua index 74a97e8..cb58d4e 100644 --- a/init.lua +++ b/init.lua @@ -52,7 +52,7 @@ cyclefocus = { display_next_count = 3, display_prev_count = 3, - -- Default preset to for entries. + -- Default preset to use for entries. -- `preset_for_offset` (below) gets added to it. default_preset = {}, @@ -68,8 +68,6 @@ cyclefocus = { preset.font = 'sans 8' preset.icon_size = 36 preset.text = escape_markup(cyclefocus.get_client_title(args.client, false)) - - preset.icon = cyclefocus.icon_loader(args.client.icon) end, -- Preset for current entry. @@ -100,6 +98,11 @@ cyclefocus = { function(c, source_c) return not c.minimized end, --luacheck: no unused args }, + -- Experimental: Width of icon column ("max_icon_size", used for margin). + -- This could be "margin" etc instead, but currently only the width for the + -- current entry is known. + icon_col_width = 50, + -- EXPERIMENTAL: only add clients to the history that have been focused by -- cyclefocus. -- This allows to switch clients using other methods, but those are then @@ -128,15 +131,61 @@ cyclefocus = { -- 1: enable, 2: verbose, 3: very verbose, 4: much verbose. debug_level = 0, -- Use naughty notifications for debugging (additional to printing)? - debug_use_naughty_notify = 1, + debug_use_naughty_notify = false, } -local has_gears, gears = pcall(require, 'gears') -if has_gears then - -- Use gears to prevent memory leaking. - cyclefocus.icon_loader = gears.surface.load + +-- Wrap icon widget with margin. +local get_icon_wrapper_widget = function(w, preset) + local icon_margin = preset.icon_margin or 5 + local icon_size = preset.icon_size + local max_icon_size = cyclefocus.icon_col_width + icon_margin = icon_margin + math.max(0, (max_icon_size - icon_size)/2) + -- local iconmarginbox = wibox.container.place(w, 'center', 'center') + local iconmarginbox = wibox.container.margin(w) + iconmarginbox:set_margins(icon_margin) + return iconmarginbox +end + +-- Get widget for client icon. Uses awful.widget.clienticon if available. +if awful.widget.clienticon then + cyclefocus.get_client_icon_widget = function(c, preset) + local w = awful.widget.clienticon(c) + w:set_forced_width(preset.icon_size) + w:set_forced_height(preset.icon_size) + return get_icon_wrapper_widget(w, preset) + end else - cyclefocus.icon_loader = function(icon) return icon end + local has_gears, gears = pcall(require, 'gears') + local icon_loader + if has_gears then + -- Use gears to prevent memory leaking. + icon_loader = gears.surface.load + else + icon_loader = function(icon) return icon end + end + + cyclefocus.get_client_icon_widget = function(c, preset) + if not c.icon then + return + end + local icon = icon_loader(c.icon) + local icon_size = preset.icon_size + + -- Code originally via naughty. + local cairo = require("lgi").cairo + local scaled = cairo.ImageSurface(cairo.Format.ARGB32, icon_size, icon_size) + local cr = cairo.Context(scaled) + cr:scale(preset.icon_size / icon:get_height(), preset.icon_size / icon:get_width()) + cr:set_source_surface(icon, 0, 0) + cr:paint() + icon = scaled + + local iconbox = wibox.widget.imagebox() + iconbox:set_resize(false) + iconbox:set_image(icon) + return get_icon_wrapper_widget(iconbox, preset) + end end -- A set of default filters, which can be used for cyclefocus.cycle_filters. @@ -693,6 +742,9 @@ cyclefocus.cycle = function(startdirection_or_args, args) end end + -- Not documented. + local get_client_icon_widget = args.get_client_icon_widget + local filter_result_cache = {} -- Holds cached filter results. local show_clients = args.show_clients @@ -964,10 +1016,9 @@ cyclefocus.cycle = function(startdirection_or_args, args) container_layout = wibox.container.background(container_layout) container_layout:set_bg(beautiful.bg_normal..'cc') - -- constraint:set_widget(layout) - -- constraint = wibox.layout.constraint(layout, "max", w, h/2) - -- wbox:set_widget(constraint) wbox:set_widget(container_layout) + -- "fixed" appears to work better for when there are no icons to + -- prevent cropping of the text. layout = wibox.layout.fixed.vertical() container_inner:set_middle(layout) else @@ -986,7 +1037,6 @@ cyclefocus.cycle = function(startdirection_or_args, args) }) end local wbox_height = 0 - local max_icon_size = 48 -- Create entry with index, name and screen. local display_entry_for_idx_offset = function(offset, c, _idx, displayed_list) -- {{{ @@ -999,12 +1049,12 @@ cyclefocus.cycle = function(startdirection_or_args, args) idx=_idx, displayed_list=displayed_list } local preset_for_offset = args.preset_for_offset - local preset_cb = preset_for_offset[tostring(offset)] -- Callback for all. if preset_for_offset.default then preset_for_offset.default(preset, args_for_cb) end -- Callback for offset. + local preset_cb = preset_for_offset[tostring(offset)] if preset_cb then preset_cb(preset, args_for_cb) end @@ -1012,32 +1062,11 @@ cyclefocus.cycle = function(startdirection_or_args, args) -- local entry_layout = wibox.layout.flex.horizontal() local entry_layout = wibox.layout.fixed.horizontal() - -- From naughty. - local icon = preset.icon - local icon_margin = 5 - local iconmarginbox - if icon then - local cairo = require("lgi").cairo - local iconbox = wibox.widget.imagebox() - local icon_size = preset.icon_size - if icon_size then - local scaled = cairo.ImageSurface(cairo.Format.ARGB32, icon_size, icon_size) - local cr = cairo.Context(scaled) - cr:scale(icon_size / icon:get_height(), icon_size / icon:get_width()) - cr:set_source_surface(icon, 0, 0) - cr:paint() - icon = scaled - icon_margin = icon_margin + math.max(0, (max_icon_size - icon_size)/2) + if preset.icon_size then + local icon_widget = get_client_icon_widget(c, preset) + if icon_widget then + entry_layout:add(icon_widget) end - - -- Margin. - iconmarginbox = wibox.container.margin(iconbox) - iconmarginbox:set_margins(icon_margin) - - iconbox:set_resize(false) - iconbox:set_image(icon) - - entry_layout:add(iconmarginbox) end local textbox = wibox.widget.textbox() @@ -1045,12 +1074,15 @@ cyclefocus.cycle = function(startdirection_or_args, args) textbox:set_font(preset.font) textbox:set_wrap("word_char") textbox:set_ellipsize("middle") + -- Set height to no wrap with fixed main layout. + local _, h = textbox:get_preferred_size(c.screen) + textbox:set_forced_height(h) local textbox_margin = wibox.container.margin(textbox) textbox_margin:set_margins(dpi(5)) entry_layout:add(textbox_margin) - entry_layout = wibox.container.margin(entry_layout, dpi(5), dpi(5), - dpi(2), dpi(2)) + entry_layout = wibox.container.margin( + entry_layout, dpi(5), dpi(5), dpi(2), dpi(2)) local entry_with_bg = wibox.container.background(entry_layout) if offset == 0 then entry_with_bg:set_fg(beautiful.fg_focus) @@ -1063,7 +1095,7 @@ cyclefocus.cycle = function(startdirection_or_args, args) -- Add height to outer wibox. local context = {dpi=beautiful.xresources.get_dpi(initial_screen)} - local _, h = entry_with_bg:fit(context, wbox.width, 2^20) + _, h = entry_with_bg:fit(context, wbox.width, 2^20) wbox_height = wbox_height + h end -- }}} From 4d7c018fa3bcd61e12806a00669c67b83a48f6b9 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 27 Dec 2018 14:06:16 +0100 Subject: [PATCH 3/4] Bump default icon size --- README.md | 4 ++-- init.lua | 17 ++++++++--------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 57e5ae8..fcdd438 100644 --- a/README.md +++ b/README.md @@ -249,7 +249,7 @@ cyclefocus = { -- Preset for current entry. ["0"] = function (preset, args) preset.font = 'sans 12' - preset.icon_size = 48 + preset.icon_size = 72 preset.text = escape_markup(cyclefocus.get_client_title(args.client, true)) -- Add screen number if there is more than one. if screen.count() > 1 then @@ -277,7 +277,7 @@ cyclefocus = { -- Experimental: Width of icon column ("max_icon_size", used for margin). -- This could be "margin" etc instead, but currently only the width for the -- current entry is known. - icon_col_width = 50, + icon_col_width = 72, -- EXPERIMENTAL: only add clients to the history that have been focused by -- cyclefocus. diff --git a/init.lua b/init.lua index cb58d4e..215b9e1 100644 --- a/init.lua +++ b/init.lua @@ -73,7 +73,7 @@ cyclefocus = { -- Preset for current entry. ["0"] = function (preset, args) preset.font = 'sans 12' - preset.icon_size = 48 + preset.icon_size = 72 preset.text = escape_markup(cyclefocus.get_client_title(args.client, true)) -- Add screen number if there is more than one. if screen.count() > 1 then @@ -101,7 +101,7 @@ cyclefocus = { -- Experimental: Width of icon column ("max_icon_size", used for margin). -- This could be "margin" etc instead, but currently only the width for the -- current entry is known. - icon_col_width = 50, + icon_col_width = 72, -- EXPERIMENTAL: only add clients to the history that have been focused by -- cyclefocus. @@ -137,13 +137,13 @@ cyclefocus = { -- Wrap icon widget with margin. local get_icon_wrapper_widget = function(w, preset) - local icon_margin = preset.icon_margin or 5 - local icon_size = preset.icon_size - local max_icon_size = cyclefocus.icon_col_width - icon_margin = icon_margin + math.max(0, (max_icon_size - icon_size)/2) - -- local iconmarginbox = wibox.container.place(w, 'center', 'center') + local icon_margin = preset.icon_margin or dpi(5) + local icon_center_margin = icon_margin + math.max(0, (cyclefocus.icon_col_width - preset.icon_size)/2) local iconmarginbox = wibox.container.margin(w) - iconmarginbox:set_margins(icon_margin) + iconmarginbox:set_left(icon_center_margin) + iconmarginbox:set_right(icon_center_margin) + iconmarginbox:set_top(icon_margin) + iconmarginbox:set_bottom(icon_margin) return iconmarginbox end @@ -1059,7 +1059,6 @@ cyclefocus.cycle = function(startdirection_or_args, args) preset_cb(preset, args_for_cb) end - -- local entry_layout = wibox.layout.flex.horizontal() local entry_layout = wibox.layout.fixed.horizontal() if preset.icon_size then From b37275395db94016ec79dea122e86d11d38c3f11 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 27 Dec 2018 14:34:23 +0100 Subject: [PATCH 4/4] Use dpi with icon sizes, bump text size --- README.md | 8 ++++---- init.lua | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index fcdd438..8ffd1f0 100644 --- a/README.md +++ b/README.md @@ -242,14 +242,14 @@ cyclefocus = { default = function (preset, args) -- Default font and icon size (gets overwritten for current/0 index). preset.font = 'sans 8' - preset.icon_size = 36 + preset.icon_size = dpi(24) preset.text = escape_markup(cyclefocus.get_client_title(args.client, false)) end, -- Preset for current entry. ["0"] = function (preset, args) - preset.font = 'sans 12' - preset.icon_size = 72 + preset.font = 'sans 14' + preset.icon_size = dpi(36) preset.text = escape_markup(cyclefocus.get_client_title(args.client, true)) -- Add screen number if there is more than one. if screen.count() > 1 then @@ -277,7 +277,7 @@ cyclefocus = { -- Experimental: Width of icon column ("max_icon_size", used for margin). -- This could be "margin" etc instead, but currently only the width for the -- current entry is known. - icon_col_width = 72, + icon_col_width = dpi(36), -- EXPERIMENTAL: only add clients to the history that have been focused by -- cyclefocus. diff --git a/init.lua b/init.lua index 215b9e1..31b192e 100644 --- a/init.lua +++ b/init.lua @@ -66,14 +66,14 @@ cyclefocus = { default = function (preset, args) -- Default font and icon size (gets overwritten for current/0 index). preset.font = 'sans 8' - preset.icon_size = 36 + preset.icon_size = dpi(24) preset.text = escape_markup(cyclefocus.get_client_title(args.client, false)) end, -- Preset for current entry. ["0"] = function (preset, args) - preset.font = 'sans 12' - preset.icon_size = 72 + preset.font = 'sans 14' + preset.icon_size = dpi(36) preset.text = escape_markup(cyclefocus.get_client_title(args.client, true)) -- Add screen number if there is more than one. if screen.count() > 1 then @@ -101,7 +101,7 @@ cyclefocus = { -- Experimental: Width of icon column ("max_icon_size", used for margin). -- This could be "margin" etc instead, but currently only the width for the -- current entry is known. - icon_col_width = 72, + icon_col_width = dpi(36), -- EXPERIMENTAL: only add clients to the history that have been focused by -- cyclefocus.