Add the dependency

In your project’s ziran.toml; pin ref to a commit for reproducible builds.

[dependencies.tk]
git = "https://git.waozi.xyz/ziranlang/tk.git"
ref = "master"

Overview

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.

tk/src/bitmap.zi

bitmap

Inferno bitmap pixels follow libmemdraw/cread.c, cload.c, and libdraw/rgb.c. See LICENSE (MIT).

Import with #import "tk/bitmap".

MAX_PIXELS :: 262144
Encoding :: enum { Palette = 0; Mask = 1; }
Bitmap :: struct { pixels: Vec(u8); left: s32; top: s32; width: s32; height: s32; pitch: s32; start_bit: s32; encoding: Encoding; }
Bit :: (bitmap: *Bitmap, x: s64, y: s64) -> bool
DecodeBlock :: (input: string, pixels: []u8, row_bytes: s64) -> bool
Coordinate :: (field: string) -> (s64, bool)

Each coordinate is a signed 32-bit Inferno rectangle endpoint. Keep span arithmetic in s64 until the allocation and renderer bounds are known.

DecodePrefix :: (bytes: string, bitmap: *Bitmap, font: bool) -> s64

Subfonts append glyph metrics after the image and use legacy depth zero.

Decode :: (bytes: string, bitmap: *Bitmap) -> bool
Color :: (index: s32) -> u32

RGBA byte order on the hosted Linux/386 surface.

Source ↗

tk/src/canvas.zi

canvas

Canvas item geometry and commands, following Inferno libtk/canvs.c.

Import with #import "tk/canvas".

NONE :: -1
BLACK :: 0x000000
WHITE :: 0xffffff
RED :: 0xff0000
CELL_WIDTH :: 12
TEXT_HEIGHT :: 14
MAX_CELLS :: 512
FRACTION :: 10000
PIXELS_PER_INCH :: 100
MAX_COORDINATE :: 1000000
Anchor :: enum { Center = 0; North = 1; Northeast = 2; East = 3; Southeast = 4; South = 5; Southwest = 6; West = 7; Northwest = 8; }
Kind :: enum { Rectangle = 0; Label = 1; Line = 2; Oval = 3; Image = 4; }
Coordinates :: struct { x0: s32; y0: s32; x1: s32; y1: s32; }
PathPoint :: struct { x: s64; y: s64; }
FixedCoordinates :: struct { x0: s64; y0: s64; x1: s64; y1: s64; }
Binding :: struct { event: s32; script: Text; }
CanvasTag :: struct { name: Text; members: Vec(s64); bindings: Vec(Binding); }
Item :: struct { id: s64; tags: Vec(s64); bindings: Vec(Binding); kind: Kind; coordinates: Coordinates; anchor: Anchor; fill: s32; outline: s32; text: Text; precise: FixedCoordinates; path: Vec(PathPoint); stroke: s64; marker_radius: s64; }
Viewport :: struct { region: FixedCoordinates; width: s32; height: s32; x: s32; y: s32; increment: s64; resized: bool; }
State :: struct { viewport: Viewport; yscroll: Text; width: s32; height: s32; precise_width: s64; precise_height: s64; background: s32; border: s32; sunken: bool; items: Vec(Item); last_id: s64; tags: Vec(CanvasTag); all_bindings: Vec(Binding); current: s64; }
Default :: () -> State
Minimum :: (a: s32, b: s32) -> s32
Maximum :: (a: s32, b: s32) -> s32
Rectangle :: (coordinates: Coordinates) -> Coordinates
AnchoredBounds :: (coordinates: Coordinates, anchor: Anchor, width: s32, height: s32) -> Coordinates
Bounds :: (item: *Item, state: *State) -> Coordinates
TextBounds :: (item: *Item) -> Coordinates
Add :: (state: *State, kind: Kind, coordinates: Coordinates, fill: s32, outline: s32, anchor: Anchor) -> s64
AddRectangle :: (state: *State, coordinates: Coordinates, fill: s32, outline: s32) -> s64
AddPolyline :: (state: *State, count: s64, bounds: Coordinates, fill: s32, stroke: s64, marker_radius: s64) -> *Item
IncludePoint :: (bounds: Coordinates, x: s64, y: s64, first: bool) -> Coordinates
Find :: (state: *State, id: s64) -> *Item
SetCoordinates :: (state: *State, id: s64, coordinates: Coordinates) -> bool
SetText :: (state: *State, id: s64, text: string) -> bool
AddText :: (state: *State, x: s32, y: s32, anchor: Anchor, text: string, fill: s32) -> s64
Clear :: (state: *State)
DeleteAll :: (state: *State)
FindTag :: (state: *State, name: string) -> s64
EnsureTag :: (state: *State, name: string) -> s64
HasTag :: (item: *Item, tag: s64) -> bool
AttachTag :: (state: *State, item: *Item, tag: s64) -> bool
RemoveNumber :: (values: *Vec(s64), value: s64)
DiscardNewItem :: (state: *State)
MoveItem :: (destination: *Item, source: *Item)

