Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-ui

Source of release 0.19.2

0.19.2 / src / layout / tree.lucb

#==============================================================================================
#
#   layout/tree - The Tree: its entries, lifetime and the lookups the other fragments share
#
#==============================================================================================

# The Tree's methods are split by concern across this module's fragments, each under
# `extend Tree:`: tree_arrange (gathering and placing), tree_focus, tree_dispatch
# (input routing, hover and cursors), tree_draw, and drops (drags from other
# applications).

struct Entry:
    let widget: interop.Interface[Widget]
    let box: interop.Reference[Layout]
    let parent: i64
    var end: usize

## A retained traversal protects callbacks against reentrant tree replacement.
## It is rebuilt transactionally and bounded independently of recursion depth.
pub struct Tree:
    let root: interop.Interface[Widget]
    let identifier: u64
    var entries: Entry[]
    var count: usize
    var width: f64
    var height: f64
    var version: u64
    var focus: u64
    var requested_focus: u64
    var capture: u64
    ## The widget a secondary (right) press went to, which its release goes to as well.
    var secondary_holder: u64
    var return_focus: u64
    ## Where focus was before it went into a non-modal popup (a dialog's
    ## field): it goes back there when that popup closes.
    var dialog_return: u64
    var appearance: Theme
    var pointer_present: bool
    var pointer_x: f64
    var pointer_y: f64
    var animating: bool
    ## The drop area a drag is over, or 0.
    var drop_hover: u64
    ## The box the pointer was last over (hovered), or 0; when the pointer moves
    ## off it, its widget hears pointer_left, so hover drawn on it ends.
    var hover_box: u64

    ## A tree rooted at `content`, which must be visible and parentless; nothing is
    ## gathered until the first arrangement.
    pub func init(content: interop.Interface[Widget], theme: Theme = Theme()) -> !:
        try require_main_thread()
        try theme.validate()
        serial = serial +? 1 else error(result.limit, "UI identities exhausted")
        self.identifier = serial
        self.root = content.clone()
        var empty: Entry[]
        self.entries = empty
        self.count = 0
        self.width = 0.0
        self.height = 0.0
        self.version = 0
        self.focus = 0
        self.requested_focus = 0
        self.hover_box = 0
        self.capture = 0
        self.secondary_holder = 0
        self.return_focus = 0
        self.dialog_return = 0
        self.appearance = theme
        self.pointer_present = false
        self.pointer_x = 0.0
        self.pointer_y = 0.0
        self.animating = false
        self.drop_hover = 0

    ## Whether a popup is still easing toward its placement, so the run loop keeps
    ## drawing frames until it settles instead of blocking.
    pub func is_animating() -> bool:
        return self.animating

    ## The application theme, inherited by every box that sets none of its own.
    pub func theme() -> Theme:
        return self.appearance

    pub func set_theme(theme: Theme) -> !:
        try theme.validate()
        self.appearance = theme
        bump_revision()

    func find(identifier: u64) -> usize?:
        if identifier == 0:
            return none
        var index: usize = 0
        while index < self.count:
            if self.entries[index].box.value().identifier == identifier:
                return index
            index += 1
        return none

    func send(index: usize, event: Event) -> interop.Outcome[unit]:
        let widget = self.entries[index].widget
        widget.enter() catch failure:
            return result.failure[unit](failure.code, failure.message)
        defer widget.leave()
        var native = widget.value()
        let delivered = native.event(event)
        return delivered

    func cancel(identifier: u64) -> interop.Outcome[unit]:
        if let index = self.find(identifier):
            let cancelled = self.send(index, Event(cancelled = true))
            if not cancelled.is_success():
                return cancelled
            cancelled.release()
        return result.success[unit](())

    ## Focus moves off `identifier`: it hears focus_lost first, so a field keeps
    ## (commits) its edit, then a cancellation, which ends any gesture it holds.
    func leave(identifier: u64) -> interop.Outcome[unit]:
        if let index = self.find(identifier):
            let lost = self.send(index, Event(kind = input.EventKind.focus_lost))
            if not lost.is_success():
                return lost
            lost.release()
        return self.cancel(identifier)

    pub func close():
        for entry in self.entries[..<self.count]:
            # Cycle collection can finalize a layout before its application.
            # Closed layouts already released their state; only detach live ones.
            if not entry.box.is_closed():
                let box = entry.box.value()
                box.application = 0
                box.focused = false
                box.focus_within = false
                box.hovered = false
                box.pointer_within = false
        # As in `arrange`: forget the entries before releasing them.
        let previous = self.entries
        let previous_count = self.count
        self.count = 0
        var empty: Entry[]
        self.entries = empty
        release_entries(previous, previous_count)
        self.root.release()

    pub func trace(visit: func(ownership.Object*, void*) -> unit, context: void*):
        self.root.trace(visit, context)
        for entry in self.entries[..<self.count]:
            entry.widget.trace(visit, context)
            entry.box.trace(visit, context)

func release_entries(entries: Entry[], count: usize):
    for entry in entries[..<count]:
        entry.widget.release()
        entry.box.release()
    if entries.length > 0:
        free(entries) in memory.heap

func path_enabled(entries: const Entry[], index: usize) -> bool:
    var cursor = (i64)index
    while cursor >= 0:
        let entry = &entries[(usize)cursor]
        if not entry.box.value().active:
            return false
        cursor = entry.parent
    return true

func require_main_thread() -> !:
    if not thread.is_main():
        error(interop.wrong_thread, "UI layout requires the main thread")

func find_entry(entries: const Entry[], count: usize, identifier: u64) -> usize?:
    for index in 0..<count:
        if entries[index].box.value().identifier == identifier:
            return index
    return none

## How many popups hold the box at `index`, itself included: 0 for the window's own.
func popup_depth(entries: const Entry[], index: usize) -> usize:
    var depth: usize = 0
    var cursor = (i64)index
    while cursor >= 0:
        if entries[(usize)cursor].box.value().popup:
            depth += 1
        cursor = entries[(usize)cursor].parent
    return depth

func in_popup(entries: const Entry[], index: usize) -> bool:
    var cursor = (i64)index
    while cursor >= 0:
        if entries[(usize)cursor].box.value().popup:
            return true
        cursor = entries[(usize)cursor].parent
    return false