Class Rect
A rectangle in logical pixels, made with Rect(...). Methods return a new rect; a rect never changes once made.
- Coordinates and sizes can have fractions.
- Where a Rect is expected, a table with
x,y,widthandheight, or four numbers, works too.
One window on the left and two stacked on the right:
local gap = 8
local area = output:usable_area():inset(gap)
local main, stack = area:split("horizontal", 0.55, gap)
local upper, lower = stack:split("vertical", 0.5, gap)
for _, tile in ipairs({ main, upper, lower }) do
print(tile)
endProperties
rect.x: numberx read only
The left edge.
rect.y: numbery read only
The top edge.
rect.width: numberwidth read only
Never negative. The right edge is x + width.
rect.height: numberheight read only
Never negative. The bottom edge is y + height.
Methods
rect:contains(point: Point) -> booleancontains
Whether the point is inside the rect.
- The left and top edges are inside, the right and bottom edges are not. Two rects that touch never both contain a point.
local function output_under(point)
for _, candidate in ipairs(wm.outputs:all()) do
if candidate:area():contains(point) then return candidate end
end
endrect:intersects(other: Rect) -> booleanintersects
Whether the two rects overlap. Rects that only touch along an edge don't.
rect:intersection(other: Rect) -> Rect?intersection
The area both rects cover.
- nil when they don't overlap, including when they only touch.
rect:merge(other: Rect) -> Rectmerge
The smallest rect that contains both, with any gap between them.
local bounds
for _, each in ipairs(wm.outputs:all()) do
bounds = bounds and bounds:merge(each:area()) or each:area()
endrect:inset(top: number, right: number, bottom: number, left: number) -> Rectinset
Moves each side inwards by its amount, in CSS order.
- A negative amount moves a side outwards.
- The size stops at 0.
Errors for two or three amounts.
local content = Rect(100, 100, 800, 600)
local titlebar = 28
local frame = content:inset(-titlebar, -2, -2, -2)rect:inset(amount: number) -> Rectinset
The same amount on every side. rect:inset(-3) grows a small target by 3 pixels all round.
rect:center() -> Pointcenter
The middle of the rect.
rect:constrain(point: Point) -> Pointconstrain
The nearest point inside the rect.
- A point already inside comes back unchanged.
- A point past the right or bottom edge lands 1 pixel inside it, so
rect:containsis true for the result. - With no width or height, that coordinate is pinned to
xory.
rect:constrain(other: Rect) -> Rectconstrain
other, moved the shortest distance that puts it inside. On an axis where it is bigger than this rect, it is shrunk to fit first.
local wanted = Rect(1700, 900, 640, 480)
local placed = output:usable_area():constrain(wanted)rect:split(axis: "horizontal" | "vertical", ratio: number, gap: number?) -> Rect, Rectsplit
Two parts with gap between them: "horizontal" side by side, "vertical" one above the other.
- The first part gets
ratioof the room left after the gap. The second gets the rest. gapdefaults to 0. A gap wider than the rect leaves both parts empty, the second at the far edge.
Errors for a ratio outside 0 to 1, a negative gap, or another axis name.
local left, right = Rect(0, 0, 1000, 600):split("horizontal", 0.3, 10)Operators
rect + point -> Rectrect + point
The rect moved by the point. point + rect works too.
local shifted = Rect(0, 0, 200, 100) + Point(15.5, -4)rect * number -> Rectrect * number
Position and size scaled by the number. number * rect works too.
- A negative number mirrors the rect through 0,0.
Errors when the other side isn't a number.
rect == other -> booleanrect == other
True only for the same rect object. Two rects made separately with the same edges are not equal, so compare the fields.
rect .. value -> stringrect .. value
The rect as text joined with value turned into text, on either side: "in " .. Rect(0, 0, 10, 20) is "in Rect { x: 0, y: 0, width: 10, height: 20 }".