Stable compaction preserves draw order and keeps IDs in search order.

TopItem :: (state: *State, name: string) -> *Item
Delete :: (state: *State, name: string) -> bool
Error :: (result: *T.Result, message: string)
Configure :: (state: *State, args: []Text, result: *T.Result) -> bool
Pixel :: (number: s64) -> s32
SetInteger :: (item: *Item, coordinates: Coordinates)
ReadCoordinate :: (word: string) -> (s64, bool)
ReadDistance :: (text: string) -> (s64, bool)

Inferno uses a fixed 100 dpi and truncates the per-unit ratios for cm and mm.

ReadRegion :: (word: string) -> (FixedCoordinates, bool)
ClampOrigin :: (origin: s64, first: s32, last: s32) -> s32
ClampViewport :: (view: *Viewport)
Resize :: (state: *State, width: s32, height: s32)
YSpan :: (view: *Viewport) -> s32
YFractions :: (view: *Viewport, callback: bool) -> Scroll.State
YView :: (view: *Viewport, args: []Text, result: *T.Result)
CanvasY :: (state: *State, args: []Text, result: *T.Result, top: s32)
SetFixed :: (item: *Item, args: []Text, count: s64) -> bool
SetLine :: (item: *Item, args: []Text, allow_options: bool) -> (s64, bool)
ParseAnchor :: (word: string) -> (Anchor, bool)
ParseColor :: (word: string) -> (s32, bool)
TagName :: (name: string) -> bool
UpdateItem :: (state: *State, id: s64, args: []Text, creating: bool) -> string
Create :: (state: *State, args: []Text, result: *T.Result)
Append :: (result: *T.Result, number: s64)
AppendCoordinates :: (result: *T.Result, coordinates: Coordinates, count: s64)
AppendFixed :: (result: *T.Result, value: s64)
BAD_TAG :: "bad/no matching tag"
WithTag :: (state: *State, name: string, ids: *Vec(s64)) -> bool
FindOrAddTags :: (state: *State, args: []Text, adding: bool, result: *T.Result)
RemoveTag :: (state: *State, args: []Text, result: *T.Result)
GetTags :: (state: *State, name: string, result: *T.Result)
PRESS_MASK :: 7
RELEASE_MASK :: 56
MOTION :: 64
ENTER :: 128
LEAVE :: 256
DOUBLE :: 512
ButtonBit :: (button: s32) -> s32
ParseEvent :: (sequence: string) -> s32
Bind :: (state: *State, args: []Text, result: *T.Result)
PointerEvent :: struct { mask: s32; x: s32; y: s32; screen_x: s32; screen_y: s32; }
AppendAction :: (result: *Text, script: string, event: PointerEvent, widget: string)
AppendActions :: (result: *Text, actions: *Vec(Binding), event: PointerEvent, widget: string)
PointerActions :: (state: *State, event: PointerEvent, widget: string) -> Text
Abs :: (value: s64) -> s64
Hit :: (state: *State, item: *Item, x: s32, y: s32) -> bool
TopHit :: (state: *State, x: s32, y: s32) -> s64
AppendRequestedSize :: (result: *T.Result, pixels: s32, precise: s64)
Command :: (state: *State, args: []Text, result: *T.Result, top: s32)

Source ↗

tk/src/check.zi

check

Selection values live in the Tk root, following Inferno libtk/varbl.c.

Import with #import "tk/check".

