From 2c9266707ed652f3cc024a32e11ccb3306b3162b Mon Sep 17 00:00:00 2001 From: David Vanderson Date: Sun, 8 Jun 2025 21:30:23 -0400 Subject: [PATCH 1/3] labels: new labelEx and init_opts This leaves `label` alone adding a new `labelEx` that takes the extra parameter. init_opts is added to `labelNoFmt` and LabelWidget. --- examples/raylib-ontop.zig | 2 +- examples/raylib-standalone.zig | 2 +- examples/sdl-standalone.zig | 2 +- src/Examples.zig | 42 +++++++++++--------------- src/Window.zig | 4 +-- src/dvui.zig | 51 +++++++++++++++++++++++--------- src/structEntry.zig | 2 +- src/widgets/DropdownWidget.zig | 4 +-- src/widgets/LabelWidget.zig | 28 +++++++++++++----- src/widgets/SuggestionWidget.zig | 2 +- src/widgets/TabsWidget.zig | 2 +- 11 files changed, 86 insertions(+), 55 deletions(-) diff --git a/examples/raylib-ontop.zig b/examples/raylib-ontop.zig index 368280e5..401037e4 100644 --- a/examples/raylib-ontop.zig +++ b/examples/raylib-ontop.zig @@ -110,7 +110,7 @@ fn colorPicker(result: *dvui.Color) !void { var hbox = try dvui.box(@src(), .horizontal, .{}); defer hbox.deinit(); - try dvui.labelNoFmt(@src(), &color_hex, .{ + try dvui.labelNoFmt(@src(), &color_hex, .{}, .{ .color_text = .{ .color = result.* }, .gravity_y = 0.5, }); diff --git a/examples/raylib-standalone.zig b/examples/raylib-standalone.zig index 5051d88d..acb44b7c 100644 --- a/examples/raylib-standalone.zig +++ b/examples/raylib-standalone.zig @@ -161,7 +161,7 @@ fn dvui_frame() !void { } } - try dvui.labelNoFmt(@src(), "Below is drawn directly by the backend, not going through DVUI.", .{ .margin = .{ .x = 4 } }); + try dvui.labelNoFmt(@src(), "Below is drawn directly by the backend, not going through DVUI.", .{}, .{ .margin = .{ .x = 4 } }); var box = try dvui.box(@src(), .vertical, .{ .expand = .horizontal, .min_size_content = .{ .h = 40 }, .background = true, .margin = .{ .x = 8, .w = 8 } }); defer box.deinit(); diff --git a/examples/sdl-standalone.zig b/examples/sdl-standalone.zig index d7803c88..d26076e0 100644 --- a/examples/sdl-standalone.zig +++ b/examples/sdl-standalone.zig @@ -179,7 +179,7 @@ fn gui_frame() !void { } } - try dvui.labelNoFmt(@src(), "Below is drawn directly by the backend, not going through DVUI.", .{ .margin = .{ .x = 4 } }); + try dvui.labelNoFmt(@src(), "Below is drawn directly by the backend, not going through DVUI.", .{}, .{ .margin = .{ .x = 4 } }); var box = try dvui.box(@src(), .horizontal, .{ .expand = .horizontal, .min_size_content = .{ .h = 40 }, .background = true, .margin = .{ .x = 8, .w = 8 } }); defer box.deinit(); diff --git a/src/Examples.zig b/src/Examples.zig index 0841af10..499e952b 100644 --- a/src/Examples.zig +++ b/src/Examples.zig @@ -628,7 +628,7 @@ pub fn themeSerialization() !void { var serialize_box = try dvui.box(@src(), .vertical, .{ .expand = .horizontal, .margin = .{ .x = 10 } }); defer serialize_box.deinit(); - try dvui.labelNoFmt(@src(), "TODO: demonstrate loading a quicktheme here", .{}); + try dvui.labelNoFmt(@src(), "TODO: demonstrate loading a quicktheme here", .{}, .{}); } /// ![image](Examples-basic_widgets.png) @@ -639,13 +639,7 @@ pub fn basicWidgets() !void { try dvui.label(@src(), "Label", .{}, .{ .gravity_y = 0.5 }); - { - // need to wrap the this, otherwise .gravity_x = 0.5 will make it - // not packed in the containing box - var vbox = try dvui.box(@src(), .vertical, .{ .gravity_y = 0.5 }); - defer vbox.deinit(); - try dvui.label(@src(), "Multi-line\nLabel", .{}, .{ .gravity_x = 0.5 }); - } + try dvui.labelEx(@src(), "Multi-line\nLabel", .{}, .{ .align_x = 0.5 }, .{ .gravity_y = 0.5 }); _ = try dvui.button(@src(), "Button", .{}, .{ .gravity_y = 0.5 }); _ = try dvui.button(@src(), "Multi-line\nButton", .{}, .{}); @@ -680,7 +674,7 @@ pub fn basicWidgets() !void { opts, ); _ = try dvui.spacer(@src(), .{ .w = 4 }, .{}); - try dvui.labelNoFmt(@src(), "Icon+Gray", opts); + try dvui.labelNoFmt(@src(), "Icon+Gray", .{}, opts); if (bw.clicked()) { try dvui.toast(@src(), .{ .message = "This button is grayed out\nbut still clickable." }); @@ -905,7 +899,7 @@ pub fn dropdownAdvanced() !void { .{}, opts.override(.{ .gravity_y = 0.5 }), ); - try dvui.labelNoFmt(@src(), "icon with text", opts.override(.{ .padding = .{ .x = 6 } })); + try dvui.labelNoFmt(@src(), "icon with text", .{}, opts.override(.{ .padding = .{ .x = 6 } })); if (mi.activeRect()) |_| { dd.close(); @@ -927,7 +921,7 @@ pub fn dropdownAdvanced() !void { var opts: Options = if (mi.show_active) dvui.themeGet().style_accent else .{}; _ = try dvui.image(@src(), .{ .name = "zig favicon", .bytes = zig_favicon }, opts.override(.{ .gravity_x = 0.5 })); - try dvui.labelNoFmt(@src(), "image above text", opts.override(.{ .gravity_x = 0.5, .padding = .{} })); + try dvui.labelNoFmt(@src(), "image above text", .{}, opts.override(.{ .gravity_x = 0.5, .padding = .{} })); if (mi.activeRect()) |_| { dd.close(); @@ -2353,7 +2347,7 @@ pub fn menus() !void { } } - try dvui.labelNoFmt(@src(), "Right click for a context menu", .{}); + try dvui.labelNoFmt(@src(), "Right click for a context menu", .{}, .{}); _ = try dvui.spacer(@src(), .{ .h = 20 }, .{}); @@ -2461,7 +2455,7 @@ pub fn menus() !void { label_opts.color_text = .text_press; } - try dvui.labelNoFmt(@src(), tabname, label_opts); + try dvui.labelNoFmt(@src(), tabname, .{}, label_opts); if (tab.clicked()) { Data.tab = i; @@ -2680,7 +2674,7 @@ pub fn scrolling(comptime data: u8) !void { .Invalid => "Invalid", .Valid, .Empty => " ", }; - try dvui.labelNoFmt(@src(), label, .{}); + try dvui.labelNoFmt(@src(), label, .{}, .{}); if (result.value == .Valid and result.enter_pressed) { scroll_to_msg = result.value.Valid; } @@ -3344,7 +3338,7 @@ pub fn animations() !void { try button_wiggle.install(); button_wiggle.processEvents(); try button_wiggle.drawBackground(); - try dvui.labelNoFmt(@src(), "Wiggle", button_wiggle.data().options.strip().override(.{ .gravity_x = 0.5, .gravity_y = 0.5 })); + try dvui.labelNoFmt(@src(), "Wiggle", .{}, button_wiggle.data().options.strip().override(.{ .gravity_x = 0.5, .gravity_y = 0.5 })); try button_wiggle.drawFocus(); if (button_wiggle.clicked()) { @@ -3385,11 +3379,11 @@ pub fn animations() !void { var hbox = try dvui.box(@src(), .horizontal, .{}); defer hbox.deinit(); - try dvui.labelNoFmt(@src(), "Animate", .{ .gravity_y = 0.5 }); + try dvui.labelNoFmt(@src(), "Animate", .{}, .{ .gravity_y = 0.5 }); _ = try dvui.dropdown(@src(), &.{ "alpha", "horizontal", "vertical" }, &global.animation_choice, .{}); - try dvui.labelNoFmt(@src(), "easing", .{ .gravity_y = 0.5 }); + try dvui.labelNoFmt(@src(), "easing", .{}, .{ .gravity_y = 0.5 }); var recalc = false; if (dvui.firstFrame(hbox.data().id)) { @@ -3464,17 +3458,17 @@ pub fn animations() !void { } if (try dvui.expander(@src(), "Spinner", .{}, .{ .expand = .horizontal })) { - try dvui.labelNoFmt(@src(), "Spinner maxes out frame rate", .{}); + try dvui.labelNoFmt(@src(), "Spinner maxes out frame rate", .{}, .{}); try dvui.spinner(@src(), .{ .color_text = .{ .color = .{ .r = 100, .g = 200, .b = 100 } } }); } if (try dvui.expander(@src(), "Clock", .{}, .{ .expand = .horizontal })) { - try dvui.labelNoFmt(@src(), "Schedules a frame at the beginning of each second", .{}); + try dvui.labelNoFmt(@src(), "Schedules a frame at the beginning of each second", .{}, .{}); const millis = @divFloor(dvui.frameTimeNS(), 1_000_000); const left = @as(i32, @intCast(@rem(millis, 1000))); - var mslabel = dvui.LabelWidget.init(@src(), "{d:0>3} ms into second", .{@as(u32, @intCast(left))}, .{}); + var mslabel = dvui.LabelWidget.init(@src(), "{d:0>3} ms into second", .{@as(u32, @intCast(left))}, .{}, .{}); try mslabel.install(); mslabel.processEvents(); try mslabel.draw(); @@ -3585,7 +3579,7 @@ pub fn debuggingErrors() !void { if (try dvui.expander(@src(), "Invalid utf-8 text", .{}, .{ .expand = .horizontal })) { var b = try dvui.box(@src(), .vertical, .{ .expand = .horizontal, .margin = .{ .x = 10 } }); defer b.deinit(); - try dvui.labelNoFmt(@src(), "this \xFFtext\xFF includes some \xFF invalid utf-8\xFF\xFF\xFF which is replaced with \xFF", .{}); + try dvui.labelNoFmt(@src(), "this \xFFtext\xFF includes some \xFF invalid utf-8\xFF\xFF\xFF which is replaced with \xFF", .{}, .{}); } if (try dvui.expander(@src(), "Scroll child after expanded child (will log error)", .{}, .{ .expand = .horizontal })) { @@ -3792,7 +3786,7 @@ pub fn icon_browser(src: std.builtin.SourceLocation, show_flag: *bool, comptime const toast_text = try std.fmt.bufPrint(&buf2, "Copied \"{s}\"", .{text}); try dvui.toast(@src(), .{ .message = toast_text }); } - try dvui.labelNoFmt(@src(), text, .{ .gravity_y = 0.5 }); + try dvui.labelNoFmt(@src(), text, .{}, .{ .gravity_y = 0.5 }); const iconboxId = iconbox.data().id; @@ -4026,7 +4020,7 @@ fn gridStyling() !void { { var hbox = try dvui.box(@src(), .horizontal, .{ .expand = .horizontal }); defer hbox.deinit(); - try dvui.labelNoFmt(@src(), "Margin:", .{ .min_size_content = .{ .w = 60 }, .gravity_y = 0.5 }); + try dvui.labelNoFmt(@src(), "Margin:", .{}, .{ .min_size_content = .{ .w = 60 }, .gravity_y = 0.5 }); const result = try dvui.textEntryNumber(@src(), f32, .{ .min = 0, .max = 10, .value = &local.margin, .show_min_max = true }, .{}); if (result.changed and result.value == .Valid) { local.margin = result.value.Valid; @@ -4036,7 +4030,7 @@ fn gridStyling() !void { { var hbox = try dvui.box(@src(), .horizontal, .{ .expand = .horizontal }); defer hbox.deinit(); - try dvui.labelNoFmt(@src(), "Padding:", .{ .min_size_content = .{ .w = 60 }, .gravity_y = 0.5 }); + try dvui.labelNoFmt(@src(), "Padding:", .{}, .{ .min_size_content = .{ .w = 60 }, .gravity_y = 0.5 }); const result = try dvui.textEntryNumber(@src(), f32, .{ .min = 0, .max = 10, .value = &local.padding, .show_min_max = true }, .{}); if (result.changed and result.value == .Valid) { local.padding = result.value.Valid; diff --git a/src/Window.zig b/src/Window.zig index cdadb01f..49bbb8e5 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -1493,7 +1493,7 @@ fn debugWindowShow(self: *Self) !void { var hbox = try dvui.box(@src(), .horizontal, .{}); defer hbox.deinit(); - try dvui.labelNoFmt(@src(), "Hex id of widget to highlight:", .{ .gravity_y = 0.5 }); + try dvui.labelNoFmt(@src(), "Hex id of widget to highlight:", .{}, .{ .gravity_y = 0.5 }); var buf = [_]u8{0} ** 20; if (self.debug_widget_id != .zero) { @@ -1578,7 +1578,7 @@ fn debugWindowShow(self: *Self) !void { self.debug_widget_id = @enumFromInt(std.fmt.parseInt(u64, std.mem.sliceTo(line, ' '), 16) catch 0); } - try dvui.labelNoFmt(@src(), line, .{ .gravity_y = 0.5 }); + try dvui.labelNoFmt(@src(), line, .{}, .{ .gravity_y = 0.5 }); } } } diff --git a/src/dvui.zig b/src/dvui.zig index ff6e3b50..7e93e006 100644 --- a/src/dvui.zig +++ b/src/dvui.zig @@ -3359,7 +3359,7 @@ pub fn floatingWindow(src: std.builtin.SourceLocation, floating_opts: FloatingWi pub fn windowHeader(str: []const u8, right_str: []const u8, openflag: ?*bool) !Rect.Physical { var over = try dvui.overlay(@src(), .{ .expand = .horizontal, .name = "WindowHeader" }); - try dvui.labelNoFmt(@src(), str, .{ .gravity_x = 0.5, .gravity_y = 0.5, .expand = .horizontal, .font_style = .heading, .padding = .{ .x = 6, .y = 6, .w = 6, .h = 4 } }); + try dvui.labelNoFmt(@src(), str, .{}, .{ .gravity_x = 0.5, .gravity_y = 0.5, .expand = .horizontal, .font_style = .heading, .padding = .{ .x = 6, .y = 6, .w = 6, .h = 4 } }); if (openflag) |of| { if (try dvui.buttonIcon( @@ -3374,7 +3374,7 @@ pub fn windowHeader(str: []const u8, right_str: []const u8, openflag: ?*bool) !R } } - try dvui.labelNoFmt(@src(), right_str, .{ .gravity_x = 1.0 }); + try dvui.labelNoFmt(@src(), right_str, .{}, .{ .gravity_x = 1.0 }); const evts = events(); for (evts) |*e| { @@ -4009,7 +4009,7 @@ pub fn toastDisplay(id: WidgetId) !void { var animator = try dvui.animate(@src(), .{ .kind = .alpha, .duration = 500_000 }, .{ .id_extra = id.asUsize() }); defer animator.deinit(); - try dvui.labelNoFmt(@src(), message, .{ .background = true, .corner_radius = dvui.Rect.all(1000), .padding = .{ .x = 16, .y = 8, .w = 16, .h = 8 } }); + try dvui.labelNoFmt(@src(), message, .{}, .{ .background = true, .corner_radius = dvui.Rect.all(1000), .padding = .{ .x = 16, .y = 8, .w = 16, .h = 8 } }); if (dvui.timerDone(id)) { animator.startEnd(); @@ -4261,7 +4261,7 @@ pub fn expander(src: std.builtin.SourceLocation, label_str: []const u8, init_opt .{ .gravity_y = 0.5 }, ); } - try labelNoFmt(@src(), label_str, options.strip()); + try labelNoFmt(@src(), label_str, .{}, options.strip()); dvui.dataSet(null, bc.wd.id, "_expand", expanded); @@ -4477,7 +4477,7 @@ pub fn gridHeading( var cell = try g.headerCell(src, opts.cellOptions(g.col_num, 0)); defer cell.deinit(); - try labelNoFmt(@src(), heading, label_options); + try labelNoFmt(@src(), heading, .{}, label_options); try gridHeadingSeparator(resize_opts); } @@ -4859,7 +4859,7 @@ pub fn menuItemLabel(src: std.builtin.SourceLocation, label_str: []const u8, ini labelopts = labelopts.override(themeGet().style_accent); } - try labelNoFmt(@src(), label_str, labelopts); + try labelNoFmt(@src(), label_str, .{}, labelopts); mi.deinit(); @@ -4903,7 +4903,7 @@ pub fn menuItem(src: std.builtin.SourceLocation, init_opts: MenuItemWidget.InitO pub fn labelClick(src: std.builtin.SourceLocation, comptime fmt: []const u8, args: anytype, opts: Options) !bool { var ret = false; - var lw = LabelWidget.init(src, fmt, args, opts.override(.{ .name = "LabelClick" })); + var lw = LabelWidget.init(src, fmt, args, .{}, opts.override(.{ .name = "LabelClick" })); // now lw has a Rect from its parent but hasn't processed events or drawn const lwid = lw.data().id; @@ -5003,16 +5003,39 @@ pub fn labelClick(src: std.builtin.SourceLocation, comptime fmt: []const u8, arg return ret; } +/// Format and display a label. +/// +/// See `labelEx` and `labelNoFmt`. +/// +/// Only valid between `Window.begin`and `Window.end`. pub fn label(src: std.builtin.SourceLocation, comptime fmt: []const u8, args: anytype, opts: Options) !void { - var lw = LabelWidget.init(src, fmt, args, opts); + var lw = LabelWidget.init(src, fmt, args, .{}, opts); try lw.install(); lw.processEvents(); try lw.draw(); lw.deinit(); } -pub fn labelNoFmt(src: std.builtin.SourceLocation, str: []const u8, opts: Options) !void { - var lw = LabelWidget.initNoFmt(src, str, opts); +/// Format and display a label with extra label options. +/// +/// See `label` and `labelNoFmt`. +/// +/// Only valid between `Window.begin`and `Window.end`. +pub fn labelEx(src: std.builtin.SourceLocation, comptime fmt: []const u8, args: anytype, init_opts: LabelWidget.InitOptions, opts: Options) !void { + var lw = LabelWidget.init(src, fmt, args, init_opts, opts); + try lw.install(); + lw.processEvents(); + try lw.draw(); + lw.deinit(); +} + +/// Display a label (no formatting) with extra label options. +/// +/// See `label` and `labelEx`. +/// +/// Only valid between `Window.begin`and `Window.end`. +pub fn labelNoFmt(src: std.builtin.SourceLocation, str: []const u8, init_opts: LabelWidget.InitOptions, opts: Options) !void { + var lw = LabelWidget.initNoFmt(src, str, init_opts, opts); try lw.install(); lw.processEvents(); try lw.draw(); @@ -5194,7 +5217,7 @@ pub fn button(src: std.builtin.SourceLocation, label_str: []const u8, init_opts: // - gets a rectangle from bw // - draws itself // - reports its min size to bw - try labelNoFmt(@src(), label_str, options); + try labelNoFmt(@src(), label_str, .{}, options); // draw focus try bw.drawFocus(); @@ -5249,7 +5272,7 @@ pub fn buttonLabelAndIcon(src: std.builtin.SourceLocation, label_str: []const u8 try icon(@src(), label_str, tvg_bytes, .{}, opts.strip().override(.{ .gravity_x = 1.0 })); var hbox = try box(src, .horizontal, .{ .expand = .horizontal }); defer hbox.deinit(); // this hbox is used to center the label. - try labelNoFmt(@src(), label_str, options.override(.{ .gravity_x = 0.5 })); + try labelNoFmt(@src(), label_str, .{}, options.override(.{ .gravity_x = 0.5 })); } const click = bw.clicked(); @@ -5901,7 +5924,7 @@ pub fn checkbox(src: std.builtin.SourceLocation, target: *bool, label_str: ?[]co if (label_str) |str| { _ = try spacer(@src(), .{ .w = checkbox_defaults.paddingGet().w }, .{}); - try labelNoFmt(@src(), str, options.strip().override(.{ .gravity_y = 0.5 })); + try labelNoFmt(@src(), str, .{}, options.strip().override(.{ .gravity_y = 0.5 })); } return ret; @@ -5987,7 +6010,7 @@ pub fn radio(src: std.builtin.SourceLocation, active: bool, label_str: ?[]const if (label_str) |str| { _ = try spacer(@src(), .{ .w = radio_defaults.paddingGet().w }, .{}); - try labelNoFmt(@src(), str, options.strip().override(.{ .gravity_y = 0.5 })); + try labelNoFmt(@src(), str, .{}, options.strip().override(.{ .gravity_y = 0.5 })); } return ret; diff --git a/src/structEntry.zig b/src/structEntry.zig index 99f6e88a..bf15f365 100644 --- a/src/structEntry.zig +++ b/src/structEntry.zig @@ -200,7 +200,7 @@ fn boolFieldWidget( .dropdown => { const entries = .{ "false", "true" }; var choice: usize = if (result.* == false) 0 else 1; - try dvui.labelNoFmt(@src(), opt.label_override orelse name, .{}); + try dvui.labelNoFmt(@src(), opt.label_override orelse name, .{}, .{}); _ = try dvui.dropdown(@src(), &entries, &choice, .{}); result.* = if (choice == 0) false else true; }, diff --git a/src/widgets/DropdownWidget.zig b/src/widgets/DropdownWidget.zig index fbc235b3..13d668b0 100644 --- a/src/widgets/DropdownWidget.zig +++ b/src/widgets/DropdownWidget.zig @@ -45,7 +45,7 @@ pub fn install(self: *DropdownWidget) !void { if (self.init_options.label) |ll| { var hbox = try dvui.box(@src(), .horizontal, .{ .expand = .both }); - var lw = LabelWidget.initNoFmt(@src(), ll, self.options.strip().override(.{ .gravity_y = 0.5 })); + var lw = LabelWidget.initNoFmt(@src(), ll, .{}, self.options.strip().override(.{ .gravity_y = 0.5 })); try lw.install(); try lw.draw(); lw.deinit(); @@ -158,7 +158,7 @@ pub fn addChoiceLabel(self: *DropdownWidget, label_text: []const u8) !bool { opts = opts.override(dvui.themeGet().style_accent); } - try dvui.labelNoFmt(@src(), label_text, opts); + try dvui.labelNoFmt(@src(), label_text, .{}, opts); if (mi.activeRect()) |_| { self.close(); diff --git a/src/widgets/LabelWidget.zig b/src/widgets/LabelWidget.zig index 194aee58..9a805282 100644 --- a/src/widgets/LabelWidget.zig +++ b/src/widgets/LabelWidget.zig @@ -14,12 +14,22 @@ pub var defaults: Options = .{ .padding = Rect.all(6), }; +pub const InitOptions = struct { + align_x: f32 = 0.5, + align_y: f32 = 0.5, + + pub fn gravityGet(self: InitOptions) Options.Gravity { + return .{ .x = self.align_x, .y = self.align_y }; + } +}; + wd: WidgetData = undefined, label_str: []const u8, /// An allocator to free `label_str` on `deinit` allocator: ?std.mem.Allocator, +init_options: InitOptions, -pub fn init(src: std.builtin.SourceLocation, comptime fmt: []const u8, args: anytype, opts: Options) LabelWidget { +pub fn init(src: std.builtin.SourceLocation, comptime fmt: []const u8, args: anytype, init_opts: InitOptions, opts: Options) LabelWidget { comptime if (!std.unicode.utf8ValidateSlice(fmt)) @compileError("Format strings must be valid utf-8"); const cw = dvui.currentWindow(); @@ -41,10 +51,10 @@ pub fn init(src: std.builtin.SourceLocation, comptime fmt: []const u8, args: any dvui.log.debug("{s}:{d}: LabelWidget format output was invalid utf8 for '{s}'.", .{ src.file, src.line, str }); break :blk .{ utf8, null }; }; - return initNoFmtAllocator(src, str, alloc, opts); + return initNoFmtAllocator(src, str, alloc, init_opts, opts); } -pub fn initNoFmt(src: std.builtin.SourceLocation, label_str: []const u8, opts: Options) LabelWidget { +pub fn initNoFmt(src: std.builtin.SourceLocation, label_str: []const u8, init_opts: InitOptions, opts: Options) LabelWidget { const arena = dvui.currentWindow().lifo(); // If the allocation fails, the textSize will be incorrect // later because of invalid utf8 @@ -52,19 +62,20 @@ pub fn initNoFmt(src: std.builtin.SourceLocation, label_str: []const u8, opts: O logAndHighlight(src, opts, err); break :blk label_str; }; - return initNoFmtAllocator(src, str, if (str.ptr != label_str.ptr) arena else null, opts); + return initNoFmtAllocator(src, str, if (str.ptr != label_str.ptr) arena else null, init_opts, opts); } /// The `allocator` argument will be used to deallocator `label_str` on /// when `deinit` is called. /// /// Assumes the label_str is valid utf8 -pub fn initNoFmtAllocator(src: std.builtin.SourceLocation, label_str: []const u8, allocator: ?std.mem.Allocator, opts: Options) LabelWidget { +pub fn initNoFmtAllocator(src: std.builtin.SourceLocation, label_str: []const u8, allocator: ?std.mem.Allocator, init_opts: InitOptions, opts: Options) LabelWidget { const options = defaults.override(opts); var size = options.fontGet().textSize(label_str); size = Size.max(size, options.min_size_contentGet()); return .{ .wd = .init(src, .{}, options.override(.{ .min_size_content = size })), + .init_options = init_opts, .label_str = label_str, .allocator = allocator, }; @@ -86,7 +97,8 @@ pub fn install(self: *LabelWidget) !void { } pub fn draw(self: *LabelWidget) !void { - const rect = dvui.placeIn(self.wd.contentRect(), self.wd.options.min_size_contentGet(), .none, self.wd.options.gravityGet()); + const label_gravity = self.init_options.gravityGet(); + const rect = dvui.placeIn(self.wd.contentRect(), self.wd.options.min_size_contentGet(), .none, label_gravity); var rs = self.wd.parent.screenRectScale(rect); const oldclip = dvui.clip(rs.r); var iter = std.mem.splitScalar(u8, self.label_str, '\n'); @@ -101,7 +113,9 @@ pub fn draw(self: *LabelWidget) !void { } const tsize = self.wd.options.fontGet().textSize(line); - const lineRect = dvui.placeIn(self.wd.contentRect(), tsize, .none, self.wd.options.gravityGet()); + + // this is only about horizontal direction + const lineRect = dvui.placeIn(self.wd.contentRect(), tsize, .none, label_gravity); const liners = self.wd.parent.screenRectScale(lineRect); rs.r.x = liners.r.x; diff --git a/src/widgets/SuggestionWidget.zig b/src/widgets/SuggestionWidget.zig index b254fa56..0fccd439 100644 --- a/src/widgets/SuggestionWidget.zig +++ b/src/widgets/SuggestionWidget.zig @@ -72,7 +72,7 @@ pub fn dropped(self: *SuggestionWidget) !bool { pub fn addChoiceLabel(self: *SuggestionWidget, label_str: []const u8) !bool { var mi = try self.addChoice(); - try dvui.labelNoFmt(@src(), label_str, .{}); + try dvui.labelNoFmt(@src(), label_str, .{}, .{}); var ret: bool = false; if (mi.activeRect()) |_| { diff --git a/src/widgets/TabsWidget.zig b/src/widgets/TabsWidget.zig index c3ea1ee4..67993a72 100644 --- a/src/widgets/TabsWidget.zig +++ b/src/widgets/TabsWidget.zig @@ -70,7 +70,7 @@ pub fn addTabLabel(self: *TabsWidget, selected: bool, text: []const u8) !bool { label_opts.color_text = .{ .name = .text_press }; } - try dvui.labelNoFmt(@src(), text, label_opts); + try dvui.labelNoFmt(@src(), text, .{}, label_opts); return tab.clicked(); } -- knotmirror 0.0.1 From cf9abd1447e6db2ca8cd3cf15ab18560b65f202d Mon Sep 17 00:00:00 2001 From: David Vanderson Date: Sun, 8 Jun 2025 21:35:41 -0400 Subject: [PATCH 2/3] fixup dx11 example --- examples/dx11-standalone.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/dx11-standalone.zig b/examples/dx11-standalone.zig index 38fb3ee0..e7113d8d 100644 --- a/examples/dx11-standalone.zig +++ b/examples/dx11-standalone.zig @@ -169,7 +169,7 @@ fn gui_frame() !void { } { - try dvui.labelNoFmt(@src(), "These are drawn directly by the backend, not going through DVUI.", .{ .margin = .{ .x = 4 } }); + try dvui.labelNoFmt(@src(), "These are drawn directly by the backend, not going through DVUI.", .{}, .{ .margin = .{ .x = 4 } }); var box = try dvui.box(@src(), .horizontal, .{ .expand = .horizontal, .min_size_content = .{ .h = 40 }, .background = true, .margin = .{ .x = 8, .w = 8 } }); defer box.deinit(); -- knotmirror 0.0.1 From 6af7af0c5659784a969dbad5ea8e14dedfb26a4b Mon Sep 17 00:00:00 2001 From: Paul Hatchman Date: Wed, 11 Jun 2025 10:19:41 +1000 Subject: [PATCH 3/3] buttonLabelAndIcon now uses labelEx for label centering --- src/dvui.zig | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/dvui.zig b/src/dvui.zig index 7e93e006..889c81d3 100644 --- a/src/dvui.zig +++ b/src/dvui.zig @@ -5270,9 +5270,7 @@ pub fn buttonLabelAndIcon(src: std.builtin.SourceLocation, label_str: []const u8 var outer_hbox = try box(src, .horizontal, .{ .expand = .horizontal }); defer outer_hbox.deinit(); try icon(@src(), label_str, tvg_bytes, .{}, opts.strip().override(.{ .gravity_x = 1.0 })); - var hbox = try box(src, .horizontal, .{ .expand = .horizontal }); - defer hbox.deinit(); // this hbox is used to center the label. - try labelNoFmt(@src(), label_str, .{}, options.override(.{ .gravity_x = 0.5 })); + try labelEx(@src(), "{s}", .{label_str}, .{ .align_x = 0.5 }, opts.strip().override(.{ .expand = .both })); } const click = bw.clicked(); -- knotmirror 0.0.1