dropdown
Show chosen entry, and click to display all entries in a floating menu.
Returns true if any entry was selected (even the already chosen one).
See DropdownWidget for more advanced usage.
Only valid between Window.beginand Window.end.
Parameters
- src:std.builtin.SourceLocation
- std.builtin.SourceLocation
- entries:[]const []const u8
- []const []const u8
- choice:DropdownChoice(usize)
- DropdownChoice(usize)
- init_opts:DropdownInitOptions
- DropdownInitOptions
- opts:Options
- Options
Source
Implementation
pub fn dropdown(src: std.builtin.SourceLocation, entries: []const []const u8, choice: DropdownChoice(usize), init_opts: DropdownInitOptions, opts: Options) bool {
// Adjust selected index by 1 if the placeholder is showing
const selected_index: ?usize = switch (choice) {
.choice => |ch| ch.*,
.choice_nullable => |ch| if (ch.*) |_|
if (init_opts.null_selectable) ch.*.? + 1 else ch.*.?
else
null,
};
var dd: dvui.DropdownWidget = undefined;
dd.init(
src,
switch (choice) {
.choice => |ch| .{ .selected_index = ch.*, .label = entries[ch.*] },
.choice_nullable => |ch| if (ch.* == null)
.{ .placeholder = init_opts.placeholder orelse dropdown_placeholder_default }
else
.{ .selected_index = selected_index, .label = entries[ch.*.?] },
},
opts,
);
var ret = false;
if (dd.dropped()) {
if (choice == .choice_nullable and init_opts.null_selectable) {
var mi = dd.addChoice();
defer mi.deinit();
dvui.labelNoFmt(@src(), init_opts.placeholder orelse dropdown_placeholder_default, .{}, opts.strip().override(.{
.gravity_y = 0.5,
.color_text = opts.color(.text).opacity(0.65),
}));
if (mi.activeRect()) |_| {
dd.close();
choice.choice_nullable.* = null;
ret = true;
}
}
for (entries, 0..) |e, i| {
if (dd.addChoiceLabel(e)) {
switch (choice) {
inline else => |ch| ch.* = i,
}
ret = true;
}
}
}
dd.deinit();
return ret;
}