init
It's expected to call this when self is undefined
Parameters
- self:*DropdownWidget
- *DropdownWidget
- src:std.builtin.SourceLocation
- std.builtin.SourceLocation
- init_opts:InitOptions
- InitOptions
- opts:Options
- Options
Source
Implementation
pub fn init(self: *DropdownWidget, src: std.builtin.SourceLocation, init_opts: InitOptions, opts: Options) void {
const options = defaults.override(opts);
self.* = .{
.options = options,
.init_options = init_opts,
// SAFETY: Set bellow
.menu = undefined,
// SAFETY: Set bellow
.menuItem = undefined,
};
self.menu.init(src, .{ .dir = .horizontal }, wrapOuter(options));
self.menuItem.init(@src(), .{ .submenu = true, .focus_as_outline = true }, wrapInner(self.options));
self.menuItem.processEvents();
self.menuItem.drawBackground();
if (dvui.dataGet(null, self.data().id, "_drop_adjust", f32)) |adjust| self.drop_adjust = adjust;
const label_text: ?[]const u8 = blk: {
if (self.init_options.selected_index == null) {
if (self.init_options.placeholder) |placeholder| {
break :blk placeholder;
}
}
if (self.init_options.label) |label| {
break :blk label;
} else {
break :blk null;
}
};
if (label_text) |ll| {
var hbox = dvui.box(@src(), .{ .dir = .horizontal }, .{ .expand = .both });
defer hbox.deinit();
var lw: LabelWidget = undefined;
lw.initNoFmt(@src(), ll, .{}, self.options.strip().override(.{
.gravity_y = 0.5,
.color_text = if (self.init_options.selected_index == null and self.init_options.placeholder != null) self.data().options.color(.text).opacity(0.65) else null,
}));
lw.draw();
lw.deinit();
_ = dvui.spacer(@src(), .{ .min_size_content = .width(6) });
dvui.icon(
@src(),
"dropdown_triangle",
dvui.entypo.chevron_small_down,
.{},
self.options.strip().override(.{ .gravity_y = 0.5, .gravity_x = 1.0, .role = .none }),
);
}
if (self.menuItem.data().accesskit_node()) |ak_node| {
AccessKit.nodeAddAction(ak_node, AccessKit.Action.focus);
AccessKit.nodeAddAction(ak_node, AccessKit.Action.click);
if (self.init_options.selected_index == null)
if (self.init_options.placeholder) |placeholder| {
AccessKit.nodeSetPlaceholderWithLength(ak_node, placeholder.ptr, placeholder.len);
};
// AK TODO: Potential case for supporting expand, when supported by accesskit.
//AccessKit.nodeAddAction(ak_node, AccessKit.Action.expand);
}
}