DEFAULT_VARIABLE :: "selectedButton"
MAX_VARIABLES :: 1024
variables: V.VariableTable
versions: [1024]u64
revision: u64
State :: struct { variable: Text; on: Text; off: Text; selected: bool; radio: bool; indicator: s32; observed: u64; }
Find :: (name: string) -> s64
Read :: (name: string, output: *Text)
Put :: (name: string, value: string) -> bool
Option :: (state: *State, name: string, output: *Text) -> bool
Indicator :: (value: string) -> s32
CopyOption :: (args: []Text, name: string, fallback: string, target: *Text) -> bool
Configure :: (state: *State, args: []Text, path: string, in_menu: bool) -> bool
Swap :: (a: *State, b: *State)
Value :: (value: string, radio: bool, fallback: string) -> string
Selected :: (state: *State) -> bool
Select :: (state: *State, selected: bool) -> bool
Invoke :: (state: *State) -> bool

Source ↗

tk/src/font.zi

font

Inferno font descriptors and packed one-bit subfonts. See libdraw/readsubfont.c.

Import with #import "tk/font".

MAX_FONTS :: 32
MAX_SUBFONTS :: 64
MAX_FONT_BYTES :: 524288
Subfont :: struct { first: s32; last: s32; offset: s32; ascent: s32; atlas: Bitmap.Bitmap; metrics: Text; }
Font :: struct { name: Text; references: s64; height: s32; ascent: s32; subfonts: Vec(Subfont); }
Store :: struct { fonts: Vec(Font); open: Images.Loader; }
Glyph :: struct { atlas: *Bitmap.Bitmap; x: s32; width: s32; top: s32; bottom: s32; left: s32; rise: s32; advance: s32; }
Token :: (source: string, at: *s64) -> string
Number :: (word: string) -> (s32, bool)
RelativePath :: (font: string, name: string) -> Text
MetricX :: (metrics: string, at: s64) -> s32
ReadSubfont :: (sub: *Subfont, bytes: string) -> bool
Load :: (font: *Font, path: string, open: Images.Loader) -> bool
Get :: (store: *Store, font: s64) -> *Font
Acquire :: (store: *Store, path: string) -> s64
Retain :: (store: *Store, font: s64)
Release :: (store: *Store, font: s64)
Lookup :: (font: *Font, rune: s32) -> Glyph
Character :: (font: *Font, rune: s32) -> Glyph
Line :: (text: string, start: s64) -> (s64, s64)

Inferno Canvas passes the line's byte length to Draw, which counts runes.

Measure :: (font: *Font, text: string) -> (s32, s32)

Source ↗

tk/src/image.zi

image

A Tk root owns its names; widgets can retain images after a name is deleted. File loading is supplied by the application using the current namespace.

Import with #import "tk/image".

MAX_IMAGES :: 32
Loader :: #type (path: string, bytes: *Text) -> bool
Entry :: struct { name: Text; pixels: Bitmap.Bitmap; mask: Bitmap.Bitmap; refs: s32; order: s64; }
Store :: struct { items: Vec(Entry); open: Loader; next_name: s64; order: s64; }
SetError :: (result: *T.Result, reason: string)
Find :: (store: *Store, name: string) -> s64
Read :: (store: *Store, source: string, bitmap: *Bitmap.Bitmap) -> bool
SetBitmap :: (target: *Bitmap.Bitmap, source: *Bitmap.Bitmap)
Allocate :: (store: *Store, bitmap: *Bitmap.Bitmap, mask: *Bitmap.Bitmap, existing: s64) -> s64
Retain :: (store: *Store, handle: s64)
Release :: (store: *Store, handle: s64)
Forget :: (store: *Store, index: s64)
Create :: (store: *Store, args: []Text, result: *T.Result)
Names :: (store: *Store, result: *T.Result)
Command :: (store: *Store, args: []Text, result: *T.Result)

Source ↗

tk/src/listbox.zi

listbox

Listbox commands and vertical viewport, following Inferno libtk/listb.c.

Import with #import "tk/listbox".

