Home /Modules/gui/SLGuiMount.sleela

SLGuiMount.sleela

lib/gui/version-military/SLGuiMount.sleela · 260 line(s) · sleela

<< lib/gui/version-military/SLGuiMount.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLGuiMount — the reconciler that reflects the SLeeLa-owned
     DOM (SLGuiDom / SLGuiNode) onto real SleelaUI widgets. It mounts the tree
     depth-first (a parent is constructed before its children so each child has a
     real parent handle), records an id->widget-handle map, and on each pass
     applies every dirty node's state to its widget through the `ui*` built-ins:
     text (uiWidgetSetText), visibility (uiWidgetVisible), sensitivity
     (uiWidgetSensitive), and the style-derived accents (suggested/destructive)
     and minimum size. This is the one-way SLeeLa->toolkit half of the loop
     (SLGuiHostAdapter is the toolkit->SLeeLa half). The DOM stays the single
     source of truth; the widgets are its projection.
   
   Family: gui. The reconciler crosses the `ui*` SLVM bridge; SLeeLa owns the
   document it mirrors. >>
#sleela 2.5
material;
class SLGuiMount {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 11339;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-10-19";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-10-16";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }
  static final int MAX_NODES = 65536;

  SLGuiDom dom;
  SLGuiWidgetFactory factory;

  < id -> widget handle map (parallel arrays). >
  String[] ids;
  int[] handles;
  int mapCount;

  < the host-supplied root container the window gives us >
  int rootHandle;
  < total nodes mounted so far >
  int mounted;
  < dirty nodes applied on the last reconcile pass >
  int applied;

  void init(SLGuiDom document, int windowRootHandle) {
    dom = document;
    factory = new SLGuiWidgetFactory();
    ids = new String[MAX_NODES];
    handles = new int[MAX_NODES];
    mapCount = 0;
    rootHandle = windowRootHandle;
    mounted = 0; applied = 0;
  }

  < ---- id -> handle map --------------------------------------------------- >

  int handleOf(String id) {
    for (int i = 0; i < mapCount; i = i + 1) { if (ids[i] == id) { return handles[i]; } }
    return -1;
  }
  void mapId(String id, int handle) {
    int existing = indexOf(id);
    if (existing >= 0) { handles[existing] = handle; return; }
    if (mapCount >= MAX_NODES) { return; }
    ids[mapCount] = id; handles[mapCount] = handle; mapCount = mapCount + 1;
  }
  int indexOf(String id) {
    for (int i = 0; i < mapCount; i = i + 1) { if (ids[i] == id) { return i; } }
    return -1;
  }
  boolean isMounted(String id) { return handleOf(id) >= 0; }

  < ---- full mount --------------------------------------------------------- >

  < Mount the whole document under the window root, depth-first. >
  void mountAll() {
    SLGuiNode root = dom.root();
    if (root == null) { return; }
    < The root element adopts the window's existing root container handle. >
    root.mount(rootHandle);
    mapId(root.getId(), rootHandle);
    mounted = mounted + 1;
    mountChildren(root);
    reconcile();
  }

  < Recursively construct and mount every child of `parent`. >
  void mountChildren(SLGuiNode parent) {
    int ph = handleOf(parent.getId());
    if (ph < 0) { return; }
    for (int i = 0; i < parent.children(); i = i + 1) {
      String childId = parent.childAt(i);
      SLGuiNode child = dom.getElementById(childId);
      if (child == null) { continue; }
      if (!isMounted(childId)) {
        String label = labelFor(child);
        < Overflow-aware construction: a plain container carrying an `overflow` >
        < (or `overflow-x`/`overflow-y`) attribute is promoted to a native >
        < scrolled viewport, so scrollbars appear AUTOMATICALLY when its content >
        < is larger than the frame. Explicit scrolled/scrollview tags already >
        < build one via the factory. >
        int wh = constructWithOverflow(ph, child, label);
        child.mount(wh);
        mapId(childId, wh);
        mounted = mounted + 1;
      }
      mountChildren(child);
    }
  }

  < Mount any nodes added since the last pass (nodes the DOM grew via script >
  < or SLeeLa after the initial mountAll). >
  void mountNew() {
    SLGuiNode root = dom.root();
    if (root != null) { mountChildren(root); }
  }

  < ---- reconcile (apply dirty node state to widgets) ---------------------- >

  < Push every dirty node's state onto its widget, then clear the dirty flag. >
  < Returns how many nodes were applied this pass. >
  int reconcile() {
    applied = 0;
    int n = dom.size();
    for (int i = 0; i < n; i = i + 1) {
      SLGuiNode node = dom.nodeAt(i);
      if (node == null || !node.isDirty()) { continue; }
      apply(node);
      node.clearDirty();
      applied = applied + 1;
    }
    return applied;
  }

  < Apply one node's state to its live widget via the `ui*` built-ins. >
  void apply(SLGuiNode node) {
    int wh = node.widgetHandle();
    if (wh < 0) { wh = handleOf(node.getId()); }
    if (wh < 0) { return; }

    < Text (labels/buttons/entries). >
    String t = node.text();
    if (t != null && t != "") { uiWidgetSetText(wh, t); }

    < Visibility and sensitivity. >
    uiWidgetVisible(wh, node.isVisible() ? 1 : 0);
    uiWidgetSensitive(wh, node.isEnabled() ? 1 : 0);

    < Style-derived accents: style "accent" = "suggested" | "destructive". >
    String accent = node.getStyle("accent");
    if (accent != null) {
      uiWidgetSuggested(wh, accent == "suggested" ? 1 : 0);
      uiWidgetDestructive(wh, accent == "destructive" ? 1 : 0);
    }

    < Style-derived minimum size: style "min-width" / "min-height" (integers >
    < as decimal strings; absent -> 0). >
    int minW = intAttr(node.getStyle("min-width"));
    int minH = intAttr(node.getStyle("min-height"));
    if (minW > 0 || minH > 0) { uiWidgetSizeRequest(wh, minW, minH); }
  }

  int mountedCount() { return mounted; }
  int appliedLast() { return applied; }

  < ---- helpers ------------------------------------------------------------ >

  < Construct a child's widget, honouring an `overflow` intent. A container >
  < whose tag is already a scrolled view (scrolled/scrollview/scrollarea) is >
  < built by the factory as usual. Otherwise, if the node carries an `overflow` >
  < attribute (or per-axis `overflow-x` / `overflow-y`), build a native >
  < scrolled viewport with the mapped policy instead of a plain container, so >
  < the content scrolls and scrollbars appear automatically on overflow. The >
  < node's children then mount inside the viewport as its scroll content. >
  < overflow / overflow-x / overflow-y : auto | scroll | always | hidden | >
  < visible | clip | none  (auto is the default when the attr is present >
  < without a value). A per-axis attr overrides the shorthand for that axis. >
  int constructWithOverflow(int parentHandle, SLGuiNode child, String label) {
    String tag = child.getTag();
    if (factory.isScrolled(tag)) {
      return factory.construct(parentHandle, tag, label);
    }
    String ov = child.getAttribute("overflow");
    String ox = child.getAttribute("overflow-x");
    String oy = child.getAttribute("overflow-y");
    if (ov == null && ox == null && oy == null) {
      < no overflow intent >
      return factory.construct(parentHandle, tag, label);
    }
    < Resolve each axis: a per-axis attr wins; else the shorthand; else auto. >
    String hx = (ox != null) ? ox : ov;
    String hy = (oy != null) ? oy : ov;
    int policyH = factory.overflowPolicy(hx);
    int policyV = factory.overflowPolicy(hy);
    return factory.constructScrolled(parentHandle, policyH, policyV);
  }

  < The construction label for a node: its text, or its "placeholder" attr for >
  < entries, or empty. >
  String labelFor(SLGuiNode node) {
    String t = node.text();
    if (t != null && t != "") { return t; }
    String ph = node.getAttribute("placeholder");
    if (ph != null) { return ph; }
    return "";
  }

  < Parse a non-negative integer from a decimal string ("" / null -> 0). >
  int intAttr(String s) {
    if (s == null || s == "") { return 0; }
    int value = 0;
    for (int i = 0; i < s.length(); i = i + 1) {
      int d = digit(s.substring(i, i + 1));
      if (d < 0) { return 0; }
      value = value * 10 + d;
    }
    return value;
  }
  int digit(String ch) {
    if (ch == "0") { return 0; } if (ch == "1") { return 1; }
    if (ch == "2") { return 2; } if (ch == "3") { return 3; }
    if (ch == "4") { return 4; } if (ch == "5") { return 5; }
    if (ch == "6") { return 6; } if (ch == "7") { return 7; }
    if (ch == "8") { return 8; } if (ch == "9") { return 9; }
    return -1;
  }
}