Home /Modules/ui/slui_api.cpp

slui_api.cpp

ui/src/slui_api.cpp · 550 line(s) · c

/* =============================================================================
 * SleelaUI public C ABI implementation.
 *
 * This file is the entire surface the installed header promises: it owns the
 * App/Window lifecycle, builds widgets into the tree, and marshals the flat C
 * configuration calls onto the C++ objects. Everything here has C linkage.
 *
 * Max Rupplin -- MEARVK LLC -- 2026
 * ===========================================================================*/
#include "sleela_ui.h"

#include "slui_backend.hpp"
#include "slui_theme.hpp"
#include "slui_widget.hpp"
#include "slui_window.hpp"

#include <cstring>
#include <memory>
#include <string>
#include <vector>
#if defined(_WIN32)
#  include <windows.h>
#  include <shellapi.h>
#elif defined(__APPLE__)
#  include <spawn.h>
#  include <sys/wait.h>
#  include <unistd.h>
extern char **environ;
#else
#  include <spawn.h>
#  include <unistd.h>
extern char **environ;
#endif

/* ---- App ----------------------------------------------------------------- */
struct SLUIApp {
    std::unique_ptr<slui::Backend> backend;
    std::vector<std::unique_ptr<slui::Window>> windows;
    std::string app_id;
};

/* The public SLUIWindow / SLUIWidget are just aliases for the C++ objects; we
 * reinterpret_cast between them at the boundary. */

static slui::Window* as_window(SLUIWindow* w) {
    return reinterpret_cast<slui::Window*>(w);
}
static slui::Widget* as_widget(SLUIWidget* w) {
    return reinterpret_cast<slui::Widget*>(w);
}
static const slui::Widget* as_widget(const SLUIWidget* w) {
    return reinterpret_cast<const slui::Widget*>(w);
}

