Home /Modules/guidoc/SLGuiDocListener.sleela

SLGuiDocListener.sleela

lib/guidoc/SLGuiDocListener.sleela · 144 line(s) · sleela

<< lib/guidoc/SLGuiDocListener.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLGuiDocListener — the GUI document listener, in white.
     Its nested Responder is an observer module (a drop-in, admitted through a
     bounded port) that listens to the standing document of one tab. Every
     change latches a repaint, and the listener tells changes made through the
     GUI apart from changes made by someone else (an external change is
     flagged, shown in the status line, and never silently absorbed). It speaks
     the white theme through SLGuiDocWhite. >>
#sleela 3.0
material;
artifact;
pure;
gasoline;
import observer;
import ui;
< Family: guidoc. Drive it from the tab settle loop: beginCycle, pump, >
< endCycle, like any observer module. >
class SLGuiDocListener {
  < ---- 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 = 39244;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-11-11";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-11-11";
  < 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; }

  class Responder {
    SLObserverPort port;
    SLGuiDocWhite white;
    int lastRevision;
    int changes;
    int externalChanges;
    String lastSource;
    String lastExternal;
    int repaint;
    int seenNotes;
    int flagging;
    int quietChanges;

    < flagExternal=false still counts every change but never flags one as >
    < external: it stays quiet in the status line. >
    void attach(String portName, SLGuiDocWhite look, boolean flagExternal) {
      port = new SLObserverPort();
      port.configure(portName);
      white = look;
      lastRevision = 0; changes = 0; externalChanges = 0;
      lastSource = ""; lastExternal = ""; repaint = 1; seenNotes = 0;
      flagging = flagExternal ? 1 : 0; quietChanges = 0;
    }
    SLObserverPort portOf() { return port; }
    int revisionSeen() { return lastRevision; }
    int changeCount() { return changes; }
    int externalCount() { return externalChanges; }
    String latestSource() { return lastSource; }
    String latestExternal() { return lastExternal; }
    int noteCount() { return seenNotes; }
    int quietCount() { return quietChanges; }
    boolean flagsExternal() { return flagging == 1; }

    < Read the mailbox. Returns the number of messages handled. >
    int pump() {
      int n = 0;
      while (port.hasMail()) {
        SLObserverMessage m = port.next();
        n = n + 1;
        if (m.getKind() == 5) {
          lastRevision = m.getRevision();
          changes = changes + 1;
          lastSource = m.getTag();
          if (m.getTag() != "gui") {
            if (flagging == 1) {
              externalChanges = externalChanges + 1;
              lastExternal = m.getTag();
            } else {
              quietChanges = quietChanges + 1;
            }
          }
          repaint = 1;
        } else if (m.getKind() == 0 && m.getFrom() == "observer") {
          seenNotes = seenNotes + 1;
          repaint = 1;
        }
      }
      return n;
    }
    < Take-the-latch: true once per batch of changes. >
    boolean takeRepaint() {
      if (repaint == 1) { repaint = 0; return true; }
      return false;
    }
    < After a load fills the document directly, bring the listener level >
    < with its revision (no change message was sent for the load). >
    void syncTo(int revision) { lastRevision = revision; repaint = 1; }
    boolean hasExternal() { return externalChanges > 0; }
    < Forget the external flag (the user has seen it). >
    void acknowledgeExternal() { lastExternal = ""; externalChanges = 0; }
    String statusRole() { return (externalChanges > 0) ? "status-external" : "status"; }
    String statusStyle() {
      return (externalChanges > 0) ? white.style("status-external") : white.style("status");
    }
    String statusText() {
      if (externalChanges > 0) {
        return "changed by " + lastExternal + " (" + externalChanges + " external change(s))";
      }
      return "in step at revision " + lastRevision;
    }
  }
}