1
0
mirror of synced 2024-11-12 02:00:52 +01:00

fix: Various issues with the new popup system

This commit is contained in:
WerWolv 2023-04-16 21:34:29 +02:00
parent 52925c99e8
commit 1690cd2740
18 changed files with 103 additions and 109 deletions

View File

@ -31,7 +31,7 @@ namespace hex {
}
[[nodiscard]] virtual ImVec2 getMaxSize() const {
return { FLT_MAX, FLT_MAX };
return { 0, 0 };
}
[[nodiscard]] static std::vector<std::unique_ptr<PopupBase>> &getOpenPopups() {
@ -50,30 +50,22 @@ namespace hex {
return this->m_modal;
}
static void close() {
if (s_openPopups.empty())
return;
TaskManager::doLater([]{
std::lock_guard lock(s_mutex);
ImGui::CloseCurrentPopup();
s_openPopups.pop_back();
});
void close() {
this->m_close = true;
}
static std::mutex& getMutex() {
return s_mutex;
[[nodiscard]] bool shouldClose() const {
return this->m_close;
}
protected:
static std::vector<std::unique_ptr<PopupBase>> s_openPopups;
static std::mutex s_mutex;
private:
std::string m_unlocalizedName;
bool m_closeButton, m_modal;
std::atomic<bool> m_close = false;
};
}
@ -87,7 +79,8 @@ namespace hex {
public:
template<typename ...Args>
static void open(Args && ... args) {
std::lock_guard lock(s_mutex);
static std::mutex mutex;
std::lock_guard lock(mutex);
auto popup = std::make_unique<T>(std::forward<Args>(args)...);

View File

@ -3,6 +3,5 @@
namespace hex::impl {
std::vector<std::unique_ptr<PopupBase>> PopupBase::s_openPopups;
std::mutex PopupBase::s_mutex;
}

View File

@ -530,59 +530,61 @@ namespace hex {
// Draw popup stack
{
std::scoped_lock lock(impl::PopupBase::getMutex());
if (auto &popups = impl::PopupBase::getOpenPopups(); !popups.empty()) {
static bool popupDisplaying = false;
static bool positionSet = false;
static bool sizeSet = false;
static ImVec2 popupSize;
auto &currPopup = popups.back();
const auto &name = LangEntry(currPopup->getUnlocalizedName());
if (!ImGui::IsPopupOpen(ImGuiID(0), ImGuiPopupFlags_AnyPopupId) && !popupDisplaying)
if (!ImGui::IsPopupOpen(ImGuiID(0), ImGuiPopupFlags_AnyPopupId))
ImGui::OpenPopup(name);
bool open = true;
ImGui::SetNextWindowSizeConstraints(currPopup->getMinSize(), currPopup->getMaxSize());
const auto closeButton = currPopup->hasCloseButton() ? &open : nullptr;
const auto flags = currPopup->getFlags();
const auto &minSize = currPopup->getMinSize();
const auto &maxSize = currPopup->getMaxSize();
const bool hasConstraints = minSize.x != 0 && minSize.y != 0 && maxSize.x != 0 && maxSize.y != 0;
auto emptyWindowSize = ImGui::GetStyle().FramePadding * 4;
if (!sizeSet && popupSize.x > emptyWindowSize.x && popupSize.y > emptyWindowSize.y && popupSize.y < ImGui::GetMainViewport()->Size.y) {
ImGui::SetNextWindowSize(popupSize, ImGuiCond_Always);
ImGui::SetNextWindowPos(ImGui::GetMainViewport()->GetCenter(), ImGuiCond_Always, ImVec2(0.5F, 0.5F));
sizeSet = true;
if (hasConstraints)
ImGui::SetNextWindowSizeConstraints(minSize, maxSize);
else
ImGui::SetNextWindowSize(ImVec2(0, 0), ImGuiCond_Appearing);
auto* closeButton = currPopup->hasCloseButton() ? &open : nullptr;
const auto flags = currPopup->getFlags() | (!hasConstraints ? (ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize) : ImGuiWindowFlags_None);
if (!positionSet) {
ImGui::SetNextWindowPos(ImHexApi::System::getMainWindowPosition() + (ImHexApi::System::getMainWindowSize() / 2.0F), ImGuiCond_Always, ImVec2(0.5F, 0.5F));
if (sizeSet)
positionSet = true;
}
if (currPopup->isModal()) {
if (ImGui::BeginPopupModal(name, closeButton, flags)) {
const auto createPopup = [&](bool displaying) {
if (displaying) {
currPopup->drawContent();
popupDisplaying = true;
popupSize = ImGui::GetWindowSize();
if (ImGui::GetWindowSize().x > ImGui::GetStyle().FramePadding.x * 10)
sizeSet = true;
ImGui::EndPopup();
} else {
popupDisplaying = false;
}
} else {
if (ImGui::BeginPopup(name, flags)) {
};
currPopup->drawContent();
popupDisplaying = true;
popupSize = ImGui::GetWindowSize();
if (currPopup->isModal())
createPopup(ImGui::BeginPopupModal(name, closeButton, flags));
else
createPopup(ImGui::BeginPopup(name, flags));
ImGui::EndPopup();
} else {
popupDisplaying = false;
}
}
if (!popupDisplaying || currPopup->shouldClose()) {
log::debug("Closing popup '{}'", name);
positionSet = sizeSet = false;
if (!open && !popupDisplaying) {
sizeSet = false;
popupDisplaying = false;
popupSize = ImVec2(0, 0);
popups.pop_back();
}
}

View File

@ -48,7 +48,7 @@ namespace hex::plugin::builtin {
class PopupInfo : public impl::PopupNotification<PopupInfo> {
public:
explicit PopupInfo(std::string message)
: PopupNotification("hex.builtin.common.info", std::move(message), []() {
: PopupNotification("hex.builtin.common.info", std::move(message), [this]() {
Popup::close();
}) { }
};
@ -56,7 +56,7 @@ namespace hex::plugin::builtin {
class PopupError : public impl::PopupNotification<PopupError> {
public:
explicit PopupError(std::string message)
: PopupNotification("hex.builtin.common.error", std::move(message), []() {
: PopupNotification("hex.builtin.common.error", std::move(message), [this]() {
Popup::close();
}) { }
};
@ -64,7 +64,7 @@ namespace hex::plugin::builtin {
class PopupFatal : public impl::PopupNotification<PopupFatal> {
public:
explicit PopupFatal(std::string message)
: PopupNotification("hex.builtin.common.fatal", std::move(message), []() {
: PopupNotification("hex.builtin.common.fatal", std::move(message), [this]() {
ImHexApi::System::closeImHex();
Popup::close();
}) { }

View File

@ -21,12 +21,16 @@ namespace hex::plugin::builtin {
auto width = ImGui::GetWindowWidth();
ImGui::SetCursorPosX(width / 9);
if (ImGui::Button("hex.builtin.common.yes"_lang, ImVec2(width / 3, 0)))
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)))
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);
}

View File

@ -78,18 +78,18 @@ namespace hex::plugin::builtin {
ImGui::NewLine();
ImGui::TextUnformatted("hex.builtin.view.pattern_editor.accept_pattern.question"_lang);
confirmButtons(
"hex.builtin.common.yes"_lang, "hex.builtin.common.no"_lang, [this, provider] {
confirmButtons("hex.builtin.common.yes"_lang, "hex.builtin.common.no"_lang,
[this, provider] {
this->m_view->loadPatternFile(this->m_view->m_possiblePatternFiles[this->m_view->m_selectedPatternFile], provider);
Popup::close();
this->close();
},
[] {
Popup::close();
[this] {
this->close();
}
);
if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_Escape)))
Popup::close();
this->close();
}
[[nodiscard]] ImGuiWindowFlags getFlags() const override {

View File

@ -829,10 +829,10 @@
"hex.builtin.welcome.plugins.author",
"hex.builtin.welcome.plugins.desc",
"hex.builtin.welcome.plugins.plugin",
"hex.builtin.welcome.safety_backup.delete",
"hex.builtin.welcome.safety_backup.desc",
"hex.builtin.welcome.safety_backup.restore",
"hex.builtin.welcome.safety_backup.title",
"hex.builtin.popup.safety_backup.delete",
"hex.builtin.popup.safety_backup.desc",
"hex.builtin.popup.safety_backup.restore",
"hex.builtin.popup.safety_backup.title",
"hex.builtin.welcome.start.create_file",
"hex.builtin.welcome.start.open_file",
"hex.builtin.welcome.start.open_other",

View File

@ -880,10 +880,10 @@
"hex.builtin.welcome.plugins.author": "Autor",
"hex.builtin.welcome.plugins.desc": "Beschreibung",
"hex.builtin.welcome.plugins.plugin": "Plugin",
"hex.builtin.welcome.safety_backup.delete": "Nein, entfernen",
"hex.builtin.welcome.safety_backup.desc": "Oh nein, ImHex ist letztes Mal abgestürzt.\nWillst du das vorherige Projekt wiederherstellen?",
"hex.builtin.welcome.safety_backup.restore": "Ja, wiederherstellen",
"hex.builtin.welcome.safety_backup.title": "Verlorene Daten wiederherstellen",
"hex.builtin.popup.safety_backup.delete": "Nein, entfernen",
"hex.builtin.popup.safety_backup.desc": "Oh nein, ImHex ist letztes Mal abgestürzt.\nWillst du das vorherige Projekt wiederherstellen?",
"hex.builtin.popup.safety_backup.restore": "Ja, wiederherstellen",
"hex.builtin.popup.safety_backup.title": "Verlorene Daten wiederherstellen",
"hex.builtin.welcome.start.create_file": "Neue Datei erstellen",
"hex.builtin.welcome.start.open_file": "Datei öffnen",
"hex.builtin.welcome.start.open_other": "Andere Provider",

View File

@ -901,10 +901,10 @@
"hex.builtin.welcome.plugins.author": "Author",
"hex.builtin.welcome.plugins.desc": "Description",
"hex.builtin.welcome.plugins.plugin": "Plugin",
"hex.builtin.welcome.safety_backup.delete": "No, Delete",
"hex.builtin.welcome.safety_backup.desc": "Oh no, ImHex crashed last time.\nDo you want to restore your past work?",
"hex.builtin.welcome.safety_backup.restore": "Yes, Restore",
"hex.builtin.welcome.safety_backup.title": "Restore lost data",
"hex.builtin.popup.safety_backup.delete": "No, Delete",
"hex.builtin.popup.safety_backup.desc": "Oh no, ImHex crashed last time.\nDo you want to restore your past work?",
"hex.builtin.popup.safety_backup.restore": "Yes, Restore",
"hex.builtin.popup.safety_backup.title": "Restore lost data",
"hex.builtin.welcome.start.create_file": "Create New File",
"hex.builtin.welcome.start.open_file": "Open File",
"hex.builtin.welcome.start.open_other": "Other Providers",

View File

@ -880,10 +880,10 @@
"hex.builtin.welcome.plugins.author": "Autore",
"hex.builtin.welcome.plugins.desc": "Descrizione",
"hex.builtin.welcome.plugins.plugin": "Plugin",
"hex.builtin.welcome.safety_backup.delete": "No, Elimina",
"hex.builtin.welcome.safety_backup.desc": "Oh no, l'ultima volta ImHex è crashato.\nVuoi ripristinare il tuo lavoro?",
"hex.builtin.welcome.safety_backup.restore": "Sì, Ripristina",
"hex.builtin.welcome.safety_backup.title": "Ripristina i dati persi",
"hex.builtin.popup.safety_backup.delete": "No, Elimina",
"hex.builtin.popup.safety_backup.desc": "Oh no, l'ultima volta ImHex è crashato.\nVuoi ripristinare il tuo lavoro?",
"hex.builtin.popup.safety_backup.restore": "Sì, Ripristina",
"hex.builtin.popup.safety_backup.title": "Ripristina i dati persi",
"hex.builtin.welcome.start.create_file": "Crea un nuovo File",
"hex.builtin.welcome.start.open_file": "Apri un File",
"hex.builtin.welcome.start.open_other": "",

View File

@ -880,10 +880,10 @@
"hex.builtin.welcome.plugins.author": "作者",
"hex.builtin.welcome.plugins.desc": "詳細",
"hex.builtin.welcome.plugins.plugin": "プラグイン",
"hex.builtin.welcome.safety_backup.delete": "破棄する",
"hex.builtin.welcome.safety_backup.desc": "ImHexがクラッシュしました。\n前のデータを復元しますか",
"hex.builtin.welcome.safety_backup.restore": "復元する",
"hex.builtin.welcome.safety_backup.title": "セッションの回復",
"hex.builtin.popup.safety_backup.delete": "破棄する",
"hex.builtin.popup.safety_backup.desc": "ImHexがクラッシュしました。\n前のデータを復元しますか",
"hex.builtin.popup.safety_backup.restore": "復元する",
"hex.builtin.popup.safety_backup.title": "セッションの回復",
"hex.builtin.welcome.start.create_file": "新規ファイルを作成",
"hex.builtin.welcome.start.open_file": "ファイルを開く…",
"hex.builtin.welcome.start.open_other": "その他のファイル",

View File

@ -880,10 +880,10 @@
"hex.builtin.welcome.plugins.author": "작성자",
"hex.builtin.welcome.plugins.desc": "설명",
"hex.builtin.welcome.plugins.plugin": "플러그인",
"hex.builtin.welcome.safety_backup.delete": "아니요, 삭제",
"hex.builtin.welcome.safety_backup.desc": "이전에 ImHex가 비 정상적으로 종료된 것 같습니다.\n이전의 작업을 복구할까요?",
"hex.builtin.welcome.safety_backup.restore": "네, 복구",
"hex.builtin.welcome.safety_backup.title": "손상된 데이터 복구",
"hex.builtin.popup.safety_backup.delete": "아니요, 삭제",
"hex.builtin.popup.safety_backup.desc": "이전에 ImHex가 비 정상적으로 종료된 것 같습니다.\n이전의 작업을 복구할까요?",
"hex.builtin.popup.safety_backup.restore": "네, 복구",
"hex.builtin.popup.safety_backup.title": "손상된 데이터 복구",
"hex.builtin.welcome.start.create_file": "새 파일 생성",
"hex.builtin.welcome.start.open_file": "파일 열기",
"hex.builtin.welcome.start.open_other": "다른 공급자 열기",

View File

@ -880,10 +880,10 @@
"hex.builtin.welcome.plugins.author": "Autor",
"hex.builtin.welcome.plugins.desc": "Descrição",
"hex.builtin.welcome.plugins.plugin": "Plugin",
"hex.builtin.welcome.safety_backup.delete": "Não, Apagar",
"hex.builtin.welcome.safety_backup.desc": "Ah não, ImHex crashou na ultima vez.\nDeseja restaurar seu trabalho anterior?",
"hex.builtin.welcome.safety_backup.restore": "Yes, Restaurar",
"hex.builtin.welcome.safety_backup.title": "Restaurar dados perdidos",
"hex.builtin.popup.safety_backup.delete": "Não, Apagar",
"hex.builtin.popup.safety_backup.desc": "Ah não, ImHex crashou na ultima vez.\nDeseja restaurar seu trabalho anterior?",
"hex.builtin.popup.safety_backup.restore": "Yes, Restaurar",
"hex.builtin.popup.safety_backup.title": "Restaurar dados perdidos",
"hex.builtin.welcome.start.create_file": "Criar Novo Arquivo",
"hex.builtin.welcome.start.open_file": "Abrir Arquivo",
"hex.builtin.welcome.start.open_other": "Outros Provedores",

View File

@ -835,10 +835,10 @@
"hex.builtin.welcome.plugins.author": "作者",
"hex.builtin.welcome.plugins.desc": "描述",
"hex.builtin.welcome.plugins.plugin": "插件",
"hex.builtin.welcome.safety_backup.delete": "删除",
"hex.builtin.welcome.safety_backup.desc": "糟糕ImHex 上次崩溃了!\n您想从异常转储中恢复之前的数据吗",
"hex.builtin.welcome.safety_backup.restore": "恢复",
"hex.builtin.welcome.safety_backup.title": "恢复崩溃数据",
"hex.builtin.popup.safety_backup.delete": "删除",
"hex.builtin.popup.safety_backup.desc": "糟糕ImHex 上次崩溃了!\n您想从异常转储中恢复之前的数据吗",
"hex.builtin.popup.safety_backup.restore": "恢复",
"hex.builtin.popup.safety_backup.title": "恢复崩溃数据",
"hex.builtin.welcome.start.create_file": "创建新文件",
"hex.builtin.welcome.start.open_file": "打开文件",
"hex.builtin.welcome.start.open_other": "其他提供器",

View File

@ -880,10 +880,10 @@
"hex.builtin.welcome.plugins.author": "作者",
"hex.builtin.welcome.plugins.desc": "說明",
"hex.builtin.welcome.plugins.plugin": "外掛程式",
"hex.builtin.welcome.safety_backup.delete": "不用,請刪除",
"hex.builtin.welcome.safety_backup.desc": "喔不ImHex 上次崩潰了。\n您要復原您的工作階段嗎",
"hex.builtin.welcome.safety_backup.restore": "好,請復原",
"hex.builtin.welcome.safety_backup.title": "復原遺失資料",
"hex.builtin.popup.safety_backup.delete": "不用,請刪除",
"hex.builtin.popup.safety_backup.desc": "喔不ImHex 上次崩潰了。\n您要復原您的工作階段嗎",
"hex.builtin.popup.safety_backup.restore": "好,請復原",
"hex.builtin.popup.safety_backup.title": "復原遺失資料",
"hex.builtin.welcome.start.create_file": "建立新檔案",
"hex.builtin.welcome.start.open_file": "開啟檔案",
"hex.builtin.welcome.start.open_other": "其他提供者",

View File

@ -39,9 +39,7 @@ namespace hex::plugin::builtin {
ImHexApi::Provider::resetDirty();
ImHexApi::System::closeImHex();
},
[]{
PopupQuestion::close();
}
[] { }
);
} else if (TaskManager::getRunningTaskCount() > 0 || TaskManager::getRunningBackgroundTaskCount() > 0) {
glfwSetWindowShouldClose(window, GLFW_FALSE);
@ -59,11 +57,8 @@ namespace hex::plugin::builtin {
PopupQuestion::open("hex.builtin.popup.close_provider.desc"_lang,
[]{
ImHexApi::Provider::remove(ImHexApi::Provider::impl::getClosingProvider(), true);
PopupQuestion::close();
},
[]{
PopupQuestion::close();
}
[] { }
);
}
});

View File

@ -88,7 +88,7 @@ namespace hex::plugin::builtin {
this->getWindowOpenState() = false;
if (!this->getWindowOpenState() && this->m_restartRequested) {
PopupQuestion::open("hex.builtin.view.settings.restart_question"_lang, ImHexApi::System::restartImHex, [] {});
PopupQuestion::open("hex.builtin.view.settings.restart_question"_lang, ImHexApi::System::restartImHex, []{});
}
}

View File

@ -67,12 +67,12 @@ namespace hex::plugin::builtin {
PopupRestoreBackup() : Popup("hex.builtin.popup.safety_backup.title") { }
void drawContent() override {
ImGui::TextUnformatted("hex.builtin.welcome.safety_backup.desc"_lang);
ImGui::TextUnformatted("hex.builtin.popup.safety_backup.desc"_lang);
ImGui::NewLine();
auto width = ImGui::GetWindowWidth();
ImGui::SetCursorPosX(width / 9);
if (ImGui::Button("hex.builtin.welcome.safety_backup.restore"_lang, ImVec2(width / 3, 0))) {
if (ImGui::Button("hex.builtin.popup.safety_backup.restore"_lang, ImVec2(width / 3, 0))) {
ProjectFile::load(s_safetyBackupPath);
ProjectFile::clearPath();
@ -85,7 +85,7 @@ namespace hex::plugin::builtin {
}
ImGui::SameLine();
ImGui::SetCursorPosX(width / 9 * 5);
if (ImGui::Button("hex.builtin.welcome.safety_backup.delete"_lang, ImVec2(width / 3, 0))) {
if (ImGui::Button("hex.builtin.popup.safety_backup.delete"_lang, ImVec2(width / 3, 0))) {
wolv::io::fs::remove(s_safetyBackupPath);
Popup::close();
@ -513,16 +513,17 @@ namespace hex::plugin::builtin {
EventManager::subscribe<EventWindowInitialized>([] {
// documentation of the value above the setting definition
int showCheckForUpdates = ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.check_for_updates", 2);
auto showCheckForUpdates = ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.check_for_updates", 2);
if (showCheckForUpdates == 2) {
ContentRegistry::Settings::write("hex.builtin.setting.general", "hex.builtin.setting.general.check_for_updates", 0);
PopupQuestion::open("hex.builtin.welcome.check_for_updates_text"_lang,
[] { // yes
[] {
ContentRegistry::Settings::write("hex.builtin.setting.general", "hex.builtin.setting.general.check_for_updates", 1);
ImGui::CloseCurrentPopup();
}, [] { // no
ImGui::CloseCurrentPopup();
});
},
[] {
}
);
}
});