Tk for Ziran

Tk provides an Inferno-compatible Tcl widget model, layout, input dispatch, and an RGBA renderer for Ziran programs. The host supplies events and presents the renderer's pixels. The package uses Ziran's standard library and the standalone Tcl interpreter.

Use Tk

Add the package with ziran add https://git.waozi.xyz/ziranlang/tk.git. If the application also imports tcl/interpreter, add Tcl at the revision selected by Tk so both packages share one interpreter type; read that revision from ziran.lock so this works with source checkouts and relocatable libraries:

tcl_revision=$(python3 - <<'PY'
import json
from pathlib import Path

lock = json.loads(Path("ziran.lock").read_text())
packages = {package["id"]: package for package in lock["packages"]}
tk = packages[lock["root"]["dependencies"]["tk"]]
print(packages[tk["dependencies"]["tcl"]]["commit"])
PY
)
ziran add https://git.waozi.xyz/ziranlang/tcl.git --ref "$tcl_revision"
using Tcl :: #import "tcl/interpreter";
using Tk :: #import "tk/widgets";
using Paint :: #import "tk/paint";

Initialize :: (interpreter: *Tcl.Interpreter) -> bool {
    if !Tk.Attach(interpreter) { return false }
    result := Tcl.Eval(interpreter, "label .ready -text Ready; pack .ready");
    return result.code == .OK;
}

Draw :: (pixels: []u8, width: s32, height: s32) {
    Tk.Layout(width, height);
    Paint.Render(pixels, width, height);
}

Attach once per application and keep the interpreter alive for the root's lifetime. Call Layout when the window size or widget geometry changes. Paint.Render takes a row-major RGBA buffer with four bytes per pixel; its optional first argument paints rows first through height - 1, with the buffer beginning at that row. Feed pointer and keyboard events to tk/widgets and evaluate any returned Tcl script with the same interpreter.

Widgets and feature defines

The default widget set includes Canvas, labels, buttons, frames, entries, text, scales, panels, checkbuttons, radiobuttons, and scrollbars. Compiler defines select additional features or a smaller build:

Define Effect
TK_BITMAP Enable image commands, bitmap labels and, with Canvas, Canvas images and bitmap fonts.
TK_LISTBOX Enable listboxes.
TK_MENU Enable menus and menubuttons.
TK_NO_CANVAS Omit the Canvas widget and painting.

With TK_BITMAP, Tk.SetImageLoader registers a callback that returns bytes for images and fonts, from in-memory data or the host's own resource lookup. @name bitmap references request name directly; plain bitmap names use the Inferno-compatible /icons/tk/ lookup prefix. Fallback stroke lettering covers ordinary text.

tk/widgets and tk/paint are the main entry points. The package also exports tk/bitmap, tk/canvas, tk/check, tk/font, tk/image, tk/listbox, tk/menu, tk/scale, tk/scrollbar, tk/text, and tk/view for direct access to their models and helpers. This is a subset of Inferno Tk: multiple root windows, grabs, grid layout, and embedded text widgets are outside the current implementation.

Local tests

make test needs a Ziran toolchain and a C compiler. It checks the default and feature-enabled exports, including the bitmap profile with Canvas off, and runs the same widget test as native C and as a .zx bundle in the instruction engine, comparing command results and the rendered pixel checksum. The test uses a bitmap supplied from memory and runs without a window server.

To develop against the sibling Tcl and Ziran source trees, create the ignored ziran.local.toml with these canonical overrides:

[overrides]
ziran = "../../ziran"
tcl = "../tcl"

Relocatable ZX libraries

With the exact compiler and Tcl default ZX library from ziran.lock cached, run make test artifact from a clean Tk checkout. Use ziran pkg zx fetch-ir with the Tcl repository, revision, profile, and digest from ziran.lock to populate the cache. This writes seven relocatable .zx libraries and build/artifacts/manifest.json, which records the provider and toolchain identities, the locked Tcl dependency, the exports, the defines, and each archive's SHA-256 digest.

Profile Defines
default None
no-canvas TK_NO_CANVAS
bitmap TK_BITMAP
bitmap-no-canvas TK_BITMAP, TK_NO_CANVAS
bitmap-menu TK_BITMAP, TK_MENU
full TK_BITMAP, TK_LISTBOX, TK_MENU
listbox-no-canvas TK_LISTBOX, TK_NO_CANVAS

A relocatable .zx library supplies typed code and metadata for ordinary imports such as tk/widgets and tk/paint; the consumer compiler links and lowers it for the selected target. The consumer's ziran.lock pins Tk, Tcl, and the exact Ziran toolchain. Once both default libraries are published, ziran add or ziran lock discovers them and records their profile, digest, and toolchain under each package's checked field. For explicit selection, add entries for the Tcl default and the selected Tk profiles in ziran.zx.lock under packages, with the package ID, profile, and sha256 for each. Keep the Tk profile consistent with the app's feature defines.

make artifact-smoke verifies all seven Tk libraries: it builds and runs the widget fixture offline as a native executable and as portable ZX, using the full Tk library and the locked default Tcl library; compares widget output and pixel checksum; confirms the provider source cache stays empty; and tests ziran add, ziran lock --offline, and an offline bundle with the two default libraries. Pass TCL_ZX_LIBRARY=<path> to supply a local copy of the locked Tcl library.

For working-tree changes, make artifact DEVELOPMENT=1 writes development libraries to build/artifacts-dev/. Test them with make artifact-smoke DEVELOPMENT=1 TCL_ZX_LIBRARY=<path> TCL_ZX_SHA256=<digest> using a Tcl development library built with the same compiler.

After local tests pass, the Gitea master job publishes <profile>.zx and <profile>.zx.tsv under tk-zx-library at the full Tk revision, then runs make artifact-smoke REGISTRY=1 against Gitea with empty consumer caches. Local publication uses make artifact-publish NETRC_FILE=<path> with a private Gitea netrc.

S
Description
No description provided
Readme MIT
132 KiB
0 Stars 1 Watchers 0 Forks