Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ zig-out/
*.a
.DS_Store
tools/parity/refgallery
zig-pkg/
32 changes: 22 additions & 10 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ implemented. User-facing docs live in [README.md](README.md); design rationale i
`zigui` is a SwiftUI-like UI library in **pure Zig 0.17**. The core (geometry,
color, state, layout, theme, text, canvas, software rasterizer, GPU scene
translation, view layer, components) has **no C dependencies** and is fully
unit-tested headlessly. SDL3 links only into example executables (`src/app.zig`
unit-tested headlessly. SDL3 is a **lazy Zig package** (castholm/SDL in
`build.zig.zon`, built from source and statically linked — no system install;
`-Dsdl3-prefix` overrides) and links only into example executables (`src/app.zig`
+ `src/gpu/gpu.zig`). On screen, rendering is **GPU-accelerated via SDL_GPU**
(Metal on macOS, Vulkan on Linux/Windows) with an automatic fallback to the
**CPU software rasterizer** presented through an SDL streaming texture — both
Expand All @@ -30,6 +32,8 @@ seams each one uses.
zig build test --summary all # 169 tests, headless. THIS is the inner loop.
zig build showcase edit # build the two examples (does NOT run them)
zig build run-showcase # opens a window — blocks on the event loop
make demo # same, ReleaseFast (SDL3 fetched/built by Zig)
make windows|linux|linux-arm64|macos-arm64 # cross-build examples → zig-out/<target>/bin
docker build -t zigui-test . # run the full suite on Linux

# Headless UI iteration (renders one frame to a BMP, no window):
Expand Down Expand Up @@ -110,7 +114,7 @@ app.zig (SDL3): build → render → (GPU encode | framebuffer upload) → prese
| `src/render/canvas.zig` | `DrawCommand` union + `Canvas` builder. The renderer-agnostic seam. |
| `src/render/raster.zig` | `Framebuffer` + software rasterizer (SDF AA). Where new draw primitives get pixels. |
| `src/render/gpu_scene.zig` | Pure GPU scene translation: `DrawCommand`s → packed `Instance` array + shelf-packed glyph/image `Atlas` + `Step` list (draw runs split at blurs). Headless-tested; no C. |
| `src/gpu/gpu.zig` | The SDL_GPU shell (app layer, links C like `app.zig`): device/pipelines/textures, replays a translated `Scene`, offscreen `renderToRgba` for verification. Shaders in `src/gpu/shaders/` (GLSL→committed SPIR-V via `compile.sh`, plus runtime-compiled MSL twins). |
| `src/gpu/gpu.zig` | The SDL_GPU shell (app layer, links C like `app.zig`): device/pipelines/textures, replays a translated `Scene`, offscreen `renderToRgba` for verification. Shaders in `src/gpu/shaders/` (GLSL→committed SPIR-V via `compile.sh`, plus runtime-compiled MSL and HLSL twins). |
| `src/text/*` | `ttf` (parser+rasterizer), `atlas` (`GlyphCache`), `shape` (measure/wrap), `font` (`drawText`). |
| `src/theme/theme.zig` | `Theme`, `Palette`, `Metrics`, `Typography`, and the **`Painter`** vtable + `Surface`/`ControlState`/`Role`. The color-scheme/painter vocabulary. |
| `src/theme/{macos,win2000,windows10,kde,mui}.zig` | The five built-in theme families: each exports `light`/`dark` `Theme` values and a `Painter` impl drawing that family's chrome (liquid glass / bevels / flat / Breeze / Material). |
Expand Down Expand Up @@ -179,10 +183,9 @@ exercises nav + tabs + sheet + material + a11y together; `examples/showcase`
demos them in a real window. Notes below record *how* each is wired and the
deliberate deviations from the original plan, so you can extend safely.

### GPU backend — SDL_GPU (Metal/Vulkan) with software fallback
On-screen rendering now goes through **SDL3's GPU API** (Metal on macOS, Vulkan
on Linux/Windows; D3D12 needs DXIL blobs, a follow-up — Windows uses its Vulkan
driver meanwhile), replacing the framebuffer-upload present path while keeping
### GPU backend — SDL_GPU (Metal/D3D12/Vulkan) with software fallback
On-screen rendering now goes through **SDL3's GPU API** (Metal on macOS,
Direct3D 12 on Windows with Vulkan as its fallback, Vulkan on Linux), replacing the framebuffer-upload present path while keeping
it as the automatic fallback (`Config.gpu = false` or `ZIGUI_SOFTWARE=1` forces
it; device/swapchain failure falls back silently). No new dependencies: the C
surface is `src/gpu/gpu.zig`, owned by `app.zig`, sharing its `@cImport`.
Expand Down Expand Up @@ -213,11 +216,20 @@ The split keeps the repo's testing philosophy intact:
scene texture, then `SDL_BlitGPUTexture` to the swapchain.
- **Shaders** in `src/gpu/shaders/`: GLSL sources compiled to **committed
SPIR-V blobs** by `compile.sh` (needs `glslc`; only when shaders change) and
hand-written **MSL twins** compiled by Metal at runtime — keep both in sync.
hand-written **MSL twins** compiled by Metal at runtime, and **HLSL twins**
(`prim.hlsl`/`blur.hlsl`) compiled on Windows at runtime to DXBC SM5.1 via
`D3DCompile` from the system's `d3dcompiler_47.dll` (loaded with
`SDL_LoadObject`; no build-time toolchain) — keep all three in sync.
SDL_GPU binding conventions: SPIR-V vertex uniforms `set=1`, fragment
textures `set=2`, fragment uniforms `set=3`; MSL uses `[[stage_in]]` +
`[[buffer(0)]]`/`[[texture(0)]]`. NDC is +Y-up on **all** backends (SDL
converts for Vulkan), so both sources share the same `-ndc.y` flip.
`[[buffer(0)]]`/`[[texture(0)]]`; HLSL uses vertex inputs `TEXCOORD<location>`,
vertex cbuffer `(b0, space1)`, pixel `t0`/`s0` in `space2`, pixel cbuffer
`(b0, space3)`, and `SampleLevel(…, 0)` (legal in divergent branches/loops).
NDC is +Y-up on **all** backends (SDL converts for Vulkan), so every source
shares the same `-ndc.y` flip. SDL tries Metal → D3D12 → Vulkan; if D3D12's
pipelines fail to build, `Gpu.init` retries without DXBC (Vulkan) before
falling back to software. Verify D3D12 on a Windows box with
`showcase.exe --gpu --screenshot` (cross-build: `make windows`).