ROW_HEIGHT :: View.ROW_HEIGHT
INSET :: 2
Row :: struct { text: Text; selected: bool; }
State :: struct { rows: Vec(Row); width: s32; height: s32; visible: s64; top: s64; active: s64; anchor: s64; browse: bool; }
Error :: (result: *T.Result, message: string)
Integer :: (result: *T.Result, value: s64)
Default :: () -> State
Clamp :: (value: s64, upper: s64) -> s64
Move :: (list: *State, top: s64)
Layout :: (list: *State, height: s32)
Fractions :: (list: *State) -> Scroll.State
See :: (list: *State, row: s64)
Activate :: (list: *State, row: s64)
Choose :: (list: *State, row: s64)
Press :: (list: *State, y: s32)
Drag :: (list: *State, y: s32)
Step :: (list: *State, step: s64)
Index :: (list: *State, word: string) -> (s64, bool)
Range :: (list: *State, args: []Text) -> (s64, s64, bool)
Reverse :: (list: *State, first: s64, end: s64)
Insert :: (list: *State, index: s64, values: []Text) -> bool
AfterDelete :: (index: s64, first: s64, last: s64) -> s64
Delete :: (list: *State, first: s64, last: s64)
Yview :: (list: *State, args: []Text, result: *T.Result)
Selection :: (list: *State, args: []Text, result: *T.Result)
Command :: (list: *State, args: []Text, result: *T.Result)

Source ↗

tk/src/menu.zi

menu

Menu entries and indices follow Inferno libtk/menus.c. See ../NOTICE.

Import with #import "tk/menu".

COMMAND_HEIGHT :: 16

The stock font's 14-pixel row and one-pixel borders determine default y positions.

SEPARATOR_HEIGHT :: 6
MAX_ROWS :: 128
RowKind :: enum { Command = 0; Separator = 1; Checkbutton = 2; Radiobutton = 3; }
MenuRow :: struct { label: Text; command: Text; check: Check.State; kind: RowKind; disabled: bool; }
State :: struct { rows: Vec(MenuRow); active: s64; border: s32; }
Initialize :: (menu: *State)
Clear :: (menu: *State)
Error :: (result: *T.Result, message: string)
WriteInteger :: (result: *T.Result, value: s64)
SelectionEntry :: (row: *MenuRow) -> bool
IndicatorWidth :: (row: *MenuRow) -> s32
Height :: (row: *MenuRow) -> s32
RowTop :: (menu: *State, index: s64) -> s32
AtY :: (menu: *State, y: s32) -> s64
Index :: (menu: *State, word: string) -> (s64, bool)
Selectable :: (menu: *State, index: s64) -> bool
Activate :: (menu: *State, index: s64)
Invoke :: (menu: *State, index: s64, script: *Text)
Configure :: (menu: *State, args: []Text, result: *T.Result) -> bool
ConfigureRow :: (row: *MenuRow, args: []Text, result: *T.Result) -> bool
SwapRows :: (a: *MenuRow, b: *MenuRow)
Insert :: (menu: *State, index: s64, args: []Text, result: *T.Result)
Delete :: (menu: *State, first: s64, last: s64)
EntryOption :: (row: *MenuRow, option: string, result: *T.Result)
Command :: (menu: *State, args: []Text, result: *T.Result, script: *Text)

The root dispatch handles posting, since it owns the popup bounds and pointer grab.

Source ↗

tk/src/paint.zi

paint

A small, host-independent RGBA renderer and original stroke lettering.

Import with #import "tk/paint".

Canvas :: struct { pixels: []u8; width: s32; height: s32; first: s32; clipped: bool; clip: Tk.Box; }
Ink :: struct { r: u8; g: u8; b: u8; }
PAPER :: Ink.{r = 244, g = 246, b = 247}
BUTTON :: Ink.{r = 224, g = 234, b = 242}
PRESSED :: Ink.{r = 188, g = 213, b = 231}
BORDER :: Ink.{r = 119, g = 149, b = 173}
LETTER :: Ink.{r = 25, g = 47, b = 67}
MUTED :: Ink.{r = 112, g = 123, b = 132}
ENTRY :: Ink.{r = 255, g = 255, b = 255}
SELECTION :: Ink.{r = 255, g = 228, b = 145}
LINK :: Ink.{r = 30, g = 98, b = 145}
Fill :: (canvas: *Canvas, box: Tk.Box, ink: Ink)
Strokes :: (rune: s32) -> string

Each group is x0,y0,x1,y1 on a 5 by 7 grid. Lowercase uses the same glyphs.

