Home /Modules/user-interface/gui-gual-settings-console.sleela

gui-gual-settings-console.sleela

lib/user-interface/example/gual/gui-gual-settings-console.sleela · 409 line(s) · sleela

<<
   example/gui-gual-settings-console.sleela
   The GUAL (2.0) "preferences console" scene as a REAL, 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). Previously
   this file only print()ed a walkthrough; now the hardened, settings-first
   surface (validated, clamp-to-a-safe-window knobs: theme, DPI scale,
   reduced-motion, vsync, poll/debounce, window min/max, document-listener
   backpressure) is painted as actual, interactive widgets.
   
   The three-layer stack is live here:
     SLeeLa decoration (this program + the theme below)
       -> model (ui* built-ins: uiWindowCreate, uiComboBox, uiSlider, uiToggle)
         -> 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-gual-settings-console.sleela
   
   With no display (headless/CI) it reports that and exits 0 without a window.
>>
#sleela 2.5
material;
// GUAL (2.0): the clamp-to-a-safe-window discipline applied to GUI settings.
// Every knob is validated; here the validated values are shown on real
// controls (sliders/toggles/combo) that reflect the clamped result.
class GuiGualSettingsConsole {

  // SLUITheme preset ordinals (mirror SLUIThemeId in the toolkit C ABI).
  // Static build policy: default keeps the normal layout within its parent width.
  // Set to 1 only in builds that intentionally permit horizontal overflow.
  static final int ALLOW_HORIZONTAL_OVERFLOW = 0;

  // Scroll-policy ordinal (mirror the toolkit C ABI: 0=auto on overflow).
  int SCROLL_AUTO = 0;

