1
0
mirror of synced 2025-01-29 19:17:28 +01:00

impr: Handle showing popups when closing unsaved providers better

This commit is contained in:
WerWolv 2023-11-07 15:31:54 +01:00
parent 6e6c5c4cb7
commit ef320b74b0
12 changed files with 94 additions and 21 deletions

View File

@ -239,7 +239,7 @@ namespace hex {
namespace impl {
void resetClosingProvider();
prv::Provider* getClosingProvider();
const std::vector<prv::Provider*>& getClosingProviders();
}

View File

@ -224,13 +224,13 @@ namespace hex {
namespace impl {
static prv::Provider *s_closingProvider = nullptr;
static std::vector<prv::Provider*> s_closingProviders;
void resetClosingProvider() {
s_closingProvider = nullptr;
s_closingProviders.clear();
}
prv::Provider* getClosingProvider() {
return s_closingProvider;
const std::vector<prv::Provider*>& getClosingProviders() {
return s_closingProviders;
}
}
@ -302,7 +302,7 @@ namespace hex {
return;
if (!noQuestions) {
impl::s_closingProvider = provider;
impl::s_closingProviders.push_back(provider);
bool shouldClose = true;
EventManager::post<EventProviderClosing>(provider, &shouldClose);

View File

@ -0,0 +1,66 @@
#include <hex/ui/popup.hpp>
#include <hex/api/localization.hpp>
#include <functional>
#include <string>
namespace hex::plugin::builtin {
class PopupUnsavedChanges : public Popup<PopupUnsavedChanges> {
public:
PopupUnsavedChanges(std::string message, std::function<void()> yesFunction, std::function<void()> noFunction)
: hex::Popup<PopupUnsavedChanges>("hex.builtin.common.question", false),
m_message(std::move(message)),
m_yesFunction(std::move(yesFunction)), m_noFunction(std::move(noFunction)) { }
void drawContent() override {
ImGui::TextFormattedWrapped("{}", this->m_message.c_str());
ImGui::NewLine();
if (ImGui::BeginTable("##unsaved_providers", 1, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg, ImVec2(0, ImGui::GetTextLineHeightWithSpacing() * 4))) {
for (const auto &provider : ImHexApi::Provider::impl::getClosingProviders()) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::TextUnformatted(provider->getName().c_str());
}
ImGui::EndTable();
}
ImGui::NewLine();
ImGui::Separator();
auto width = ImGui::GetWindowWidth();
ImGui::SetCursorPosX(width / 9);
if (ImGui::Button("hex.builtin.common.yes"_lang, ImVec2(width / 3, 0))) {
this->m_yesFunction();
this->close();
}
ImGui::SameLine();
ImGui::SetCursorPosX(width / 9 * 5);
if (ImGui::Button("hex.builtin.common.no"_lang, ImVec2(width / 3, 0))) {
this->m_noFunction();
this->close();
}
ImGui::SetWindowPos((ImHexApi::System::getMainWindowSize() - ImGui::GetWindowSize()) / 2, ImGuiCond_Appearing);
}
[[nodiscard]] ImGuiWindowFlags getFlags() const override {
return ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse;
}
[[nodiscard]] ImVec2 getMinSize() const override {
return scaled({ 400, 100 });
}
[[nodiscard]] ImVec2 getMaxSize() const override {
return scaled({ 600, 300 });
}
private:
std::string m_message;
std::function<void()> m_yesFunction, m_noFunction;
};
}

View File

@ -12,7 +12,7 @@ namespace hex::plugin::builtin {
explicit ViewProvider() {
EventManager::subscribe<EventProviderClosing>(this, [this](prv::Provider *provider, bool*) {
if (this->m_provider == provider)
ImHexApi::Provider::remove(this, true);
ImHexApi::Provider::remove(this, false);
});
}
~ViewProvider() override {

View File

@ -447,7 +447,7 @@
"hex.builtin.pl_visualizer.coordinates.query": "Adresse finden",
"hex.builtin.pl_visualizer.coordinates.querying": "Adresse abfragen...",
"hex.builtin.pl_visualizer.coordinates.querying_no_address": "Keine Adresse gefunden",
"hex.builtin.popup.close_provider.desc": "Es wurden ungespeicherte Änderungen an diesem Provider vorgenommen.\nBist du sicher, dass du ihn schliessen willst?",
"hex.builtin.popup.close_provider.desc": "Es wurden ungespeicherte Änderungen an einem oder mehreren Provider vorgenommen.\nBist du sicher, dass du diese schliessen willst?",
"hex.builtin.popup.close_provider.title": "Provider schliessen?",
"hex.builtin.popup.docs_question.title": "Dokumentationsabfrage",
"hex.builtin.popup.docs_question.no_answer": "Die Dokumentation enthielt keine Antwort auf diese Frage",

View File

@ -454,7 +454,7 @@
"hex.builtin.pl_visualizer.coordinates.query": "Find address",
"hex.builtin.pl_visualizer.coordinates.querying": "Querying address...",
"hex.builtin.pl_visualizer.coordinates.querying_no_address": "No address found",
"hex.builtin.popup.close_provider.desc": "There are unsaved changes made to this Provider\nthat haven't been saved to a Project yet.\n\nAre you sure you want to close it?",
"hex.builtin.popup.close_provider.desc": "There are unsaved changes made to one or more Providers\nthat haven't been saved to a Project yet.\n\nAre you sure you want to close them?",
"hex.builtin.popup.close_provider.title": "Close Provider?",
"hex.builtin.popup.docs_question.title": "Documentation query",
"hex.builtin.popup.docs_question.no_answer": "The documentation didn't have an answer for this question",

View File

@ -447,7 +447,7 @@
"hex.builtin.pl_visualizer.coordinates.query": "",
"hex.builtin.pl_visualizer.coordinates.querying": "",
"hex.builtin.pl_visualizer.coordinates.querying_no_address": "",
"hex.builtin.popup.close_provider.desc": "Hay cambios sin guardar en este Proveedor.\nque aún no han sido guardados en un Proyecto.\n\n¿Está seguro de que quiera cerrarlo?",
"hex.builtin.popup.close_provider.desc": "",
"hex.builtin.popup.close_provider.title": "¿Cerrar Proveedor?",
"hex.builtin.popup.docs_question.title": "Búsqueda de Documentación",
"hex.builtin.popup.docs_question.no_answer": "La documentación no tuvo una respuesta esta pregunta",

View File

@ -447,7 +447,7 @@
"hex.builtin.pl_visualizer.coordinates.query": "",
"hex.builtin.pl_visualizer.coordinates.querying": "",
"hex.builtin.pl_visualizer.coordinates.querying_no_address": "",
"hex.builtin.popup.close_provider.desc": "変更がプロジェクトとして保存されていません。\nファイルを閉じてもよろしいですか",
"hex.builtin.popup.close_provider.desc": "",
"hex.builtin.popup.close_provider.title": "タブを閉じますか?",
"hex.builtin.popup.docs_question.title": "",
"hex.builtin.popup.docs_question.no_answer": "",

View File

@ -447,7 +447,7 @@
"hex.builtin.pl_visualizer.coordinates.query": "주소 찾기",
"hex.builtin.pl_visualizer.coordinates.querying": "주소 쿼리 중...",
"hex.builtin.pl_visualizer.coordinates.querying_no_address": "찾은 주소가 없습니다",
"hex.builtin.popup.close_provider.desc": "이 공급자에 대해 아직 프로젝트에\n저장되지 않은 변경 사항이 있습니다.\n\n정말 종료하시겠습니까?",
"hex.builtin.popup.close_provider.desc": "",
"hex.builtin.popup.close_provider.title": "공급자를 종료하시겠습니까?",
"hex.builtin.popup.docs_question.title": "설명서 쿼리",
"hex.builtin.popup.docs_question.no_answer": "설명서에 이 질문에 대한 답변이 없습니다",

View File

@ -447,7 +447,7 @@
"hex.builtin.pl_visualizer.coordinates.query": "查找地址",
"hex.builtin.pl_visualizer.coordinates.querying": "正在查找地址...",
"hex.builtin.pl_visualizer.coordinates.querying_no_address": "找不到地址",
"hex.builtin.popup.close_provider.desc": "有对此提供器做出的未保存的更改。\n你确定要关闭吗",
"hex.builtin.popup.close_provider.desc": "",
"hex.builtin.popup.close_provider.title": "关闭提供器?",
"hex.builtin.popup.docs_question.title": "查找文档",
"hex.builtin.popup.docs_question.no_answer": "文档中没有这个问题的答案",

View File

@ -447,7 +447,7 @@
"hex.builtin.pl_visualizer.coordinates.query": "查詢地址",
"hex.builtin.pl_visualizer.coordinates.querying": "正在查詢地址...",
"hex.builtin.pl_visualizer.coordinates.querying_no_address": "找不到地址",
"hex.builtin.popup.close_provider.desc": "您對此提供者有未儲存的更動。\n您確定要關閉嗎",
"hex.builtin.popup.close_provider.desc": "",
"hex.builtin.popup.close_provider.title": "關閉提供者?",
"hex.builtin.popup.docs_question.title": "說明文件查詢",
"hex.builtin.popup.docs_question.no_answer": "說明文件回答不了此問題",

View File

@ -18,6 +18,7 @@
#include <content/popups/popup_notification.hpp>
#include <content/popups/popup_question.hpp>
#include <content/popups/popup_tasks_waiting.hpp>
#include <content/popups/popup_unsaved_changes.hpp>
namespace hex::plugin::builtin {
@ -36,13 +37,15 @@ namespace hex::plugin::builtin {
void registerEventHandlers() {
static bool imhexClosing = false;
EventManager::subscribe<EventWindowClosing>([](GLFWwindow *window) {
if (ImHexApi::Provider::isDirty()) {
if (ImHexApi::Provider::isDirty() && !imhexClosing) {
glfwSetWindowShouldClose(window, GLFW_FALSE);
PopupQuestion::open("hex.builtin.popup.exit_application.desc"_lang,
[]{
ImHexApi::Provider::resetDirty();
ImHexApi::System::closeImHex();
[] {
imhexClosing = true;
for (const auto &provider : auto(ImHexApi::Provider::getProviders()))
ImHexApi::Provider::remove(provider);
},
[] { }
);
@ -59,11 +62,15 @@ namespace hex::plugin::builtin {
EventManager::subscribe<EventProviderClosing>([](hex::prv::Provider *provider, bool *shouldClose) {
if (provider->isDirty()) {
*shouldClose = false;
PopupQuestion::open("hex.builtin.popup.close_provider.desc"_lang,
PopupUnsavedChanges::open("hex.builtin.popup.close_provider.desc"_lang,
[]{
ImHexApi::Provider::remove(ImHexApi::Provider::impl::getClosingProvider(), true);
for (auto &provider : ImHexApi::Provider::impl::getClosingProviders())
ImHexApi::Provider::remove(provider, true);
},
[] { }
[] {
ImHexApi::Provider::impl::resetClosingProvider();
imhexClosing = false;
}
);
}
});