Home /Modules/user-interface/gui-mual-text-editor.sleela

gui-mual-text-editor.sleela

lib/user-interface/example/mual/gui-mual-text-editor.sleela · 400 line(s) · sleela

<<
   example/gui-mual-text-editor.sleela
   The MUAL (1.0 baseline) "text editor" scene as a REAL, INTERACTIVE on-screen
   window -- driven by SleelaUI, SLeeLa's own native C/C++ toolkit, through the
   ui* SLVM bridge (OP_UI_CALL -> user-interface/ C ABI -> X11/Cocoa/Win32).
   
   What changed: the toolbar buttons now FUNCTION. Instead of calling the
   toolkit's own uiAppRun() loop (which just waits for the window to close),
   this program runs its OWN event loop: each turn it pumps the toolkit, then
   polls every button's one-shot activation latch (uiWidgetTakeActivated) and
   reacts -- switching which panel is visible (uiWidgetVisible) and updating the
   status line (uiWidgetSetText). So clicking "Editor" / "Documents" / "About"
   actually swaps the panel on screen, and "Save" / "New" update the status.
   
   It also shows a NEW GUI feature: the Documents panel is a uiScrolledView, so
   its long watch-list scrolls and a scrollbar appears AUTOMATICALLY when the
   content overflows the frame.
   
   The three-layer stack is live here:
     SLeeLa decoration (this program + the theme + the event loop)
       -> model (ui* built-ins: uiWindowCreate, uiButton, uiScrolledView, ...)
         -> C/C++ (SleelaUI software rasterizer + native window backend)
   
   Run (needs a display: X on Linux, native on macOS/Windows):
     SLEELA_SHEET=./SHEET.sheet \
     SLEELA_SHA256_MANIFEST=./security/sha256-manifest.json \
     ./impl/build/sleela run example/gui-mual-text-editor.sleela
   
   With no display (headless/CI) it reports that and exits 0 without a window.
>>
#sleela 2.5
material;
// MUAL (1.0) is the stable baseline: the original 25-class surface. SLeeLa owns
// business logic and the meaning of events; here that model is projected onto
// the native toolkit AND its events are handled, so the baseline window is both
// drawn and interactive.
class GuiMualTextEditor {

  // SLUITheme preset ordinals (mirror SLUIThemeId in the toolkit C ABI).
  // Orientation + scroll-policy ordinals (mirror the toolkit C ABI).
  int VERTICAL = 0;
  int HORIZONTAL = 1;
  int SCROLL_AUTO = 0;

  // --- live widget handles the event loop reacts on/to ---
  int editorPanel;      // panel: the editor
  int docsPanel;        // panel: the 1..14 document listener (scrolled)
  int aboutPanel;       // panel: about/help
  int statusLabel;      // the status line text
  // Lower-right status-bar connectivity control and its dismissible menu.
  int wifiButton;
  int wifiPanel;
  int wifiStatusLabel;
  int wifiScanButton;
  int wifiConnectButton;
  int wifiDisconnectButton;
  int wifiSettingsButton;
  int wifiInfoButton;
  int wifiProfileEntry;
  int wifiCloseButton;
  int btnEditor;        // toolbar buttons
  int btnDocs;
  int btnAbout;
  int btnSave;
  int btnNew;
  // Menubar strip and its dropdown panels (built from existing ui* widgets).
  int openMenuIndex = -1;
  int menuFileButton;
  int menuFilePanel;
  int menuFileItem1;
  int menuFileItem2;
  int menuEditButton;
  int menuEditPanel;
  int menuEditItem1;
  int menuEditItem2;
  int menuViewButton;
  int menuViewPanel;
  int menuViewItem1;
  int menuViewItem2;
  int menuHelpButton;
  int menuHelpPanel;
  int menuHelpItem1;
  int menuHelpItem2;


  int rgba(int r, int g, int b, int a) { return r * 16777216 + g * 65536 + b * 256 + a; }
  int rgb(int r, int g, int b) { return rgba(r, g, b, 255); }

