Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-ui

Source of release 0.19.1

0.19.1 / src / layout / placement.lucb

#==============================================================================================
#
#   layout/placement - Placing children: stacks, splits and custom arrangements
#
#==============================================================================================

func inside(rectangle: Rect, x: f64, y: f64) -> bool:
    return x >= rectangle.x and y >= rectangle.y and x < rectangle.x + rectangle.width and y < rectangle.y + rectangle.height

func intersection(a: Rect, b: Rect) -> Rect:
    let x = math.max(a.x, b.x)
    let y = math.max(a.y, b.y)
    return Rect(x, y, math.max(0.0, math.min(a.x + a.width, b.x + b.width) - x),
                math.max(0.0, math.min(a.y + a.height, b.y + b.height) - y))

func along(size: Size, horizontal: bool) -> f64:
    return size.width if horizontal else size.height

## One exponential step of `current` toward `target`; called each frame while a popup
## animates, so the motion is fast at first and eases in as it approaches its place.
func ease_rect(current: Rect, target: Rect, factor: f64) -> Rect:
    return Rect(current.x + (target.x - current.x) * factor,
                current.y + (target.y - current.y) * factor,
                current.width + (target.width - current.width) * factor,
                current.height + (target.height - current.height) * factor)

## Within half a pixel on every side, so the ease can snap to the target and stop.
func settled(a: Rect, b: Rect) -> bool:
    return math.abs(a.x - b.x) < 0.5 and math.abs(a.y - b.y) < 0.5 and math.abs(a.width - b.width) < 0.5 and math.abs(a.height - b.height) < 0.5

## Give entry `index` its rectangle and clip, then place its subtree: a split's
## two panes and divider, a custom arrangement's plan, or a stack. A stack
## starts every child at its measured size along the axis, grows or shrinks
## them by weight within their limits to fill the space left after spacing, and
## sizes and aligns them across the axis; an overlay gives every child the whole
## content area within its limits. Popup children are skipped; the tree places
## them afterwards.
func place(entries: Entry[], index: usize, bounds: Rect, clip: Rect) -> !:
    let box = entries[index].box.value()
    box.rectangle = bounds
    box.clipping = intersection(bounds, clip)
    let horizontal = box.axis == Axis.horizontal
    let inner = Rect(bounds.x + box.padding_x, bounds.y + box.padding_y,
                     math.max(0.0, bounds.width - box.padding_x * 2.0), math.max(0.0, bounds.height - box.padding_y * 2.0))
    # A child may retain its minimum size in a smaller window. Clip it to the
    # content area so it cannot paint or hit-test over its parent's frame.
    let content_clip = intersection(box.clipping, inner)
    if let fraction = box.split:
        try place_split(entries, index, inner, content_clip, fraction)
        return
    if let arrangement = box.arrangement:
        try place_custom(entries, index, inner, content_clip, arrangement)
        return
    var child = index + 1
    var total: f64 = 0.0
    var length: usize = 0
    while child < entries[index].end:
        let item = entries[child].box.value()
        if item.popup:
            child = entries[child].end
            continue
        item.rectangle = Rect(width = item.measured.width, height = item.measured.height)
        total += along(item.measured, horizontal)
        length += 1
        child = entries[child].end
    let gaps = (f64)(length - 1) * box.spacing if length > 0 else 0.0
    let available = math.max(0.0, (inner.width if horizontal else inner.height) - gaps)
    var remaining = available - total
    # Weighted water filling respects per-child maxima. Shrinking distributes
    # according to available capacity rather than driving any child below minimum.
    var pass: usize = 0
    while box.axis != Axis.overlay and math.abs(remaining) > 0.000001 and pass <= length:
        var weight: f64 = 0.0
        child = index + 1
        while child < entries[index].end:
            let item = entries[child].box.value()
            if item.popup:
                child = entries[child].end
                continue
            let current = item.rectangle.width if horizontal else item.rectangle.height
            if remaining > 0.0 and current < along(item.maximum, horizontal):
                weight += along(item.growth, horizontal)
            elif remaining < 0.0:
                weight += math.max(0.0, current - along(item.minimum, horizontal)) * along(item.shrink, horizontal)
            child = entries[child].end
        if weight <= 0.0:
            break
        var changed: f64 = 0.0
        child = index + 1
        while child < entries[index].end:
            let item = entries[child].box.value()
            if item.popup:
                child = entries[child].end
                continue
            let current = item.rectangle.width if horizontal else item.rectangle.height
            let capacity = along(item.maximum, horizontal) - current if remaining > 0.0 else current - along(item.minimum, horizontal)
            let share = along(item.growth, horizontal) if remaining > 0.0 else math.max(0.0, capacity) * along(item.shrink, horizontal)
            let delta = math.min(math.max(0.0, capacity), math.abs(remaining) * share / weight) * (1.0 if remaining > 0.0 else -1.0)
            if horizontal:
                item.rectangle.width += delta
            else:
                item.rectangle.height += delta
            changed += delta
            child = entries[child].end
        remaining -= changed
        pass += 1
    var cursor = inner.x if horizontal else inner.y
    child = index + 1
    while child < entries[index].end:
        let item = entries[child].box.value()
        if item.popup:
            child = entries[child].end
            continue
        var rectangle = item.rectangle
        if box.axis == Axis.overlay:
            rectangle = Rect(inner.x, inner.y, math.clamp(inner.width, item.minimum.width, item.maximum.width),
                             math.clamp(inner.height, item.minimum.height, item.maximum.height))
        else:
            let cross = inner.height if horizontal else inner.width
            let desired = rectangle.height if horizontal else rectangle.width
            let minimum = item.minimum.height if horizontal else item.minimum.width
            let maximum = item.maximum.height if horizontal else item.maximum.width
            let placed = math.clamp(cross if box.alignment == Alignment.stretch else math.min(cross, desired), minimum, maximum)
            let spare = math.max(0.0, cross - placed)
            let offset = spare * 0.5 if box.alignment == Alignment.center else (spare if box.alignment == Alignment.end else 0.0)
            if horizontal:
                rectangle.x = cursor
                rectangle.y = inner.y + offset
                rectangle.height = placed
                cursor += rectangle.width + box.spacing
            else:
                rectangle.x = inner.x + offset
                rectangle.y = cursor
                rectangle.width = placed
                cursor += rectangle.height + box.spacing
        try place(entries, child, rectangle, content_clip)
        child = entries[child].end

