Home /Modules/gui/gui-events-demo.sleela

gui-events-demo.sleela

lib/gui/version-dual/gui-events-demo.sleela · 132 line(s) · sleela

<< lib/gui/version-dual/gui-events-demo.sleela
   SLeeLa Standard Library Definition.
   Definition: Runnable demonstrator for the /lib/gui event + DHTML + scripting
     layer. It builds a small document (a window with a button and an entry),
     binds callbacks (onClick / onInput) the DHTML way, raises events and shows
     the dispatcher routing them to operations with the take-the-latch latch,
     demonstrates preventDefault/stopPropagation, mutates the DOM live
     (setText/setStyle/hide), and finally drives the SAME interface from an
     SLGuiScript command list — proving the UI can be authored and driven by
     scripting. Self-contained (inline flow + main), in the style of
     gui-demo.sleela.
   
   Family: gui. SLeeLa owns dispatch and the meaning of events; a host only
   raises them and mirrors the DOM onto widgets. >>
#sleela 2.5
material;
class GuiEventsDemo {
  < ---- 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 = 11119;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-10-16";
  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; }

  String boolToStr(boolean b) { return b ? "yes" : "no"; }

  void banner(String t) {
    print("================================================================");
    print("  " + t);
    print("================================================================");
  }

  void main() {
    banner("SLeeLa GUI events: callbacks, DHTML DOM, and scripting");

    < --- Build a document (DHTML-style) ------------------------------------ >
    SLGuiDom dom = new SLGuiDom();
    dom.init();
    SLGuiNode win = dom.createRoot("window", "app");
    SLGuiNode button = dom.appendElement("app", "button", "save");
    button.setText("Save");
    SLGuiNode entry = dom.appendElement("app", "entry", "name");
    entry.setAttribute("placeholder", "Your name");
    print("document nodes -> " + dom.size()
        + "; root children -> " + dom.getElementById("app").children());

    < --- Bind callbacks the DHTML way -------------------------------------- >
    SLGuiHandler onSave = dom.onClick("save", "doSave");
    SLGuiHandler onType = dom.onInput("name", "doValidate");
    print("bound click 'save' -> doSave; input 'name' -> doValidate");

    < --- Raise events; the dispatcher routes them -------------------------- >
    int fired = dom.fire(SLGuiEventType.CLICK, "save", "");
    print("click 'save' -> handlers fired: " + fired
        + "; onSave latched: " + boolToStr(onSave.take()));

    dom.fire(SLGuiEventType.INPUT, "name", "Ada");
    print("input 'name'='Ada' -> onType value: '" + onType.value() + "'");

    < --- preventDefault / stopPropagation ---------------------------------- >
    SLGuiEvent key = new SLGuiEvent();
    key.configure(SLGuiEventType.KEYDOWN, "name");
    key.setKey("Enter");
    dom.addEventListener("name", "keydown", "doSubmit");
    dom.dispatchEvent(key);
    key.preventDefault();
    print("keydown Enter -> defaultPrevented: " + boolToStr(key.isDefaultPrevented()));

    < --- Mutate the DOM live ----------------------------------------------- >
    dom.setText("save", "Saved!");
    dom.setStyle("save", "accent", "suggested");
    dom.setEnabled("save", false);
    print("live mutate -> save.text='" + dom.getElementById("save").text()
        + "', enabled=" + boolToStr(dom.getElementById("save").isEnabled()));

    < --- Drive the SAME DOM from a script ---------------------------------- >
    banner("Scripting: build + drive a panel from a command list");
    SLGuiScript script = new SLGuiScript();
    script.init(dom);
    script.create("box", "panel", "app");
    script.create("label", "status", "panel");
    script.text("status", "Ready");
    script.style("status", "color", "dim");
    script.on("panel", "click", "doPanelClick");
    script.fire("panel", "click", "from-script");
    int ran = script.run();
    print("script commands executed -> " + ran + "/" + script.size()
        + "; ok: " + boolToStr(script.ok()));
    print("status node text -> '" + dom.getElementById("status").text()
        + "'; total nodes -> " + dom.size());

    banner("Done");
    print("SLeeLa owns the DOM, dispatch, and script execution; the host raises");
    print("events and mirrors the dirty nodes onto real SleelaUI widgets.");
  }
}