DVUI

init

It's expected to call this when self is undefined

Parameters

#
self:*LabelWidget
*LabelWidget
src:std.builtin.SourceLocation
std.builtin.SourceLocation
fmt:[]const u8
[]const u8
args:anytype
anytype
init_opts:InitOptions
InitOptions
opts:Options
Options

Source

Implementation

#
pub fn init(self: *LabelWidget, src: std.builtin.SourceLocation, comptime fmt: []const u8, args: anytype, init_opts: InitOptions, opts: Options) void {
    comptime if (!std.unicode.utf8ValidateSlice(fmt)) @compileError("Format strings must be valid utf-8");

    const cw = dvui.currentWindow();
    // Validate utf8 formatting
    const str, const alloc = blk: {
        const str = std.fmt.allocPrint(cw.lifo(), fmt, args) catch |err| {
            const newid = dvui.parentGet().extendId(src, opts.idExtra());
            dvui.logError(@src(), err, "Label {x} could not print its content", .{newid});
            break :blk .{ fmt, null };
        };
        // We need to use `long_term_arena` because otherwise we
        // will not be able to free the memory of the allocPrint
        const utf8 = dvui.toUtf8(cw.arena(), str) catch |err| {
            const newid = dvui.parentGet().extendId(src, opts.idExtra());
            dvui.logError(@src(), err, "Label {x} could not allocate valid utf8 slice", .{newid});
            // We contained invalid utf8, so textSize will fail later
            break :blk .{ str, cw.lifo() };
        };
        if (str.ptr == utf8.ptr) break :blk .{ str, cw.lifo() };
        cw.lifo().free(str);
        dvui.log.debug("LabelWidget format output was invalid utf8 for {s} with '{any}'.", .{ fmt, args });
        break :blk .{ utf8, null };
    };
    return self.initNoFmtAllocator(src, str, alloc, init_opts, opts);
}