dvui
david-vanderson/dvui
This repository has no description.
david-vanderson/dvui
This repository has no description.
A Zig GUI toolkit for whole applications or extra debugging windows in an existing application.
Tested with Zig 0.13
How to run the built-in examples:
zig build sdl-standalone
zig build sdl-ontop
zig build raylib-standalone
zig build raylib-ontop
zig build web-test
zig-out/bin/index.html
Online Docs This document is a broad overview. See implementation details for how to write and modify widgets.
Online discussion happens in #gui-dev on the zig discord server: https://discord.gg/426ZADhs
Below is a screenshot of the demo window, whose source code can be found at src/Examples.zig
.
DVUI Demo is a template project you can use as a starting point.
The build.zig and build.zig.zon files there show how to reference dvui as a zig dependency.
if (try dvui.button(@src(), "Ok", .{}, .{})) {
dialog.close();
}
Widgets are not stored between frames like in traditional gui toolkits (gtk, win32, cocoa). dvui.button()
processes input events, draws the button on the screen, and returns true if a button click happened this frame.
For an intro to immediate mode guis, see: https://github.com/ocornut/imgui/wiki#about-the-imgui-paradigm
// 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 = try dvui.box(src, .horizontal, opts);
defer hbox.deinit();
var red: f32 = @floatFromInt(color.r);
var green: f32 = @floatFromInt(color.g);
var blue: f32 = @floatFromInt(color.b);
_ = try dvui.sliderEntry(@src(), "R: {d:0.0}", .{ .value = &red, .min = 0, .max = 255, .interval = 1 }, .{ .gravity_y = 0.5 });
_ = try dvui.sliderEntry(@src(), "G: {d:0.0}", .{ .value = &green, .min = 0, .max = 255, .interval = 1 }, .{ .gravity_y = 0.5 });
_ = try 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);
}
DVUI processes every input event, making it useable in low framerate 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:
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:
dvui.floatingWindow()
callsdvui.addEvent...
functions return false if event won't be handled by dvui (main application should handle it)dvui.cursorRequested()
to dvui.cursorRequestedFloating()
which returns null if the mouse cursor should be set by the main applicationFloating 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 window.end()
.
If your app is running at a fixed framerate, use window.begin()
and window.end()
which handle bookkeeping and rendering.
If you want to only render frames when needed, add window.beginWait()
at the start and window.waitTime()
at the end. These cooperate to sleep the right amount and render frames when:
dvui.refresh(null, ...)
(if your code knows you need a frame after the current one)dvui.refresh(window, ...)
which in turn calls backend.refresh()
window.waitTime()
also accepts a max fps parameter which will ensure the framerate stays below the given value.
window.beginWait()
and 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 1ms.
The easiest way to use widgets is through the high-level functions that create and install them:
{
var box = try dvui.box(@src(), .vertical, .{.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.
Instead you can allocate the widget on the stack using the lower-level functions:
{
var box = BoxWidget.init(@src(), .vertical, false, .{.expand = .both});
// box now has an id, can look up animations/timers
try box.install();
// box is now parent widget
try 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.
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 vs. horizontal.
When a child widget is laid out (sized and positioned), it sends 2 pieces of info to the parent:
If parent is not expand
ed, the intent is to pack as tightly as possible, so it will give all children only their min size.
If parent has more space than the children need, it will lay them out using the hints:
Each widget has the following options that can be changed through the Options struct when creating the widget:
.color_text = .{ .color = .{ .r = 0xe0, .g = 0x1b, .b = 0x24 } }
.color_text = .{ .name = .err }
(get current theme's color_err
)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) {
win.theme = &dvui.Theme.AdwaitaDark;
}
else {
win.theme = &dvui.Theme.AdwaitaLight;
}
The theme's color_accent is also used to show keyboard focus.
See implementation details for more information.