diff options
| author | Aldrik Ramaekers <aldrikboy@gmail.com> | 2025-10-26 08:27:48 +0100 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrikboy@gmail.com> | 2025-10-26 08:27:48 +0100 |
| commit | 5abb2cbd8f201b8a8101a661f1dd9a68412d8674 (patch) | |
| tree | d11d1483100ed89eabf37efaaf3fae16df3cc4e6 /src/ui | |
| parent | 8df1486ca41edb625feb269fe7f7997fa5ffacfe (diff) | |
refactor ui, fix autocomplete bug
Diffstat (limited to 'src/ui')
| -rw-r--r-- | src/ui/imgui_extensions.cpp | 66 | ||||
| -rw-r--r-- | src/ui/ui_contacts.cpp | 8 | ||||
| -rw-r--r-- | src/ui/ui_expenses.cpp | 7 | ||||
| -rw-r--r-- | src/ui/ui_invoices.cpp | 5 | ||||
| -rw-r--r-- | src/ui/ui_main.cpp | 1 | ||||
| -rw-r--r-- | src/ui/ui_settings.cpp | 4 |
6 files changed, 69 insertions, 22 deletions
diff --git a/src/ui/imgui_extensions.cpp b/src/ui/imgui_extensions.cpp index b3b20f8..494cfc8 100644 --- a/src/ui/imgui_extensions.cpp +++ b/src/ui/imgui_extensions.cpp @@ -82,18 +82,28 @@ namespace ImGui return result; } - void FormCountryCombo(char* buffer, size_t buf_size) + void FormCountryCombo(char* buffer, size_t buf_size, bool activated_only) { const char* selected_country = 0; float widthAvailable = ImGui::GetContentRegionAvail().x; ImGui::SetNextItemWidth(widthAvailable*0.5f); + u32 country_cursor = 0; const char* countries[300]; + bool enabled_countries[300] = {1}; + for (int x = 0; x < country::get_count(); x++) { char locale_str[20]; strops::format(locale_str, 20, "country.%s", country::get_code_by_index(x)); countries[x] = locale::get(locale_str); + + if (activated_only) { + enabled_countries[x] = country::is_enabled(country::get_code_by_index(x)); + } + else { + enabled_countries[x] = 1; + } } for (int i = 0; i < country::get_count(); i++) @@ -116,10 +126,12 @@ namespace ImGui { for (int n = 0; n < country::get_count(); n++) { - bool is_selected = (selected_country == countries[n]); - if (ImGui::Selectable(countries[n], is_selected)) { - selected_country = countries[n]; - selected_country_index = n; + if (enabled_countries[n]) { + bool is_selected = (selected_country == countries[n]); + if (ImGui::Selectable(countries[n], is_selected)) { + selected_country = countries[n]; + selected_country_index = n; + } } } ImGui::EndCombo(); @@ -151,11 +163,42 @@ namespace ImGui } } + typedef struct + { + const char* id; + bool is_open; + } open_autocomplete_ref; + int TextInputWithAutocomplete(const char* hint, char* buffer, size_t buf_size, char* suggestions[], int suggestion_count, bool has_error) { int result = -1; - static bool is_open = false; + + const u32 max_ref_count = 10; + static open_autocomplete_ref open_refs[max_ref_count] = {0}; + open_autocomplete_ref* found_ref = 0; + { + for (u32 i = 0; i < max_ref_count; i++) { + if (open_refs[i].id == buffer) { + found_ref = &open_refs[i]; + break; + } + } + + if (!found_ref) { + for (u32 i = 0; i < max_ref_count; i++) { + if (open_refs[i].id == 0) { + found_ref = &open_refs[i]; + found_ref->id = buffer; + found_ref->is_open = false; + break; + } + } + } + if (!found_ref) assert(0); + } + + bool is_open = found_ref->is_open; if (has_error) { ImGui::PushStyleColor(ImGuiCol_Border, config::colors::COLOR_ERROR_OUTLINE); @@ -172,8 +215,10 @@ namespace ImGui } - if (buffer[0] == '\0' && is_open) is_open = false; - if (suggestion_count == 0 && is_open) is_open = false; + 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) @@ -192,7 +237,8 @@ namespace ImGui (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; + if (mouse_clicked_outside) + is_open = false; for (int i = 0; i < suggestion_count; ++i) { @@ -211,6 +257,8 @@ namespace ImGui ImGui::EndChild(); } } + + found_ref->is_open = is_open; return result; } diff --git a/src/ui/ui_contacts.cpp b/src/ui/ui_contacts.cpp index ff1f424..4e15bb1 100644 --- a/src/ui/ui_contacts.cpp +++ b/src/ui/ui_contacts.cpp @@ -28,7 +28,7 @@ static contact selected_for_removal; static contact active_contact; -void draw_address_form(address* buffer, a_err last_err) +void draw_address_form(address* buffer, a_err last_err, bool active_countries_only = false) { ImGui::FormInputTextWithErrorHint(locale::get("contact.form.address1"), buffer->address1, IM_ARRAYSIZE(buffer->address1), last_err & A_ERR_MISSING_ADDRESS1); ImGui::FormInputTextWithErrorHint(locale::get("contact.form.address2"), buffer->address2, IM_ARRAYSIZE(buffer->address2), 0); @@ -36,7 +36,7 @@ void draw_address_form(address* buffer, a_err last_err) ImGui::FormInputTextWithErrorHint(locale::get("contact.form.postal"), buffer->postal, IM_ARRAYSIZE(buffer->postal), last_err & A_ERR_MISSING_POSTAL); ImGui::FormInputTextWithErrorHint(locale::get("contact.form.region"), buffer->region, IM_ARRAYSIZE(buffer->region), 0); - ImGui::FormCountryCombo(buffer->country_code, IM_ARRAYSIZE(buffer->country_code)); + ImGui::FormCountryCombo(buffer->country_code, IM_ARRAYSIZE(buffer->country_code), active_countries_only); } void ui::setup_contacts() @@ -65,7 +65,7 @@ void draw_addressee_form_ex(delivery_info* buffer, bool viewing_only = false) ImGui::PopID(); } -void draw_contact_form_ex(contact* buffer, bool viewing_only = false, bool with_autocomplete = false) +void draw_contact_form_ex(contact* buffer, bool viewing_only = false, bool with_autocomplete = false, bool active_countries_only = false) { a_err last_err = administration::contact_is_valid(*buffer); @@ -79,7 +79,7 @@ void draw_contact_form_ex(contact* buffer, bool viewing_only = false, bool with_ if (with_autocomplete) ImGui::FormContactAutocomplete(buffer, last_err & A_ERR_MISSING_NAME); else ImGui::FormInputTextWithErrorHint(locale::get("contact.form.fullname"), buffer->name, IM_ARRAYSIZE(buffer->name), last_err & A_ERR_MISSING_NAME); - draw_address_form(&buffer->address, last_err); + draw_address_form(&buffer->address, last_err, active_countries_only); ImGui::FormContactTypeCombo(&buffer->type); diff --git a/src/ui/ui_expenses.cpp b/src/ui/ui_expenses.cpp index 6a4eebd..e09db5b 100644 --- a/src/ui/ui_expenses.cpp +++ b/src/ui/ui_expenses.cpp @@ -35,12 +35,11 @@ static invoice selected_for_removal = {0}; static billing_item* invoice_items_buffer = 0; void draw_addressee_form_ex(delivery_info* buffer, bool viewing_only = false); -void draw_contact_form_ex(contact* buffer, bool viewing_only = false, bool with_autocomplete = false); +void draw_contact_form_ex(contact* buffer, bool viewing_only, bool with_autocomplete, bool active_countries_only); void draw_invoice_items_form(invoice* invoice, bool outgoing = true); void ui::destroy_expenses() { - administration::invoice_destroy(&active_invoice); memops::unalloc(invoice_items_buffer); } @@ -99,12 +98,12 @@ static void draw_expense_form(invoice* buffer, bool viewing_only = false) ImGui::Separator(); ImGui::Text(locale::get("invoice.form.billinginformation")); - draw_contact_form_ex(&buffer->customer, false, true); + draw_contact_form_ex(&buffer->customer, false, true, false); ImGui::Separator(); ImGui::Text(locale::get("invoice.form.supplier")); - draw_contact_form_ex(&buffer->supplier, false, true); + draw_contact_form_ex(&buffer->supplier, false, true, false); ImGui::Checkbox(locale::get("invoice.form.triangulation"), &buffer->is_triangulation); if (buffer->is_triangulation) { diff --git a/src/ui/ui_invoices.cpp b/src/ui/ui_invoices.cpp index d932bfd..4ae325f 100644 --- a/src/ui/ui_invoices.cpp +++ b/src/ui/ui_invoices.cpp @@ -32,12 +32,11 @@ static invoice selected_for_removal = {0}; static billing_item* invoice_items_buffer = 0; -void draw_contact_form_ex(contact* buffer, bool viewing_only = false, bool with_autocomplete = false); +void draw_contact_form_ex(contact* buffer, bool viewing_only, bool with_autocomplete, bool active_countries_only); void draw_addressee_form_ex(delivery_info* buffer, bool viewing_only = false); void ui::destroy_invoices() { - administration::invoice_destroy(&active_invoice); memops::unalloc(invoice_items_buffer); } @@ -211,7 +210,7 @@ static void draw_invoice_form(invoice* buffer, bool viewing_only = false) ImGui::Separator(); ImGui::Text(locale::get("invoice.form.billinginformation")); - draw_contact_form_ex(&buffer->customer, false, true); + draw_contact_form_ex(&buffer->customer, false, true, false); ImGui::Checkbox(locale::get("invoice.form.triangulation"), &buffer->is_triangulation); if (buffer->is_triangulation) { diff --git a/src/ui/ui_main.cpp b/src/ui/ui_main.cpp index 84dab1b..48b1364 100644 --- a/src/ui/ui_main.cpp +++ b/src/ui/ui_main.cpp @@ -74,7 +74,6 @@ void ui::draw_main() { if (ImGui::MenuItem("New")) { administration_reader::open_new(); } if (ImGui::MenuItem("Open")) { administration_reader::open_existing(NULL); } - if (ImGui::MenuItem("Save")) { administration_reader::save_new(); } ImGui::EndMenu(); } diff --git a/src/ui/ui_settings.cpp b/src/ui/ui_settings.cpp index 2a310d3..06409b7 100644 --- a/src/ui/ui_settings.cpp +++ b/src/ui/ui_settings.cpp @@ -325,6 +325,8 @@ static void draw_services() } } +void draw_contact_form_ex(contact* buffer, bool viewing_only, bool with_autocomplete, bool active_countries_only); + void ui::draw_settings() { if (ImGui::BeginTabBar("SettingsTabBar")) @@ -332,7 +334,7 @@ void ui::draw_settings() if (ImGui::BeginTabItem(locale::get("settings.table.company"), nullptr, select_company_tab == 1 ? ImGuiTabItemFlags_SetSelected : 0)) { select_company_tab = 0; - draw_contact_form(&company_info); + draw_contact_form_ex(&company_info, false, false, true); // Save button. bool can_save = administration::contact_is_valid(company_info) == A_ERR_SUCCESS; |
