#include "ui.hpp" #include "imgui.h" #include "locales.hpp" #include "strops.hpp" void ui_helper_draw_required_tag() { ImDrawList* draw_list = ImGui::GetWindowDrawList(); const char* text = localize("form.required"); ImVec2 text_pos = ImGui::GetCursorScreenPos(); ImVec2 text_size = ImGui::CalcTextSize(text); text_pos.y += text_size.y/4.0f; ImVec4 bg_color = ImVec4(0.9f, 0.235f, 0.235f, 0.4f); // Red background ImVec4 text_color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); // White text float rounding = 2.0f; float padding = 2.0f; // Background rectangle ImVec2 bg_min = ImVec2(text_pos.x - padding, text_pos.y - padding); ImVec2 bg_max = ImVec2(text_pos.x + text_size.x + padding, text_pos.y + text_size.y + padding); draw_list->AddRectFilled(bg_min, bg_max, ImColor(bg_color), rounding); // Foreground text ImGui::PushStyleColor(ImGuiCol_Text, text_color); ImGui::TextUnformatted(text); ImGui::PopStyleColor(); } int TextInputWithAutocomplete(const char* label, const char* hint, char* buffer, size_t buf_size, char* suggestions[], int suggestion_count) { int result = -1; static bool is_open = false; ImGui::InputTextWithHint(label, hint, buffer, buf_size); if (buffer[0] == '\0' && is_open) is_open = false; if (suggestion_count == 0 && is_open) is_open = false; bool is_active = ImGui::IsItemActive(); if (is_active && buffer[0] != '\0' && suggestion_count > 0) { is_open = true; } if (is_open) { ImGui::BeginChild("autocomplete_popup", ImVec2(0, 10.0f + suggestion_count*23.0f), 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) { ImGui::PushID(i); if (ImGui::Selectable(suggestions[i])) { // Copy selected suggestion to buffer strops_copy(buffer, suggestions[i], buf_size); buffer[buf_size - 1] = '\0'; result = i; is_open = false; } ImGui::PopID(); } ImGui::EndChild(); } } return result; }