Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-ui

Source of release 0.19.3

0.19.3 / src / docking / state / layout.lucb

#==============================================================================================
#
#   state/layout - The dock's arrangement as text, for keeping it across runs
#
#   DESCRIPTION:
#       One line a node, the split tree in pre-order from the root, then the floating
#       windows back to front:
#
#           luce-dock 1
#           split h 0.76            a split side by side (h) or stacked (v), the
#                                   fraction going to its first child; the first
#                                   child's lines follow, then the second's
#           group 1 layers history  a tab group: the selected tab's place among
#                                   them, then each tab's panel id in order
#           float 40 60 300 240 color   a floating window in stack coordinates
#
#       Panels are named by their ids (`Panel(id =)`); a panel without one is written
#       as `-`, which an application restoring the text should treat as unknown. The
#       application rebuilds the arrangement through the DStack's own calls.
#
#==============================================================================================

import strings

## Text appended to a fixed buffer; running out of room is an error rather
## than a cut-off arrangement.
struct LayoutWriter:
    var bytes: u8[]
    var length: usize

    func put(text: str) -> !:
        if self.length + text.length > self.bytes.length:
            error(result.limit, "the dock arrangement is too long to write")
        memory.copy(self.bytes[self.length..<self.length + text.length], text.bytes, text.length)
        self.length += text.length

extend State:
    ## The arrangement as text.
    pub func arrangement_text() -> interop.Owned[str]!:
        let bytes = try new u8[(usize)1 << 16] --- in memory.heap
        defer free(bytes) in memory.heap
        var writer = LayoutWriter(bytes = bytes, length = 0)
        try writer.put("luce-dock 1\n")
        if self.model.root != 0:
            try self.write_node(&writer, self.model.root)
        for order in 0..<self.float_count:
            let index = self.stacking[order]
            let window = self.floats[index]
            var number: u8[96]
            try writer.put(try strings.format(number, f"float {window.x} {window.y} {window.width} {window.height} "))
            try self.write_id(&writer, index)
            try writer.put("\n")
        return try interop.copy_text((str)bytes[..<writer.length])

    ## Node `node` and everything under it, in pre-order.
    func write_node(writer: LayoutWriter*, node: usize) -> !:
        let current = self.model.nodes[node]
        var number: u8[64]
        if current.first == 0:
            let count = self.model.tab_count(node)
            let selected = self.model.tabs[current.selected].order if current.selected != 0 else 0
            try writer.put(try strings.format(number, f"group {selected}"))
            for order in 0..<count:
                try writer.put(" ")
                try self.write_id(writer, self.model.at(node, order))
            try writer.put("\n")
            return
        let axis = "h" if current.horizontal else "v"
        try writer.put(try strings.format(number, f"split {axis} {current.fraction}\n"))
        try self.write_node(writer, current.first)
        try self.write_node(writer, current.second)

    ## Panel slot `index`'s id, or `-` for a panel with none (or with a space in it).
    func write_id(writer: LayoutWriter*, index: usize) -> !:
        let panel = try self.panel(index)
        defer panel.release()
        let id = panel.value().id()
        defer id.release()
        var plain = id.value.length > 0
        for byte in id.value.bytes:
            if byte <= ' ':
                plain = false
        try writer.put(id.value if plain else "-")