From c685b6d72509ca90626a1283e803684dba4dbf52 Mon Sep 17 00:00:00 2001 From: aidinio Date: Wed, 19 Feb 2025 14:00:04 +0330 Subject: [PATCH 1/3] Add a rect shape with corners having different radii --- lib/gears/shape.lua | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/lib/gears/shape.lua b/lib/gears/shape.lua index e9f50e8f53..a1ab2b09a2 100644 --- a/lib/gears/shape.lua +++ b/lib/gears/shape.lua @@ -317,6 +317,66 @@ function module.partially_rounded_rect(cr, width, height, tl, tr, br, bl, rad) cr:close_path() end +--- A rounded rect with individually defined corner radii. +-- +-- @DOC_gears_shape_individually_rounded_rect_EXAMPLE@ +-- +-- @param cr A cairo context +-- @tparam number width The shape width +-- @tparam number height The shape height +-- @tparam boolean tl The top left corner's radius +-- @tparam boolean tr The top right corner's radius +-- @tparam boolean br The bottom right corner's radius +-- @tparam boolean bl The bottom left corner's radius +-- @noreturn +-- @staticfct gears.shape.individually_rounded_rect +function module.individually_rounded_rect(cr, width, height, tl, tr, br, bl) + local corners = {tl = tl, tr = tr, br = br, bl = bl} + for key, val in pairs(corners) do + corners[key] = val or 10 + if width / 2 < val then + corners[key] = width / 2 + end + if height / 2 < val then + corners[key] = height / 2 + end + end + + -- In case there is already some other path on the cairo context: + -- Make sure the close_path() below goes to the right position. + cr:new_sub_path() + + -- Top left + if tl then + cr:arc( corners.tl, corners.tl, corners.tl, math.pi, 3*(math.pi/2)) + else + cr:move_to(0,0) + end + + -- Top right + if tr then + cr:arc( width-corners.tr, corners.tr, corners.tr, 3*(math.pi/2), math.pi*2) + else + cr:line_to(width, 0) + end + + -- Bottom right + if br then + cr:arc( width-corners.br, height-corners.br, corners.br, math.pi*2 , math.pi/2) + else + cr:line_to(width, height) + end + + -- Bottom left + if bl then + cr:arc( corners.bl, height-corners.bl, corners.bl, math.pi/2, math.pi) + else + cr:line_to(0, height) + end + + cr:close_path() +end + --- A rounded rectangle with a triangle at the top. -- -- @DOC_gears_shape_infobubble_EXAMPLE@ From b85a0d99b32f2f2f698cab37d3a2ec8b0d0289e2 Mon Sep 17 00:00:00 2001 From: aidinio Date: Wed, 19 Feb 2025 21:04:23 +0330 Subject: [PATCH 2/3] Fix the issues and redundancies --- lib/gears/shape.lua | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/gears/shape.lua b/lib/gears/shape.lua index a1ab2b09a2..8d4861b604 100644 --- a/lib/gears/shape.lua +++ b/lib/gears/shape.lua @@ -318,22 +318,18 @@ function module.partially_rounded_rect(cr, width, height, tl, tr, br, bl, rad) end --- A rounded rect with individually defined corner radii. --- --- @DOC_gears_shape_individually_rounded_rect_EXAMPLE@ --- -- @param cr A cairo context -- @tparam number width The shape width -- @tparam number height The shape height --- @tparam boolean tl The top left corner's radius --- @tparam boolean tr The top right corner's radius --- @tparam boolean br The bottom right corner's radius --- @tparam boolean bl The bottom left corner's radius +-- @tparam number tl The top left corner's radius +-- @tparam number tr The top right corner's radius +-- @tparam number br The bottom right corner's radius +-- @tparam number bl The bottom left corner's radius -- @noreturn -- @staticfct gears.shape.individually_rounded_rect function module.individually_rounded_rect(cr, width, height, tl, tr, br, bl) local corners = {tl = tl, tr = tr, br = br, bl = bl} for key, val in pairs(corners) do - corners[key] = val or 10 if width / 2 < val then corners[key] = width / 2 end From 73e98f448567b733d1df276e913e5ee49810a124 Mon Sep 17 00:00:00 2001 From: aidinio Date: Fri, 21 Feb 2025 15:09:00 +0330 Subject: [PATCH 3/3] Add tests --- lib/gears/shape.lua | 3 +++ .../gears/shape/individually_rounded_rect.lua | 13 +++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 tests/examples/gears/shape/individually_rounded_rect.lua diff --git a/lib/gears/shape.lua b/lib/gears/shape.lua index 8d4861b604..94e535f9db 100644 --- a/lib/gears/shape.lua +++ b/lib/gears/shape.lua @@ -318,6 +318,9 @@ function module.partially_rounded_rect(cr, width, height, tl, tr, br, bl, rad) end --- A rounded rect with individually defined corner radii. +-- +-- @DOC_gears_shape_individually_rounded_rect_EXAMPLE@ +-- -- @param cr A cairo context -- @tparam number width The shape width -- @tparam number height The shape height diff --git a/tests/examples/gears/shape/individually_rounded_rect.lua b/tests/examples/gears/shape/individually_rounded_rect.lua new file mode 100644 index 0000000000..a79c908f28 --- /dev/null +++ b/tests/examples/gears/shape/individually_rounded_rect.lua @@ -0,0 +1,13 @@ +--DOC_GEN_IMAGE --DOC_HIDE +local shape,cr,show = ... --DOC_HIDE + +shape.individually_rounded_rect(cr, 70, 70, 0, 10, 20, 30) +show(cr) --DOC_HIDE + +shape.individually_rounded_rect(cr, 70, 70, 10, 0, 35, 5) +show(cr) --DOC_HIDE + +shape.individually_rounded_rect(cr, 70, 70, 30, 20, 10, 1) +show(cr) --DOC_HIDE + +--DOC_HIDE vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80