From 679926331763b15e6b82fbbb06e8083e2006fada Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sat, 28 Jan 2023 12:16:41 +0100 Subject: [PATCH] sys: Added a info popup when closing ImHex while tasks are still running --- plugins/builtin/romfs/lang/en_US.json | 4 +++- plugins/builtin/source/content/events.cpp | 7 ++++++ plugins/builtin/source/content/ui_items.cpp | 26 ++++++++++++++++++--- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/plugins/builtin/romfs/lang/en_US.json b/plugins/builtin/romfs/lang/en_US.json index f5485e8f2..6fc3c775b 100644 --- a/plugins/builtin/romfs/lang/en_US.json +++ b/plugins/builtin/romfs/lang/en_US.json @@ -325,7 +325,7 @@ "hex.builtin.popup.close_provider.title": "Close Provider?", "hex.builtin.popup.error.create": "Failed to create new file!", "hex.builtin.popup.error.file_dialog.common": "An error occurred while opening the file browser!", - "hex.builtin.popup.error.file_dialog.portal": "There was an error while opening the file browser. This might be caused by your system not having a xdg-desktop-portal backend installed correctly.\n\nOn KDE, it's xdg-desktop-portal-kde.\nOn Gnome it's xdg-desktop-portal-gnome.\nOn wlroots it's xdg-desktop-portal-wlr.\nOtherwise, you can try to use xdg-desktop-portal-gtk.\n\nReboot your system after installing it.\n\nIf the file browser still doesn't work after this, submit an issue at https://github.com/WerWolv/ImHex/issues\n\nIn the meantime files can still be opened by dragging them onto the ImHex window!", + "hex.builtin.popup.error.file_dialog.portal": "There was an error while opening the file browser.\nThis might be caused by your system not having a xdg-desktop-portal backend installed correctly.\n\nOn KDE, it's xdg-desktop-portal-kde.\nOn Gnome it's xdg-desktop-portal-gnome.\nOn wlroots it's xdg-desktop-portal-wlr.\nOtherwise, you can try to use xdg-desktop-portal-gtk.\n\nReboot your system after installing it.\n\nIf the file browser still doesn't work after this, submit an issue at https://github.com/WerWolv/ImHex/issues\n\nIn the meantime files can still be opened by dragging them onto the ImHex window!", "hex.builtin.popup.error.open": "Failed to open file!", "hex.builtin.popup.error.project.load": "Failed to load project!", "hex.builtin.popup.error.project.save": "Failed to save project!", @@ -333,6 +333,8 @@ "hex.builtin.popup.error.task_exception": "Exception thrown in Task '{}':\n\n{}", "hex.builtin.popup.exit_application.desc": "You have unsaved changes made to your Project.\nAre you sure you want to exit?", "hex.builtin.popup.exit_application.title": "Exit Application?", + "hex.builtin.popup.waiting_for_tasks.title": "Waiting for Tasks", + "hex.builtin.popup.waiting_for_tasks.desc": "There are still tasks running in the background.\nImHex will close after they are finished.", "hex.builtin.provider.disk": "Raw Disk Provider", "hex.builtin.provider.disk.disk_size": "Disk Size", "hex.builtin.provider.disk.reload": "Reload", diff --git a/plugins/builtin/source/content/events.cpp b/plugins/builtin/source/content/events.cpp index 7028fe7b8..6e6fe4c2d 100644 --- a/plugins/builtin/source/content/events.cpp +++ b/plugins/builtin/source/content/events.cpp @@ -32,6 +32,13 @@ namespace hex::plugin::builtin { if (ImHexApi::Provider::isDirty()) { glfwSetWindowShouldClose(window, GLFW_FALSE); TaskManager::doLater([] { ImGui::OpenPopup("hex.builtin.popup.exit_application.title"_lang); }); + } else if (TaskManager::getRunningTaskCount() > 0 || TaskManager::getRunningBackgroundTaskCount() > 0) { + glfwSetWindowShouldClose(window, GLFW_FALSE); + TaskManager::doLater([] { + for (auto &task : TaskManager::getRunningTasks()) + task->interrupt(); + ImGui::OpenPopup("hex.builtin.popup.waiting_for_tasks.title"_lang); + }); } }); diff --git a/plugins/builtin/source/content/ui_items.cpp b/plugins/builtin/source/content/ui_items.cpp index 3d57e155f..f8450ab98 100644 --- a/plugins/builtin/source/content/ui_items.cpp +++ b/plugins/builtin/source/content/ui_items.cpp @@ -31,9 +31,12 @@ namespace hex::plugin::builtin { ImGui::NewLine(); View::confirmButtons("hex.builtin.common.yes"_lang, "hex.builtin.common.no"_lang, - [] { ImHexApi::Common::closeImHex(true); }, - [] { ImGui::CloseCurrentPopup(); } - ); + [] { + ImHexApi::Provider::resetDirty(); + ImHexApi::Common::closeImHex(); + }, + [] { ImGui::CloseCurrentPopup(); } + ); if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_Escape))) ImGui::CloseCurrentPopup(); @@ -58,6 +61,23 @@ namespace hex::plugin::builtin { ImGui::EndPopup(); } + if (ImGui::BeginPopupModal("hex.builtin.popup.waiting_for_tasks.title"_lang, nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove)) { + ImGui::TextUnformatted("hex.builtin.popup.waiting_for_tasks.desc"_lang); + ImGui::Separator(); + + ImGui::NewLine(); + ImGui::SetCursorPosX((ImGui::GetWindowWidth() - ImGui::CalcTextSize("[-]").x) / 2); + ImGui::TextSpinner(""); + ImGui::NewLine(); + + if (TaskManager::getRunningTaskCount() == 0 && TaskManager::getRunningBackgroundTaskCount() == 0) { + ImGui::CloseCurrentPopup(); + ImHexApi::Common::closeImHex(); + } + + ImGui::EndPopup(); + } + auto windowSize = ImHexApi::System::getMainWindowSize(); // Info popup