Skip to content

Commit c685b6d

Browse files
aidinioactionless
authored andcommitted
Add a rect shape with corners having different radii
1 parent 9475bb9 commit c685b6d

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

lib/gears/shape.lua

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,66 @@ function module.partially_rounded_rect(cr, width, height, tl, tr, br, bl, rad)
317317
cr:close_path()
318318
end
319319

320+
--- A rounded rect with individually defined corner radii.
321+
--
322+
-- @DOC_gears_shape_individually_rounded_rect_EXAMPLE@
323+
--
324+
-- @param cr A cairo context
325+
-- @tparam number width The shape width
326+
-- @tparam number height The shape height
327+
-- @tparam boolean tl The top left corner's radius
328+
-- @tparam boolean tr The top right corner's radius
329+
-- @tparam boolean br The bottom right corner's radius
330+
-- @tparam boolean bl The bottom left corner's radius
331+
-- @noreturn
332+
-- @staticfct gears.shape.individually_rounded_rect
333+
function module.individually_rounded_rect(cr, width, height, tl, tr, br, bl)
334+
local corners = {tl = tl, tr = tr, br = br, bl = bl}
335+
for key, val in pairs(corners) do
336+
corners[key] = val or 10
337+
if width / 2 < val then
338+
corners[key] = width / 2
339+
end
340+
if height / 2 < val then
341+
corners[key] = height / 2
342+
end
343+
end
344+
345+
-- In case there is already some other path on the cairo context:
346+
-- Make sure the close_path() below goes to the right position.
347+
cr:new_sub_path()
348+
349+
-- Top left
350+
if tl then
351+
cr:arc( corners.tl, corners.tl, corners.tl, math.pi, 3*(math.pi/2))
352+
else
353+
cr:move_to(0,0)
354+
end
355+
356+
-- Top right
357+
if tr then
358+
cr:arc( width-corners.tr, corners.tr, corners.tr, 3*(math.pi/2), math.pi*2)
359+
else
360+
cr:line_to(width, 0)
361+
end
362+
363+
-- Bottom right
364+
if br then
365+
cr:arc( width-corners.br, height-corners.br, corners.br, math.pi*2 , math.pi/2)
366+
else
367+
cr:line_to(width, height)
368+
end
369+
370+
-- Bottom left
371+
if bl then
372+
cr:arc( corners.bl, height-corners.bl, corners.bl, math.pi/2, math.pi)
373+
else
374+
cr:line_to(0, height)
375+
end
376+
377+
cr:close_path()
378+
end
379+
320380
--- A rounded rectangle with a triangle at the top.
321381
--
322382
-- @DOC_gears_shape_infobubble_EXAMPLE@

0 commit comments

Comments
 (0)