Compare commits

...

2 Commits

Author SHA1 Message Date
d47b835020 fix cursor placement 2025-09-27 12:43:38 +02:00
f1b6321d3d optional anchor in text_box 2025-09-27 12:43:21 +02:00
3 changed files with 15 additions and 17 deletions

View File

@ -414,19 +414,12 @@ fn Rect? Ctx.layout_string(&ctx, String text, Rect bounds, Anchor anchor, int z_
next_line_x += bounds.w; next_line_x += bounds.w;
} }
// reset the cursor to the line
if (line_start <= cursor && cursor <= line_end) { if (line_start <= cursor && cursor <= line_end) {
cursor_rect.x = origin.x; cursor_rect.x = origin.x;
cursor_rect.y = origin.y; cursor_rect.y = origin.y;
cursor_rect.w = 0; cursor_rect.w = 0;
if (cursor && text[cursor-1] == '\n') {
cursor_rect.x = next_line_x;
cursor_rect.y += line_height;
}
} }
Point line_origin = origin;
// see the fixme when measuring the height // see the fixme when measuring the height
//ctx.push_rect({.x = origin.x,.y=origin.y,.w=(short)line_width,.h=(short)string_height}, z_index, &&(Style){.bg=0xff000042u.@to_rgba()})!; //ctx.push_rect({.x = origin.x,.y=origin.y,.w=(short)line_width,.h=(short)string_height}, z_index, &&(Style){.bg=0xff000042u.@to_rgba()})!;
String line = text[line_start:line_end-line_start]; String line = text[line_start:line_end-line_start];
@ -454,21 +447,25 @@ fn Rect? Ctx.layout_string(&ctx, String text, Rect bounds, Anchor anchor, int z_
}; };
ctx.push_sprite(b, uv, texture_id, z_index, hue)!; ctx.push_sprite(b, uv, texture_id, z_index, hue)!;
//ctx.push_rect(b, z_index, &&(Style){.bg=0x0000ff66u.@to_rgba()})!; //ctx.push_rect(b, z_index, &&(Style){.bg=0x0000ff66u.@to_rgba()})!;
origin.x += gp.adv; origin.x += gp.adv;
} }
if (line_start + off < cursor && text[cursor-1] != '\n') { if (line_start + off + x == cursor) {
cursor_rect.x = origin.x; cursor_rect.x = origin.x;
cursor_rect.y = origin.y;
cursor_rect.w = gp.adv;
} }
} }
// done with the line // done with the line
line_start = line_end; line_start = line_end;
origin.y += line_height + line_gap; origin.y += line_height + line_gap;
if (cursor == text.len && text[^1] == '\n') {
cursor_rect.x = next_line_x;
cursor_rect.y = origin.y;
}
} while(line_end < text.len); } while(line_end < text.len);
ctx.reset_scissor(z_index)!; ctx.reset_scissor(z_index)!;

View File

@ -39,9 +39,9 @@ fn void? Ctx.text_id(&ctx, Id id, String text)
} }
macro Ctx.text_box(&ctx, Size w, Size h, TextEdit* te, ...) macro Ctx.text_box(&ctx, Size w, Size h, TextEdit* te, Anchor text_alignment = TOP_LEFT, ...)
=> ctx.text_box_id(@compute_id($vasplat), w, h, te); => ctx.text_box_id(@compute_id($vasplat), w, h, te, text_alignment);
fn ElemEvents? Ctx.text_box_id(&ctx, Id id, Size w, Size h, TextEdit* te) fn ElemEvents? Ctx.text_box_id(&ctx, Id id, Size w, Size h, TextEdit* te, Anchor text_alignment)
{ {
id = ctx.gen_id(id)!; id = ctx.gen_id(id)!;
@ -77,10 +77,11 @@ fn ElemEvents? Ctx.text_box_id(&ctx, Id id, Size w, Size h, TextEdit* te)
Rect text_bounds = elem.bounds.pad(elem.layout.content_offset); Rect text_bounds = elem.bounds.pad(elem.layout.content_offset);
ctx.push_rect(bg_bounds, parent.div.z_index, style)!; ctx.push_rect(bg_bounds, parent.div.z_index, style)!;
Rect cur; Rect cur;
cur = ctx.layout_string(elem.text.te.to_string(), text_bounds, TOP_LEFT, parent.div.z_index, style.fg, elem.text.te.cursor)!; cur = ctx.layout_string(elem.text.te.to_string(), text_bounds, text_alignment, parent.div.z_index, style.fg, elem.text.te.cursor)!;
// draw the cursor if the element has focus // draw the cursor if the element has focus
cur.w = 2; cur.w = 2;
cur.x -= 2;
if (elem.events.has_focus) { if (elem.events.has_focus) {
ctx.push_scissor(text_bounds, parent.div.z_index)!; ctx.push_scissor(text_bounds, parent.div.z_index)!;
ctx.push_rect(cur, parent.div.z_index, &&(Style){.bg = style.fg})!; ctx.push_rect(cur, parent.div.z_index, &&(Style){.bg = style.fg})!;

View File

@ -423,7 +423,7 @@ fn void calculator(ugui::Ctx* ui, TextEdit* te)
ui.slider_ver(ugui::@exact(20), ugui::@exact(100), &f)!!; ui.slider_ver(ugui::@exact(20), ugui::@exact(100), &f)!!;
}!!; }!!;
ui.@div(ugui::@grow(), ugui::@fit(), anchor: CENTER, scroll_y: true) { ui.@div(ugui::@grow(), ugui::@fit(), anchor: CENTER, scroll_y: true) {
ui.text_box(ugui::@grow(), ugui::@exact(100), te)!!; ui.text_box(ugui::@grow(), ugui::@exact(100), te, RIGHT)!!;
}!!; }!!;
}!!; }!!; }!!; }!!;