From 535508566ae35590ab20146f60be0ab75d0f2eb9 Mon Sep 17 00:00:00 2001 From: delta Date: Thu, 18 May 2023 01:56:35 +0200 Subject: [PATCH 1/2] imagebox: add a "cover" fit policy This fit policy emulates the behaviour of CSS' `background-size: cover` Signed-off-by: delta --- lib/wibox/widget/imagebox.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/wibox/widget/imagebox.lua b/lib/wibox/widget/imagebox.lua index 79b6f710b0..8c6e8a5c4e 100644 --- a/lib/wibox/widget/imagebox.lua +++ b/lib/wibox/widget/imagebox.lua @@ -203,6 +203,8 @@ function imagebox:draw(ctx, cr, width, height) aspects[aspect] = 1 elseif policy[aspect] == "auto" then aspects[aspect] = math.min(width / w, height / h) + elseif policy[aspect] == "cover" then + aspects[aspect] = math.max(width / w, height / h) end end @@ -519,6 +521,7 @@ end -- @propertyvalue "auto" Honor the `resize` variable and preserve the aspect ratio. -- @propertyvalue "none" Do not resize at all. -- @propertyvalue "fit" Resize to the widget width. +-- @propertyvalue "cover" Resize to fill widget and preserve the aspect ratio. -- @propemits true false -- @see vertical_fit_policy -- @see resize @@ -534,6 +537,7 @@ end -- @propertyvalue "auto" Honor the `resize` variable and preserve the aspect ratio. -- @propertyvalue "none" Do not resize at all. -- @propertyvalue "fit" Resize to the widget height. +-- @propertyvalue "cover" Resize to fill widget and preserve the aspect ratio. -- @propemits true false -- @see horizontal_fit_policy -- @see resize From e3990e4a93bdac1bd6317a3932b7924abebe285b Mon Sep 17 00:00:00 2001 From: delta Date: Thu, 18 May 2023 02:33:20 +0200 Subject: [PATCH 2/2] modify *_fit_policy examples to include "cover" Signed-off-by: delta --- .../wibox/widget/imagebox/horizontal_fit_policy.lua | 6 ++++-- .../examples/wibox/widget/imagebox/vertical_fit_policy.lua | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/examples/wibox/widget/imagebox/horizontal_fit_policy.lua b/tests/examples/wibox/widget/imagebox/horizontal_fit_policy.lua index 6bfcbc326e..4c60bf2062 100644 --- a/tests/examples/wibox/widget/imagebox/horizontal_fit_policy.lua +++ b/tests/examples/wibox/widget/imagebox/horizontal_fit_policy.lua @@ -69,13 +69,15 @@ parent:add(l) l:add_widget_at(wibox.widget.textbox('horizontal_fit_policy = "auto"'), 1, 1) l:add_widget_at(wibox.widget.textbox('horizontal_fit_policy = "none"'), 2, 1) l:add_widget_at(wibox.widget.textbox('horizontal_fit_policy = "fit"'), 3, 1) -l:add_widget_at(wibox.widget.textbox('imagebox size'), 4, 1) +l:add_widget_at(wibox.widget.textbox('vertical_fit_policy = "cover"'), 4, 1) +l:add_widget_at(wibox.widget.textbox('imagebox size'), 5, 1) for i,size in ipairs({16, 32, 64}) do l:add_widget_at(build_ib(size, "auto"), 1, i + 1) l:add_widget_at(build_ib(size, "none"), 2, i + 1) l:add_widget_at(build_ib(size, "fit" ), 3, i + 1) - l:add_widget_at(cell_centered_widget(wibox.widget.textbox(size..'x'..size)), 4, i + 1) + l:add_widget_at(build_ib(size, "cover"), 4, i + 1) + l:add_widget_at(cell_centered_widget(wibox.widget.textbox(size..'x'..size)), 5, i + 1) end --DOC_HIDE vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/tests/examples/wibox/widget/imagebox/vertical_fit_policy.lua b/tests/examples/wibox/widget/imagebox/vertical_fit_policy.lua index 24744f960e..0d34f45c46 100644 --- a/tests/examples/wibox/widget/imagebox/vertical_fit_policy.lua +++ b/tests/examples/wibox/widget/imagebox/vertical_fit_policy.lua @@ -69,13 +69,15 @@ parent:add(l) l:add_widget_at(wibox.widget.textbox('vertical_fit_policy = "auto"'), 1, 1) l:add_widget_at(wibox.widget.textbox('versical_fit_policy = "none"'), 2, 1) l:add_widget_at(wibox.widget.textbox('vertical_fit_policy = "fit"'), 3, 1) -l:add_widget_at(wibox.widget.textbox('imagebox size'), 4, 1) +l:add_widget_at(wibox.widget.textbox('vertical_fit_policy = "cover"'), 4, 1) +l:add_widget_at(wibox.widget.textbox('imagebox size'), 5, 1) for i,size in ipairs({16, 32, 64}) do l:add_widget_at(build_ib(size, "auto"), 1, i + 1) l:add_widget_at(build_ib(size, "none"), 2, i + 1) l:add_widget_at(build_ib(size, "fit" ), 3, i + 1) - l:add_widget_at(cell_centered_widget(wibox.widget.textbox(size..'x'..size)), 4, i + 1) + l:add_widget_at(build_ib(size, "cover"), 4, i + 1) + l:add_widget_at(cell_centered_widget(wibox.widget.textbox(size..'x'..size)), 5, i + 1) end --DOC_HIDE vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80