Magnitude :: (value: s32) -> s32
Stroke :: (canvas: *Canvas, origin_x: s32, origin_y: s32, segment: string, ink: Ink)
DrawText :: (canvas: *Canvas, box: Tk.Box, text: string, center: bool, ink: Ink)
TextRow :: (canvas: *Canvas, view: *View.State, box: Tk.Box, line: string, start: s64)
DrawEntry :: (canvas: *Canvas, widget: *Tk.Widget, active: bool)
RenderFocusedEntry :: (pixels: []u8, width: s32, height: s32)
Render :: (pixels: []u8, width: s32, height: s32, first: s32)

Paint the rows from first (inclusive) through height (exclusive).

Color :: (rgb: s32) -> Ink
DrawCanvas :: (canvas: *Canvas, widget: *Tk.Widget)
Vertex :: struct { x: float64; y: float64; }
CeilPixel :: (value: float64) -> s32
DrawLine :: (canvas: *Canvas, x0: s32, y0: s32, x1: s32, y1: s32, half: s32, ink: Ink)
EllipseSpan :: (rx: s32, ry: s32, offset: s32) -> s32
DrawOval :: (canvas: *Canvas, x: s32, y: s32, rx: s32, ry: s32, half: s32, fill: s32, outline: s32)
DrawIndicator :: (canvas: *Canvas, x: s32, y: s32, radio: bool, selected: bool, ink: Ink)
DrawCheck :: (canvas: *Canvas, widget: *Tk.Widget)
ScrollSegment :: (box: Tk.Box, horizontal: bool, first: s32, last: s32, inset: s32) -> Tk.Box
DrawScroll :: (canvas: *Canvas, widget: *Tk.Widget, index: s64)
DrawScale :: (canvas: *Canvas, widget: *Tk.Widget, index: s64)
DrawPanel :: (canvas: *Canvas, widget: *Tk.Widget)
RenderPanels :: (pixels: []u8, width: s32, height: s32)

Animated panels can refresh their existing rectangles between layout changes.

Source ↗

tk/src/scale.zi

scale

Fixed-point scale values and slider geometry, following Inferno libtk/scale.c.

Import with #import "tk/scale".

UNIT :: 1000
SLIDER :: 16
INSET :: 3
MINOR :: 18
VALUE_HEIGHT :: 20
State :: struct { first: s64; last: s64; value: s64; resolution: s64; big_increment: s64; length: s32; horizontal: bool; show_value: bool; }
Default :: () -> State
Parse :: (text: string, unit: s64) -> (s64, bool)
Append :: (target: *Text, value: s64, unit: s64)
Bound :: (scale: *State, value: s64) -> s64
Step :: (scale: *State, direction: s64) -> s64
Travel :: (length: s32) -> s32
Position :: (scale: *State, length: s32) -> s32
Drag :: (scale: *State, length: s32, delta: s32, base: s64) -> s64

Source ↗

tk/src/scrollbar.zi

scrollbar

Fractional scrollbar geometry and pointer hit testing.

Import with #import "tk/scrollbar".

UNIT :: 10000
WIDTH :: 18
LENGTH :: 54
MIN_THUMB :: 8
State :: struct { first: s64; last: s64; horizontal: bool; }
Geometry :: struct { arrow: s32; travel: s32; first: s32; last: s32; }
Clamp :: (fraction: s64) -> s64
Track :: (state: *State, length: s32) -> Geometry
Hit :: (track: Geometry, offset: s32, length: s32) -> string

Source ↗

tk/src/text.zi

text

UTF-8 character boundaries shared by entry editing and stroke rendering.

Import with #import "tk/text".

Decode :: (text: string, at: s64) -> (s32, s64)
Next :: (text: string, at: s64) -> s64
Previous :: (text: string, at: s64) -> s64
Offset :: (text: string, count: s64) -> (s64, bool)
Count :: (text: string, limit: s32) -> s32
SingleLine :: (text: string) -> bool

Source ↗

tk/src/view.zi

view

Reusable line wrapping and vertical viewport for a Tk text widget.

Import with #import "tk/view".

