1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2025-04-14 17:43:01 -04:00

Fix cursor position

This commit is contained in:
Zebra2711 2025-01-08 21:05:52 +07:00
parent 2462e75841
commit 84b1339764

View file

@ -257,7 +257,7 @@ textbox *textbox_create(widget *parent, WidgetType type, const char *name,
if (password_mask_char == NULL || (*password_mask_char) == '\0'){
tb->password_mask_char = "*";
} else {
tb->password_mask_char = password_mask_char;
tb->password_mask_char = g_markup_escape_text(password_mask_char, -1);
}
textbox_text(tb, txt ? txt : "");
@ -554,11 +554,18 @@ static void textbox_draw(widget *wid, cairo_t *draw) {
// We want to place the cursor based on the text shown.
const char *text = pango_layout_get_text(tb->layout);
// Clamp the position, should not be needed, but we are paranoid.
int cursor_offset = MIN(tb->cursor, g_utf8_strlen(text, -1));
int cursor_offset;
// Calculate cursor position based on mask length
if ((tb->flags & TB_PASSWORD) == TB_PASSWORD) {
int mask_len = strlen(tb->password_mask_char);
cursor_offset = MIN(tb->cursor * mask_len, strlen(text));
} else {
cursor_offset = MIN(tb->cursor, g_utf8_strlen(text, -1));
}
PangoRectangle pos;
// convert to byte location.
char *offset = g_utf8_offset_to_pointer(text, cursor_offset);
pango_layout_get_cursor_pos(tb->layout, offset - text, &pos, NULL);
pango_layout_get_cursor_pos(tb->layout, cursor_offset, &pos, NULL);
int cursor_x = pos.x / PANGO_SCALE;
int cursor_y = pos.y / PANGO_SCALE;
int cursor_height = pos.height / PANGO_SCALE;