From ecba8563968e328845a2feedd8213d36c289aecc Mon Sep 17 00:00:00 2001 From: mqrause Date: Sun, 17 Nov 2024 00:48:37 +0100 Subject: [PATCH] move non-uilist history handling to imgui callback --- src/input_popup.cpp | 52 ++++++++++++++++++++++++++++++++++----------- src/input_popup.h | 2 +- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/input_popup.cpp b/src/input_popup.cpp index 3a1110bc3643d..ace3fb18f51d1 100644 --- a/src/input_popup.cpp +++ b/src/input_popup.cpp @@ -129,9 +129,30 @@ string_input_popup_imgui::string_input_popup_imgui( const std::string &title, in set_text( old_input ); } +static int history_callback( ImGuiInputTextCallbackData *data ) +{ + string_input_popup_imgui *popup = static_cast( data->UserData ); + + if( data->EventFlag == ImGuiInputTextFlags_CallbackHistory ) { + popup->update_input_history( data ); + } + + return 0; +} + void string_input_popup_imgui::draw_input_control() { - ImGui::InputText( "##string_input", text.data(), max_input_length ); + ImGuiInputTextCallback callback = nullptr; + void *data = nullptr; + ImGuiInputTextFlags flags = ImGuiInputTextFlags_None; + + if( !is_uilist_history ) { + callback = history_callback; + data = this; + flags |= ImGuiInputTextFlags_CallbackHistory; + } + + ImGui::InputText( "##string_input", text.data(), max_input_length, flags, callback, data ); } void string_input_popup_imgui::set_identifier( const std::string &ident ) @@ -185,8 +206,20 @@ void string_input_popup_imgui::show_history() } } -void string_input_popup_imgui::update_input_history( bool up ) +static void set_text( ImGuiInputTextCallbackData *data, const std::string &text ) { + data->DeleteChars( 0, data->BufTextLen ); + data->InsertChars( 0, text.c_str() ); +} + +void string_input_popup_imgui::update_input_history( ImGuiInputTextCallbackData *data ) +{ + if( data->EventKey != ImGuiKey_UpArrow && data->EventKey != ImGuiKey_DownArrow ) { + return; + } + + bool up = data->EventKey == ImGuiKey_UpArrow; + if( identifier.empty() ) { return; } @@ -214,7 +247,7 @@ void string_input_popup_imgui::update_input_history( bool up ) } } else { if( history_index == 1 ) { - set_text( session_input ); + ::set_text( data, session_input ); //show initial string entered and 'return' history_index = 0; return; @@ -224,7 +257,7 @@ void string_input_popup_imgui::update_input_history( bool up ) } history_index += up ? 1 : -1; - set_text( hist[hist.size() - history_index] ); + ::set_text( data, hist[hist.size() - history_index] ); } void string_input_popup_imgui::add_to_history( const std::string &value ) const @@ -255,14 +288,9 @@ std::string string_input_popup_imgui::query() return txt; } else if( action == "TEXT.QUIT" ) { break; - } else if( action == "TEXT.UP" ) { - if( is_uilist_history ) { - show_history(); - } else { - update_input_history( true ); - } - } else if( action == "TEXT.DOWN" && !is_uilist_history ) { - update_input_history( false ); + } else if( action == "TEXT.UP" && is_uilist_history ) { + // non-uilist history is handled inside input callback + show_history(); } // mouse click on x to close leads here diff --git a/src/input_popup.h b/src/input_popup.h index 212ec9f360bf4..9ceb3c63bce77 100644 --- a/src/input_popup.h +++ b/src/input_popup.h @@ -80,11 +80,11 @@ class string_input_popup_imgui : public input_popup void set_identifier( const std::string &ident ); void set_text( const std::string &txt ); void use_uilist_history( bool use_uilist ); + void update_input_history( ImGuiInputTextCallbackData *data ); protected: void draw_input_control() override; private: void add_to_history( const std::string &value ) const; - void update_input_history( bool up ); void show_history(); std::array text; // currently entered input for history handling