The ztk plan

ztk is a Tk replacement in Zig. It uses the ztcl interpreter. This page is the master plan. Tasks has the work of Stage 1. Review has the checklist of the reviewer. The size of Tk has the measured counts and the commands that made them. Lessons from Printdeck has what an earlier UI framework of the same author taught.

The repository has no toolkit code yet. Each statement about ztk in this page is a plan.

1. Goal

A script that runs in wish 8.6 runs in ztk with no change. That is the long-term goal.

2. Non-goals

3. The size of Tk

All numbers are from core-8-6-16 on 2026-11-23. The size of Tk gives the method and the table for each file.

What Count
Manual pages for script commands (doc/*.n) 87 pages, 25 088 lines
Manual pages for C functions (doc/*.3), out of scope 87 pages
Global commands that wish adds to tclsh 55
Classic widget classes 18, with 542 options and 174 widget subcommands
ttk widget classes 18, with 226 options and 170 widget subcommands
Standard options (options.n), standard ttk options (ttk_widget.n) 42, 16
Canvas item types 9, with 160 item options
Menu entry types 5, with 73 entry options
Text tag options 27
wm subcommands, winfo subcommands 32, 49
grid, pack, place subcommands 13, 6, 5
Event types of bind, % fields of bind 28, 32
Predefined virtual events 32
Default class bindings: classic, ttk 378, 281
Named colors (colors.n), key symbols (keysyms.n) 760, 1 615
ttk themes on X11, elements of the default theme 4, 39
Test files 109
Tests, by a count of test lines 10 080
Tests that a real wish 8.6.16 counted 10 221: 8 781 pass, 1 383 skip, 57 fail
Source lines of Tk (C, headers, Objective-C, and Tcl) 322 187

Tk 9.1.0 has 93 manual pages, 119 test files, and 12 016 test lines.

The 1 383 skipped tests are mostly for another platform (win 336, nt 62, aqua 29), or they need a test command of the Tk test binary (testobjconfig 215, testImageType 145, testwrapper 54, testsend 54), or they need exact X fonts (fonts 126).

Three widgets have a large part of the work. The eight text*.test files have 1 776 tests, and text.n has 2 293 lines. The seven canv*.test files have 395 tests, and canvas.n has 1 930 lines. The ttk files have 505 tests.

4. Architecture

                         Tcl script
                             |
              ztcl interpreter, with the commands of ztk
                             |
  +------------------------------------------------------------------+
  | widget tree | options | geometry managers | bind | focus         |
  +------------------------------------------------------------------+
  | style system (CSS subset)       | text (font engine, layout)     |
  +------------------------------------------------------------------+
  | paint: widgets to a scene (a list of quads)                      |
  +------------------------------------------------------------------+
  | renderer: CPU | Vulkan | Metal | Direct3D 12 | WebGL 2           |
  +------------------------------------------------------------------+
  | backend: headless | direct | Wayland | X11 | AppKit | Win32 | web |
  +------------------------------------------------------------------+

Two threads do the work. The UI thread runs the interpreter, the widget tree, the style, the layout, and the paint, and makes a scene. The render thread draws the scene and shows it. The hand-over is the scene: plain data with no pointer into the widget tree. Decision D23 has the reason.

The core (the widget tree, the layout, the style system, the scene, the text code, and the CPU renderer) is Zig only and links no library. Each backend links only the interfaces of its platform. The same core runs on Linux, macOS, Windows, and the web. A build for a small device has the core and the direct backend, and is one static program.

4.1 The ztcl binding

Section 10 lists what ztk needs from ztcl.

4.2 The widget tree

4.3 Layout

4.4 Events and bind

4.5 Focus

4.6 The scene and the renderer

4.7 Text

4.8 The style system

A stylesheet is a subset of CSS. The widget tree is the document.

CSS ztk
Type selector Button The class of the widget (winfo class). TButton is the ttk button.
#name The last part of the path. #ok matches .f.ok.
.name A style class of the widget. See below.
A B, A > B The widget tree.
:hover The pointer is on the widget. This is the active state of ttk.
:active A button is down on the widget. This is the pressed state of ttk.
:focus, :disabled, :checked, :read-only, :invalid The states focus, disabled, selected, readonly, invalid.
:indeterminate, :window-inactive The states alternate and background.
:state(name) Each other state, for example user1.
::part A part of a widget: ::selection, and later the elements of ttk such as ::thumb and ::trough.
--name, var() Variables. They inherit down the tree.
@media prefers-color-scheme, and -ztk-platform: linux, macos, windows, or web.

The properties are the box and the paint of a widget: color, background, border, border-radius, padding, font, opacity, box-shadow, outline, min-width, min-height, transition, caret-color, cursor, and text-align. The layout properties of CSS (display, position, flex, grid, margin) are not in the subset, because the geometry managers of Tk do that work.

The cascade has these origins, from the lowest to the highest:

  1. The base sheet of ztk.
  2. The theme sheet (modern.css or classic.css).
  3. The platform sheet of the theme (modern-linux.css).
  4. The option database (option add), in the priority order of Tk.
  5. The settings of ttk::style configure and ttk::style map.
  6. The stylesheets of the application (ztk::style load, ztk::style add).
  7. The options that the script set on the widget (-background red).

Inside one origin, the specificity and then the order decide, as in CSS.

How Tk maps onto it:

The style system computes a style only for a widget with the style flag. A change of state on one widget does not compute the style of its siblings. A change of a variable on :root computes all.

4.9 Accessibility

4.10 Clipboard, selection, and drag and drop

4.11 HiDPI

4.12 Backends

Platform Window and input GPU Stage
Headless Memory only, events from the tests CPU renderer 1
Linux, Wayland libwayland-client, xdg-shell, libxkbcommon Vulkan, or the CPU renderer 1
Linux, direct The kernel only: evdev, the framebuffer device, own key map Vulkan on the display (VK_KHR_display), or the CPU renderer 1
Linux, X11 libxcb Vulkan 2
macOS AppKit Metal 3
Windows Win32 Direct3D 12 3
Web A canvas element, DOM events WebGL 2 3
Remote A browser that draws the scenes of a ztk program on another machine The web renderer 3

A backend implements one interface: make and delete a window, set its title and size, wait for events with a timeout, give the time, give the clipboard, and give the surface for the renderer.

The GPU rule has one more part: each platform draws with its own native API and no translation layer between ztk and the driver. That is Vulkan on Linux, Metal on macOS, and Direct3D 12 on Windows. The Metal and Direct3D 12 renderers wait for Stage 3. The rule for each backend is the same on each platform: use the lowest interface that the platform gives to a program, and no library between ztk and it. That is the window system and Vulkan on a Linux desktop, AppKit and Metal on macOS, Win32 and Direct3D 12 on Windows, and the canvas and WebGL 2 in a browser. The direct backend is the same rule for a Linux device with no window system: the kernel and Vulkan. The core, the font engine, the scene, and the CPU renderer are the same code in all of them, and the CPU renderer works behind each backend.

5. Decisions

Each decision has the options, the recommendation, and the reason. The user decides the ones with User in the last line.

D1. The Tk version

D2. The dependency on ztcl

D3. The GPU layer

D4. One quad form for all drawing

D5. A CPU reference renderer

D6. The text engine

D7. Bidi and line breaks

D8. Windows on Linux

D9. Where ztk uses native UI

Thing Native Drawn by ztk
All widgets, including scrollbars all platforms
Menu bar macOS Linux, Windows, web
Popup menus all platforms
File and folder dialogs macOS, Windows, web, and Linux through the desktop portal Linux with no portal
Message box, color dialog, font dialog macOS Linux, Windows, web
Window decorations where the platform gives them Wayland with no decoration protocol
Accessibility tree all platforms
Input methods, clipboard, drag and drop all platforms
System tray and notifications (Tk 9) all platforms

Why: the user of a platform knows its file dialog and its menu bar, and the platform enforces them (the sandbox portals, the macOS menu bar). The widgets stay drawn so that one stylesheet styles them.

D10. The style language

D11. The CSS names of the states

D12. The unit of winfo and -width

D13. The default class bindings

D14. The event loop

D15. Shaders

D16. The Vulkan headers

D17. Accessibility adapters

D18. The default font

D19. What the ztk program allows

D20. Image goldens in the repository

D21. The display with no display server

The request of the user: use the GPU when possible, as directly as possible, with no dependency except Vulkan.

D23. The thread for drawing

The request of the user: drawing is not on the same thread as the calculations.

D22. A remote display

6. Dependencies

The rule: ztk needs only the interfaces of the platform, ztcl, and the Zig standard library. It needs no third-party library on any platform. Each optional library has a build option and a fallback.

Platform What ztk links or opens Why
All ztcl, the Zig standard library The interpreter
Linux, direct, CPU Nothing. One static program with no libc. Kernel system calls only
Linux, direct, GPU libc, and the Vulkan loader at run time The loader is a shared library
Linux, Wayland libc, libwayland-client, libxkbcommon, and the Vulkan loader at run time Vulkan needs the Wayland client library for its surface. The compositor sends the key map in the XKB format.
Linux, X11 (Stage 2) libc, libxcb, libxkbcommon, and the Vulkan loader at run time The X11 protocol and its key maps
macOS (Stage 3) The system frameworks: AppKit, Metal, Core Text Windows, the GPU, the list of fonts
Windows (Stage 3) The system libraries: user32, d3d12, dxgi, dwrite Windows, the GPU, the list of fonts
Web (Stage 3) The browser: a canvas, WebGL 2, DOM events No library in the WebAssembly file
Optional Use License Without it
Vulkan loader The GPU on Linux Apache-2.0 The CPU renderer
fontconfig, opened at run time (Stage 2) The font choices of the user on Linux HPND The font folders
HarfBuzz (Stage 4) Shaping of complex scripts Old MIT No shaping for these scripts
AccessKit (Stage 8, D17) Accessibility adapters MIT OR Apache-2.0 No accessibility tree
Data and build tools Use License
A font (D18) The fallback font and the font of the tests SIL OFL 1.1
Vulkan headers Declarations Apache-2.0 OR MIT
glslc, wayland-scanner Build tools Apache-2.0, MIT

The license names of the system libraries are from the package data of this machine (pacman -Qi). No dependency has a copyleft license.

7. Performance targets

The reference machine is the development machine: an Intel Core i7-6700K with an Intel GPU and an NVIDIA GPU. Each result in the docs names its machine and its date.

Target Number How to measure
Frame rate 60 frames in each second during a resize, a scroll, and a transition ZTK_FRAME_STATS=1 prints the time of each frame stage and the count of late frames
CPU time of one frame At most 4 ms at the 99th percentile for a window with 1 000 widgets zig build bench, on the headless backend, in ReleaseFast
GPU submit time At most 2 ms for 10 000 quads The same statistics, on the Vulkan renderer
Idle No frame and no wake-up when no input and no timer exists Count the frames in 5 seconds: the count is 0
Start First frame of the demo in at most 100 ms on Wayland, and at most 30 ms headless ZTK_FRAME_STATS=1 prints the time from process start to the first frame
Memory At most 24 MB of private memory for the demo on the headless backend. At most 1 KB for each plain widget. Private_Dirty of /proc/self/smaps_rollup at the first frame, and the difference after 10 000 labels
Interpreter memory No growth after 100 000 events with a binding The arena size of ztcl before and after, in a test
Direct backend, idle No write to the panel and no timer A test compares the panel memory. On a device: the CPU use of the process is 0 %
Direct backend, CPU output, small change A frame that changes one line of text writes under 10 % of the rows A test counts the rows
Direct backend, CPU output Under 5 % of one core of a Raspberry Pi 5 while the caret blinks in the demo top on the device, by a person
Direct backend, Vulkan output 60 frames in each second during a transition on a Raspberry Pi 5 The frame statistics on the device, by a person
Program size The static program with the CPU output is under 3 MB in ReleaseSmall, without the fonts ls -l
Slow frame Each frame over 100 ms prints one line with the time of each stage Always on

For comparison, wish 8.6.16 on this machine, with a virtual X server and other load: the time to the first update of one button was 59 to 168 ms in seven runs, and the resident memory was 17 MB.

The GPU driver maps memory that ztk does not control. The memory target for a window on a GPU comes from a measurement in Stage 1.

8. Verification

This is the main risk. A toolkit can pass each unit test and still draw a wrong window. Seven layers check it.

  1. Unit tests (zig build test). The parser of CSS, the cascade, the grid solver, the pattern parser of bind, the text edit model, and the option parser are pure functions with tables of cases.
  2. Script tests. A Tcl script runs on the headless backend and a test checks the results of winfo, cget, and the variables. Input comes from event generate and from the test interface of the headless backend.
  3. Scene dumps. The scene of a window prints as text: one line for each quad. The test compares the text with a file in tests/golden/. A dump has no dependence on a rasterizer.
  4. Golden images. The CPU renderer makes a PNG. The test compares it with a file in tests/golden/. Each image must be equal in each byte, also with text, because the font engine and the rasterizer are ztk code. A failed test writes the actual image and a difference image.
  5. GPU against CPU. The Vulkan renderer draws the same scene to an offscreen image, with no window. The test compares it with the CPU image: at most 2 levels in each channel, in at most 0.1 % of the pixels. This test needs a GPU and skips with a message when the machine has none.
  6. The oracle. A machine with wish 8.6.16 and Xvfb runs the same script in wish and in ztk. tools/oracle.sh compares the geometry of each widget. With frames of fixed size, the geometry does not depend on fonts, so grid, pack, and place must be equal exactly.
  7. Scene checks. After a frame, a check reads the scene and the widget tree. It reports text that its widget cuts, text with a contrast under 4.5 to 1, and a widget outside its window. It runs for the light and the dark scheme, at three scales, and at 1.5 times the font size. Printdeck has the same two rules: a label is never cut, and the theme colors have a tested contrast.

The compatibility measure is the test suite of Tk:

What still needs a real machine and a person:

Each stage has a short manual checklist for these. The result goes in the stage report with the date and the machine.

9. Rules for sources

10. What ztk needs from ztcl

ztcl is a separate project. Each item is a request to its maintainers. Until an item exists, ztk uses the fallback.

# Request Why Fallback in ztk
1 The event loop (after, vwait, update) on main Tk scripts use them Own commands in src/loop.zig (task 04)
2 An event source for the embedder: a descriptor for the wait, a function for "work is ready", and a function that runs the work. Also the time to the next timer. vwait and update must handle window events, and the wait must include the display connection ztk keeps its own loop and does not turn on the event capability
3 compact for an interpreter that has capabilities, or a temporary arena for each callback The arena of ztcl grows with each callback. A GUI runs callbacks for days. ztk calls compact between events when no capability is on, and measures the growth
4 register with a ctx argument, and a callback when a command is deleted or renamed A widget command belongs to a widget ztk sets Command.ctx after register, and wraps rename
5 A native variable trace with a ctx -textvariable and -variable A script trace: trace add variable with a ::ztk::priv command
6 A compiled script handle that stays valid across compact A binding runs many times ztk compiles again after each compact
7 package provide Tk 8.6 works with the package capability package require Tk is the first line of many scripts The ztk program turns on package
8 TclOO Many Tk programs use it None. The programs that need it fail.
9 All features on ARM (aarch64) The direct backend runs on ARM devices. The design page of ztcl says that the stack switch of coroutines is x86-64 code. ztcl builds for aarch64-linux (checked). A script with coroutine on ARM was not run.

Known facts about ztcl at commit 125392f that the design depends on:

11. Stages

An estimate is in weeks of full-time work of one engineer who knows the field. It includes tests and review. No measurement of the speed of the agents on this project exists yet. Stage 1 records the time of each task, and the plan gets new estimates after it.

Stage Content Exit criteria Estimate
0 This plan The user accepts the plan and the decisions done
1 The demo on Linux: frame, label, button, entry, ., grid, bind, the CSS subset, the modern theme, the font engine, the CPU and Vulkan renderers, the render thread, and the headless, Wayland, and direct backends examples/demo.tcl runs in ztk and in wish. All tests of the Stage 1 tasks pass. The frame statistics show no late frame in the demo at 60 Hz. The demo draws to a framebuffer in memory in the tests. The static build has no dependency. The stage report has the measured numbers. 6 to 8 weeks, about 19 000 lines
2 The core of Tk on Linux: pack, place, the complete bind and event, focus and grab, wm, winfo, option, font, image (photo with PNG, GIF, and PPM; bitmap), the other simple classic widgets (checkbutton, radiobutton, message, labelframe, scale, scrollbar, listbox, spinbox, panedwindow, menubutton, toplevel), the classic theme, the X11 backend, own window decorations, the fonts of the system, touch scroll and fling, image caches, KMS for the direct backend if a device needs it, the Tk suite runner The compatibility table exists for all 109 files. Each in-scope file of the Stage 2 commands has a pass rate of at least 70 %. The oracle test passes for 1 000 random layouts of each manager. 18 to 26 weeks
3 The macOS, Windows, and web backends at the level of Stage 2, and the remote display if the user wants it (D22) The demo and the Stage 2 golden images pass on each platform, with the GPU test against the CPU reference. A person does the manual checklist on each platform. 12 to 22 weeks
4 Complete text: CFF outlines, variable fonts, color glyphs, ligatures and marks, the optional HarfBuzz shaper, bidi, line breaks, font fallback, input methods, and the text widget The Unicode conformance files pass. The eight text*.test files have a pass rate of at least 70 %. The fonts of a default install of each platform draw. 24 to 36 weeks
5 The canvas widget: 9 item types, tags, bindings, scrolling The seven canv*.test files have a pass rate of at least 70 %, without canvPs.test. 10 to 16 weeks
6 ttk: 18 widgets, the complete ttk::style, and the four theme names The 17 ttk test files have a pass rate of at least 80 %. 14 to 22 weeks
7 Menus, dialogs, and platform services: menu, tk_popup, the native menu bar on macOS, the message box, the file dialogs, the color and font dialogs, all clipboard formats, drag and drop menu.test, msgbox.test, filebox.test, clipboard.test, and select.test have a pass rate of at least 70 %. A person does the manual checklist. 10 to 16 weeks
8 Accessibility and input methods on all platforms A screen reader reads and operates the demo on each platform. 8 to 14 weeks
9 Compatibility: the pass targets for all in-scope files, the commands of Tk 9, the private commands that real programs use, performance, and the docs At least 90 % of the in-scope tests pass. Three real Tk programs that the user selects run with no change. 16 to 30 weeks

Stage 1 measured (2026-11-23, the report): 19 800 lines of Zig and 303 tests. An agent run made tasks 10 to 21 in about 2.5 hours of wall clock time on a shared machine, after tasks 01 to 09 in an earlier run. The run needed no review round yet, so the estimates of the later stages stay as they are until a reviewer has read Stage 1.

The sum is 118 to 190 weeks, or two and a quarter to three and a half years for one engineer. Tk has 322 187 lines, and its first release was in 1991. A much smaller estimate is not credible.

Each stage after Stage 1 gets its own task list when it starts. The task list of a stage is small tasks in order, in the form of Stage 1.

12. Risks

Risk Effect Answer
The tests pass and the window is wrong A broken release The seven layers of section 8, and the manual checklist of each stage
The memory of ztcl grows with each callback A long session runs out of memory Request 3 of section 10. Task 18 measures the growth.
The direct backend has no test on a device in Stage 1 It fails on real hardware The tests use memory and a pipe. A person runs the demo on a device before Stage 2 depends on it.
The font engine draws worse glyphs than FreeType, or cannot read a font Text looks wrong Golden images that the reviewer opens, a comparison with fontTools in the tests, and the font of the package as the fallback
ztcl has no TclOO and has gaps in info and namespace Real Tk programs fail in the interpreter before they reach ztk The Stage 9 programs show the gaps. The requests go to ztcl.
The geometry of Tk depends on font metrics Many tests of Tk cannot pass with another font Measure in Stage 2 with the test font. Report the count honestly.
Four renderers differ A platform shows a different image One quad form, one shader source for each API, and the GPU test against the CPU reference
Two threads share a frame A race draws a wrong frame or crashes One mutex and three frames, no other shared state, a stress test in a loop, and a review of each shared field
The text widget and the canvas are large Stages 4 and 5 are late They come after the platform work, so the base is stable
A person must check each platform The review of an agent cannot find a wrong look The manual checklists, with the date and the machine in each report
Zig changes between releases The build breaks Pin Zig 0.16.0 in build.zig.zon and move in one commit

13. Decisions for the user

  1. D6: the font engine in Zig, with HarfBuzz only as an optional shaper. This is the largest change that the request for no dependency causes.
  2. D17: AccessKit (a Rust library) for accessibility, or own adapters.
  3. D18: Inter as the font in the package.
  4. D19: the default host access of the ztk program.
  5. D9: the list of native UI, in particular a drawn menu bar on Windows and Linux.
  6. D12: logical pixels for winfo and -width. A script that reads winfo screenwidth on a HiDPI monitor gets a smaller number than in Tk on X11.
  7. The order of Stages 3 to 7. The plan has the other platforms before the large widgets. The other order gives more of Tk on Linux sooner.
  8. The three real Tk programs of Stage 9.
  9. The requests to ztcl in section 10, in particular requests 2 and 3.
  10. A software Vulkan driver (the Mesa lavapipe package) on the development machine. With it, the GPU tests also run with no GPU.
  11. D21: the direct backend in Stage 1 (task 20), with Vulkan on the display and the framebuffer, and a person who runs it on a device.
  12. D22: a remote display in a browser, as Printdeck has.
  13. Printdeck has the AGPL, so ztk uses its ideas and no code. The author of Printdeck owns its code and can permit a copy under BSD-2-Clause. The plan does not assume that.