impr: Make unsaved changes popup behave more like in other applications

This commit is contained in:
WerWolv
2024-04-23 21:01:58 +02:00
parent 7ec245925a
commit fd61e757f0
9 changed files with 103 additions and 27 deletions

View File

@@ -10,6 +10,7 @@
#include <hex/ui/view.hpp>
#include <imgui.h>
#include <content/global_actions.hpp>
#include <content/providers/file_provider.hpp>
@@ -80,17 +81,30 @@ namespace hex::plugin::builtin {
if (provider->isDirty()) {
*shouldClose = false;
PopupUnsavedChanges::open("hex.builtin.popup.close_provider.desc"_lang,
[]{
for (const auto &provider : ImHexApi::Provider::impl::getClosingProviders())
ImHexApi::Provider::remove(provider, true);
[]{
const bool projectSaved = ProjectFile::hasPath() ? saveProject() : saveProjectAs();
if (projectSaved) {
for (const auto &provider : ImHexApi::Provider::impl::getClosingProviders())
ImHexApi::Provider::remove(provider, true);
if (imhexClosing)
ImHexApi::System::closeImHex(true);
},
[] {
ImHexApi::Provider::impl::resetClosingProvider();
imhexClosing = false;
}
if (imhexClosing)
ImHexApi::System::closeImHex(true);
} else {
ImHexApi::Provider::impl::resetClosingProvider();
imhexClosing = false;
}
},
[] {
for (const auto &provider : ImHexApi::Provider::impl::getClosingProviders())
ImHexApi::Provider::remove(provider, true);
if (imhexClosing)
ImHexApi::System::closeImHex(true);
},
[] {
ImHexApi::Provider::impl::resetClosingProvider();
imhexClosing = false;
}
);
}
});

View File

@@ -1,4 +1,5 @@
#include <content/global_actions.hpp>
#include <hex/ui/view.hpp>
#include <hex/api/project_file_manager.hpp>
#include <hex/helpers/logger.hpp>
@@ -18,18 +19,25 @@ namespace hex::plugin::builtin {
});
}
void saveProject() {
if (ImHexApi::Provider::isValid() && ProjectFile::hasPath()) {
bool saveProject() {
if (!ImHexApi::Provider::isValid())
return false;
if (ProjectFile::hasPath()) {
if (!ProjectFile::store()) {
ui::ToastError::open("hex.builtin.popup.error.project.save"_lang);
return false;
} else {
log::debug("Project saved");
return true;
}
} else {
return saveProjectAs();
}
}
void saveProjectAs() {
fs::openFileBrowser(fs::DialogMode::Save, { {"Project File", "hexproj"} },
bool saveProjectAs() {
return fs::openFileBrowser(fs::DialogMode::Save, { {"Project File", "hexproj"} },
[](std::fs::path path) {
if (path.extension() != ".hexproj") {
path.replace_extension(".hexproj");