ROW_HEIGHT :: 19
INSET :: 6
FRACTION :: 1000000
Tag :: struct { name: Text; release: Text; underline: bool; }
Range :: struct { tag: s64; first: s64; last: s64; }
State :: struct { rows: Vec(s64); tags: Vec(Tag); ranges: Vec(Range); columns: s32; visible: s32; top: s32; appended: bool; }
FindTag :: (view: *State, name: string) -> s64
EnsureTag :: (view: *State, name: string) -> s64
AddRange :: (view: *State, tag: s64, first: s64, last: s64) -> bool
ClearRanges :: (view: *State, tag: s64)
Reset :: (view: *State)
Clamp :: (view: *State)
Reflow :: (view: *State, source: string, columns: s32, visible: s32)
Row :: (view: *State, source: string, row: s32) -> string
Point :: (view: *State, source: string, x: s32, y: s32, width: s32) -> (s64, bool)

Coordinates are relative to the wrapped content, after the widget inset.

See :: (view: *State, at: s64)
Offset :: (source: string, line: s64, column: s64) -> s64
Index :: (source: string, at: s64, output: *Vec(u8))
Match :: (source: string, pattern: string, start: s64, stop: s64, backwards: bool) -> s64
Move :: (view: *State, lines: s64)
PageSize :: (view: *State) -> s32
AddFraction :: (output: *Vec(u8), value: s64)
Position :: (view: *State, output: *Vec(u8))
Moveto :: (view: *State, fraction: string) -> bool

Source ↗

tk/src/widgets.zi

widgets

Optional Tk command model, following Inferno libtk and tcl_tk.b. See ../NOTICE.

Import with #import "tk/widgets".

MAX_WIDGETS :: 32
MAX_ENTRY_BYTES :: 16384
LABEL :: 0
BUTTON :: 1
FRAME :: 2
ENTRY :: 3
TEXT :: 4
SCALE :: 5
PANEL :: 6
CHECKBUTTON :: 7
SCROLLBAR :: 8
CANVAS :: 9
LISTBOX :: 10
MENU :: 11
MENUBUTTON :: 12
RADIOBUTTON :: 13
SelectionButton :: (kind: s64) -> bool
MAX_PANEL_BYTES :: 16777216
CHAR_WIDTH :: Canvas.CELL_WIDTH
ENTRY_INSET :: 8
ROOT_INSET :: 12
DEFAULT_PAD_X :: 8
DEFAULT_PAD_Y :: 5
Box :: struct { x: s32; y: s32; w: s32; h: s32; }
Packing :: struct { side: s32; padx: s32; pady: s32; fill: s32; expand: s32; anchor_x: s32; anchor_y: s32; }
Panel :: struct { pixels: Vec(u8); width: s32; height: s32; border: s32; }
Widget :: struct { name: Text; text: Text; command: Text; box: Box; kind: s64; parent: s64; cursor: s64; view: View.State; scale: Scale.State; panel: Panel; check: Check.State; scroll: Scroll.State; canvas: Canvas.State; text_height: s32; last_scroll: Scroll.State; sent_scroll: bool; disabled: bool; requested: Box; order: s32; pack: Packing; }
widgets: Vec(Widget)

One root per application.

attached: bool
closed: bool
pressed: s64
pressed_range: s64
focused: s64
sequence: s32
drag_start: s32
drag_value: s64
canvas_hover: s64
canvas_buttons: s32
canvas_x: s32
canvas_y: s32
canvas_pointer_known: bool
last_canvas_button: s32
last_canvas_press: s64
Attach :: (interpreter: *Tcl.Interpreter) -> bool
Find :: (name: string) -> s64
ParentEnd :: (path: string) -> s64
SetError :: (result: *Result, message: string)
Configure :: (widget: *Widget, args: []Text, result: *Result, path: string) -> bool
Create :: (kind: s64, args: []Text, result: *Result)
Pack :: (_: s64, args: []Text, result: *Result)
Destroy :: (_: s64, args: []Text, result: *Result)
WidgetCommand :: (_: s64, name: string, args: []Text, result: *Result, script: *Text)
Focus :: (_: s64, args: []Text, result: *Result)
Bind :: (_: s64, args: []Text, result: *Result)
EntryOffset :: (widget: *Widget, index: string) -> (s64, bool)
Insert :: (widget: *Widget, at: s64, value: string) -> bool
Delete :: (widget: *Widget, first: s64, last: s64)
EntryCommand :: (widget: *Widget, args: []Text, result: *Result)
TextLayout :: (widget: *Widget)
TextOffset :: (widget: *Widget, name: string) -> (s64, bool)
TextSearch :: (widget: *Widget, args: []Text, result: *Result)
TextTag :: (widget: *Widget, args: []Text, result: *Result)
AppendText :: (widget: *Widget, value: string) -> bool
TextCommand :: (widget: *Widget, args: []Text, result: *Result)
EntryKey :: enum { Return = 0; Backspace = 1; Delete = 2; Left = 3; Right = 4; Home = 5; End = 6; Up = 7; Down = 8; PageUp = 9; PageDown = 10; }
KeyDown :: (key: EntryKey) -> Text
TextInput :: (value: string)
Minimum :: (a: s32, b: s32) -> s32
Maximum :: (a: s32, b: s32) -> s32
NonNegative :: (x: s32) -> s32
NextChild :: (parent: s64, after: s32) -> s64
Horizontal :: (pack: Packing) -> bool
EntryWidth :: (widget: *Widget) -> s32
Measure :: (parent: s64) -> Box
Expansion :: (index: s64, cavity: s32, horizontal: bool) -> s32

