From 6b67ac6bb2fd636d841fc2dfd033a5a1b97669fe Mon Sep 17 00:00:00 2001 From: Paul Hatchman Date: Mon, 9 Mar 2026 18:44:56 +1100 Subject: [PATCH] add per-field struct options --- src/Examples/widgetpedia.zig | 17 ++++++++++----- src/struct_ui.zig | 41 ++++++++++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/Examples/widgetpedia.zig b/src/Examples/widgetpedia.zig index b93bb8f4..0ccd68de 100644 --- a/src/Examples/widgetpedia.zig +++ b/src/Examples/widgetpedia.zig @@ -2053,21 +2053,27 @@ const DisplayScrollArea = struct { }, .{ .scroll_info = &scroll_info, .frame_viewport = .{ .x = 100, .y = 100 }, .user_scroll = &user_scroll }); const si_opts: StructOptions(dvui.ScrollInfo) = .initWithDefaults(.{ .viewport = .{ .number = .{ .customDisplayFn = displayViewport } }, - .velocity = .{ .number = .{ .customDisplayFn = displayVelocity } }, }, null); - // TODO: This is another example of need for struct_ui enhancement. There are multiple points and we - // need to override one of them (velocity). Ideally we'd just like to override velocity with it's own options + // Only override the Point type display for fields named "velocity" + const velocity_opts = StructOptions(dvui.Point).init(.{ + .x = .{ .number = .{ .widget_type = .number_placeholder } }, + .y = .{ .number = .{ .widget_type = .number_placeholder } }, + }, null).forFieldName("velocity"); + + // TODO: Need struct_ui enhancement to mark struct fields as read-only + // and propogate that to the struct's fields. + // But ew also need a soft "const" that recursively propogates read-only to all children. const point_opts: StructOptions(dvui.Point) = .init(.{ .x = .defaultReadOnly, .y = .defaultReadOnly, }, null); - // TODO: Same with size + // TODO: Same issue with size const size_opts: StructOptions(dvui.Size) = .init(.{ .w = .defaultReadOnly, .h = .defaultReadOnly, }, null); - dvui.structUI(@src(), "init_opts", &init_opts, 2, .{ display_opts, si_opts, point_opts, size_opts }, .{}); + dvui.structUI(@src(), "init_opts", &init_opts, 2, .{ display_opts, velocity_opts, si_opts, point_opts, size_opts }, .{}); } fn displayViewport(field_name: []const u8, ptr: *anyopaque, _: bool, _: *dvui.Alignment) void { @@ -2968,4 +2974,5 @@ const lorem: []const []const u8 = &.{ const struct_ui = dvui.struct_ui; const StructOptions = struct_ui.StructOptions; +const StructOptionsForField = struct_ui.StructOptionsForField; const Rect = dvui.Rect; diff --git a/src/struct_ui.zig b/src/struct_ui.zig index 049396b4..90543ae5 100644 --- a/src/struct_ui.zig +++ b/src/struct_ui.zig @@ -168,16 +168,20 @@ pub fn StructOptions(Struct: type) type { else => @compileError(std.fmt.comptimePrint("StructOptions(T) requires Struct or Union, but received a {s}.", .{@typeName(Struct)})), } return struct { - pub const StructOptionsT = std.EnumMap(std.meta.FieldEnum(StructT), FieldOptions); const Self = @This(); + + pub const StructOptionsT = std.EnumMap(std.meta.FieldEnum(StructT), FieldOptions); // Type of struct or union these options belong to pub const StructT = Struct; + // display options for each field to be displayed field_options: StructOptionsT, // A default value to be used whenever an instance of this type is created default_value: ?StructT = null, // Display the struct using this function, instead of the default struct_ui function. customDisplayFn: ?*const fn ([]const u8, *anyopaque, bool, *dvui.Alignment) void = null, + // If set, this struct_option will only apply to fields with this name + for_field_name: ?[]const u8 = null, /// Initialize and display only the fields provided. /// options: field options for all the fields to be displayed. @@ -243,6 +247,7 @@ pub fn StructOptions(Struct: type) type { }; } + /// Use a custom display function to display this struct. pub fn initWithDisplayFn( // Display the struct using this function, instead of the default struct_ui function. customDisplayFn: *const fn (field_name: []const u8, field_value_ptr: *anyopaque, read_only: bool, *dvui.Alignment) void, @@ -255,6 +260,23 @@ pub fn StructOptions(Struct: type) type { }; } + /// Helper for setting `for_field_name` after construction. + /// If `for_field_name` is set, these options will only apply to fields + /// field with that field name. + /// + /// Useful for common struct such as dvui.Point where you want to display + /// different fields of the same type using different widgets. + /// + /// NOTE: Ordering is important. If there are multiple options for the same struct type + /// order the field_name variants before the generic struct options. + pub fn forFieldName(self: Self, field_name: []const u8) Self { + // This should be rarely used, so this is fine. But if we add more of these + // options, move to using an init_opts struct, rather than this builder pattern. + var result = self; + result.for_field_name = field_name; + return result; + } + /// Return a default value for a field if no default for that field has been supplied through /// StructOptions. pub fn defaultFieldOption(FieldType: type) FieldOptions { @@ -1014,7 +1036,7 @@ pub inline fn displayField( switch (@typeInfo(@typeInfo(PtrT).pointer.child)) { .@"struct", .@"union" => { - const struct_options = findMatchingStructOption(@TypeOf(field_value_ptr.*), options); + const struct_options = findMatchingStructOption(@TypeOf(field_value_ptr.*), field_name, options); if (struct_options) |so| { if (so.customDisplayFn) |displayFn| { const read_only = @typeInfo(@TypeOf(field_value_ptr)).pointer.is_const or field_option.displayMode() == .read_only; @@ -1233,7 +1255,7 @@ pub fn displayUnion( inline else => |*active, active_tag| { var inner_vbox = dvui.box(@src(), .{ .dir = .vertical }, .{ .expand = .horizontal, .id_extra = @intFromEnum(active_tag) }); defer inner_vbox.deinit(); - const struct_options: StructOptions(UnionT) = findMatchingStructOption(UnionT, options) orelse .initWithDefaults(.{}, null); + const struct_options: StructOptions(UnionT) = findMatchingStructOption(UnionT, field_name, options) orelse .initWithDefaults(.{}, null); var alignment: dvui.Alignment = .init(@src(), depth); defer alignment.deinit(); @@ -1401,7 +1423,7 @@ pub fn displayStruct( // Potentially introduce a "const" options that propgates read-only to all children. const StructT = @TypeOf(field_value_ptr.*); - const struct_options: StructOptions(StructT) = findMatchingStructOption(StructT, options) orelse .initWithDefaults(.{}, null); + const struct_options: StructOptions(StructT) = findMatchingStructOption(StructT, field_name orelse "", options) orelse .initWithDefaults(.{}, null); if (field_option.displayMode() == .none) return null; const vbox: ?*dvui.BoxWidget = displayContainer(src, if (field_name) |name| field_option.displayLabel(name) else null); @@ -1610,10 +1632,17 @@ pub fn validateFieldPtrTypeString(field_name: ?[]const u8, comptime caller: []co } /// Returns the option from the passed in options tuple for type T. -pub fn findMatchingStructOption(T: type, struct_options: anytype) ?StructOptions(T) { +pub fn findMatchingStructOption(T: type, field_name: []const u8, struct_options: anytype) ?StructOptions(T) { inline for (struct_options) |struct_option| { if (@TypeOf(struct_option).StructT == T) { - return struct_option; + // Check if these options are for a specific field. + if (struct_option.for_field_name) |for_name| { + if (std.mem.eql(u8, field_name, for_name)) { + return struct_option; + } + } else { + return struct_option; + } } } return null; -- knotmirror 0.0.1