summaryrefslogtreecommitdiff
path: root/src/ui/helpers.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui/helpers.cpp')
-rw-r--r--src/ui/helpers.cpp45
1 files changed, 44 insertions, 1 deletions
diff --git a/src/ui/helpers.cpp b/src/ui/helpers.cpp
index e4b25c8..a239f4c 100644
--- a/src/ui/helpers.cpp
+++ b/src/ui/helpers.cpp
@@ -1,6 +1,7 @@
#include "ui.hpp"
#include "imgui.h"
#include "locales.hpp"
+#include "strops.hpp"
static float toast_timer = 0.0f;
static const char* toast_msg = nullptr;
@@ -62,4 +63,46 @@ void ui_helper_draw_required_tag()
ImGui::PushStyleColor(ImGuiCol_Text, text_color);
ImGui::TextUnformatted(text);
ImGui::PopStyleColor();
-} \ No newline at end of file
+}
+
+void ui_helper_TextInputWithAutocomplete(const char* label, const char* hint, char* buffer, size_t buf_size,
+ char* suggestions[], int suggestion_count)
+{
+ static bool is_open = false;
+ ImGui::InputTextWithHint(label, hint, buffer, buf_size);
+
+ bool is_active = ImGui::IsItemActive();
+ if (is_active && buffer[0] != '\0')
+ {
+ is_open = true;
+ }
+
+ if (is_open) {
+ ImGui::BeginChild("autocomplete_popup", ImVec2(0, 100), true);
+ {
+ ImVec2 win_pos = ImGui::GetWindowPos();
+ ImVec2 win_size = ImGui::GetWindowSize();
+ ImVec2 mouse_pos = ImGui::GetMousePos();
+
+ bool mouse_clicked_outside = !is_active && ImGui::IsMouseClicked(0) &&
+ (mouse_pos.x < win_pos.x || mouse_pos.x > win_pos.x + win_size.x ||
+ mouse_pos.y < win_pos.y || mouse_pos.y > win_pos.y + win_size.y);
+
+ if (mouse_clicked_outside) is_open = false;
+
+ for (int i = 0; i < suggestion_count; ++i)
+ {
+ if (ImGui::Selectable(suggestions[i]))
+ {
+ // Copy selected suggestion to buffer
+ strops_copy(buffer, suggestions[i], buf_size);
+ buffer[buf_size - 1] = '\0';
+
+ is_open = false;
+ }
+ }
+
+ ImGui::EndChild();
+ }
+ }
+}