Reserve room for later siblings before sharing the cavity among expanders.

Place :: (parent: s64, cavity: Box)
Layout :: (width: s32, height: s32)
EntrySlots :: (box: Box) -> s32
EntryStart :: (widget: *Widget) -> s64
Over :: (x: s32, y: s32, kind: s64) -> s64
BoundRange :: (widget: *Widget, x: s32, y: s32) -> s64
PointerDown :: (x: s32, y: s32)
PointerUp :: (x: s32, y: s32) -> Text
PointerScript :: (script: string, local_x: s32, local_y: s32) -> Text
Closed :: () -> bool
MenuPosted :: () -> bool
ConfigureScale :: (widget: *Widget, args: []Text, result: *Result) -> bool
ScaleCommand :: (widget: *Widget, args: []Text, result: *Result)
ScaleLength :: (widget: *Widget) -> s32
ScaleCoordinate :: (widget: *Widget, x: s32, y: s32) -> s32
ScaleInput :: (widget: *Widget, value: s64) -> Text
ScaleTrack :: (widget: *Widget) -> Box
PointerPress :: (x: s32, y: s32) -> Text
Dragging :: () -> bool
PointerMove :: (x: s32, y: s32) -> Text
CanvasPointerKind :: enum { Move = 0; Press = 1; Release = 2; }
CanvasCallback :: #type (tcl: *Tcl.Interpreter, script: Text) -> bool
CanvasDispatch :: (tcl: *Tcl.Interpreter, index: s64, x: s32, y: s32, mask: s32, callback: CanvasCallback) -> bool
CanvasTarget :: (index: s64, x: s32, y: s32) -> s64
CanvasPointer :: (tcl: *Tcl.Interpreter, x: s32, y: s32, kind: CanvasPointerKind, button: s32, time: s64, callback: CanvasCallback) -> bool
ScaleDrag :: (widget: *Widget, x: s32, y: s32) -> Text
ConfigurePanel :: (widget: *Widget, args: []Text, result: *Result) -> bool
PanelCommand :: (widget: *Widget, args: []Text, result: *Result)
PanelImage :: (name: string, width: s32, height: s32) -> s64

Allocate a writable RGBA8 image for the panel and return its widget index. Callers take a fresh pixel view while the widget is alive.

VariableCommand :: (_: s64, args: []Text, result: *Result)
CheckCommand :: (widget: *Widget, args: []Text, result: *Result, script: *Text)
Distance :: (value: string) -> (s32, bool)
ConfigureScroll :: (widget: *Widget, args: []Text, result: *Result) -> bool
ScrollCommand :: (widget: *Widget, args: []Text, result: *Result)
ScrollLength :: (widget: *Widget) -> s32
ScrollOffset :: (widget: *Widget, x: s32, y: s32) -> s32
ScrollPress :: (index: s64, x: s32, y: s32) -> Text
ScrollDrag :: (widget: *Widget, x: s32, y: s32) -> Text
TextScroll :: (widget: *Widget, script: *Text)
AppendScroll :: (widget: *Widget, script: *Text, fractions: Scroll.State)
AppendScrollCommand :: (command: string, script: *Text, fractions: Scroll.State)
CanvasScroll :: (canvas: *Canvas.State, script: *Text) -> bool
CollectScroll :: (script: *Text)
Update :: (_: s64, name: string, args: []Text, result: *Result, script: *Text)

Source ↗