Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-2d

Source of release 0.5.0

0.5.0 / src / kit.luc

## The control builders every panel of the window uses, in eleusis's look:
## captioned rows, square icon buttons, flag checkboxes, and a number with its
## slider (a one-row ParameterPanel: scrub, the value ladder, typed arithmetic). A Kit keeps each signal connection it makes; `close`
## drops them all. Also two helpers widgets share: a one-point outline, and
## the dragged files a drop area takes.
from luce_ui.ui import Font, Text, Button, Dialog, HStack, Size, Checkbox, ButtonStyle, Widget, Command, Layout, Painter, ParameterPanel, ParameterSpec
from luce_gpu.gpu import Rect, Color
from interop import Connection

## A number field with its slider, as one parameter row without a label:
## the field scrubs and takes arithmetic, the slider drags.
pub class SliderField:
    ## The row, to place after a caption.
    pub let row: ParameterPanel

    pub func init(self, row: ParameterPanel):
        self.row = row

    pub func value(self) -> float:
        return self.row.value(0) catch _:
            recover 0.0

    ## Show `value` from code, reporting nothing.
    pub func set_value(self, value: float) -> unit!:
        self.row.set_value(0, value)

pub class Kit:
    pub let font: Font
    let connections: list[Connection]
    ## The window's top-level dialogs, one open at a time.
    let dialogs: list[Dialog]

    pub func init(self, font: Font):
        self.font = font
        self.connections = []
        self.dialogs = []

    ## Escape and Enter on the open `dialog` do what its Cancel and OK buttons
    ## do. The dialog becomes one of the window's top-level dialogs.
    pub func dialog_keys(self, dialog: Dialog, cancel: func() -> unit!, accept: func() -> unit!) -> unit!:
        self.connections.append(dialog.on_cancel(cancel))
        self.connections.append(dialog.on_accept(accept))
        self.dialogs.append(dialog)

    ## Whether `dialog` may open: it is open already, or no other top-level
    ## dialog is (a second would stack over the first, as Ctrl-N twice did).
    pub func may_open(self, dialog: Dialog) -> bool:
        if dialog.is_open():
            return true
        for other in self.dialogs:
            if other.is_open():
                return false
        return true

    ## Keep a connection made elsewhere, so `close` drops it too.
    pub func keep(self, connection: Connection):
        self.connections.append(connection)

    ## Run `callback` whenever `command` triggers, from a menu, a shortcut or a button.
    pub func bind(self, command: Command, callback: func() -> unit!) -> unit!:
        self.connections.append(command.on_trigger(callback))

    pub func button(self, text: str, callback: func() -> unit!) -> Button!:
        let button = Button(text, font = self.font)
        self.connections.append(button.on_click(callback))
        return button

    pub func icon_button(self, icon: str, callback: func() -> unit!) -> Button!:
        let button = Button(icon = icon, font = self.font)
        self.connections.append(button.on_click(callback))
        return button

    ## A tool square includes ten points around the shared font line.
    pub func tool_side(self) -> float:
        return self.font.line_height() + 10.0

    ## A square icon button that triggers a tool command.
    pub func tool_button(self, icon: str, command: Command) -> Button!:
        let button = Button(icon = icon, font = self.font)
        self.connections.append(button.on_click(func () -> unit!:
            command.trigger()))
        button.layout().set_size(self.tool_side(), self.tool_side())
        return button

    ## A shelf button: a font line plus three points, plain until hovered.
    pub func shelf_button(self, icon: str, callback: func() -> unit!) -> Button!:
        let button = Button(icon = icon, font = self.font)
        button.set_style(ButtonStyle.plain)
        self.connections.append(button.on_click(callback))
        button.layout().set_size(self.font.line_height() + 3.0, self.font.line_height() + 3.0)
        return button

    ## A flag checkbox; the state itself comes from the model on refresh, so the
    ## callback toggles the flag rather than copying the box's value.
    pub func checkbox(self, label: str, callback: func() -> unit!) -> Checkbox!:
        let box = Checkbox(label, font = self.font)
        self.connections.append(box.on_change(func (value: bool) -> unit!:
            _ = value
            callback()))
        return box

    ## A muted caption in a fixed column.
    pub func caption(self, label: str, width: float) -> Text!:
        let caption = Text(label, font = self.font, muted = true)
        caption.layout().set_limits(Size(width, 0.0), Size(width, 16384.0))
        return caption

    ## A header control with its muted caption before it.
    pub func labelled(self, label: str, control: Widget) -> HStack!:
        return HStack([Text(label, font = self.font, muted = true), control], spacing = 4.0)

    ## A dialog row: a muted label in a `width`-point column, then the control.
    pub func dialog_row(self, label: str, control: Widget, width: float = 140.0) -> Widget!:
        return HStack([self.caption(label, width), control], spacing = 10.0)

    ## A number from `minimum` to `maximum` starting at `value` (also its
    ## default), `decimals` places (the step follows), `suffix` after it, with
    ## a slider; `changed` hears every edit, live while dragging.
    pub func slider_field(self, minimum: float, maximum: float, value: float, decimals: int, suffix: str, changed: func(float) -> unit!) -> SliderField!:
        let row = ParameterPanel([self.number_spec("value", "", minimum, maximum, value, decimals, suffix)], font = self.font)
        row.set_margin(0.0)
        row.layout().set_flex(vertical = 0.0)
        self.connections.append(row.on_change(func (index: int) -> unit!:
            changed(row.value(index))))
        return SliderField(row)

    ## A number parameter with a slider, as the adjustments and dialogs show them.
    pub func number_spec(self, name: str, label: str, minimum: float, maximum: float, value: float, decimals: int, suffix: str) -> ParameterSpec:
        let step = 0.01 if decimals == 2 else (0.1 if decimals == 1 else 1.0)
        return ParameterSpec(name = name, label = label, value = value, minimum = minimum, maximum = maximum, step = step,
                             decimals = decimals, suffix = suffix, slider = true)

    pub func close(self):
        for connection in self.connections:
            connection.disconnect()
        self.connections.clear()

## A one-point outline just inside `rectangle`.
pub func outline(painter: Painter, rectangle: Rect, ink: Color) -> unit!:
    let x = rectangle.x
    let y = rectangle.y
    painter.rectangle(Rect(x, y, rectangle.width, 1.0), ink)
    painter.rectangle(Rect(x, y + rectangle.height - 1.0, rectangle.width, 1.0), ink)
    painter.rectangle(Rect(x, y, 1.0, rectangle.height), ink)
    painter.rectangle(Rect(x + rectangle.width - 1.0, y, 1.0, rectangle.height), ink)

## The paths dragged over `box` that `accept` takes.
pub func dropped_paths(box: Layout, accept: func(str) -> bool) -> list[str]!:
    var found: list[str] = []
    var index = 0
    while index < box.drop_count():
        let path = box.drop_path(index)
        if accept(path):
            found.append(path)
        index += 1
    return found