[ui, textbox] Scroll text inside text box to always show edit cursor.

This commit is contained in:
Martin Fouilleul 2023-03-05 16:35:55 +01:00
parent 592f4cdecd
commit 1af132352a
2 changed files with 27 additions and 4 deletions

View File

@ -922,7 +922,7 @@ static void mp_process_mouse_button(NSEvent* nsEvent, mp_window_data* window, mp
mp_event event = {};
event.window = mp_window_handle_from_ptr(window);
event.type = MP_EVENT_KEYBOARD_KEY;
event.key.action = MP_KEY_PRESS;
event.key.action = action;
event.key.code = mp_convert_osx_key([nsEvent keyCode]);
event.key.mods = mp_convert_osx_mods([nsEvent modifierFlags]);
@ -930,7 +930,7 @@ static void mp_process_mouse_button(NSEvent* nsEvent, mp_window_data* window, mp
event.key.labelLen = label.len;
memcpy(event.key.label, label.ptr, label.len);
mp_update_key_state(&__mpApp.inputState.keyboard.keys[event.key.code], MP_KEY_PRESS);
mp_update_key_state(&__mpApp.inputState.keyboard.keys[event.key.code], action);
mp_queue_event(&event);

View File

@ -2214,7 +2214,8 @@ ui_text_box_result ui_text_box(const char* name, mem_arena* arena, str8 text)
{
const ui_edit_command* command = &(UI_EDIT_COMMANDS[i]);
if(mp_key_pressed(command->key) && mods == command->mods)
if( (mp_key_pressed(command->key) || mp_key_repeated(command->key))
&& mods == command->mods)
{
codepoints = ui_edit_perform_operation(ui, command->operation, command->move, command->direction, codepoints);
break;
@ -2235,7 +2236,29 @@ ui_text_box_result ui_text_box(const char* name, mem_arena* arena, str8 text)
result.text = utf8_push_from_codepoints(arena, codepoints);
}
//TODO slide contents
//NOTE slide contents
{
if(ui->editCursor < ui->editFirstDisplayedChar)
{
ui->editFirstDisplayedChar = ui->editCursor;
}
else
{
f32 textMargin = 5.; //TODO: pull this out and make it configurable
i32 firstDisplayedChar = ui->editFirstDisplayedChar;
str32 firstToCursor = str32_slice(codepoints, firstDisplayedChar, ui->editCursor);
mp_rect firstToCursorBox = mg_text_bounding_box_utf32(style->font, style->fontSize, firstToCursor);
while(firstToCursorBox.w > (frame->rect.w - 2*textMargin))
{
firstDisplayedChar++;
firstToCursor = str32_slice(codepoints, firstDisplayedChar, ui->editCursor);
firstToCursorBox = mg_text_bounding_box_utf32(style->font, style->fontSize, firstToCursor);
}
ui->editFirstDisplayedChar = firstDisplayedChar;
}
}
//NOTE: set renderer
str32* renderCodepoints = mem_arena_alloc_type(&ui->frameArena, str32);