  // Standard Pattern ordinal (mirror SLUIThemeId in the toolkit C ABI):
  // 4=WHITE, 5=BLACK, 6=BLUE, 7=GRAY. This line (mual) uses WHITE.
  int THEME_WHITE = 4;

  // Build the baseline editor theme and return its handle.
  // mual is the WHITE Standard Pattern: a light surface family with dark text
  // and a blue accent, supplied by the toolkit preset (no per-colour overrides);
  // only the family metrics are tuned here.
  int baselineTheme() {
    int t = uiThemePreset(THEME_WHITE);
    uiThemeSetRadius(t, 8);
    uiThemeSetControlHeight(t, 32);
    return t;
  }

  // Show exactly one panel; mark the active toolbar button as suggested.
  void showPanel(int which) {
    uiWidgetVisible(editorPanel, which == 0 ? 1 : 0);
    uiWidgetVisible(docsPanel,   which == 1 ? 1 : 0);
    uiWidgetVisible(aboutPanel,  which == 2 ? 1 : 0);
    uiWidgetSuggested(btnEditor, which == 0 ? 1 : 0);
    uiWidgetSuggested(btnDocs,   which == 1 ? 1 : 0);
    uiWidgetSuggested(btnAbout,  which == 2 ? 1 : 0);
  }


  // Show one dropdown at a time; selecting the open menu closes it.
  void openMenu(int which) {
    int next = openMenuIndex == which ? -1 : which;
    openMenuIndex = next;
    uiWidgetVisible(menuFilePanel, next == 0 ? 1 : 0);
    uiWidgetVisible(menuEditPanel, next == 1 ? 1 : 0);
    uiWidgetVisible(menuViewPanel, next == 2 ? 1 : 0);
    uiWidgetVisible(menuHelpPanel, next == 3 ? 1 : 0);
    uiWidgetVisible(wifiPanel, next == 4 ? 1 : 0);
  }

  void wifiActionUnavailable(String action) {
    uiWidgetSetText(wifiStatusLabel, action + " requires the native Wi-Fi host bridge.");
    uiWidgetSetText(statusLabel, "Connectivity · " + action + " unavailable (host bridge required)");
    openMenu(-1);
  }

