From 004d94f1f62f9a4ac4e8aa7999b71ea255d2de69 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Tue, 12 Mar 2024 19:10:11 +0100 Subject: show failed export popup --- src/export.cpp | 45 +++++++++++++++++++++++++++++++++------------ src/export.h | 9 ++++++++- src/main.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++----- src/search.cpp | 2 +- 4 files changed, 86 insertions(+), 19 deletions(-) diff --git a/src/export.cpp b/src/export.cpp index 4fc390d..ba6e5db 100644 --- a/src/export.cpp +++ b/src/export.cpp @@ -29,6 +29,7 @@ static bool _str_has_extension(const utf8_int8_t *str, const utf8_int8_t *suffix static utf8_int8_t* _json_escape_str(utf8_int8_t* str, utf8_int8_t* buffer, size_t buffer_size) { memset(buffer, 0, buffer_size); + if (str == NULL) return buffer; utf8_int8_t* buffer_orig = buffer; utf8_int32_t ch; @@ -136,6 +137,7 @@ static bool _ts_export_csv(ts_search_result* result, const utf8_int8_t* path) { static utf8_int8_t* _xml_escape_str(utf8_int8_t* str, utf8_int8_t* buffer, size_t buffer_size) { memset(buffer, 0, buffer_size); + if (str == NULL) return buffer; utf8_int8_t* buffer_orig = buffer; utf8_int32_t ch; @@ -199,22 +201,41 @@ static bool _ts_export_xml(ts_search_result* result, const utf8_int8_t* path) { return true; } -bool ts_export_result(ts_search_result* result, const utf8_int8_t* path) { - if (result == NULL || path == NULL) return false; - if (!result->search_completed) return false; - result->is_saving = true; +struct t_export_thread_args { + ts_search_result* result; + const utf8_int8_t* path; +}; + +static void* _ts_export_thread(void* args) { + struct t_export_thread_args* arg = (struct t_export_thread_args*)args; - if (_str_has_extension(path, ".json")) { - return _ts_export_json(result, path); + if (_str_has_extension(arg->path, ".json")) { + _ts_export_json(arg->result, arg->path); } - if (_str_has_extension(path, ".csv")) { - return _ts_export_csv(result, path); + if (_str_has_extension(arg->path, ".csv")) { + _ts_export_csv(arg->result, arg->path); } - if (_str_has_extension(path, ".xml")) { - return _ts_export_xml(result, path); + if (_str_has_extension(arg->path, ".xml")) { + _ts_export_xml(arg->result, arg->path); } - result->is_saving = false; + arg->result->is_saving = false; + free(arg); + + return 0; +} + +export_result ts_export_result(ts_search_result* result, const utf8_int8_t* path) { + if (result == NULL || path == NULL) return EXPORT_NO_RESULT; + if (!result->search_completed) return EXPORT_SEARCH_ACTIVE; + if (result->is_saving) return EXPORT_SAVE_PENDING; + result->is_saving = true; + + struct t_export_thread_args* args = (struct t_export_thread_args*)malloc(sizeof(struct t_export_thread_args)); + args->result = result; + args->path = path; + + ts_thread_start(_ts_export_thread, args); - return false; + return EXPORT_NONE; } \ No newline at end of file diff --git a/src/export.h b/src/export.h index 1e7f824..ad83c77 100644 --- a/src/export.h +++ b/src/export.h @@ -3,4 +3,11 @@ #include "search.h" #include "platform.h" -bool ts_export_result(ts_search_result* result, const utf8_int8_t* path); \ No newline at end of file +typedef enum ts_export_result { + EXPORT_NONE, + EXPORT_NO_RESULT, + EXPORT_SEARCH_ACTIVE, + EXPORT_SAVE_PENDING, +} export_result; + +export_result ts_export_result(ts_search_result* result, const utf8_int8_t* path); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index e1bb34f..03c4010 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,6 +16,7 @@ // Popups bool open_settings_window = false; bool open_about_window = false; +export_result last_export_result = EXPORT_NONE; const char* help_text = "1. Search directory\n" @@ -145,7 +146,7 @@ static int _ts_create_menu(int window_w, int window_h) { if (strlen(save_path) == 0) ifd::FileDialog::Instance().Save("FileSaveAsDialog", "Save results to file", "File (*.csv;*.json;*.xml){.csv,.json,.xml}"); else - ts_export_result(current_search_result, (const utf8_int8_t *)save_path); + last_export_result = ts_export_result(current_search_result, (const utf8_int8_t *)save_path); } if (ImGui::MenuItem("Save As")) { ifd::FileDialog::Instance().Save("FileSaveAsDialog", "Save results to file", "File (*.csv;*.json;*.xml){.csv,.json,.xml}"); @@ -179,7 +180,8 @@ static int _ts_create_menu(int window_w, int window_h) { if (ifd::FileDialog::Instance().IsDone("FileSaveAsDialog", window_w, window_h)) { if (ifd::FileDialog::Instance().HasResult()) { std::string res = ifd::FileDialog::Instance().GetResult().u8string(); - ts_export_result(current_search_result, (const utf8_int8_t *)res.c_str()); + last_export_result = ts_export_result(current_search_result, (const utf8_int8_t *)res.c_str()); + printf("%d\n", last_export_result); utf8ncpy(save_path, (const utf8_int8_t *)res.c_str(), sizeof(save_path)); // Set titlebar name. @@ -190,6 +192,37 @@ static int _ts_create_menu(int window_w, int window_h) { ifd::FileDialog::Instance().Close(); } + if (last_export_result != EXPORT_NONE) { + ImGui::OpenPopup("Export Failed"); + ImGuiIO& io = ImGui::GetIO(); + ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x * 0.5f, io.DisplaySize.y * 0.5f), ImGuiCond_Always, ImVec2(0.5f,0.5f)); + } + + // export error popup + if (ImGui::BeginPopupModal("Export Failed", (bool*)&last_export_result, ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove)) { + ImGui::SetWindowSize({300, 0}); + + switch (last_export_result) + { + case EXPORT_NO_RESULT: ImGui::Text("No results to export"); break; + case EXPORT_SEARCH_ACTIVE: ImGui::Text("Can't export while save is active"); break; + case EXPORT_SAVE_PENDING: ImGui::Text("Export is pending"); break; + + default: + break; + } + + ImGui::Dummy({0, 70}); + ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f); + if (ImGui::Button("Close")) { + last_export_result = EXPORT_NONE; + ImGui::CloseCurrentPopup(); + } + ImGui::PopStyleVar(); + + ImGui::EndPopup(); + } + return menu_bar_h; } @@ -520,9 +553,15 @@ void ts_create_gui(int window_w, int window_h) { ImGui::SameLine(); - if (current_search_result && current_search_result->search_completed) { - ImGui::SetCursorPosX(window_w - 10.0f - ImGui::CalcTextSize("999.999s elapsed").x); - ImGui::Text("%.3fs elapsed", current_search_result->timestamp/1000.0f); + if (current_search_result) { + if (current_search_result->is_saving) { + ImGui::SetCursorPosX(window_w - 20.0f - ImGui::CalcTextSize("Saving |").x); + ImGui::Text("Saving %c", "|/-\\"[(int)(ImGui::GetTime() / 0.05f) & 3]); + } + else if (current_search_result->search_completed) { + ImGui::SetCursorPosX(window_w - 10.0f - ImGui::CalcTextSize("999.999s elapsed").x); + ImGui::Text("%.3fs elapsed", current_search_result->timestamp/1000.0f); + } } ImGui::EndChild(); ImGui::PopStyleVar(); diff --git a/src/search.cpp b/src/search.cpp index 6762779..9eb3ca4 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -370,7 +370,7 @@ static void *_ts_list_files_thread(void *args) // Use this thread to cleanup previous result. if (info->prev_result) { - while (!info->prev_result->search_completed && !info->prev_result->is_saving) { + while (!info->prev_result->search_completed || info->prev_result->is_saving) { ts_thread_sleep(10); } ts_destroy_result(info->prev_result); -- cgit v1.2.3-70-g09d2