render edit only for draw event

This commit is contained in:
Chu'vok
2024-09-18 11:59:15 +02:00
parent e92901cada
commit 5432245bc8
2 changed files with 12 additions and 10 deletions

21
tim.h
View File

@@ -1194,21 +1194,22 @@ static bool edit_event(struct edit* e, struct rect r) {
static inline bool edit(struct edit* e, int x, int y, int w, uint64_t color) {
struct rect r = abs_xywh(x, y, w, 3);
// uninitialized
// uninitialized edit state
if (e->str[0] && e->cursor == 0 && e->length == 0) {
e->length = utflen(e->str);
e->cursor = e->length;
}
draw_box(r.x, r.y, r.w, r.h, color >> 16, color >> 8);
if (tim.focus == (uintptr_t)e) {
int cur = MIN(r.w - 4, e->cursor);
int off = utfpos(e->str, e->cursor - r.w + 4);
draw_str(e->str + off, r.x + 2, r.y + 1, r.w - 3, color, color >> 8);
draw_invert(r.x + cur + 2, r.y + 1, 1);
} else {
draw_str(e->str, r.x + 2, r.y + 1, r.w - 3, color, color >> 8);
if (tim.event.type == DRAW_EVENT) {
draw_box(r.x, r.y, r.w, r.h, color >> 16, color >> 8);
if (tim.focus == (uintptr_t)e) {
char* str = e->str + utfpos(e->str, e->cursor - r.w + 4);
int cur = MIN(r.w - 4, e->cursor);
draw_str(str, r.x + 2, r.y + 1, r.w - 3, color, color >> 8);
draw_invert(r.x + cur + 2, r.y + 1, 1);
} else {
draw_str(e->str, r.x + 2, r.y + 1, r.w - 3, color, color >> 8);
}
}
return edit_event(e, r);