Skip to content

Commit

Permalink
move non-uilist history handling to imgui callback
Browse files Browse the repository at this point in the history
  • Loading branch information
mqrause committed Nov 16, 2024
1 parent 8ac17ea commit ca626ca
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
52 changes: 40 additions & 12 deletions src/input_popup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<string_input_popup_imgui *>( 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 )
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/input_popup.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<char, 256> text;
// currently entered input for history handling
Expand Down

0 comments on commit ca626ca

Please sign in to comment.