DVUI

displayStruct

Display a struct and allow the user to view and/or edit the fields.

If the struct is being displayed, returns a pointer to a BoxWidget (which must be deinit()-ed), otherwise returns null. field_name: The name of the field holding the struct. field_value_ptr: A pointer to the struct depth: How many nested levels of structs to display. A depth of 0 will only display this struct's fields. options: A tuple of StructOptions(T) of .{} to use default options. al: If adding your own, pass in an alignment to be shared between the struct display and your own widgets, otherwise pass null.

The returned BoxWidget be used to add custom display fields or additional widgets to the struct's display.

NOTE: Any modified text fields are dynamically allocated. These are cleaned up during Window.deinit() If a string should not be automatically cleaned up (i.e will be cleaned up by a struct's deinit() method), remove the string from struct_ui.string map prior to the Window.deinit() being called.

The displayStringBuf() function can be used as an alternative to display strings with a user-supplied buffer.

Parameters

#
src:std.builtin.SourceLocation
std.builtin.SourceLocation
field_name:?[]const u8
?[]const u8
field_value_ptr:anytype
anytype
depth:usize
usize
field_option:FieldOptions
FieldOptions
options:anytype
anytype
al:?*dvui.Alignment
?*dvui.Alignment

Source

Implementation

#
pub fn displayStruct(
    src: std.builtin.SourceLocation,
    field_name: ?[]const u8,
    field_value_ptr: anytype,
    comptime depth: usize,
    field_option: FieldOptions,
    options: anytype,
    al: ?*dvui.Alignment,
) ?*dvui.BoxWidget {
    validateFieldPtrType(field_name, &.{.@"struct"}, "displayStruct", @TypeOf(field_value_ptr));
    if (!validFieldOptionsType(field_name orelse "null", field_option, .standard)) return null;
    {
        const typeinfo = @typeInfo(@TypeOf(options));
        if (typeinfo != .@"struct" or !typeinfo.@"struct".is_tuple) {
            @compileError("The struct_ui.displayStruct() options parameter must be passed as a tuple of StructOptions");
        }
    }
    if (field_option.displayMode() == .none) return null;

    const StructT = @TypeOf(field_value_ptr.*);
    const struct_options: StructOptions(StructT) = findMatchingStructOption(StructT, field_name orelse "", options) orelse .initWithDefaults(.{}, null);

    const vbox: ?*dvui.BoxWidget = displayContainer(src, if (field_name) |name| field_option.displayLabel(name) else null, field_option.defaultExpanded());
    if (vbox != null) {
        var struct_alignment: dvui.Alignment = .init(@src(), depth);
        defer struct_alignment.deinit();
        const alignment = al orelse &struct_alignment;

        inline for (0..struct_options.field_options.values.len) |field_num| {
            const field = comptime @TypeOf(struct_options.field_options).Indexer.keyForIndex(field_num);
            if (struct_options.field_options.get(field)) |child_option_| {
                var child_option = child_option_;
                if (field_option.displayMode() == .constant and child_option.displayMode() != .none) {
                    child_option.markConst();
                }
                var box = dvui.box(@src(), .{ .dir = .vertical }, .{ .id_extra = field_num });
                defer box.deinit();
                displayField(
                    @src(),
                    StructT,
                    @tagName(field),
                    &@field(field_value_ptr, @tagName(field)),
                    depth,
                    child_option,
                    options,
                    alignment,
                );
            }
        }
    }
    return vbox;
}