From 1af132352a1f5c21ba47563712b751976a4b3068 Mon Sep 17 00:00:00 2001 From: Martin Fouilleul Date: Sun, 5 Mar 2023 16:35:55 +0100 Subject: [PATCH] [ui, textbox] Scroll text inside text box to always show edit cursor. --- src/osx_app.m | 4 ++-- src/ui.c | 27 +++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/osx_app.m b/src/osx_app.m index 1d387e2..31d8c65 100644 --- a/src/osx_app.m +++ b/src/osx_app.m @@ -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); diff --git a/src/ui.c b/src/ui.c index 5fc6297..f706ca4 100644 --- a/src/ui.c +++ b/src/ui.c @@ -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);