Zig GUI toolkit for whole applications or debugging windows in existing apps/games.
Tested with Zig v0.15.2 (for Zig v0.14.1, use DVUI tag v0.3.0).
Homepage · Demo · Docs · Devlog
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 |
| Web | web-app |
none | none |
dvui-demo is a template repository that also includes these examples. See Getting Started.
zig build docs -Dgenerate-images- Load
./zig-out/docs/index.html - Online Docs
- If you encounter error
No Wayland, then also add flag-Dlinux_display_backend=X11
- 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
- Python
- Outputs are stored in
./zig-out/bin/web-app/
The following projects use DVUI:
- Graphl Visual Programming Language Demo
- Podcast Player
- Graphical Janet REPL
- FIDO2/ Passkey compatible authenticator implementation for Linux
- QEMU frontend
- Static site generator GUI
- File explorer for Altair 8800 disk images
- Kanji flashcard app
- Azem - WIP micro-mouse simulator / maze solver - Demo
- Pixi - Pixel art editor
Discuss yours on:
- Zig Discord
#gui-dev - Zig Libera IRC
#dvui - DVUI GitHub Discussions
- Immediate-mode API:
- see Design
- Processs every input event
- suitable for low frame rate situations
- Appropriate for
- whole UI (
*-appexamples) - debugging on top of existing application
- see Ontop-Floating-Windows
- whole UI (
- Backends
- TinyVG icons
zig-lib-svg2tvg- more icons at
zig-lib-icons
- Raster images
- Fonts
- Touch
- selection draggables in text entries
- pinch-zoom scaling
- Accessibility:
- AccessKit, enabled by adding flag
-Daccesskittozig build - see Accessibility
- AccessKit, enabled by adding flag
- Native file dialogs
- Animations
- Themes
- FPS throttling
- see FPS-Throttling
Further reading:
- Implementation details for how to write and modify container widgets:
dvui-demo is a template repository
build.zigandbuild.zig.zonreference dvui as a zig dependency- includes all the examples
Alternatively:
- Add DVUI as a dependency:
zig fetch --save git+https://github.com/david-vanderson/dvui#main - Add
build.ziglogic (here using SDL3 backend):const dvui_dep = b.dependency("dvui", .{ .target = target, .optimize = optimize, .backend = .sdl3 }); exe.root_module.addImport("dvui", dvui_dep.module("dvui_sdl3"));
Further reading:
- Using a version of
raylib-zigthat's not bundled with DVUI:
How can I enable LSP autocompletion for DVUI?
For ZLS autocomplete to work on DVUI's backend, you must import the latter directly:-
In `build.zig` (here using the SDL3 backend):
exe.root_module.addImport("sdl-backend", dvui_dep.module("sdl3")); -
Then in your code:
const SDLBackend = @import("sdl-backend");
How to debug DVUI?
Use the debug windowdvui.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.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
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.
- 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 Color pub fn colorSliders(src: std.builtin.SourceLocation, color: *dvui.Color, opts: Options) void { var hbox = dvui.box(src, .{ .dir = .horizontal }, opts); defer hbox.deinit(); var red: f32 = @floatFromInt(color.r); var green: f32 = @floatFromInt(color.g); var blue: f32 = @floatFromInt(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 = @intFromFloat(red); color.g = @intFromFloat(green); color.b = @intFromFloat(blue); }
- Since running a widget is a function, you can wrap a widget easily:
- 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
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.
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 returnfalseif event won't be handled by DVUI (the main application should handle it) - Change
dvui.Window.cursorRequested()todvui.Window.cursorRequestedFloating()which returnsnullif the mouse cursor should be set by the main application
- Use widgets only inside
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().
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 callsbackend.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.
The easiest way to use widgets is through the high-level functions that create them:
{
var box = dvui.box(@src(), .{}, .{.expand = .both});
defer box.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:
{
var box: BoxWidget = undefined;
box.init(@src(), .{}, .{.expand = .both});
// Box is now parent widget
box.drawBackground();
// Might draw the background in a different way
defer box.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.
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, andgravity_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 notgravity— if notexpanded, where to position child in larger space
See readme-implementation for more information.
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_shadowstyle(use theme's colors)colors(directly specify):color_fillcolor_fill_hovercolor_fill_presscolor_textcolor_text_hovercolor_text_presscolor_border
font(directly specify):- Can reference theme fonts via
Font.theme(.body)(or.heading,.title,.mono)
- Can reference theme fonts via
theme(use a separate theme altogether)ninepatch_fill(also_hoverand_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.
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.roleandOptions.labelfrom AccessKit integration - AccessKit integration
- Add
-Daccesskittozig build
- Uses
Further reading:
- Track accessibility progress:
