Packages

@kama/ui 0.6.0

One set of HTML documents for kama UIs: the browser's own DOM on the web, RmlUi natively

Install

kama pkg add kama.json @kama/ui --version ^0.6.0

or in kama.json: "@kama/ui": { "version": "^0.6.0" }

License
MIT OR Apache-2.0
Repository
github.com/cosmic-canopy/kama-ui
Keywords
ui html dom rmlui gui
Dependencies
@kama/rmlui ^0.2.0

Versions

VersionPublishedRevisionIntegrity
0.6.0a3819921db2fsha256-b14c42a4ed74…
0.5.24275ea8118bbsha256-52a668dadcaa…
0.5.176681365dfc8sha256-91485471cc60…
0.5.0209827903d67sha256-ac63e20d45ab…
0.4.04466737e911fsha256-b8a8daca14b6…
0.3.05b730746181bsha256-269a767f1578…
0.2.1a3a7eec39a73sha256-da581cfba4b4…
0.2.0e14f15ac88ffsha256-77a85612b169…
0.1.0a420babbb498sha256-8b8700a2b2da…

@kama/ui

One set of HTML-like documents for a kama program's UI. It has two backends behind one contract:

The program writes ui::UiContext and never knows which backend it has. Each build has exactly one: native.kama and web.kama declare UiContext under complementary @compileFor gates, and both are checked against the UiHost contract where they are written.

Status: published (registry.kama-lang.org, 0.6.0 on 2026-10-29): the Commit event. Needs kama ≥ 0.9.758; tested with 0.9.765. Tested natively on Linux x86_64 (debug, release and AddressSanitizer) and in headless Firefox 157 (emcc 6.0.9).

The plan. RmlUi is today's native engine. Matt's own HTML/CSS engine replaces @kama/rmlui later, behind this same contract. Only native.kama changes. The documents, the web leg and every program using UiContext stay as they are. What a replacement must provide is exactly UiHost in src/host.kama:

The contract

src/host.kama is the authority. Every method there carries its reasons. In short:

group methods
documents loadDocument(path), loadDocumentText(rml), closeDocument(), setInnerRml(handle, rml) (releases what it replaces), appendInnerRml(handle, rml) (keeps it), childCount(h), removeChildren(h, from, count)
assets loadFont(path, fallback), provideImage(name, rgba: ConstView<uint8>, w, h), setImageRewrite(nativeExt, webExt)
frame setSize, setViewport(offsetX, offsetY, scale, visible), setDpRatio, update(), updateAt(seconds), render()
drawing (native) commandCount, command(i) → UiCommand (kind, vertices, indices, texture, translation, scissor, transform), textureCount, texture(h), textureGeneration(h)
pointer (native) mouseMove, mouseButton(button, down, mods), mouseWheel
keyboard key(code, down, mods) with UI_KEY_* (GLFW's numbers) and UI_MOD_*, claimKey(key, mods), typeChar(codepoint), focusHeld(), focusDocument(), focusElement(h), focusedElement(), textFocused() (any control a person types into)
clipboard copyText(text), provideClipboard(text) / takeClipboardWrite() (native; no-op / None on the web)
files chooseFiles(accept, multiple) → a file list, fileCount / fileName / fileSize / fileType / filePath(files, i), releaseFiles, readFile(files, i, offset, count) → a read, readBytes(read, into), readError(read), saveFile(name, bytes); uiFileType(name)
media (web; None / false natively) mediaTime(h) / mediaDuration(h) → Optional<float64> seconds, mediaPaused(h) → Optional<bool>, seekMedia(h, seconds), playMedia(h), pauseMedia(h) → bool (asked)
files (native, the program's half) takeFileRequest(), fileRequestAccept / fileRequestMultiple, provideFiles(files, paths), dropFiles(paths, x, y), pasteFiles(paths), takeSaveRequest(), saveRequestName, saveRequestBytes (0 and no-ops on the web)
elements bindElement(id) → handle, findElement(id) → a handle that hears nothing, elementId(h) → its id, elementRect, elementValue / setElementValue (a float), elementText / setElementText (no length cap), elementChecked(h), setElementClass, setElementProperty, setElementAttribute(h, name, value) / removeElementAttribute(h, name), elementContent(h) / setElementContent(h, text) (plain text), scrollToEnd(h), isScrolledToEnd(h), scrollIntoView(h)
events pollEvent() → UiEvent { kind: Click | Change | DoubleClick | Key | Focus | Blur | Drop | Files | Paste | Read | Media | Commit, element, target, source, key, mods, files, atX, atY }
lifetime UiContext.open(width, height, fontPath) (a stage), UiContext.openPage(width, height, fontPath) (a page), isOpen(), close()

ui::UiNav turns a pad's d-pad and stick into arrow presses (hysteresis, one direction at a time, delay then repeat). ui::uidoc holds the RML → HTML rewriter (rmlToHtml) and escapeText, the escaping both parsers agree on.

The rules that matter most:

Using it

Natively

@kama/rmlui and @kama/freetype vendor RmlUi and FreeType, and kama compiles them into the program. There is nothing to fetch or build first, and no -L path to set. Once the packages are on the registry, depend on this one alone:

"dependencies": { "@kama/ui": { "version": "^0.4.0" } }

@kama/rmlui and @kama/freetype come with it. To build against a checkout instead (working on the package itself), override it in your gitignored kama.local.json, never in kama.json:

{ "overrides": { "@kama/ui": { "path": "../kama-ui" } } }

@kama/rmlui's README covers the rest: build times, "debug": { "optimize": ["rmlui", "freetype"] } to keep the layout engine at release speed in a debug build, and Windows' static libstdc++.

import { ui::UiContext, ui::UiEvent, ui::UiCommand, ui::UI_KEY_DOWN };

fn void frame(ref UiContext ui, int32 play) {
    ui.updateAt(seconds: 12.5);      // a monotonic clock: hover transitions, the caret
    ui.render();
    int32 i = 0;
    while (i < ui.commandCount()) {
        UiCommand c = ui.command(index: i);
        bool draw = match (c.kind) { case Draw: true; case _: false; };
        // if (draw) { upload c.vertices (8 floats a vertex, premultiplied), c.indices; texture c.texture,
        //             keyed on ui.textureGeneration(handle: c.texture) }
        i = i + 1;
    }
    bool more = true;
    while (more) {
        Optional<UiEvent> e = ui.pollEvent();
        match (e) {
            case None: { more = false; }
            case Some(value: ev): { if (ev.element == play) { /* start */ } }
        };
    }
}

fn void open() {
    UiContext ui = UiContext.open(width: 1920, height: 1080, fontPath: "assets/fonts/Inter-Regular.ttf");
    ui.setDpRatio(ratio: 1.0);
    ui.loadDocument(path: "assets/ui/menu.rml");
    int32 play = ui.bindElement(elementId: "play");
    frame(ui: ui, play: play);
}

A page — a tool's window, say — opens with openPage, follows the window with setSize, and needs one binding for a whole list:

fn void openTool(int32 width, int32 height) {
    UiContext ui = UiContext.openPage(width: width, height: height, fontPath: "assets/fonts/Inter-Regular.ttf");
    ui.loadDocument(path: "assets/ui/items.rml");   // <div id="items"><div id="item-17">…</div>…</div>
    int32 items = ui.bindElement(elementId: "items");
    // every window resize: ui.setSize(width: w, height: h);  (a no-op on the web, where CSS follows the window)
    Optional<UiEvent> e = ui.pollEvent();
    match (e) {
        case None: { }
        case Some(value: ev): {
            if (ev.element == items) { string row = ui.elementId(handle: ev.target); /* "item-17" */ }
        }
    };
}

The program owns the window natively, so five things are its part. The browser does all five by itself.

In a browser

Build the program with --target WASM. This package's jsLibraries and emSettings reach the build on their own.

Writing documents for both legs

ui::uidoc repairs the five places RCSS and CSS disagree: borders have no style in RCSS, RmlUi has no default stylesheet, the browser's UA sheet, inheritance, and form-control accents. It also turns dp into px and scopes every selector under #ui-root, mapping body and RmlUi's scrollbar elements. Beyond that, the document's own job:

Tests

KAMA=/path/to/kama tools/test.sh       # native: debug, release, and AddressSanitizer (Linux), through @kama/rmlui
KAMA=/path/to/kama tools/test-web.sh   # emcc (EMSDK, default ~/emsdk), served locally, run in headless Firefox

The two scripts build one program, tests/. Shared on both legs:

On the native leg the input is RmlUi's own: the pointer, the buttons (a click half a second of the clock apart from the last, so it never pairs into a double click), the wheel and keys. On the web it is the browser's own hit test (document.elementFromPoint) and the events a browser fires, in its order. A synthetic event performs no default action, so the helpers perform the ones that matter: a press focuses what it hits; typing focuses the field and sends an input per character (a change only as a browser does, when the field is let go); an option is selected, then input and change; a drag is mousedown, dragstart, dragenter, dragover, drop, dragend; a range's thumb dragged is the press, an input per step, the release with its change, and the click (a press on its track the same, with one input for the jump); a key is a keydown at the focused element, then the same key forwarded to key. Natively a range is dragged by RmlUi's pointer at its bar, and an option is chosen by a click in the box laid out below its select. Each leg adds its own checks:

Results: native 285 checks, passing in debug, in release and under AddressSanitizer with leak checking. The ASan leg wraps cc/c++, so RmlUi, FreeType and both shims are instrumented. Web 305 checks, passing in headless Firefox. The web leg serves the media suite's clip beside the page (tools/test-web.sh copies it there). The native leg's font is Lato (SIL OFL 1.1), committed under tests/fonts/ with its licence and never published; nor is the clip, which the package's own code made (tools/make-test-clip.py, with PyAV).

The web verdict comes back over HTTP. The page POSTs {run, failed, output} to tools/web-test-server.py, so nothing reads a screenshot or a DOM dump. The page then closes its own window, which ends the browser. A failing check makes the script exit 1 with the check's name.

⚠️ Brave 144 hangs in --headless on this machine, even for a one-line page. Firefox is the default; set BROWSER to override.

Where it came from

Extracted from the UI layer of earlier kama games. Two copies were reconciled: the older had no lines the newer lacked, and its document-rewriter test suite (tests/src/uidoc_test.kama) came across with it. Prefixes became kama_ui_ / KamaUi. The changes made on the way:

decision why
The native backend is native.kama over @kama/rmlui. The original C++ shim is gone from this package. The binding is general now. This file holds only the seam's policy: one document, bind by id, the GLFW key subset, first-press focus, the 1/60 step, quiet writes.
UiCommand gained kind (clip masks), transformed and transform. Its pointers are UnsafeConstPtr. RmlUi 6's clip masks and transforms are recorded now, not dropped. The const pointers were owed (the original headers said so).
provideImage takes ConstView<uint8>, not UnsafeConstPtr<uint8>. Safe and length-checked; the web leg ignores it either way.
Added loadDocumentText(rml). Generated documents, and tests that need no files.
uidoc's .pam" → .webp" rule became setImageRewrite(nativeExt:, webExt:), applied by the web JS before parsing. One game's convention no longer sits inside a general rewriter. It now reaches setInnerRml content too, and it runs before the browser fetches the wrong file.
uidoc is a module, ui::uidoc, not its own package. One package to depend on and publish.
The web ring includes input, select and textarea, and excludes tabindex="-1" and inline tab-index: none. The legs agree. RmlUi rings every form control. Form fields with no tabindex attribute were skipped on the web and stopped on natively.
The web leg focuses [autofocus] when a document is set. autofocus does nothing in innerHTML. RmlUi's show focuses it, so the rings start in the same place.
The web leg's setElementProperty turns dp into px and reports whether the browser took the value. "150dp" was invalid CSS there, and was dropped without a word.
The contract's comments are generalised (nothing game-specific); the measured warnings stay. It is a package now.

Publishing

kama publish ships exactly what git tracks, minus publish.exclude (tools/, tests/, the agent notes). The three packages published in order on 2026-10-13: @kama/freetype, then @kama/rmlui, then @kama/ui, each once the one before it was live, because a published package cannot carry a path dependency. Publish from kama-registry with ./ops publish ../kama-ui/kama.json.

License

@kama/ui is licensed under either of

at your option. It vendors nothing. Its native leg builds @kama/rmlui and @kama/freetype, and their READMEs say what a program that ships them must carry:

The web leg ships only this package's own code. A wasm build compiles FreeType (kama cannot gate a dependency by target, @kama/rmlui's KAMA_GAPS.md #4), but nothing calls it, and the link drops it.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in @kama/ui by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.