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 your
application also imports tcl/interpreter, add Tcl at the revision selected by
Tk so both packages use the same interpreter type. Read that revision from
ziran.lock so this works with either source checkouts or 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 first row. Feed pointer and keyboard events to
tk/widgets and evaluate any returned Tcl script with the same interpreter.
The default widget set includes Canvas, labels, buttons, frames, entries, text, scales, panels, checkbuttons, radiobuttons and scrollbars. Pass the following compiler defines to 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. The callback can use 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. The
package supplies fallback stroke lettering for 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
Run make test with a Ziran toolchain and C compiler. It checks the default
and feature-enabled exports, including the bitmap profile with Canvas off. It
runs the same widget test as native C and a
.zx bundle in the instruction engine, and compares the command results and
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 in 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, ordinary ziran add or
ziran lock discovers them and records their profile, digest, and toolchain
under each package's checked field in ziran.lock. For explicit selection,
add entries for the Tcl default and 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.
Run make artifact-smoke to verify all seven Tk libraries. The test 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. It compares the
widget output and pixel checksum and confirms the provider source cache stays
empty. It also 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 these 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 the local tests pass, the Gitea master job publishes <profile>.zx and
<profile>.zx.tsv under tk-zx-library at the full Tk revision. It 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.