From b95adcf0f052cc8106b79bc8c7929a74b454cf30 Mon Sep 17 00:00:00 2001 From: Yousef Khadadeh Date: Wed, 1 Apr 2026 18:40:52 +0300 Subject: [PATCH] Fixed textbox --- src/GUIStuff/Elements/TextBox.decl.hpp | 3 ++- src/GUIStuff/Elements/TextBox.impl.hpp | 37 +++++++++++++++----------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/GUIStuff/Elements/TextBox.decl.hpp b/src/GUIStuff/Elements/TextBox.decl.hpp index d8f8c5e..2ac1172 100644 --- a/src/GUIStuff/Elements/TextBox.decl.hpp +++ b/src/GUIStuff/Elements/TextBox.decl.hpp @@ -31,9 +31,10 @@ template class TextBox : public Element { virtual void input_key_callback(const InputManager::KeyCallbackArgs& key) override; ~TextBox(); private: - void init_textbox(UpdateInputData& io); + void init_textbox(UpdateInputData& io, bool forceTextUpdate); bool update_data(); + std::optional oldData; bool isSelected = false; TextBoxData userInfo; std::shared_ptr textbox; diff --git a/src/GUIStuff/Elements/TextBox.impl.hpp b/src/GUIStuff/Elements/TextBox.impl.hpp index a0735ca..33401df 100644 --- a/src/GUIStuff/Elements/TextBox.impl.hpp +++ b/src/GUIStuff/Elements/TextBox.impl.hpp @@ -8,7 +8,7 @@ template TextBox::TextBox(GUIManager& gui): Element(gui) {} template void TextBox::layout(const Clay_ElementId& id, const TextBoxData& userInfo) { auto& io = gui.io; this->userInfo = userInfo; - init_textbox(io); + init_textbox(io, false); CLAY(id, { .layout = { .sizing = {.width = CLAY_SIZING_GROW(static_cast(io.fontSize * 2)), .height = CLAY_SIZING_FIXED(static_cast(io.fontSize * 1.25f))} @@ -72,8 +72,8 @@ template bool TextBox::input_mouse_button_callback(const InputMa else if(isSelected) { gui.io.input->remove_rich_text_box_input(textbox); if(update_data()) - gui.set_post_callback_func(userInfo.onEdit); - init_textbox(gui.io); + gui.set_post_callback_func_high_priority(userInfo.onEdit); + init_textbox(gui.io, true); isSelected = false; } } @@ -91,31 +91,37 @@ template bool TextBox::input_mouse_motion_callback(const InputMa } template void TextBox::input_key_callback(const InputManager::KeyCallbackArgs& key) { - if(key.key == InputManager::KEY_TEXT_ENTER && key.down && userInfo.onEnter) { + if(key.key == InputManager::KEY_TEXT_ENTER && key.down && isSelected) { bool success = update_data(); - gui.set_post_callback_func([&, success] { + gui.set_post_callback_func_high_priority([&, success] { if(success && userInfo.onEdit) userInfo.onEdit(); - userInfo.onEnter(); + if(userInfo.onEnter) userInfo.onEnter(); + init_textbox(gui.io, true); }); } } -template void TextBox::init_textbox(UpdateInputData& io) { - textbox = std::make_shared(); - textbox->set_width(std::numeric_limits::max()); - textbox->set_font_data(io.fonts); - textbox->set_allow_newlines(false); +template void TextBox::init_textbox(UpdateInputData& io, bool forceTextUpdate) { + if(!textbox) { + textbox = std::make_shared(); + textbox->set_width(std::numeric_limits::max()); + textbox->set_font_data(io.fonts); + textbox->set_allow_newlines(false); + cur = std::make_shared(); + rect = std::make_shared>(); + } - cur = std::make_shared(); - rect = std::make_shared>(); + if(forceTextUpdate || !oldData.has_value() || oldData.value() != *userInfo.data) { + textbox->clear_text(); + cur->pos = cur->selectionBeginPos = cur->selectionEndPos = textbox->insert({0, 0}, userInfo.toStr(*userInfo.data)); + } - cur->pos = cur->selectionBeginPos = cur->selectionEndPos = textbox->insert({0, 0}, userInfo.toStr(*userInfo.data)); textbox->onUserTextEdit = [&] { if(userInfo.immutable) cur->pos = cur->selectionBeginPos = cur->selectionEndPos = textbox->insert({0, 0}, userInfo.toStr(*userInfo.data)); else { if(update_data() && userInfo.onEdit) - gui.set_post_callback_func(userInfo.onEdit); + gui.set_post_callback_func_high_priority(userInfo.onEdit); } }; } @@ -124,6 +130,7 @@ template bool TextBox::update_data() { std::optional dataToAssign = userInfo.fromStr(textbox->get_string()); if(dataToAssign.has_value()) { *userInfo.data = dataToAssign.value(); + oldData = dataToAssign; return true; } return false;