Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-ui

Source of release 0.19.0

0.19.0 / src / widgets / hstack.lucb

## The HStack container widget.
from luce_gpu import gpu
from theme import default_spacing
import interop
from luce_std import math
import ownership
import layout
import result
from layout import Widget, Layout, Size, Constraints, Event
from painter import Painter

## Children laid out in a row, `spacing` points apart inside `padding`, and
## aligned across the row by `alignment`. It draws nothing itself.
pub struct HStack: Widget:
    let box: interop.Reference[Layout]

    pub func init(children: const interop.Interface[Widget][], spacing: f64 = default_spacing,
                  padding: f64 = 0.0, alignment: layout.Alignment = layout.Alignment.stretch) -> !:
        let box = try layout.make_layout(layout.Axis.horizontal, spacing, padding, alignment)
        errdefer box.release()
        try box.value().set_children(children)
        self.box = box

    pub func layout() -> interop.Reference[Layout]:
        return self.box.clone()

    ## Replace the children; see `Layout.set_children`.
    pub func set_children(children: const interop.Interface[Widget][]) -> !:
        try self.box.value().set_children(children)

    pub func measure(available: Constraints) -> interop.Outcome[Size]:
        let size = self.box.value().content_size(available) catch failure:
            return result.failure[Size](failure.code, failure.message)
        return result.success[Size](size)

    pub func draw(target: interop.View[gpu.RenderTarget]) -> interop.Outcome[unit]:
        return result.success[unit](())

    pub func event(event: Event) -> interop.Outcome[unit]:
        return result.success[unit](())

    pub func close():
        self.box.release()

    func trace(visit: func(ownership.Object*, void*) -> unit, context: void*):
        self.box.trace(visit, context)

pub let hstack_type: interop.Type[HStack] = interop.Type[HStack]("HStack", HStack.close, HStack.trace, closeable = true, main_thread = true)