Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.9

0.11.9 / src / shelf_state.luc

## The viewport tool shelf's state (tool_shelf.luc): whether it is open,
## and the tab last shown in each context (shelf_context.context_of's), kept
## in the UI settings (ui_state.luc) as one line of text.

pub class ShelfState:
    pub var open: bool = false
    ## Each context's last tab, by name, side by side.
    var contexts: list[str]
    var names: list[str]

    pub func init(self):
        self.contexts = []
        self.names = []

    ## Context `context`'s last tab ("" none yet).
    pub func tab(self, context: str) -> str:
        for at in 0..<self.contexts.length:
            if self.contexts[at] == context:
                return self.names[at]
        return ""

    ## Tab `name` shown in context `context`.
    pub func set_tab(self, context: str, name: str):
        for at in 0..<self.contexts.length:
            if self.contexts[at] == context:
                self.names[at] = name
                return
        self.contexts.append(context)
        self.names.append(name)

    ## The tabs as text: "edit mesh=Modify;edit cad=Blends / Faces".
    pub func text(self) -> str:
        let parts: list[str] = []
        for at in 0..<self.contexts.length:
            parts.append(self.contexts[at] + "=" + self.names[at])
        return parts.join(";")

    ## The tabs read back from text(); a part without "=" is left out.
    pub func set_text(self, text: str):
        self.contexts.clear()
        self.names.clear()
        for part in text.split(";"):
            let pair = part.split("=")
            if pair.length == 2 and pair[0] != "":
                self.set_tab(pair[0], pair[1])