  // Live handles the event loop reacts on/to.
  int btnJfx;
  int btnSwing;
  int btnNative;
  int btnQt;
  int statusLabel;
  // 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;
  // Menubar strip and its dropdown panels (built from existing ui* widgets).
  int openMenuIndex = -1;
  int menuFileButton;
  int menuFilePanel;
  int menuFileItem1;
  int menuFileItem2;
  int menuSettingsButton;
  int menuSettingsPanel;
  int menuSettingsItem1;
  int menuSettingsItem2;
  int menuBackendButton;
  int menuBackendPanel;
  int menuBackendItem1;
  int menuBackendItem2;
  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); }

  // Mark one accepted backend button as selected (fail-closed: qt stays off).
  void selectBackend(int which) {
    uiWidgetSuggested(btnJfx,    which == 0 ? 1 : 0);
    uiWidgetSuggested(btnSwing,  which == 1 ? 1 : 0);
    uiWidgetSuggested(btnNative, which == 2 ? 1 : 0);
  }

  // Clamp an int into [lo, hi] — the heart of GUAL's settings discipline.
  int clamp(int v, int lo, int hi) {
    if (v < lo) { return lo; }
    if (v > hi) { return hi; }
    return v;
  }
  String intToStr(int v) { return "" + v; }

  // A hardened, neutral "settings" theme.
  // Shared SLeeLa family palette: chrome/title bar #1D2025,
  // Standard Pattern ordinal (mirror SLUIThemeId): 5=BLACK. gual uses BLACK.
  int THEME_BLACK = 5;
  // gual is the BLACK Standard Pattern: deep black surfaces with light text,
  // supplied by the toolkit preset (no per-colour overrides); metrics only.
  int settingsTheme() {
    int t = uiThemePreset(THEME_BLACK);
    uiThemeSetRadius(t, 8);
    uiThemeSetControlHeight(t, 32);
    return t;
  }

  // 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(menuSettingsPanel, next == 1 ? 1 : 0);
    uiWidgetVisible(menuBackendPanel, 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 GUAL Preferences Console (native SleelaUI) — backend "
        + uiBackendName(uiBackend()) + ", toolkit " + uiVersion());

    int app = uiAppCreate("com.mearvk.SleelaUI.GualSettingsConsole");
    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 = settingsTheme();
    // 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 = 960;
    int winH = 620;
    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");
    menuSettingsButton = uiButton(menuStrip, "Settings");
    menuBackendButton = uiButton(menuStrip, "Backend");
    menuHelpButton = uiButton(menuStrip, "Help");
    menuFilePanel = uiBox(root, 0, 2);
    uiWidgetMargin(menuFilePanel, 4, 10, 4, 10);
    menuFileItem1 = uiButton(menuFilePanel, "Export Settings");
    menuFileItem2 = uiButton(menuFilePanel, "Reset Defaults");
    uiWidgetVisible(menuFilePanel, 0);
    menuSettingsPanel = uiBox(root, 0, 2);
    uiWidgetMargin(menuSettingsPanel, 4, 10, 4, 10);
    menuSettingsItem1 = uiButton(menuSettingsPanel, "Appearance");
    menuSettingsItem2 = uiButton(menuSettingsPanel, "Listener Timing");
    uiWidgetVisible(menuSettingsPanel, 0);
    menuBackendPanel = uiBox(root, 0, 2);
    uiWidgetMargin(menuBackendPanel, 4, 10, 4, 10);
    menuBackendItem1 = uiButton(menuBackendPanel, "Select JavaFX");
    menuBackendItem2 = uiButton(menuBackendPanel, "Backend Policy");
    uiWidgetVisible(menuBackendPanel, 0);
    menuHelpPanel = uiBox(root, 0, 2);
    uiWidgetMargin(menuHelpPanel, 4, 10, 4, 10);
    menuHelpItem1 = uiButton(menuHelpPanel, "Preferences Help");
    menuHelpItem2 = uiButton(menuHelpPanel, "About GUAL");
    uiWidgetVisible(menuHelpPanel, 0);

    // --- Chrome ---
    int header = uiHeaderBar(root);
    uiWidgetSizeRequest(header, 0, 48);          // Coherent Dignity: 6X header banner (48px)
    int htitle = uiLabel(header, "\u2699  Preferences");
    uiWidgetMargin(htitle, 0, 10, 0, 12);
    uiSpacer(header);

    // --- Content: two columns of validated settings, inside a scrolled view so
    //     the panel scrolls (and shows scrollbars automatically) rather than
    //     spilling past the frame when the window is smaller than the content. ---
    int scroller = uiScrolledView(root, SCROLL_AUTO, SCROLL_AUTO);
    uiWidgetExpand(scroller, 1, 1);
    int content = uiBox(scroller, 1, 14);     // the scroll CONTENT (1st child)
    uiWidgetMargin(content, 16, 16, 16, 16);
    uiWidgetExpand(content, 1, 0); // let content grow vertically so the scroller can scroll

    int contentAxis = ALLOW_HORIZONTAL_OVERFLOW == 1 ? 0 : 1;
    int cols = uiBox(content, contentAxis, 16); // stack cards vertically to keep them within the frame width
    uiWidgetExpand(cols, 1, 0);
    int colL = uiBox(cols, 1, 14);
    uiWidgetExpand(colL, 1, 0);
    int colR = uiBox(cols, 1, 14);
    uiWidgetExpand(colR, 1, 0);

    // Appearance card: theme allow-list + clamped DPI scale.
    int appear = uiCard(colL);
    uiWidgetExpand(appear, 1, 0);
    int appBox = uiBox(appear, 1, 8);
    uiWidgetExpand(appBox, 1, 0);
    uiWidgetMargin(appBox, 14, 14, 14, 14);

    int themePick = uiComboBox(appBox);
    uiWidgetExpand(themePick, 1, 0);
    uiComboBoxAdd(themePick, "system  (fail-safe fallback for 'midnight')");
    uiComboBoxAdd(themePick, "dark");
    uiComboBoxAdd(themePick, "light");
    int dpi = clamp(400, 50, 300);             // 400% requested -> clamped to 300

    int dpiSlider = uiSlider(appBox, 50.0, 300.0, dpi);
    uiWidgetExpand(dpiSlider, 1, 0);
    uiToggle(appBox, "reduced motion", 1);
    uiToggle(appBox, "vsync", 1);

    // Listener timing card: clamped poll / debounce / queue cap.
    int timing = uiCard(colR);
    uiWidgetExpand(timing, 1, 0);
    int timBox = uiBox(timing, 1, 8);
    uiWidgetExpand(timBox, 1, 0);
    uiWidgetMargin(timBox, 14, 14, 14, 14);

    int poll = clamp(5, 50, 5000);             // 5 -> 50ms

    int pollSlider = uiSlider(timBox, 50.0, 5000.0, poll);
    uiWidgetExpand(pollSlider, 1, 0);
    int debounce = clamp(2000, 0, 1000);       // 2000 -> 1000ms

    int debounceSlider = uiSlider(timBox, 0.0, 1000.0, debounce);
    uiWidgetExpand(debounceSlider, 1, 0);
    int queueCap = clamp(0, 1, 256);           // 0 -> 1

    // Backend + window card (full width).
    int sys = uiCard(content);
    uiWidgetExpand(sys, 1, 0);
    int sysBox = uiBox(sys, 1, 8);
    uiWidgetExpand(sysBox, 1, 0);
    uiWidgetMargin(sysBox, 14, 14, 14, 14);

    int brow = uiBox(sysBox, 1, 8); // avoid a long backend row overflowing narrow windows
    uiWidgetExpand(brow, 1, 0);
    btnJfx = uiButton(brow, "javafx");
    uiWidgetSuggested(btnJfx, 1);              // selected by default
    btnSwing = uiButton(brow, "swing");
    btnNative = uiButton(brow, "native");
    btnQt = uiButton(brow, "qt  \u2715 refused");
    uiWidgetDestructive(btnQt, 1);
    uiSpacer(brow);
    int wrow = uiBox(sysBox, 1, 10);
    uiWidgetExpand(wrow, 1, 0);

    uiSpacer(wrow);

    // --- Status bar ---
    // --- 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);

    uiWindowShow(win);
    print("Window shown. Click an accepted backend to select it; qt is refused.");

    // --- Interactive event loop: pump, then poll the backend buttons. ---
    boolean running = true;
    while (running) {
      int dispatched = uiAppPump(app, 1);
      if (dispatched < 0) { running = false; }
      else {

        // Menubar top-level buttons toggle their dropdown panels.
        if (uiWidgetTakeActivated(menuFileButton) == 1) { openMenu(0); }
        if (uiWidgetTakeActivated(menuSettingsButton) == 1) { openMenu(1); }
        if (uiWidgetTakeActivated(menuBackendButton) == 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 · Export Settings");
          openMenu(-1);
        }
        if (uiWidgetTakeActivated(menuFileItem2) == 1) {
          uiWidgetSetText(statusLabel, "File · Reset Defaults");
          openMenu(-1);
        }
        if (uiWidgetTakeActivated(menuSettingsItem1) == 1) {
          uiWidgetSetText(statusLabel, "Settings · Appearance");
          openMenu(-1);
        }
        if (uiWidgetTakeActivated(menuSettingsItem2) == 1) {
          uiWidgetSetText(statusLabel, "Settings · Listener Timing");
          openMenu(-1);
        }
        if (uiWidgetTakeActivated(menuBackendItem1) == 1) {
          uiWidgetSetText(statusLabel, "Backend · Select JavaFX");
          openMenu(-1);
        }
        if (uiWidgetTakeActivated(menuBackendItem2) == 1) {
          uiWidgetSetText(statusLabel, "Backend · Backend Policy");
          openMenu(-1);
        }
        if (uiWidgetTakeActivated(menuHelpItem1) == 1) {
          uiWidgetSetText(statusLabel, "Help · Preferences Help");
          openMenu(-1);
        }
        if (uiWidgetTakeActivated(menuHelpItem2) == 1) {
          uiWidgetSetText(statusLabel, "Help · About GUAL");
          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(btnJfx) == 1) {

          openMenu(-1);
          selectBackend(0); uiWidgetSetText(statusLabel, "backend selected: javafx \u2713 (configureStrict accepted)");
        }
        if (uiWidgetTakeActivated(btnSwing) == 1) {
          openMenu(-1);
          selectBackend(1); uiWidgetSetText(statusLabel, "backend selected: swing \u2713 (configureStrict accepted)");
        }
        if (uiWidgetTakeActivated(btnNative) == 1) {
          openMenu(-1);
          selectBackend(2); uiWidgetSetText(statusLabel, "backend selected: native \u2713 (configureStrict accepted)");
        }
        if (uiWidgetTakeActivated(btnQt) == 1) {
          openMenu(-1);
          // Fail-closed: refused. Selection does not change.
          uiWidgetSetText(statusLabel, "backend 'qt' REFUSED \u2715 \u00B7 configureStrict is fail-closed; selection unchanged");
        }
      }
    }

    uiThemeDestroy(theme);
    uiAppDestroy(app);
    print("event loop exited.");
  }
}