Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.8

0.11.8 / src / ui_state.luc

## Per-user panel/window/display preferences. Tests and captures opt out by default.
from luce_prism.prism import Document, Value, Encoding
from panels import Panels
import project_values as values
from luce_std import files, paths
from luce_ui import ui
from luce_ui.ui import Application
from luce_std import math
from view_panes import panes_size, valid_panes

pub func settings_path() -> str!:
    return paths.join(paths.join(files.home_directory(), ".luced-3d"), "ui.prisma")

pub class UIState:
    pub var width: int = 1400
    pub var height: int = 900
    pub var warning: str = ""
    var path: str = ""
    var restored: Document? = none
    var written: Document? = none
    ## A write is asked for (`changed`) and not yet done.
    var scheduled: bool = false
    var writable: bool = true

    pub func init(self, path: str = "") -> unit!:
        self.path = path
        if path == "": return
        let text = files.read_text(path, limit = 65536) catch failure:
            return
        self.read(text) catch failure:
            self.warning = "Could not restore UI layout: " + failure.message
            self.writable = false
            return

    func read(self, text: str) -> unit!:
        let doc = Document.parse(text)
        if doc.type_at("/ui") != "luced3d_ui":
            error(ui.invalid, "Unknown UI settings version")
        # The one format save() writes; anything else is not restored.
        _ = values.integer(doc, "/ui", "version", 7, 7)
        self.width = values.integer(doc, "/ui", "width", 800, 8192)
        self.height = values.integer(doc, "/ui", "height", 600, 8192)
        # The viewport's layout and panes' views, and each pane's shading
        # and grid.
        if not valid_panes(values.numbers(doc, "/ui", "panes", panes_size, panes_size)):
            error(ui.invalid, "Invalid viewport panes")
        for mode in values.numbers(doc, "/ui", "shading", 4, 4):
            if not (mode in [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 7.0, 8.0, 9.0, 10.0]):
                error(ui.invalid, "Invalid shading mode")
        _ = values.numbers(doc, "/ui", "grid", 4, 4)
        _ = values.integer(doc, "/ui", "curvature", 0, 4)
        _ = values.scalar(doc, "/ui", "curvature_range")
        _ = values.scalar(doc, "/ui", "curvature_band_low")
        _ = values.scalar(doc, "/ui", "curvature_band_high")
        _ = values.integer(doc, "/ui", "draft_axis", 0, 5)
        _ = values.scalar(doc, "/ui", "draft_angle")
        _ = values.integer(doc, "/ui", "zebra_stripes", 1, 256)
        _ = values.scalar(doc, "/ui", "zebra_duty")
        _ = values.integer(doc, "/ui", "zebra_axis", 0, 3)
        _ = values.flag(doc, "/ui", "zebra_planar")
        _ = values.flag(doc, "/ui", "zebra_stripes_only")
        _ = values.flag(doc, "/ui", "hulls")
        _ = values.flag(doc, "/ui", "normals")
        _ = values.text(doc, "/ui", "dock")
        _ = values.flag(doc, "/ui", "shelf_open")
        _ = values.text(doc, "/ui", "shelf_tabs")
        self.restored = doc

    pub func restore(self, panels: Panels) -> unit!:
        let doc = self.restored else return
        panels.dock_layout.restore(values.text(doc, "/ui", "dock")) catch failure:
            self.warning = "Could not restore panel layout: " + failure.message
            self.writable = false
            return
        let w = panels.workspace
        _ = w.panes.set_state(values.numbers(doc, "/ui", "panes", panes_size, panes_size))
        let modes = values.numbers(doc, "/ui", "shading", 4, 4)
        let grids = values.numbers(doc, "/ui", "grid", 4, 4)
        for at in 0..<4:
            _ = w.panes.views[at].set_display_mode(int(modes[at]))
            w.panes.views[at].grid_visible = grids[at] != 0.0
        w.view = w.panes.view()
        w.display.curvature_kind = values.integer(doc, "/ui", "curvature", 0, 4)
        w.display.curvature_range = math.max(0.0, values.scalar(doc, "/ui", "curvature_range"))
        w.display.curvature_band_low = math.max(0.0, values.scalar(doc, "/ui", "curvature_band_low"))
        w.display.curvature_band_high = math.max(0.0, values.scalar(doc, "/ui", "curvature_band_high"))
        w.display.draft_axis = values.integer(doc, "/ui", "draft_axis", 0, 5)
        w.display.draft_angle = math.max(0.0, math.min(88.0, values.scalar(doc, "/ui", "draft_angle")))
        w.display.zebra_stripes = values.integer(doc, "/ui", "zebra_stripes", 1, 256)
        w.display.zebra_duty = math.max(0.1, math.min(0.9, values.scalar(doc, "/ui", "zebra_duty")))
        w.display.zebra_axis = values.integer(doc, "/ui", "zebra_axis", 0, 3)
        w.display.zebra_planar = values.flag(doc, "/ui", "zebra_planar")
        w.display.zebra_stripes_only = values.flag(doc, "/ui", "zebra_stripes_only")
        w.display.hulls_visible = values.flag(doc, "/ui", "hulls")
        w.display.normals_visible = values.flag(doc, "/ui", "normals")
        w.display.mesh_check_visible = values.flag(doc, "/ui", "mesh_check")
        # The tool shelf: open or not, and its tab in each context.
        let shelf = panels.view.tools.chrome.shelf
        shelf.open = values.flag(doc, "/ui", "shelf_open")
        shelf.set_text(values.text(doc, "/ui", "shelf_tabs"))
        w.touch()
        self.written = doc

    ## Something the settings keep may have changed: they are written half a
    ## second after the first such change, if they did change; one wake for
    ## however many changes come meanwhile, and no disk work per input.
    pub func changed(self, app: Application, panels: Panels) -> unit!:
        if self.path == "" or not self.writable or self.scheduled: return
        self.scheduled = true
        _ = app.after(0.5, func () -> unit!:
            self.scheduled = false
            self.save(panels))

    pub func save(self, panels: Panels) -> unit!:
        if self.path == "" or not self.writable: return
        let bounds = panels.body.layout().bounds()
        if bounds.width < 800.0 or bounds.height < 600.0: return
        let doc = Document()
        doc.add("/ui", "luced3d_ui")
        doc.set("/ui", "version", Value.integer(7))
        doc.set("/ui", "width", Value.integer(int(bounds.width)))
        doc.set("/ui", "height", Value.integer(int(bounds.height)))
        doc.set("/ui", "dock", Value.text(panels.dock_layout.text()))
        let w = panels.workspace
        doc.set("/ui", "panes", Value.numbers([panes_size], w.panes.state()))
        let modes: list[float] = []
        let grids: list[float] = []
        for view in w.panes.views:
            modes.append(float(view.display_mode))
            grids.append(1.0 if view.grid_visible else 0.0)
        doc.set("/ui", "shading", Value.numbers([4], modes))
        doc.set("/ui", "grid", Value.numbers([4], grids))
        doc.set("/ui", "hulls", Value.boolean(w.display.hulls_visible))
        doc.set("/ui", "normals", Value.boolean(w.display.normals_visible))
        doc.set("/ui", "mesh_check", Value.boolean(w.display.mesh_check_visible))
        let shelf = panels.view.tools.chrome.shelf
        doc.set("/ui", "shelf_open", Value.boolean(shelf.open))
        doc.set("/ui", "shelf_tabs", Value.text(shelf.text()))
        doc.set("/ui", "zebra_stripes", Value.integer(w.display.zebra_stripes))
        doc.set("/ui", "zebra_duty", Value.number(w.display.zebra_duty))
        doc.set("/ui", "zebra_axis", Value.integer(w.display.zebra_axis))
        doc.set("/ui", "zebra_planar", Value.boolean(w.display.zebra_planar))
        doc.set("/ui", "zebra_stripes_only", Value.boolean(w.display.zebra_stripes_only))
        doc.set("/ui", "curvature", Value.integer(w.display.curvature_kind))
        doc.set("/ui", "curvature_range", Value.number(w.display.curvature_range))
        doc.set("/ui", "curvature_band_low", Value.number(w.display.curvature_band_low))
        doc.set("/ui", "curvature_band_high", Value.number(w.display.curvature_band_high))
        doc.set("/ui", "draft_axis", Value.integer(w.display.draft_axis))
        doc.set("/ui", "draft_angle", Value.number(w.display.draft_angle))
        if let previous = self.written:
            if previous.equals(doc): return
        files.ensure_directory(paths.directory(self.path))
        doc.save(self.path, Encoding.text)
        self.written = doc