  void main() {
    print("SLeeLa MUAL Text Editor (native SleelaUI) — backend "
        + uiBackendName(uiBackend()) + ", toolkit " + uiVersion());

    int app = uiAppCreate("com.mearvk.SleelaUI.MualTextEditor");
    if (app < 0) {
      print("No display available (headless): the ui* bridge is wired, but the");
      print("toolkit could not open a window. Run on a desktop (X/Cocoa/Win32).");
      return;
    }

    int theme = baselineTheme();
    // Derive the initial frame from the real screen. Requested design dimensions
    // are preferences only; the physical screen is the hard upper bound.
    int screenW = uiScreenWidth(app);
    int screenH = uiScreenHeight(app);
    int winW = 900;
    int winH = 640;
    if (screenW > 0) {
      int maxW = screenW - 32;
      if (maxW < 1) { maxW = screenW; }
      if (winW > maxW) { winW = maxW; }
    }
    if (screenH > 0) {
      int maxH = screenH - 64;
      if (maxH < 1) { maxH = screenH; }
      if (winH > maxH) { winH = maxH; }
    }
    int win = uiWindowCreate(app, "International Criminal Court - GR3+ Grades", winW, winH, theme);
    int root = uiWindowRoot(win);

    // --- Functional menubar strip and dropdowns ---
    int menuStrip = uiBox(root, 1, 4);
    uiWidgetMargin(menuStrip, 4, 8, 4, 8);
    menuFileButton = uiButton(menuStrip, "File");
    menuEditButton = uiButton(menuStrip, "Edit");
    menuViewButton = uiButton(menuStrip, "View");
    menuHelpButton = uiButton(menuStrip, "Help");
    menuFilePanel = uiBox(root, 0, 2);
    uiWidgetMargin(menuFilePanel, 4, 10, 4, 10);
    menuFileItem1 = uiButton(menuFilePanel, "New Document");
    menuFileItem2 = uiButton(menuFilePanel, "Save Document");
    uiWidgetVisible(menuFilePanel, 0);
    menuEditPanel = uiBox(root, 0, 2);
    uiWidgetMargin(menuEditPanel, 4, 10, 4, 10);
    menuEditItem1 = uiButton(menuEditPanel, "Undo Edit");
    menuEditItem2 = uiButton(menuEditPanel, "Select All");
    uiWidgetVisible(menuEditPanel, 0);
    menuViewPanel = uiBox(root, 0, 2);
    uiWidgetMargin(menuViewPanel, 4, 10, 4, 10);
    menuViewItem1 = uiButton(menuViewPanel, "Editor Panel");
    menuViewItem2 = uiButton(menuViewPanel, "Document List");
    uiWidgetVisible(menuViewPanel, 0);
    menuHelpPanel = uiBox(root, 0, 2);
    uiWidgetMargin(menuHelpPanel, 4, 10, 4, 10);
    menuHelpItem1 = uiButton(menuHelpPanel, "About SLeeLa");
    menuHelpItem2 = uiButton(menuHelpPanel, "Keyboard Shortcuts");
    uiWidgetVisible(menuHelpPanel, 0);


    // --- Toolbar: the Path-1 actions bound to SLeeLa operations ---
    int menu = uiHeaderBar(root);
    uiWidgetSizeRequest(menu, 0, 48);            // Coherent Dignity: 6X header banner (48px)
    int title = uiLabel(menu, "\u25A2  Editor");
    uiWidgetMargin(title, 0, 10, 0, 12);
    btnEditor = uiButton(menu, "Editor");       // -> show the editor panel
    btnDocs = uiButton(menu, "Documents");      // -> show the document listener
    btnAbout = uiButton(menu, "About");         // -> show the about panel
    uiSpacer(menu);
    btnSave = uiButton(menu, "Save");           // -> operation onSave
    uiWidgetSuggested(btnSave, 1);
    btnNew = uiButton(menu, "New");             // -> operation onNew

    // --- Content area: holds the three switchable panels, stacked. ---
    int content = uiBox(root, VERTICAL, 10);
    uiWidgetMargin(content, 14, 14, 14, 14);
    uiWidgetExpand(content, 1, 1);

    // Panel 0: the editable text area.
    editorPanel = uiBox(content, VERTICAL, 8);
    uiWidgetExpand(editorPanel, 1, 1);
    int editor = uiEntry(editorPanel, "Untitled \u2014 ready.");
    uiWidgetExpand(editor, 1, 0); // single-line entry follows its parent width, not its height

    // Panel 1: the 1..14 document-change listener, inside a SCROLLED VIEW so a
    // long watch-list scrolls and a scrollbar appears automatically on overflow.
    docsPanel = uiScrolledView(content, SCROLL_AUTO, SCROLL_AUTO);
    uiWidgetExpand(docsPanel, 1, 1);
    int watchBox = uiBox(docsPanel, VERTICAL, 6); // the scroll CONTENT (1st child)
    uiWidgetExpand(watchBox, 1, 0); // fill viewport width; retain natural scroll height
    uiWidgetMargin(watchBox, 12, 12, 12, 12);
    
    int i = 0;
    while (i < 14) {
      int rowId = i + 1;
      int rowBox = uiBox(watchBox, HORIZONTAL, 10);
      
      uiSpacer(rowBox);
      
      i = i + 1;
    }
    int overRow = uiBox(watchBox, VERTICAL, 10); // controls wrap vertically instead of exceeding the viewport width
    
    uiSpacer(overRow);
    int rej = uiBadge(overRow, "rejected");
    uiWidgetDestructive(rej, 1);

    // Panel 2: about / help.
    aboutPanel = uiBox(content, VERTICAL, 8);
    uiWidgetExpand(aboutPanel, 1, 1);
    uiHeading(aboutPanel, "SLeeLa Editor — MUAL 1.0", 18);
    uiLabel(aboutPanel, "The 1.0 baseline: backend selector, window intent,");
    
    

    // --- Status line (component 'status', not clickable) ---
    // --- Connectivity popover: opened from the lower-right Wi-Fi status icon. ---
    // Native Wi-Fi scan/connect/disconnect are backed by wifi-common via the SLVM.
    wifiPanel = uiPopupCreate(root);
    uiWidgetExpand(wifiPanel, 1, 0);
    uiWidgetMargin(wifiPanel, 4, 8, 4, 8);
    int wifiBox = uiBox(wifiPanel, 1, 5);
    uiWidgetExpand(wifiBox, 1, 0);
    uiWidgetMargin(wifiBox, 8, 10, 8, 10);
    int wifiHeading = uiHeading(wifiBox, "Connectivity", 18);
    wifiStatusLabel = uiLabel(wifiBox, "Scan to list nearby networks; connect with an OS-saved profile.");
    wifiProfileEntry = uiEntry(wifiBox, "Optional saved profile (blank = OS authentication)");
    wifiScanButton = uiButton(wifiBox, "Scan for Networks");
    wifiConnectButton = uiButton(wifiBox, "Connect to First Listed Network");
    wifiDisconnectButton = uiButton(wifiBox, "Disconnect");
    wifiSettingsButton = uiButton(wifiBox, "Network Settings");
    wifiInfoButton = uiButton(wifiBox, "Connection Details");
    wifiCloseButton = uiButton(wifiBox, "Close Menu");
    uiWidgetVisible(wifiPanel, 0);

    int status = uiStatusBar(root);
    statusLabel = uiLabel(status, "Sleela™ GUI - Version 1.0");
    uiWidgetMargin(statusLabel, 0, 12, 0, 12);
    uiSpacer(status);
    wifiButton = uiButton(status, "Wi-Fi");
    // Permanent Wi-Fi identity: Wifi; PNG is primary, SVG/JPEG are retained alternates.
    uiWidgetSetIcon(wifiButton, "user-interface/icons/wifi.png", "Wifi");
    uiWidgetMargin(wifiButton, 0, 8, 0, 8);
    uiWidgetSuggested(wifiButton, 1);

    showPanel(0);   // start on the editor
    uiWindowShow(win);
    print("Window shown. Buttons switch panels and update the status line.");

    // --- The interactive event loop: pump, then poll activations. ---
    int saves = 0;
    boolean running = true;
    while (running) {
      int dispatched = uiAppPump(app, 1);       // block for the next event
      if (dispatched < 0) { running = false; }
      else {

        // Menubar top-level buttons toggle their dropdown panels.
        if (uiWidgetTakeActivated(menuFileButton) == 1) { openMenu(0); }
        if (uiWidgetTakeActivated(menuEditButton) == 1) { openMenu(1); }
        if (uiWidgetTakeActivated(menuViewButton) == 1) { openMenu(2); }
        if (uiWidgetTakeActivated(menuHelpButton) == 1) { openMenu(3); }
        if (uiWidgetTakeActivated(wifiButton) == 1) { openMenu(4); }
        // Menu items announce the selected command, then dismiss the dropdown.
        if (uiWidgetTakeActivated(menuFileItem1) == 1) {
          uiWidgetSetText(statusLabel, "File · New Document");
          openMenu(-1);
        }
        if (uiWidgetTakeActivated(menuFileItem2) == 1) {
          uiWidgetSetText(statusLabel, "File · Save Document");
          openMenu(-1);
        }
        if (uiWidgetTakeActivated(menuEditItem1) == 1) {
          uiWidgetSetText(statusLabel, "Edit · Undo Edit");
          openMenu(-1);
        }
        if (uiWidgetTakeActivated(menuEditItem2) == 1) {
          uiWidgetSetText(statusLabel, "Edit · Select All");
          openMenu(-1);
        }
        if (uiWidgetTakeActivated(menuViewItem1) == 1) {
          uiWidgetSetText(statusLabel, "View · Editor Panel");
          openMenu(-1);
        }
        if (uiWidgetTakeActivated(menuViewItem2) == 1) {
          uiWidgetSetText(statusLabel, "View · Document List");
          openMenu(-1);
        }
        if (uiWidgetTakeActivated(menuHelpItem1) == 1) {
          uiWidgetSetText(statusLabel, "Help · About SLeeLa");
          openMenu(-1);
        }
        if (uiWidgetTakeActivated(menuHelpItem2) == 1) {
          uiWidgetSetText(statusLabel, "Help · Keyboard Shortcuts");
          openMenu(-1);
        }
        // Scan results are sourced from the native OS provider, never fabricated.
        if (uiWidgetTakeActivated(wifiScanButton) == 1) {
          int scanResult = uiWifiScan();
          int networkCount = uiWifiNetworkCount();
          if (scanResult == 0 && networkCount > 0) {
            uiWidgetSetText(wifiStatusLabel, "Found " + networkCount + " network(s). First: " + uiWifiNetworkSsid(0) + " · " + uiWifiNetworkSignal(0) + "% · " + uiWifiNetworkSecurity(0));
            uiWidgetSetText(statusLabel, "Connectivity · scan complete");
          } else {
            uiWidgetSetText(wifiStatusLabel, "Wi-Fi scan: " + uiWifiStatusName(scanResult) + " (" + networkCount + " networks)");
          }
        }
        if (uiWidgetTakeActivated(wifiConnectButton) == 1) {
          String profileName = uiWidgetGetText(wifiProfileEntry);
          int connectResult = uiWifiConnect(0, profileName);
          uiWidgetSetText(wifiStatusLabel, "Connect: " + uiWifiStatusName(connectResult));
          uiWidgetSetText(statusLabel, "Connectivity · " + uiWifiStatusName(connectResult));
        }
        if (uiWidgetTakeActivated(wifiDisconnectButton) == 1) {
          int disconnectResult = uiWifiDisconnect();
          uiWidgetSetText(wifiStatusLabel, "Disconnect: " + uiWifiStatusName(disconnectResult));
        }
        if (uiWidgetTakeActivated(wifiSettingsButton) == 1) {
          int settingsResult = uiWifiOpenSettings();
          if (settingsResult == 0) {
            uiWidgetSetText(wifiStatusLabel, "Opened the operating system's network settings.");
            uiWidgetSetText(statusLabel, "Connectivity · opened system network settings");
            openMenu(-1);
          } else {
            wifiActionUnavailable("Opening network settings");
          }
        }
        if (uiWidgetTakeActivated(wifiInfoButton) == 1) {
          if (uiWifiNetworkCount() > 0) {
            uiWidgetSetText(wifiStatusLabel, uiWifiNetworkSsid(0) + " · " + uiWifiNetworkBssid(0) + " · " + uiWifiNetworkSignal(0) + "% · " + uiWifiNetworkSecurity(0));
          } else {
            uiWidgetSetText(wifiStatusLabel, "Scan before requesting connection details.");
          }
        }
        if (uiWidgetTakeActivated(wifiCloseButton) == 1) { openMenu(-1); }

        if (uiWidgetTakeActivated(btnEditor) == 1) {

          openMenu(-1);
          showPanel(0); uiWidgetSetText(statusLabel, "Editor \u00B7 editing the current document");
        }
        if (uiWidgetTakeActivated(btnDocs) == 1) {
          openMenu(-1);
          showPanel(1); uiWidgetSetText(statusLabel, "Documents \u00B7 watching 1..14 (scroll for more)");
        }
        if (uiWidgetTakeActivated(btnAbout) == 1) {
          openMenu(-1);
          showPanel(2); uiWidgetSetText(statusLabel, "About \u00B7 MUAL 1.0 baseline");
        }
        if (uiWidgetTakeActivated(btnSave) == 1) {
          openMenu(-1);
          saves = saves + 1;
          uiWidgetSetText(statusLabel, "Saved \u2713  (" + saves + " total this session)");
        }
        if (uiWidgetTakeActivated(btnNew) == 1) {
          openMenu(-1);
          uiWidgetSetText(editor, "Untitled \u2014 new document");
          uiWidgetSetText(statusLabel, "New document created");
          showPanel(0);
        }
      }
    }

    uiThemeDestroy(theme);
    uiAppDestroy(app);
    print("event loop exited; saves this session: " + saves);
  }
}