Output parity: verified pixel-identical to the rasterizer (max 1 LSB/channel)
across every showcase section, dark mode, all theme families, sheets/alerts,
Expand Down Expand Up @@ -597,7 +609,7 @@ Powers `examples/edit` (a TextEdit/gedit-like editor); all headless-tested.
- Colors always go through `multiplyAlpha(ctx.opacity)` when painting.
- New shapes/effects: add a `DrawCommand` and handle it in **both** backends —
the software rasterizer (`render/raster.zig`) and the GPU path
(`render/gpu_scene.zig` + the shaders in `src/gpu/shaders/`, GLSL **and** MSL,
(`render/gpu_scene.zig` + the shaders in `src/gpu/shaders/`, GLSL, MSL **and** HLSL,
then re-run `compile.sh`) — keeping command semantics backend-neutral. Verify
with `showcase --screenshot --gpu` diffed against the software BMP.
- Run `zig build test` after every change; keep it green.
161 changes: 161 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
###############################################
#
# Makefile — zigui
#
# `make validate` is the pre-commit gate; `make build` is the entry point.
#
###############################################

# Optional include to extend the makefile targets
-include ../styleguide/Makefile.in

.DEFAULT_GOAL := all

.PHONY: build dist run demo edit test docs clean windows linux linux-arm64 macos-arm64 cross

# ---------------------------------------------
# Configuration
# ---------------------------------------------

# The released version, read from build.zig.zon (the source of truth).
VERSION := $(shell sed -n -E 's/^[[:space:]]*\.version[[:space:]]*=[[:space:]]*"([^"]+)".*/\1/p' build.zig.zon)

# ---------------------------------------------
# Primary workflows
# ---------------------------------------------

# Build, format, and test.
all: build format test
@echo "done"

# ---------------------------------------------
# Build
# ---------------------------------------------

# Build the library and examples.
build:
zig build

# Build optimized for release.
dist:
zig build --release=fast

# ---------------------------------------------
# Cross-platform builds
# ---------------------------------------------

# Release builds of both examples for each platform, into zig-out/<target>/bin.
# SDL3 is compiled from source per target, so no SDL install is needed for any
# of them. Linux binaries load X11/Wayland/GL/Vulkan at runtime.
CROSS = zig build showcase edit -Doptimize=ReleaseFast -Dtarget=$(1) --prefix zig-out/$(1) $(2)

# The macOS SDK (Xcode or Command Line Tools): an explicit macOS target is a
# cross build to Zig, so SDL needs the SDK's frameworks passed in.
MACOS_SDK = $(shell xcrun --show-sdk-path 2>/dev/null)

windows:
$(call CROSS,x86_64-windows-gnu)

linux:
$(call CROSS,x86_64-linux-gnu)

linux-arm64:
$(call CROSS,aarch64-linux-gnu)

macos-arm64:
@test -n "$(MACOS_SDK)" || { echo "❌ macOS SDK not found (needs macOS + xcode-select --install)"; exit 1; }
$(call CROSS,aarch64-macos,-Dmacos-sdk=$(MACOS_SDK))

# All four platforms.
cross: windows linux linux-arm64 macos-arm64

# Build and launch the showcase (the kitchen-sink gallery) in a window.
# SDL3 is fetched and compiled from source by Zig on first use — no Homebrew.
# ReleaseFast because the software rasterizer is ~10x slower in Debug.
demo:
zig build run-showcase -Doptimize=ReleaseFast -- $(ARGS)

# Build the edit example (the multi-line text editor) into zig-out/bin.
edit:
zig build edit

# Build and run the showcase in a window. Pass arguments with `make run ARGS="--dark"`.
run:
zig build run-showcase -- $(ARGS)

# ---------------------------------------------
# Test
# ---------------------------------------------

# Run the unit test suite.
test:
zig build test --summary all

# ---------------------------------------------
# Documentation
# ---------------------------------------------

# Build the API docs and serve them locally (Ctrl-C stops the server).
# The browser opens only once the server answers — opening it first raced the
# server's startup and showed "can't connect". A busy port fails loudly.
# Not 8080: other local tools (e.g. llama.cpp's web UI) leave a service worker
# registered on 127.0.0.1:8080 that keeps serving their app instead of these
# docs. A service worker is per origin, so a dedicated port sidesteps it.
DOCS_PORT ?= 8080
OPEN ?= open
docs:
zig build docs
@! curl -s -o /dev/null "http://127.0.0.1:$(DOCS_PORT)/" || { echo "❌ port $(DOCS_PORT) is already serving something (an old 'make docs'?) — stop it or use DOCS_PORT=8081"; exit 1; }
@echo "Serving zig-out/docs at http://127.0.0.1:$(DOCS_PORT)/ (Ctrl-C to stop)"
@python3 -m http.server -b 127.0.0.1 $(DOCS_PORT) -d zig-out/docs & pid=$$!; \
trap 'kill $$pid 2>/dev/null' INT TERM EXIT; \
until curl -fs -o /dev/null "http://127.0.0.1:$(DOCS_PORT)/"; do \
kill -0 $$pid 2>/dev/null || { echo "❌ docs server failed to start (port $(DOCS_PORT) in use? try DOCS_PORT=8081)"; exit 1; }; \
sleep 0.1; \
done; \
$(OPEN) "http://127.0.0.1:$(DOCS_PORT)/"; \
wait $$pid

# ---------------------------------------------
# Release
# ---------------------------------------------

# Tag the build.zig.zon version (e.g. 1.0.0) and push it.
tag:
@test -n "$(VERSION)" || { echo "❌ could not read .version from build.zig.zon"; exit 1; }
git tag -a "$(VERSION)" -m "$(VERSION)"
git push
git push --tags

# ---------------------------------------------
# Environment
# ---------------------------------------------

# Open the working copy in SourceTree.
st:
open -a SourceTree .

# Open the project in the editor.
open:
code .

# Open the repository on GitHub.
github:
open "https://github.com/ddalcu/zigui"

# Clone a fresh working copy.
clone:
git clone git@github.com:ddalcu/zigui.git

# Start Claude Code here.
claude:
claude

# ---------------------------------------------
# Housekeeping
# ---------------------------------------------

# Remove every build artifact.
clean:
rm -rf .zig-cache
rm -rf zig-out
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ zig build test --summary all
zig build docs # API docs into zig-out/docs
```

Run the demo apps (require SDL3 — `brew install sdl3` on macOS,
`apt install libsdl3-dev` on Linux):
Run the demo apps. SDL3 is a Zig package dependency (castholm/SDL), fetched and
compiled from source on first build and linked statically — nothing to install
but Zig. (`-Dsdl3-prefix=<dir>` links a prebuilt SDL3 instead.)

```sh
zig build run-showcase # the kitchen-sink gallery: every component,
Expand All @@ -165,6 +166,21 @@ zig build showcase edit # build the examples without running them
./zig-out/bin/showcase --screenshot out.bmp [section] [--dark] [--accent N] [--gpu]
```

The Makefile provides optional convenience targets that wrap `zig build` for each
supported target triple. `zig build` remains the primary build interface.

```sh
make demo # build (ReleaseFast) + launch the showcase
make windows | linux | linux-arm64 | macos-arm64 # cross-build both examples
# into zig-out/<target>/bin (`make cross` = all)
# Windows builds are GUI apps (no console);
# add -Dwindows-console=true to see logs
# App icon: assets/icon/zigz.svg; regenerate the
# .ico/.png with tools/icon/mkico.py
# Linux: run `showcase --install-desktop` once so
# GNOME/Wayland shows the icon (adds a .desktop entry)
```

> If the software fallback is in use, run with `-Doptimize=ReleaseFast` for
> smooth UI — the CPU rasterizer is much slower in the default Debug build.

Expand Down
4 changes: 4 additions & 0 deletions assets/icon/zigui.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// The app icon, embedded into the Windows examples by build.zig
// (addWin32ResourceFile). SDL also picks it up for the window class icon.
// Regenerate zigz.ico with tools/icon/mkico.py after editing zigz.svg.
1 ICON "zigz.ico"
Binary file added assets/icon/zigz.ico
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icon/zigz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions assets/icon/zigz.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading