1
0
Fork
You've already forked dvui
0
No description
  • Zig 97.5%
  • JavaScript 2.1%
  • Tree-sitter Query 0.1%
Mikael Säker 23eaa68410 render: decode the sanitized text in renderText's glyph loop
renderText sanitizes opts.text into utf8_text (toUtf8) precisely
because callers may pass invalid UTF-8 — e.g. a span sliced
mid-codepoint — but the glyph loop then decoded the RAW opts.text with
`catch unreachable`. On invalid input those unreachables are genuinely
reachable, which is UB in ReleaseFast: cplen could come out 0, the
loop stopped advancing, and the unchecked appendVertex
(appendAssumeCapacity) calls wrote past the vertex buffer. Observed in
the wild as a SIGSEGV one byte past a 256KB allocation, crashing in
renderText under editor span drawing.
Iterating utf8_text (guaranteed valid) makes the unreachables hold;
invalid input now renders replacement characters instead of corrupting
memory.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026年07月08日 10:51:49 +02:00
.github/workflows ci: fix web backend installing dependencies 2025年07月02日 03:21:56 +02:00
docs fix docs 2026年07月01日 10:42:10 -04:00
examples feat: devtools 2026年07月03日 12:38:50 -03:00
src render: decode the sanitized text in renderText's glyph loop 2026年07月08日 10:51:49 +02:00
tools replace deprecated Unmanaged aliases 2026年05月17日 20:42:54 -05:00
vendor don't need to implement our own memset/memcpy/memmove 2025年11月13日 21:56:25 -05:00
.gitignore ignore zig-pkg 2026年03月24日 18:49:20 +03:00
.gitmodules remove all git submodules 2023年04月16日 22:02:11 -04:00
build.zig build: shorten option names 2026年07月04日 14:25:54 +01:00
build.zig.zon update wio 2026年07月03日 11:14:45 +01:00
LICENSE license: add contributors to copyright line 2025年10月02日 15:20:13 -04:00
package.json remove postinstall script 2026年01月25日 14:13:27 -05:00
readme-accessibility.md update accessibility readme 2026年01月25日 21:34:30 +11:00
readme-implementation.md readme 2026年01月28日 19:01:50 -05:00
readme-raylib-zig-custom.md Rename raylib_zig_custom.md to readme-raylib-zig-custom.md 2026年02月21日 21:39:11 +01:00
README.md add wio-ontop example 2026年07月03日 12:03:08 +01:00
screenshot_demo.png readme: screenshot 2026年03月03日 21:16:38 -05:00
tsconfig.json Initial cleanup of web.js ( #706 ) 2026年02月07日 14:40:59 +00:00

DVUI — Immediate Zig GUI for Apps and Games

Zig GUI toolkit for whole applications or debugging windows in existing apps/games.

Tested with Zig v0.16.0 (for Zig v0.15.2, use DVUI branch zig15 or tag v0.4.0).

Homepage · Demo · Docs · Devlog

Screenshot of DVUI Standalone Example (Application Window)

Examples

zig build sdl3-app

  • sdl3 backend, dvui handles mainloop
  • good place to start, try changing frame() inside ./examples/app.zig
  • see Getting Started
Backend As app
dvui handles main loop
app.zig
Standalone
you control main loop
*-standalone.zig
On top
debug HUD on existing app/game
*-ontop.zig
SDL3 sdl3-app sdl3-standalone sdl3-ontop
SDL3GPU
Rendering via SDL GPU
todo sdl3gpu-standalone sdl3gpu-ontop
SDL2 sdl2-app sdl2-standalone sdl2-ontop
Raylib
C API
raylib-app raylib-standalone raylib-ontop
Raylib
Bindings raylib-zig
raylib-zig-app raylib-zig-standalone raylib-zig-ontop
DX11 dx11-app dx11-standalone dx11-ontop
GLFW glfw-app todo glfw-opengl-ontop
wio
OpenGL on wio
wio-app wio-standalone wio-ontop
Web web-app none none

dvui-demo is a template repository that also includes these examples. See Getting Started.

Docs

  • zig build docs -Dgenerate-images
  • Load ./zig-out/docs/index.html
  • Online Docs

Troubleshooting Raylib

  • If you encounter error No Wayland, then also add flag -Dlinux_display_backend=X11

Troubleshooting Web

  • To load examples for this backend, they must first be served through a (local) web server using:
    • Python python -m http.server -d ./zig-out/bin/web-app
    • Caddy caddy file-server --root ./zig-out/bin/web-app --listen :8000
    • Any other web server
  • Outputs are stored in ./zig-out/bin/web-app/

The following projects use DVUI:

Discuss yours on:

Feature Overview

Further reading:

Getting Started

dvui-demo is a template repository

  • build.zig and build.zig.zon reference dvui as a zig dependency
  • includes all the examples

Alternatively:

  1. Add DVUI as a dependency:
    zig fetch --save git+https://github.com/david-vanderson/dvui#main
    
  2. Add build.zig logic (here using SDL3 backend):
    constdvui_dep=b.dependency("dvui",.{.target=target,.optimize=optimize,.backend=.sdl3});exe.root_module.addImport("dvui",dvui_dep.module("dvui_sdl3"));

Further reading:

Frequently Asked Questions

How can I enable LSP autocompletion for DVUI? For ZLS autocomplete to work on DVUI's backend, you must import the latter directly:
  1. In `build.zig` (here using the SDL3 backend):
    exe.root_module.addImport("sdl-backend", dvui_dep.module("sdl3"));
  2. Then in your code:
    const SDLBackend = @import("sdl-backend");
How to debug DVUI? Use the debug window dvui.toggleDebugWindow(). Its preview is available as a Debug Window button on the front page of the online demo.
Where to receive updates on new DVUI features? Read the DVUI Devlog which also covers topics such as units in DVUI. Subscribing to its RSS feed is possible.

Built-in Widgets

Widgets implemented so far:

  • Text entry:
    • Single- and multi-line
    • Includes touch support (selection draggables and menu)
  • Number entry:
    • Supports all Integer and floating point types
  • Text layout:
    • Parts can be clickable
    • Parts separately styled
  • Floating window
  • Menu
  • Popup/context window
  • Scroll Area
  • Button
  • Multi-line label:
    • Can be clickable for links
  • Tooltips
  • Slider
  • Slider entry:
    • Combo slider and text entry
  • Checkbox
  • Radio buttons
  • Toast
  • Panes with draggable sash
  • Dropdown
  • Combo box
  • Reorderable lists:
    • Drag to reorder/remove/add
  • Data grid
  • Group box (fieldset)

Widgets to be implemented:

  • Docking

Design

Immediate Mode

Widgets are not stored between frames like in traditional GUI toolkits (GTK, Win32, Cocoa). In the example below, dvui.button() processes input events, draws the button on the screen, and returns true if a button click happened this frame:

if(dvui.button(@src(),"Ok",.{},.{})){dialog.close();}

For an intro to immediate-mode GUIs (IMGUIs), see this respective section from Dear ImGui.

Advantages

  • Reducing widget state
    • For example, a checkbox which directly uses your app's bool
  • Reducing GUI state
    • The widgets shown each frame directly reflect the code run each frame
    • Harder to be in a state where the GUI is showing one thing but the app thinks it's showing something else
    • Don't have to clean up widgets that aren't needed anymore
  • Functions are the composable building blocks of the GUI
    • Since running a widget is a function, you can wrap a widget easily:
      // Let's wrap the sliderEntry widget so we have 3 that represent a ColorpubfncolorSliders(src:std.builtin.SourceLocation,color:*dvui.Color,opts:Options)void{varhbox=dvui.box(src,.{.dir=.horizontal},opts);deferhbox.deinit();varred:f32=color.r;vargreen:f32=color.g;varblue:f32=color.b;_=dvui.sliderEntry(@src(),"R: {d:0.0}",.{.value=&red,.min=0,.max=255,.interval=1},.{.gravity_y=0.5});_=dvui.sliderEntry(@src(),"G: {d:0.0}",.{.value=&green,.min=0,.max=255,.interval=1},.{.gravity_y=0.5});_=dvui.sliderEntry(@src(),"B: {d:0.0}",.{.value=&blue,.min=0,.max=255,.interval=1},.{.gravity_y=0.5});color.r=@trunc(red);color.g=@trunc(green);color.b=@trunc(blue);}

Drawbacks

  • Hard to do fire-and-forget
    • For example, showing a dialog with an error message from code that won't be run next frame
    • DVUI includes a retained mode space for dialogs and toasts for this
  • Hard to do dialog sequence
    • Retained mode GUIs can run a modal dialog recursively so that dialog code can only exist in a single function
    • DVUI's retained dialogs can be chained together for this

Handling All Events

DVUI processes every input event, making it useable in low frame rate situations. A button can receive a mouse-down event and a mouse-up event in the same frame and correctly report a click. A custom button could even report multiple clicks per frame (the higher level dvui.button() function only reports 1 click per frame).

In the same frame, these can all happen:

  • Text entry field A receives text events
  • Text entry field A receives a tab that moves keyboard focus to field B
  • Text entry field B receives more text events

Because everything is in a single pass, this works in the normal case where widget A is run before widget B. It doesn't work in the opposite order (widget B receives a tab that moves focus to A) because A ran before it got focus.

Ontop-Floating-Windows

This library can be used in 2 ways:

  • As the GUI for the whole application, drawing over the entire OS window
  • As floating windows on top of an existing application with minimal changes:
    • Use widgets only inside dvui.floatingWindow() calls
    • The dvui.Window.addEvent... functions return false if event won't be handled by DVUI (the main application should handle it)
    • Change dvui.Window.cursorRequested() to dvui.Window.cursorRequestedFloating() which returns null if the mouse cursor should be set by the main application

Floating windows and popups are handled by deferring their rendering so that they render properly on top of windows below them. Rendering of all floating windows and popups happens during dvui.Window.end().

FPS-Throttling

If your app is running at a fixed framerate, use dvui.Window.begin() and dvui.Window.end() which handle bookkeeping and rendering.

If you want dvui to handle the mainloop for you, use dvui.App.

If you want to only render frames when needed, add dvui.Window.beginWait() at the start and dvui.Window.waitTime() at the end. These cooperate to sleep the right amount and render frames when:

  • An event comes in
  • An animation is ongoing
  • A timer has expired
  • GUI code calls dvui.refresh(null, ...) (if your code knows you need a frame after the current one)
  • A background thread calls dvui.refresh(window, ...) which in turn calls backend.refresh()

dvui.Window.waitTime() also accepts a maximum FPS parameter which will ensure the frame rate stays below the given value.

dvui.Window.beginWait() and dvui.Window.waitTime() maintain an internal estimate of how much time is spent outside of the rendering code. This is used in the calculation for how long to sleep for the next frame.

The estimate is visible in the demo window Animations > Clock > Estimate of frame overhead. The estimate is only updated on frames caused by a timer expiring (like the clock example), and it starts at 1 ms.

Widget init and deinit

The easiest way to use widgets is through the high-level functions that create them:

{varbox=dvui.box(@src(),.{},.{.expand=.both});deferbox.deinit();// Widgets run here will be children of box}

These functions allocate memory for the widget onto an internal arena allocator that is flushed each frame.

You can instead allocate the widget on the stack using the lower-level functions:

{varbox:BoxWidget=undefined;box.init(@src(),.{},.{.expand=.both});// Box is now parent widgetbox.drawBackground();// Might draw the background in a different waydeferbox.deinit();// Widgets run here will be children of box}

The lower-level functions give a lot more customization options including animations, intercepting events, and drawing differently.

Start with the high-level functions, and when needed, copy the body of the high-level function and customize from there.

Parent, Child, and Layout

The primary layout mechanism is nesting widgets. DVUI keeps track of the current parent widget. When a widget runs, it is a child of the current parent. A widget may then make itself the current parent, and reset back to the previous parent when it runs deinit().

The parent widget decides what rectangle of the screen to assign to each child, unless the child passes .rect = in their dvui.Options.

Usually you want each part of a GUI to either be packed tightly (take up only min size), or expand to take the available space. The choice might be different for vertical versus horizontal.

When a child widget is laid out (sized and positioned), it sends 2 pieces of information to the parent:

  • Minimum size
  • Hints for when space is larger than minimum size (expand, gravity_x, and gravity_y)

If parent is not expanded, the intent is to pack as tightly as possible, so it will give all children only their minimum size.

If parent has more space than the children need, it will lay them out using the hints:

  • expand — whether this child should take more space or not
  • gravity — if not expanded, where to position child in larger space

See readme-implementation for more information.

Appearance

Each widget has the following options that can be changed through the Options struct when creating the widget:

  • margin (space outside border)
  • border (on each side)
  • padding (space inside border)
  • min_size_content (margin/border/padding added to get min size)
  • max_size_content (margin/border/padding added to get maximum min size)
  • background (fills space inside border with background color)
  • corner_radius (for each corner)
  • box_shadow
  • style (use theme's colors)
  • colors (directly specify):
    • color_fill
    • color_fill_hover
    • color_fill_press
    • color_text
    • color_text_hover
    • color_text_press
    • color_border
  • font (directly specify):
    • Can reference theme fonts via Font.theme(.body) (or .heading, .title, .mono)
  • theme (use a separate theme altogether)
  • ninepatch_fill (also _hover and _press):
    • Draws an image over the background

Each widget has its own default options. These can be changed directly:

dvui.ButtonWidget.defaults.background=false;

Themes can be changed between frames or even within a frame. The theme controls the fonts and colors referenced by font_style and named colors:

if(theme_dark){dvui.themeSet(dvui.Theme.builtin.adwaita_dark);}else{dvui.themeSet(dvui.Theme.builtin.adwaita_light);}

The theme's focus color is used to show keyboard focus.

If no theme is passed to Window.init() the default theme will attempt to follow the system dark or light mode.

Accessibility

DVUI has varying support for different kinds of accessibility infrastructure. The current state, including areas commonly tied to accessibility, is:

  • Keyboard navigation:
    • Most widgets support keyboard navigation
  • Language support:
    • Text rendering is simple left-to-right single glyph for each unicode codepoint
    • Grapheme clusters currently unsupported
    • No right-to-left or mixed text direction
  • Language input:
    • IME (input method editor) works in SDL and web backends
  • High-contrast themes:
    • DVUI's themes can support this
    • No current OS integration
  • Screen reading and alternate input:
    • Uses Options.role and Options.label from AccessKit integration
    • AccessKit integration
    • Add -Daccesskit to zig build

Further reading: