From a8b2c1dc224c21f9ba37c0ec2f5a32e0890ff532 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Fri, 15 Mar 2024 20:13:46 +0100 Subject: move import export dialogs --- src/export.cpp | 50 +++++++++++++++++++++++++++++++ src/export.h | 3 +- src/import.cpp | 63 ++++++++++++++++++++++++++++++++++++++ src/import.h | 3 +- src/main.cpp | 95 ++-------------------------------------------------------- 5 files changed, 119 insertions(+), 95 deletions(-) (limited to 'src') diff --git a/src/export.cpp b/src/export.cpp index 5eeab36..67388fb 100644 --- a/src/export.cpp +++ b/src/export.cpp @@ -1,6 +1,7 @@ #include "export.h" #include "array.h" #include "config.h" +#include "../imfiledialog/ImFileDialog.h" #include export_result last_export_result = EXPORT_NONE; @@ -277,4 +278,53 @@ export_result ts_export_result(ts_search_result* result, const utf8_int8_t* path ts_thread_start(_ts_export_thread, args); return EXPORT_NONE; +} + +void ts_create_export_popup(int window_w, int window_h) { + // File exporting. + if (ifd::FileDialog::Instance().IsDone("FileSaveAsDialog", window_w, window_h)) { + if (ifd::FileDialog::Instance().HasResult()) { + std::string res = ifd::FileDialog::Instance().GetResult().u8string(); + last_export_result = ts_export_result(current_search_result, (const utf8_int8_t *)res.c_str()); + utf8ncpy(save_path, (const utf8_int8_t *)res.c_str(), sizeof(save_path)); + + // Set titlebar name. + utf8_int8_t new_name[MAX_INPUT_LENGTH]; + snprintf(new_name, MAX_INPUT_LENGTH, "Text-Search > %s", res.c_str()); + ts_platform_set_window_title(new_name); + } + 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, 20}); + ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f); + if (ImGui::Button("Close")) { + last_export_result = EXPORT_NONE; + ImGui::CloseCurrentPopup(); + } + ImGui::PopStyleVar(); + + ImGui::EndPopup(); + } } \ No newline at end of file diff --git a/src/export.h b/src/export.h index d440997..9f6c4c1 100644 --- a/src/export.h +++ b/src/export.h @@ -13,4 +13,5 @@ typedef enum t_export_result { extern export_result last_export_result; bool ts_str_has_extension(const utf8_int8_t *str, const utf8_int8_t *suffix); -export_result ts_export_result(ts_search_result* result, const utf8_int8_t* path); \ No newline at end of file +export_result ts_export_result(ts_search_result* result, const utf8_int8_t* path); +void ts_create_export_popup(int window_w, int window_h); \ No newline at end of file diff --git a/src/import.cpp b/src/import.cpp index 4cb1a97..d6bc09a 100644 --- a/src/import.cpp +++ b/src/import.cpp @@ -1,6 +1,7 @@ #include "import.h" #include "search.h" #include "export.h" +#include "../imfiledialog/ImFileDialog.h" #include #include @@ -125,6 +126,16 @@ static void* _ts_import_thread(void* args) { arg->result->done_finding_files = true; arg->result->search_completed = true; + + // Destroy previous result. + if (arg->result->prev_result) { + while (!arg->result->prev_result->search_completed || arg->result->prev_result->is_saving) { + ts_thread_sleep(10); + } + ts_destroy_result(arg->result->prev_result); + arg->result->prev_result = NULL; + } + free(arg); return 0; @@ -135,6 +146,10 @@ ts_search_result* ts_import_result(const utf8_int8_t* path) { res->done_finding_files = false; res->search_completed = false; res->cancel_search = false; + + if (res->prev_result) res->prev_result->cancel_search = true; + + current_search_result = res; // set this now because old result will be destroyed in import thread. struct t_import_thread_args* args = (struct t_import_thread_args*)malloc(sizeof(struct t_import_thread_args)); if (!args) exit_oom(); @@ -144,4 +159,52 @@ ts_search_result* ts_import_result(const utf8_int8_t* path) { ts_thread_start(_ts_import_thread, args); return res; +} + +void ts_create_import_popup(int window_w, int window_h) { + // File importing dialog. + if (ifd::FileDialog::Instance().IsDone("FileOpenDialog", window_w, window_h)) { + if (ifd::FileDialog::Instance().HasResult()) { + std::string res = ifd::FileDialog::Instance().GetResult().u8string(); + utf8ncpy(save_path, (const utf8_int8_t *)res.c_str(), sizeof(save_path)); + + // Set titlebar name. + utf8_int8_t new_name[MAX_INPUT_LENGTH]; + snprintf(new_name, MAX_INPUT_LENGTH, "Text-Search > %s", res.c_str()); + ts_platform_set_window_title(new_name); + + current_search_result = ts_import_result(save_path); + } + ifd::FileDialog::Instance().Close(); + } + + if (last_import_result != EXPORT_NONE) { + ImGui::OpenPopup("Import 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)); + } + + // import error popup + if (ImGui::BeginPopupModal("Import Failed", (bool*)&last_import_result, ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove)) { + ImGui::SetWindowSize({300, 0}); + + switch (last_import_result) + { + case IMPORT_INVALID_DATA: ImGui::Text("File has invalid format"); break; + case IMPORT_INVALID_VERSION: ImGui::Text("File has unknown version"); break; + case IMPORT_FILE_ERROR: ImGui::Text("Failed to open file"); break; + default: + break; + } + + ImGui::Dummy({0, 20}); + ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f); + if (ImGui::Button("Close")) { + last_import_result = IMPORT_NONE; + ImGui::CloseCurrentPopup(); + } + ImGui::PopStyleVar(); + + ImGui::EndPopup(); + } } \ No newline at end of file diff --git a/src/import.h b/src/import.h index a3fedf9..0b163d0 100644 --- a/src/import.h +++ b/src/import.h @@ -11,4 +11,5 @@ typedef enum t_import_result { extern import_result last_import_result; -ts_search_result* ts_import_result(const utf8_int8_t* path); \ No newline at end of file +ts_search_result* ts_import_result(const utf8_int8_t* path); +void ts_create_import_popup(int window_w, int window_h); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index c06fa2b..ee73d7e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -178,99 +178,8 @@ static int _ts_create_menu(int window_w, int window_h) { ImGui::PopStyleColor(); _ts_create_popups(); - - // File exporting. - if (ifd::FileDialog::Instance().IsDone("FileSaveAsDialog", window_w, window_h)) { - if (ifd::FileDialog::Instance().HasResult()) { - std::string res = ifd::FileDialog::Instance().GetResult().u8string(); - last_export_result = ts_export_result(current_search_result, (const utf8_int8_t *)res.c_str()); - utf8ncpy(save_path, (const utf8_int8_t *)res.c_str(), sizeof(save_path)); - - // Set titlebar name. - utf8_int8_t new_name[MAX_INPUT_LENGTH]; - snprintf(new_name, MAX_INPUT_LENGTH, "Text-Search > %s", res.c_str()); - ts_platform_set_window_title(new_name); - } - ifd::FileDialog::Instance().Close(); - } - - // File importing. - if (ifd::FileDialog::Instance().IsDone("FileOpenDialog", window_w, window_h)) { - if (ifd::FileDialog::Instance().HasResult()) { - std::string res = ifd::FileDialog::Instance().GetResult().u8string(); - utf8ncpy(save_path, (const utf8_int8_t *)res.c_str(), sizeof(save_path)); - - // Set titlebar name. - utf8_int8_t new_name[MAX_INPUT_LENGTH]; - snprintf(new_name, MAX_INPUT_LENGTH, "Text-Search > %s", res.c_str()); - ts_platform_set_window_title(new_name); - - current_search_result = ts_import_result(save_path); - } - ifd::FileDialog::Instance().Close(); - } - - - if (last_import_result != EXPORT_NONE) { - ImGui::OpenPopup("Import 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)); - } - - // import error popup - if (ImGui::BeginPopupModal("Import Failed", (bool*)&last_import_result, ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove)) { - ImGui::SetWindowSize({300, 0}); - - switch (last_import_result) - { - case IMPORT_INVALID_DATA: ImGui::Text("File has invalid format"); break; - case IMPORT_INVALID_VERSION: ImGui::Text("File has unknown version"); break; - case IMPORT_FILE_ERROR: ImGui::Text("Failed to open file"); break; - default: - break; - } - - ImGui::Dummy({0, 20}); - ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f); - if (ImGui::Button("Close")) { - last_import_result = IMPORT_NONE; - ImGui::CloseCurrentPopup(); - } - ImGui::PopStyleVar(); - - ImGui::EndPopup(); - } - - 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, 20}); - ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f); - if (ImGui::Button("Close")) { - last_export_result = EXPORT_NONE; - ImGui::CloseCurrentPopup(); - } - ImGui::PopStyleVar(); - - ImGui::EndPopup(); - } + ts_create_import_popup(window_w, window_h); + ts_create_export_popup(window_w, window_h); return menu_bar_h + 15; } -- cgit v1.2.3-70-g09d2