## Place a split's first pane, divider and second pane: the divider keeps its
## measured thickness and the panes share the rest by `fraction`, within their
## limits.
func place_split(entries: Entry[], index: usize, bounds: Rect, clip: Rect, fraction: f64) -> !:
    let first = index + 1
    if first >= entries[index].end:
        return
    let divider = entries[first].end
    if divider >= entries[index].end:
        return
    let second = entries[divider].end
    if second >= entries[index].end:
        return
    let horizontal = entries[index].box.value().axis == Axis.horizontal
    let leading = entries[first].box.value()
    let trailing = entries[second].box.value()
    let handle = entries[divider].box.value()
    let gap = along(handle.measured, horizontal)
    let available = math.max(0.0, (bounds.width if horizontal else bounds.height) - gap)
    let sizes = splitting.distribute(available, fraction, along(leading.minimum, horizontal),
                                     along(leading.maximum, horizontal), along(trailing.minimum, horizontal), along(trailing.maximum, horizontal))
    if horizontal:
        try place(entries, first, Rect(bounds.x, bounds.y, sizes.first, math.clamp(bounds.height, leading.minimum.height, leading.maximum.height)), clip)
        try place(entries, divider, Rect(bounds.x + sizes.first, bounds.y, gap, bounds.height), clip)
        try place(entries, second, Rect(bounds.x + sizes.first + gap, bounds.y, sizes.second, math.clamp(bounds.height, trailing.minimum.height, trailing.maximum.height)), clip)
    else:
        try place(entries, first, Rect(bounds.x, bounds.y, math.clamp(bounds.width, leading.minimum.width, leading.maximum.width), sizes.first), clip)
        try place(entries, divider, Rect(bounds.x, bounds.y + sizes.first, bounds.width, gap), clip)
        try place(entries, second, Rect(bounds.x, bounds.y + sizes.first + gap, math.clamp(bounds.width, trailing.minimum.width, trailing.maximum.width), sizes.second), clip)

## Place direct children where the Arrangement says, relative to the content
## area. The whole plan is gathered and validated before any rectangle changes.
func place_custom(entries: Entry[], index: usize, bounds: Rect, clip: Rect, arrangement: interop.Interface[Arrangement]) -> !:
    # Validate the complete direct-child plan before applying any rectangle.
    # Stable child identities let policies retain geometry across tab visibility.
    var rectangles: Rect[1024]
    var count: usize = 0
    var child = index + 1
    let started = epoch()
    try arrangement.enter()
    defer arrangement.leave()
    while child < entries[index].end:
        let item = entries[child].box.value()
        if not item.popup:
            let placed = arrangement.value().place((i64)item.identifier, Size(bounds.width, bounds.height))
            defer placed.release()
            let rectangle = try placed.get()
            if not math.is_finite(rectangle.x) or not math.is_finite(rectangle.y):
                error(result.invalid, "custom placement coordinates must be finite")
            try extent(rectangle.width)
            try extent(rectangle.height)
            rectangles[count] = rectangle
            count += 1
        child = entries[child].end
    if epoch() != started:
        error(result.busy, "a custom arrangement changed the widget tree")
    count = 0
    child = index + 1
    while child < entries[index].end:
        if not entries[child].box.value().popup:
            let rectangle = rectangles[count]
            try place(entries, child, Rect(bounds.x + rectangle.x, bounds.y + rectangle.y, rectangle.width, rectangle.height), clip)
            count += 1
        child = entries[child].end