init
Parameters
- options:InitOptions
- InitOptions
Source
Implementation
pub fn init(options: InitOptions) !Self {
dvui.io = options.io;
// init SDL backend (creates and owns OS window)
const backend = try options.allocator.create(Backend);
errdefer options.allocator.destroy(backend);
backend.* = switch (Backend.kind) {
.sdl2, .sdl3 => try Backend.initWindow(.{
.io = options.io,
.size = options.window_size,
.vsync = false,
.title = "",
.hidden = true,
}),
.testing => Backend.init(.{
.allocator = options.allocator,
.size = .cast(options.window_size),
.size_pixels = options.window_size.scale(2, dvui.Size.Physical),
}),
inline else => |kind| {
std.debug.print("dvui.testing does not support the {s} backend\n", .{@tagName(kind)});
return error.SkipZigTest;
},
};
if (should_write_snapshots()) {
// ensure snapshot directory exists
// NOTE: do fs operation through cwd to handle relative and absolute paths
std.Io.Dir.cwd().createDir(dvui.io, options.snapshot_dir, .default_dir) catch |err| switch (err) {
error.PathAlreadyExists => {},
else => return err,
};
}
var window_init_opts = options.window_init_opts;
// Define a defaults so that tests aren't platform dependent.
// These would otherwise check for OS tag or OS configuration.
if (window_init_opts.color_scheme == null) {
window_init_opts.color_scheme = .light;
}
if (window_init_opts.keybinds == null) {
window_init_opts.keybinds = .windows;
}
const window = try options.allocator.create(Window);
window.* = try dvui.Window.init(@src(), options.allocator, backend.backend(), window_init_opts);
window.begin(0) catch unreachable;
return .{
.allocator = options.allocator,
.backend = backend,
.window = window,
.image_dir = options.image_dir orelse @import("build_options").image_dir,
.snapshot_dir = options.snapshot_dir,
};
}