extern "C" {

const char* slui_version_string(void) { return SLUI_VERSION_STRING; }

int slui_wifi_open_settings(void) {
#if defined(_WIN32)
    HINSTANCE result = ShellExecuteA(NULL, "open", "ms-settings:network-wifi",
                                     NULL, NULL, SW_SHOWNORMAL);
    return reinterpret_cast<intptr_t>(result) > 32 ? SLUI_OK : SLUI_ERR_BACKEND;
#elif defined(__APPLE__)
    pid_t pid = 0;
    char *const argv[] = {const_cast<char *>("open"),
                          const_cast<char *>("x-apple.systempreferences:com.apple.wifi"),
                          nullptr};
    if (posix_spawnp(&pid, "open", nullptr, nullptr, argv, environ) != 0)
        return SLUI_ERR_BACKEND;
    return SLUI_OK;
#else
    /* Launch only fixed executable/argument pairs; never pass user text to a
     * shell. Prefer NetworkManager's editor, then GNOME network settings. */
    pid_t pid = 0;
    char *const nm_argv[] = {const_cast<char *>("nm-connection-editor"), nullptr};
    if (posix_spawnp(&pid, "nm-connection-editor", nullptr, nullptr, nm_argv, environ) == 0)
        return SLUI_OK;
    char *const cc_argv[] = {const_cast<char *>("gnome-control-center"),
                             const_cast<char *>("wifi"), nullptr};
    if (posix_spawnp(&pid, "gnome-control-center", nullptr, nullptr, cc_argv, environ) == 0)
        return SLUI_OK;
    return SLUI_ERR_UNSUPPORTED;
#endif
}

SLUIBackend slui_backend(void) {
#if defined(SLUI_BACKEND_WIN32_BUILD)
    return SLUI_BACKEND_WIN32;
#elif defined(SLUI_BACKEND_COCOA_BUILD)
    return SLUI_BACKEND_COCOA;
#elif defined(SLUI_BACKEND_X11_BUILD)
    return SLUI_BACKEND_X11;
#else
    return SLUI_BACKEND_UNKNOWN;
#endif
}

const char* slui_backend_name(SLUIBackend backend) {
    switch (backend) {
    case SLUI_BACKEND_WIN32: return "win32";
    case SLUI_BACKEND_COCOA: return "cocoa";
    case SLUI_BACKEND_X11: return "x11";
    default: return "unknown";
    }
}

/* ---- App lifecycle ------------------------------------------------------- */
SLUIApp* slui_app_create(const char* app_id) {
    auto app = new (std::nothrow) SLUIApp();
    if (!app) return nullptr;
    app->app_id = app_id ? app_id : "com.mearvk.SleelaUI";
    app->backend = slui::create_backend(app->app_id);
    if (!app->backend) {
        delete app;
        return nullptr;
    }
    return app;
}

void slui_app_destroy(SLUIApp* app) {
    if (!app) return;
    app->windows.clear();
    app->backend.reset();
    delete app;
}

int slui_app_pump(SLUIApp* app, int block) {
    if (!app || !app->backend) return SLUI_ERR_INVALID;
    return app->backend->pump(block != 0);
}

int slui_app_run(SLUIApp* app) {
    if (!app || !app->backend) return SLUI_ERR_INVALID;
    int exit_code = 0;
    for (;;) {
        if (app->backend->should_quit(&exit_code)) break;
        /* Stop when every window has closed. */
        bool any_open = false;
        for (auto& w : app->windows)
            if (!w->closed()) {
                any_open = true;
                break;
            }
        if (!any_open && !app->windows.empty()) break;
        int n = app->backend->pump(true);
        if (n < 0) {
            exit_code = n;
            break;
        }
    }
    return exit_code;
}

void slui_app_quit(SLUIApp* app, int exit_code) {
    if (app && app->backend) app->backend->quit(exit_code);
}

SLUIStatus slui_screen_size(SLUIApp* app, int* w, int* h) {
    if (!app || !app->backend) return SLUI_ERR_INVALID;
    int sw = 0, sh = 0;
    if (!app->backend->screen_size(&sw, &sh)) return SLUI_ERR_INVALID;
    if (w) *w = sw;
    if (h) *h = sh;
    return SLUI_OK;
}

/* ---- Window -------------------------------------------------------------- */
SLUIWindow* slui_window_create(SLUIApp* app, const SLUIWindowConfig* config) {
    if (!app || !config) return nullptr;
    auto win = std::make_unique<slui::Window>(app, app->backend.get(), *config);
    slui::Window* raw = win.get();
    app->windows.push_back(std::move(win));
    return reinterpret_cast<SLUIWindow*>(raw);
}

void slui_window_destroy(SLUIWindow* window) {
    if (!window) return;
    slui::Window* w = as_window(window);
    w->hide();
    /* Ownership stays with the App's vector; mark hidden/closed. The App frees
     * it at destroy time. This keeps the simple handle model safe. */
}

void slui_window_show(SLUIWindow* window) {
    if (window) as_window(window)->show();
}
int slui_window_closed(const SLUIWindow* window) {
    if (!window) return 0;
    return as_window(const_cast<SLUIWindow*>(window))->closed() ? 1 : 0;
}
void slui_window_hide(SLUIWindow* window) {
    if (window) as_window(window)->hide();
}
void slui_window_set_title(SLUIWindow* window, const char* title) {
    if (window) as_window(window)->set_title(title ? title : "");
}
SLUIWidget* slui_window_root(SLUIWindow* window) {
    if (!window) return nullptr;
    return as_window(window)->root_handle();
}
const SLUITheme* slui_window_theme(const SLUIWindow* /*window*/) {
    /* The decoded theme lives inside the Window; we don't retain the ABI copy.
     * Hosts that need the palette should keep the SLUITheme they passed in. */
    return nullptr;
}
void slui_window_set_theme(SLUIWindow* window, const SLUITheme* theme) {
    if (!window || !theme) return;
    as_window(window)->set_theme(slui::Theme::from_abi(*theme));
}
void slui_window_request_redraw(SLUIWindow* window) {
    if (window) as_window(window)->request_redraw();
}
void slui_window_render(SLUIWindow* window) {
    if (window) as_window(window)->render();
}
void slui_window_set_buffering(SLUIWindow* window, int mode) {
    if (!window) return;
    if (mode < SLUI_BUFFERING_SINGLE) mode = SLUI_BUFFERING_SINGLE;
    if (mode > SLUI_BUFFERING_TRIPLE) mode = SLUI_BUFFERING_TRIPLE;
    as_window(window)->set_buffering(static_cast<SLUIBufferingMode>(mode));
}
int slui_window_buffering(const SLUIWindow* window) {
    if (!window) return SLUI_BUFFERING_SINGLE;
    return static_cast<int>(
        as_window(const_cast<SLUIWindow*>(window))->buffering());
}

SLUIStatus slui_window_set_min_size(SLUIWindow* window, int width, int height) {
    return window ? as_window(window)->set_min_size(width, height) : SLUI_ERR_INVALID;
}
SLUIStatus slui_window_set_max_size(SLUIWindow* window, int width, int height) {
    return window ? as_window(window)->set_max_size(width, height) : SLUI_ERR_INVALID;
}
SLUIStatus slui_window_set_size_limits(SLUIWindow* window, int minw, int minh, int maxw, int maxh) {
    return window ? as_window(window)->set_size_limits(minw, minh, maxw, maxh) : SLUI_ERR_INVALID;
}
SLUIStatus slui_window_get_min_size(const SLUIWindow* window, SLUISize* out) {
    return window ? as_window(const_cast<SLUIWindow*>(window))->get_min_size(out) : SLUI_ERR_INVALID;
}
SLUIStatus slui_window_get_max_size(const SLUIWindow* window, SLUISize* out) {
    return window ? as_window(const_cast<SLUIWindow*>(window))->get_max_size(out) : SLUI_ERR_INVALID;
}

void slui_window_set_event_handler(SLUIWindow* window, SLUIEventHandler handler,
                                   void* user) {
    if (window) as_window(window)->set_event_handler(handler, user);
}

/* ---- Widget construction ------------------------------------------------- */
static SLUIWidget* attach(SLUIWidget* parent, std::unique_ptr<slui::Widget> w) {
    if (!parent) return nullptr;
    slui::Widget* p = as_widget(parent);
    slui::Widget* raw = p->add_child(std::move(w));
    /* Propagate the invalidate hook from the parent so the new widget can ask
     * its window to repaint. */
    return reinterpret_cast<SLUIWidget*>(raw);
}

SLUIWidget* slui_box(SLUIWidget* parent, SLUIOrientation orient, int spacing) {
    return attach(parent, std::make_unique<slui::Box>(orient, spacing));
}
SLUIWidget* slui_header_bar(SLUIWidget* parent) {
    return attach(parent, std::make_unique<slui::HeaderBar>());
}
SLUIWidget* slui_label(SLUIWidget* parent, const char* text) {
    return attach(parent, std::make_unique<slui::Label>(text ? text : ""));
}
SLUIWidget* slui_button(SLUIWidget* parent, const char* text) {
    return attach(parent, std::make_unique<slui::Button>(text ? text : ""));
}
SLUIWidget* slui_toggle(SLUIWidget* parent, const char* text, int initial_on) {
    return attach(parent,
                  std::make_unique<slui::Toggle>(text ? text : "", initial_on != 0));
}
SLUIWidget* slui_entry(SLUIWidget* parent, const char* placeholder) {
    return attach(parent,
                  std::make_unique<slui::Entry>(placeholder ? placeholder : ""));
}
SLUIWidget* slui_slider(SLUIWidget* parent, double min, double max, double value) {
    return attach(parent, std::make_unique<slui::Slider>(min, max, value));
}
SLUIWidget* slui_separator(SLUIWidget* parent, SLUIOrientation orient) {
    return attach(parent, std::make_unique<slui::Separator>(orient));
}
SLUIWidget* slui_spacer(SLUIWidget* parent) {
    return attach(parent, std::make_unique<slui::Spacer>());
}

/* ---- Expanded widget collection ------------------------------------------ */
SLUIWidget* slui_check_box(SLUIWidget* parent, const char* text, int on) {
    return attach(parent,
                  std::make_unique<slui::CheckBox>(text ? text : "", on != 0));
}
SLUIWidget* slui_radio_button(SLUIWidget* parent, const char* text, int group,
                              int on) {
    return attach(parent, std::make_unique<slui::RadioButton>(text ? text : "",
                                                              group, on != 0));
}
SLUIWidget* slui_combo_box(SLUIWidget* parent) {
    return attach(parent, std::make_unique<slui::ComboBox>());
}
void slui_combo_box_add(SLUIWidget* combo, const char* option) {
    if (combo && option) {
        auto* c = dynamic_cast<slui::ComboBox*>(as_widget(combo));
        if (c) c->add_option(option);
    }
}
SLUIWidget* slui_spin_button(SLUIWidget* parent, double min, double max,
                             double step, double value) {
    return attach(parent,
                  std::make_unique<slui::SpinButton>(min, max, step, value));
}
SLUIWidget* slui_progress_bar(SLUIWidget* parent, double fraction) {
    return attach(parent, std::make_unique<slui::ProgressBar>(fraction));
}
SLUIWidget* slui_level_bar(SLUIWidget* parent, double fraction) {
    return attach(parent, std::make_unique<slui::LevelBar>(fraction));
}
SLUIWidget* slui_spinner(SLUIWidget* parent) {
    return attach(parent, std::make_unique<slui::Spinner>());
}
SLUIWidget* slui_scroll_bar(SLUIWidget* parent, SLUIOrientation orient,
                            double value, double page) {
    return attach(parent,
                  std::make_unique<slui::ScrollBar>(orient, value, page));
}
SLUIWidget* slui_scrolled_view(SLUIWidget* parent, int policy_h, int policy_v) {
    return attach(parent,
                  std::make_unique<slui::ScrolledView>(policy_h, policy_v));
}
void slui_scrolled_view_scroll_to(SLUIWidget* view, int x, int y) {
    if (!view) return;
    auto* w = as_widget(view);
    if (w->kind() != slui::WidgetKind::ScrolledView) return;
    auto* sv = static_cast<slui::ScrolledView*>(w);
    sv->set_scroll_x(x);
    sv->set_scroll_y(y);
}
void slui_scrolled_view_scroll_by(SLUIWidget* view, int dx, int dy) {
    if (!view) return;
    auto* w = as_widget(view);
    if (w->kind() != slui::WidgetKind::ScrolledView) return;
    static_cast<slui::ScrolledView*>(w)->scroll_by(dx, dy);
}
int slui_scrolled_view_scroll_x(SLUIWidget* view) {
    if (!view) return 0;
    auto* w = as_widget(view);
    if (w->kind() != slui::WidgetKind::ScrolledView) return 0;
    return static_cast<slui::ScrolledView*>(w)->scroll_x();
}
int slui_scrolled_view_scroll_y(SLUIWidget* view) {
    if (!view) return 0;
    auto* w = as_widget(view);
    if (w->kind() != slui::WidgetKind::ScrolledView) return 0;
    return static_cast<slui::ScrolledView*>(w)->scroll_y();
}
SLUIWidget* slui_frame(SLUIWidget* parent, const char* title) {
    return attach(parent, std::make_unique<slui::Frame>(title ? title : ""));
}
SLUIWidget* slui_card(SLUIWidget* parent) {
    return attach(parent, std::make_unique<slui::Card>());
}

/* Shared popup surface: a normal themed card with explicit open/close state.
 * It remains owned by the parent widget tree, so dismissal is safe and does
 * not invalidate a handle retained by the SLeeLa VM. */
SLUIWidget* slui_popup_create(SLUIWidget* parent) {
    SLUIWidget* popup = slui_card(parent);
    if (popup) {
        slui_widget_set_visible(popup, 0);
        /* A popup floats forward of the frame: mark it an overlay so the Window
         * paints it last (above everything) and unclipped, instead of inline in
         * its parent's layout where later siblings would paint over it. */
        as_widget(popup)->set_overlay(true);
    }
    return popup;
}
void slui_popup_set_open(SLUIWidget* popup, int open) {
    slui_widget_set_visible(popup, open != 0);
}
void slui_popup_set_anchor(SLUIWidget* popup, int top_left, int x) {
    if (popup) as_widget(popup)->set_overlay_anchor(top_left != 0, x);
}
void slui_popup_anchor_to(SLUIWidget* popup, SLUIWidget* trigger) {
    if (popup) as_widget(popup)->set_overlay_anchor_widget(trigger ? as_widget(trigger) : nullptr);
}
int slui_popup_is_open(const SLUIWidget* popup) {
    return popup ? as_widget(popup)->visible() : 0;
}
SLUIWidget* slui_grid(SLUIWidget* parent, int columns, int spacing) {
    return attach(parent, std::make_unique<slui::Grid>(columns, spacing));
}
SLUIWidget* slui_status_bar(SLUIWidget* parent) {
    return attach(parent, std::make_unique<slui::StatusBar>());
}
SLUIWidget* slui_image(SLUIWidget* parent, const char* glyph, int w, int h) {
    return attach(parent,
                  std::make_unique<slui::Image>(glyph ? glyph : "", w, h));
}
SLUIWidget* slui_avatar(SLUIWidget* parent, const char* initial, int diameter) {
    return attach(parent, std::make_unique<slui::Avatar>(initial ? initial : "",
                                                        diameter));
}
SLUIWidget* slui_badge(SLUIWidget* parent, const char* text) {
    return attach(parent, std::make_unique<slui::Badge>(text ? text : ""));
}
SLUIWidget* slui_chip(SLUIWidget* parent, const char* text) {
    return attach(parent, std::make_unique<slui::Chip>(text ? text : ""));
}
SLUIWidget* slui_heading(SLUIWidget* parent, const char* text, int size_pt) {
    return attach(parent,
                  std::make_unique<slui::Heading>(text ? text : "", size_pt));
}
SLUIWidget* slui_tooltip(SLUIWidget* parent, const char* text) {
    return attach(parent, std::make_unique<slui::Tooltip>(text ? text : ""));
}
SLUIWidget* slui_link_button(SLUIWidget* parent, const char* text) {
    return attach(parent, std::make_unique<slui::LinkButton>(text ? text : ""));
}
SLUIWidget* slui_search_entry(SLUIWidget* parent, const char* placeholder) {
    return attach(parent,
                  std::make_unique<slui::SearchEntry>(placeholder ? placeholder : ""));
}
SLUIWidget* slui_password_entry(SLUIWidget* parent, const char* placeholder) {
    return attach(parent, std::make_unique<slui::PasswordEntry>(
                              placeholder ? placeholder : ""));
}
SLUIWidget* slui_info_bar(SLUIWidget* parent, const char* text, int severity) {
    return attach(parent,
                  std::make_unique<slui::InfoBar>(text ? text : "", severity));
}

/* ---- Motion & custom drawing --------------------------------------------- */
SLUIWidget* slui_throbber(SLUIWidget* parent, int width_px) {
    return attach(parent, std::make_unique<slui::Throbber>(width_px));
}
void slui_throbber_set_width(SLUIWidget* t, int width_px) {
    if (!t) return;
    auto* th = dynamic_cast<slui::Throbber*>(as_widget(t));
    if (th) th->set_width_px(width_px);
}
void slui_throbber_set_intensity(SLUIWidget* t, double intensity) {
    if (!t) return;
    auto* th = dynamic_cast<slui::Throbber*>(as_widget(t));
    if (th) th->set_intensity(intensity);
}
void slui_throbber_set_hue(SLUIWidget* t, double hue_degrees) {
    if (!t) return;
    auto* th = dynamic_cast<slui::Throbber*>(as_widget(t));
    if (th) th->set_hue(hue_degrees);
}

SLUIWidget* slui_canvas_view(SLUIWidget* parent, int width, int height) {
    return attach(parent, std::make_unique<slui::CanvasView>(width, height));
}
void slui_canvas_view_set_draw(SLUIWidget* view, SLUICanvasDrawFn fn,
                               void* user) {
    if (!view) return;
    auto* cv = dynamic_cast<slui::CanvasView*>(as_widget(view));
    if (cv)
        cv->set_draw_fn(reinterpret_cast<slui::CanvasDrawFn>(fn), user);
}
void slui_canvas_view_set_fps(SLUIWidget* view, double fps) {
    if (!view) return;
    auto* cv = dynamic_cast<slui::CanvasView*>(as_widget(view));
    if (cv) cv->set_fps(fps);
}

/* ---- Widget configuration ------------------------------------------------ */
void slui_widget_set_margin(SLUIWidget* w, int top, int right, int bottom,
                            int left) {
    if (w) as_widget(w)->set_margin(slui::Margin{top, right, bottom, left});
}
void slui_widget_set_align(SLUIWidget* w, SLUIAlign h, SLUIAlign v) {
    if (w) as_widget(w)->set_align(h, v);
}
void slui_widget_set_expand(SLUIWidget* w, int h_expand, int v_expand) {
    if (w) as_widget(w)->set_expand(h_expand != 0, v_expand != 0);
}
void slui_widget_set_size_request(SLUIWidget* w, int min_w, int min_h) {
    if (w) as_widget(w)->set_size_request(min_w, min_h);
}
void slui_widget_set_sensitive(SLUIWidget* w, int sensitive) {
    if (w) as_widget(w)->set_sensitive(sensitive != 0);
}
void slui_widget_set_visible(SLUIWidget* w, int visible) {
    if (w) as_widget(w)->set_visible(visible != 0);
}

void slui_widget_set_text(SLUIWidget* w, const char* text) {
    if (w) as_widget(w)->set_text(text ? text : "");
}
void slui_widget_set_icon(SLUIWidget* w, const char* asset_path,
                          const char* symbol_name) {
    if (!w) return;
    auto* button = dynamic_cast<slui::Button*>(as_widget(w));
    if (button)
        button->set_icon(asset_path ? asset_path : "",
                         symbol_name ? symbol_name : "");
}
size_t slui_widget_get_text(const SLUIWidget* w, char* out, size_t cap) {
    if (!w) return 0;
    std::string s = as_widget(w)->text();
    if (out && cap > 0) {
        size_t n = s.size() < cap - 1 ? s.size() : cap - 1;
        std::memcpy(out, s.data(), n);
        out[n] = '\0';
    }
    return s.size();
}

void slui_widget_set_toggle(SLUIWidget* w, int on) {
    if (w) as_widget(w)->set_toggle(on != 0);
}
int slui_widget_get_toggle(const SLUIWidget* w) {
    return w ? (as_widget(w)->toggle() ? 1 : 0) : 0;
}
void slui_widget_set_value(SLUIWidget* w, double value) {
    if (w) as_widget(w)->set_value(value);
}
double slui_widget_get_value(const SLUIWidget* w) {
    return w ? as_widget(w)->value() : 0.0;
}
int slui_widget_take_activated(SLUIWidget* w) {
    return w ? (as_widget(w)->take_activated() ? 1 : 0) : 0;
}
int slui_widget_is_hovered(SLUIWidget* w) { return w ? (as_widget(w)->hovered() ? 1 : 0) : 0; }
int slui_widget_take_pressed(SLUIWidget* w) { return w ? (as_widget(w)->take_pressed() ? 1 : 0) : 0; }
int slui_widget_take_released(SLUIWidget* w) { return w ? (as_widget(w)->take_released() ? 1 : 0) : 0; }
int slui_widget_take_hover_entered(SLUIWidget* w) { return w ? (as_widget(w)->take_hover_entered() ? 1 : 0) : 0; }
int slui_widget_take_hover_left(SLUIWidget* w) { return w ? (as_widget(w)->take_hover_left() ? 1 : 0) : 0; }
void slui_widget_set_suggested(SLUIWidget* w, int suggested) {
    if (w) as_widget(w)->set_suggested(suggested != 0);
}
void slui_widget_set_destructive(SLUIWidget* w, int destructive) {
    if (w) as_widget(w)->set_destructive(destructive != 0);
}

void slui_widget_on_activate(SLUIWidget* w, SLUIActivateHandler handler,
                             void* user) {
    if (w) as_widget(w)->on_activate(handler, user);
}
void slui_widget_on_value_changed(SLUIWidget* w, SLUIValueHandler handler,
                                  void* user) {
    if (w) as_widget(w)->on_value_changed(handler, user);
}

} /* extern "C" */