From c80cd630f48550b32c6891c5aa473f615f521d47 Mon Sep 17 00:00:00 2001 From: Minijackson Date: Fri, 20 Jan 2023 17:01:08 +0100 Subject: lua-bindings: add constraint.inset --- lua-bindings/diaphragm.lua | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/lua-bindings/diaphragm.lua b/lua-bindings/diaphragm.lua index ad8aac6..57f065f 100644 --- a/lua-bindings/diaphragm.lua +++ b/lua-bindings/diaphragm.lua @@ -173,19 +173,9 @@ function M.rectangle.new(params) end function M.rectangle.surrounding(content, params) - params = params or {} - local margin = params.margin or 0 - local margin_left = params.margin_left or margin - local margin_right = params.margin_right or margin - local margin_top = params.margin_top or margin - local margin_bottom = params.margin_bottom or margin - - return M.rectangle.new(M.util.tbl_extend({ - left = content.left - margin_left, - right = content.right + margin_right, - top = content.top - margin_top, - bottom = content.bottom + margin_bottom, - }, params)) + local result = M.rectangle.new(params) + M.constraint.inset(content, result, params) + return result end M.straight_path = {} @@ -270,6 +260,8 @@ end -- TODO: factor with vstack -- TODO: also as just a set of constraints +-- TODO: add support for "growing" element height (or elements have same size) +-- TODO: add orientation (ltr or rtl) function M.layout.hstack(params) local result = M.shape(params) @@ -398,6 +390,21 @@ function M.constraint.same_width(elems) end end +-- TODO: factor with rectangle surrounding +function M.constraint.inset(child, parent, params) + params = params or {} + local margin = params.margin or 0 + local margin_left = params.margin_left or margin + local margin_right = params.margin_right or margin + local margin_top = params.margin_top or margin + local margin_bottom = params.margin_bottom or margin + + M.constrain(parent.left:eq(child.left - margin_left)) + M.constrain(parent.right:eq(child.right + margin_right)) + M.constrain(parent.top:eq(child.top - margin_top)) + M.constrain(parent.bottom:eq(child.bottom + margin_bottom)) +end + M.constrain = M.core.constrain M.draw = function(params) M.core.draw(M.util.tbl_extend(params, { -- cgit v1.2.3