DVUI

enumFieldWidget

Parameters

#
src:std.builtin.SourceLocation
std.builtin.SourceLocation
field_name:[]const u8
[]const u8
field_value_ptr:anytype
anytype
opt:StandardFieldOptions
StandardFieldOptions
alignment:*dvui.Alignment
*dvui.Alignment

Source

Implementation

#
pub fn enumFieldWidget(
    src: std.builtin.SourceLocation,
    field_name: []const u8,
    field_value_ptr: anytype,
    opt: StandardFieldOptions,
    alignment: *dvui.Alignment,
) void {
    validateFieldPtrType(null, &.{.@"enum"}, "enumFieldWidget", @TypeOf(field_value_ptr));
    if (opt.display == .none) return;

    const T = @TypeOf(field_value_ptr.*);
    const exhaustive = @typeInfo(T).@"enum".is_exhaustive;
    const read_only = @typeInfo(@TypeOf(field_value_ptr)).pointer.is_const or opt.display != .read_write;
    if (!read_only and !exhaustive) {
        // TODO: Display these as numbers and do the enum<->int conversion.
        log.debug("non-exhaustive enum {s}.{s} can only be displayed read-only", .{ @typeName(T), field_name });
    }

    var box = dvui.box(src, .{ .dir = .horizontal }, .{ .expand = .horizontal });
    defer box.deinit();

    dvui.label(@src(), "{s}", .{opt.label orelse field_name}, .{ .margin = .{ .y = 4 } });
    var hbox_aligned = dvui.box(@src(), .{ .dir = .horizontal }, .{ .margin = alignment.margin(box.data().id) });
    defer hbox_aligned.deinit();
    alignment.record(box.data().id, hbox_aligned.data());

    if (read_only and exhaustive) {
        dvui.label(@src(), "{s}", .{@tagName(field_value_ptr.*)}, .{ .margin = .{ .y = 4 } });
    } else if (!exhaustive) {
        dvui.label(@src(), "{d}", .{@intFromEnum(field_value_ptr.*)}, .{ .margin = .{ .y = 4 } });
    } else {
        const choices = std.meta.FieldEnum(T);
        const entries = std.meta.fieldNames(choices);
        var choice: usize = @intFromEnum(std.meta.stringToEnum(std.meta.FieldEnum(T), @tagName(field_value_ptr.*)).?);
        _ = dvui.dropdown(@src(), entries, .{ .choice = &choice }, .{}, .{});

        field_value_ptr.* = std.meta.stringToEnum(T, @tagName(@as(std.meta.FieldEnum(T), @enumFromInt(choice)))).?;
    }
}