local Constraints = require "lib.simple_ui.constraints" local SingleChildElement = require "lib.simple_ui.single_child_element" --- @class Padding : SingleChildElement --- @field left number --- @field right number --- @field top number --- @field bottom number local element = setmetatable({}, SingleChildElement) element.__index = element element.type = "Placeholder" element.left = 0 element.right = 0 element.top = 0 element.bottom = 0 --- "When passing layout constraints to its child, padding shrinks the constraints by the given padding, causing the child to layout at a smaller size. --- Padding then sizes itself to its child's size, inflated by the padding, effectively creating empty space around the child." --- --- as in https://api.flutter.dev/flutter/widgets/Padding-class.html function element:layout() if not self.child then return end local c = Constraints(self.constraints) c.maxWidth = c.maxWidth - self.left - self.right c.maxHeight = c.maxHeight - self.top - self.bottom c.maxWidth = c.maxWidth > 0 and c.maxWidth or 0 c.maxHeight = c.maxHeight > 0 and c.maxHeight or 0 self.child.constraints = c self.child:layout() self.size = Vec3 { self.child.size.x + self.left + self.right, self.constraints.maxHeight + self.top + self.bottom } self.child.offset = self.offset + Vec3 { self.left, self.top } end return element