added .masked and destructor for EditState

This commit is contained in:
2026-01-09 13:31:08 +05:00
parent d417b5bbd5
commit 6f8f2a54c0
3 changed files with 48 additions and 15 deletions

View File

@@ -1,9 +1,9 @@
#include "tim.h"
void TimEditState_init(TimEditState* e, i32 capacity, cstr initial_content){
e->length = tim_utf8_len(initial_content);
e->cursor = tim_utf8_len(initial_content);
void TimEditState_construct(TimEditState* e, i32 capacity, cstr initial_content){
e->masked = false;
e->length = initial_content ? tim_utf8_len(initial_content) : 0;
e->cursor = e->length;
e->capacity = capacity;
e->s = (char*)malloc(capacity + 1);
i32 byte_len = strlen(initial_content);
@@ -90,13 +90,31 @@ TimKey tim_edit(TimEditState* e, i32 x, i32 y, i32 w, TimStyle style) {
if (tim->event.type == TimEvent_Draw) {
tim_draw_box(r.x, r.y, r.w, r.h, style.brd, style.bg);
if (tim->focus == e) {
char* s = e->s + tim_utf8_pos(e->s, e->cursor - r.w + 4);
i32 cur = MIN(r.w - 4, e->cursor);
tim_draw_str(s, r.x + 2, r.y + 1, r.w - 3, style.fg, style.bg);
tim_draw_invert(r.x + cur + 2, r.y + 1, 1);
} else {
tim_draw_str(e->s, r.x + 2, r.y + 1, r.w - 3, style.fg, style.bg);
i32 s_x = r.x + 2, s_y = r.y + 1, s_w = r.w - 3, s_w_sub1 = s_w - 1;
char* s = e->s;
bool focused = tim->focus == e;
if (focused) {
// rewind long buffer so cursor is at the most right cell
s = e->s + tim_utf8_pos(e->s, e->cursor - s_w_sub1);
}
if(e->masked){
// draw '*' instead of buffer characters
TimCell cell_masked = tim_cell("*", style.fg, style.bg);
i32 w_mask = MIN(s_w, e->length);
tim_draw_row(cell_masked, s_x, s_y, w_mask);
}
else {
// draw part of buffer content
tim_draw_str(s, s_x, s_y, s_w, style.fg, style.bg);
}
if (focused) {
// invert color of last cell
i32 cur = MIN(s_w_sub1, e->cursor);
tim_draw_invert(s_x + cur, s_y, 1);
}
}