boolFieldWidget
Parameters
- src:std.builtin.SourceLocation
- std.builtin.SourceLocation
- field_name:[]const u8
- []const u8
- field_value_ptr:anytype
- anytype
- opt:BoolFieldOptions
- BoolFieldOptions
- alignment:*dvui.Alignment
- *dvui.Alignment
Source
Implementation
pub fn boolFieldWidget(
src: std.builtin.SourceLocation,
field_name: []const u8,
field_value_ptr: anytype,
opt: BoolFieldOptions,
alignment: *dvui.Alignment,
) void {
validateFieldPtrType(null, &.{.bool}, "boolFieldWidget", @TypeOf(field_value_ptr));
if (opt.display == .none) return;
const read_only = @typeInfo(@TypeOf(field_value_ptr)).pointer.is_const or opt.display != .read_write;
var box = dvui.box(src, .{ .dir = .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) {
if (opt.widget_type == .manual_reset) {
const prev_state = dvui.dataGetDefault(null, box.data().id, "bool", bool, false);
var state = prev_state or field_value_ptr.*;
var data_out: dvui.WidgetData = undefined;
_ = dvui.checkbox(@src(), &state, "", .{ .data_out = &data_out, .margin = .{ .y = 4 } });
if (state)
dvui.tooltip(@src(), .{ .active_rect = data_out.borderRectScale().r, .delay = 1_000_000 }, "Value was set to true since last manual reset.", .{}, .{})
else
dvui.tooltip(@src(), .{ .active_rect = data_out.borderRectScale().r, .delay = 1_000_000 }, "Value was not set to true since last manual reset", .{}, .{});
dvui.dataSet(null, box.data().id, "bool", state);
} else if (opt.widget_type == .trigger_on) {
if (opt.widget_type.trigger_on == field_value_ptr.*) {
dvui.animation(box.data().id, "trigger", .{ .start_val = 0, .end_val = 1.0, .start_time = 0, .end_time = 1_000_000, .easing = easing });
}
if (dvui.animationGet(box.data().id, "trigger")) |a| {
const prev_alpha = dvui.alpha(a.value());
defer dvui.alphaSet(prev_alpha);
if (!a.done()) {
dvui.label(@src(), "{}", .{opt.widget_type.trigger_on}, .{ .margin = .{ .y = 4 } });
} else {
dvui.label(@src(), "{}", .{field_value_ptr.*}, .{ .margin = .{ .y = 4 } });
dvui.refresh(null, @src(), null);
}
} else {
dvui.label(@src(), "{}", .{field_value_ptr.*}, .{ .margin = .{ .y = 4 } });
}
} else {
dvui.label(@src(), "{}", .{field_value_ptr.*}, .{ .margin = .{ .y = 4 } });
}
} else {
if (opt.widget_type == .checkbox) {
_ = dvui.checkbox(@src(), field_value_ptr, "", .{});
} else {
const entries = .{ "false", "true" };
var choice: usize = if (field_value_ptr.* == false) 0 else 1;
_ = dvui.dropdown(@src(), &entries, .{ .choice = &choice }, .{}, .{});
field_value_ptr.* = if (choice == 0) false else true;
}
}
}