feat: add support for input box icons
This commit is contained in:
parent
4d2c7d8a8c
commit
92b1354d4d
2 changed files with 13 additions and 4 deletions
|
@ -12,6 +12,8 @@ pub fn Options(context: type) type {
|
||||||
label: []const u8 = "Enter text",
|
label: []const u8 = "Enter text",
|
||||||
pos: Widget.Box = .{ .y = 0, .x = 0, .w = 12, .h = 1 },
|
pos: Widget.Box = .{ .y = 0, .x = 0, .w = 12, .h = 1 },
|
||||||
ctx: Context,
|
ctx: Context,
|
||||||
|
padding: u8 = 1,
|
||||||
|
icon: ?[]const u8 = null,
|
||||||
|
|
||||||
on_click: *const fn (ctx: context, button: *State(Context)) void = do_nothing,
|
on_click: *const fn (ctx: context, button: *State(Context)) void = do_nothing,
|
||||||
on_render: *const fn (ctx: context, button: *State(Context), theme: *const Widget.Theme) bool = on_render_default,
|
on_render: *const fn (ctx: context, button: *State(Context), theme: *const Widget.Theme) bool = on_render_default,
|
||||||
|
@ -29,18 +31,21 @@ pub fn Options(context: type) type {
|
||||||
self.plane.set_style(style_label);
|
self.plane.set_style(style_label);
|
||||||
self.plane.fill(" ");
|
self.plane.fill(" ");
|
||||||
self.plane.home();
|
self.plane.home();
|
||||||
|
for (0..self.opts.padding) |_| _ = self.plane.putchar(" ");
|
||||||
|
if (self.opts.icon) |icon|
|
||||||
|
_ = self.plane.print("{s}", .{icon}) catch {};
|
||||||
if (self.text.items.len > 0) {
|
if (self.text.items.len > 0) {
|
||||||
_ = self.plane.print(" {s} ", .{self.text.items}) catch {};
|
_ = self.plane.print("{s} ", .{self.text.items}) catch {};
|
||||||
} else {
|
} else {
|
||||||
_ = self.plane.print(" {s} ", .{self.label.items}) catch {};
|
_ = self.plane.print("{s} ", .{self.label.items}) catch {};
|
||||||
}
|
}
|
||||||
if (self.cursor) |cursor| {
|
if (self.cursor) |cursor| {
|
||||||
const pos: c_int = @intCast(cursor);
|
const pos: c_int = @intCast(cursor);
|
||||||
if (tui.config().enable_terminal_cursor) {
|
if (tui.config().enable_terminal_cursor) {
|
||||||
const y, const x = self.plane.rel_yx_to_abs(0, pos + 1);
|
const y, const x = self.plane.rel_yx_to_abs(0, pos + self.opts.padding + self.icon_width);
|
||||||
tui.rdr().cursor_enable(y, x, tui.get_cursor_shape()) catch {};
|
tui.rdr().cursor_enable(y, x, tui.get_cursor_shape()) catch {};
|
||||||
} else {
|
} else {
|
||||||
self.plane.cursor_move_yx(0, pos + 1) catch return false;
|
self.plane.cursor_move_yx(0, pos + self.opts.padding + self.icon_width) catch return false;
|
||||||
var cell = self.plane.cell_init();
|
var cell = self.plane.cell_init();
|
||||||
_ = self.plane.at_cursor_cell(&cell) catch return false;
|
_ = self.plane.at_cursor_cell(&cell) catch return false;
|
||||||
cell.set_style(theme.editor_cursor);
|
cell.set_style(theme.editor_cursor);
|
||||||
|
@ -68,6 +73,7 @@ pub fn create(ctx_type: type, allocator: std.mem.Allocator, parent: Plane, opts:
|
||||||
.opts = opts,
|
.opts = opts,
|
||||||
.label = std.ArrayList(u8).init(allocator),
|
.label = std.ArrayList(u8).init(allocator),
|
||||||
.text = std.ArrayList(u8).init(allocator),
|
.text = std.ArrayList(u8).init(allocator),
|
||||||
|
.icon_width = @intCast(if (opts.icon) |icon| n.egc_chunk_width(icon, 0, 1) else 0),
|
||||||
};
|
};
|
||||||
try self.label.appendSlice(self.opts.label);
|
try self.label.appendSlice(self.opts.label);
|
||||||
self.opts.label = self.label.items;
|
self.opts.label = self.label.items;
|
||||||
|
@ -83,6 +89,7 @@ pub fn State(ctx_type: type) type {
|
||||||
label: std.ArrayList(u8),
|
label: std.ArrayList(u8),
|
||||||
opts: Options(ctx_type),
|
opts: Options(ctx_type),
|
||||||
text: std.ArrayList(u8),
|
text: std.ArrayList(u8),
|
||||||
|
icon_width: c_int,
|
||||||
cursor: ?usize = 0,
|
cursor: ?usize = 0,
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
|
@ -70,6 +70,8 @@ pub fn Create(options: type) type {
|
||||||
.inputbox = (try self.menu.add_header(try InputBox.create(*Self, self.allocator, self.menu.menu.parent, .{
|
.inputbox = (try self.menu.add_header(try InputBox.create(*Self, self.allocator, self.menu.menu.parent, .{
|
||||||
.ctx = self,
|
.ctx = self,
|
||||||
.label = options.label,
|
.label = options.label,
|
||||||
|
.padding = 2,
|
||||||
|
.icon = if (@hasDecl(options, "icon")) options.icon else null,
|
||||||
}))).dynamic_cast(InputBox.State(*Self)) orelse unreachable,
|
}))).dynamic_cast(InputBox.State(*Self)) orelse unreachable,
|
||||||
.view_rows = get_view_rows(tui.screen()),
|
.view_rows = get_view_rows(tui.screen()),
|
||||||
.entries = std.ArrayList(Entry).init(allocator),
|
.entries = std.ArrayList(Entry).init(allocator),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue