From ab29303c2ea399cbb4a452265ec2012c6c2df225 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 7 Jan 2023 17:31:22 +0100 Subject: [PATCH] sys: Clear project context when closing all providers, Display project name in window title (#860) * clear project context when closing all providers * Show project name on window title * refactor RequestChangeWindowTitle to RequestUpdateWindowTitle * add spaces --- lib/libimhex/include/hex/api/event.hpp | 2 +- .../include/hex/api/project_file_manager.hpp | 2 ++ lib/libimhex/source/api/project_file_manager.cpp | 10 +++++++++- main/source/window/window.cpp | 16 +++++++++++----- plugins/builtin/source/content/events.cpp | 9 +++------ .../builtin/source/content/welcome_screen.cpp | 9 +++++++++ 6 files changed, 35 insertions(+), 13 deletions(-) diff --git a/lib/libimhex/include/hex/api/event.hpp b/lib/libimhex/include/hex/api/event.hpp index 19219224b..9f1eaffcc 100644 --- a/lib/libimhex/include/hex/api/event.hpp +++ b/lib/libimhex/include/hex/api/event.hpp @@ -126,7 +126,7 @@ namespace hex { EVENT_DEF(RequestSelectionChange, Region); EVENT_DEF(RequestAddBookmark, Region, std::string, std::string, color_t); EVENT_DEF(RequestSetPatternLanguageCode, std::string); - EVENT_DEF(RequestChangeWindowTitle, std::string); + EVENT_DEF(RequestUpdateWindowTitle); EVENT_DEF(RequestCloseImHex, bool); EVENT_DEF(RequestRestartImHex); EVENT_DEF(RequestOpenFile, std::fs::path); diff --git a/lib/libimhex/include/hex/api/project_file_manager.hpp b/lib/libimhex/include/hex/api/project_file_manager.hpp index 17eafde8b..db64a8cba 100644 --- a/lib/libimhex/include/hex/api/project_file_manager.hpp +++ b/lib/libimhex/include/hex/api/project_file_manager.hpp @@ -34,6 +34,8 @@ namespace hex { static bool store(std::optional filePath = std::nullopt); static bool hasPath(); + static void clearPath(); + static std::fs::path getPath(); static void registerHandler(const Handler &handler) { getHandlers().push_back(handler); diff --git a/lib/libimhex/source/api/project_file_manager.cpp b/lib/libimhex/source/api/project_file_manager.cpp index bb968fd04..b2882e57e 100644 --- a/lib/libimhex/source/api/project_file_manager.cpp +++ b/lib/libimhex/source/api/project_file_manager.cpp @@ -73,7 +73,7 @@ namespace hex { } ProjectFile::s_currProjectPath = filePath; - + EventManager::post(); return true; } @@ -124,4 +124,12 @@ namespace hex { return !ProjectFile::s_currProjectPath.empty(); } + void ProjectFile::clearPath() { + ProjectFile::s_currProjectPath.clear(); + } + + std::fs::path ProjectFile::getPath() { + return ProjectFile::s_currProjectPath; + } + } \ No newline at end of file diff --git a/main/source/window/window.cpp b/main/source/window/window.cpp index 7f0cb703d..33d628274 100644 --- a/main/source/window/window.cpp +++ b/main/source/window/window.cpp @@ -115,13 +115,19 @@ namespace hex { EventManager::post(this->m_window); }); - EventManager::subscribe(this, [this](const std::string &windowTitle) { + EventManager::subscribe(this, [this]() { std::string title = "ImHex"; - if (ImHexApi::Provider::isValid()) { + if (ProjectFile::hasPath()) { + title += " - Project " + hex::limitStringLength(ProjectFile::getPath().stem().string(), 32); + + if (ImHexApi::Provider::isDirty()) + title += " (*)"; + + } else if (ImHexApi::Provider::isValid()) { auto provider = ImHexApi::Provider::get(); - if (!windowTitle.empty() && provider != nullptr) { - title += " - " + hex::limitStringLength(windowTitle, 32); + if (provider != nullptr) { + title += " - " + hex::limitStringLength(provider->getName(), 32); if (provider->isDirty()) title += " (*)"; @@ -177,7 +183,7 @@ namespace hex { Window::~Window() { EventManager::unsubscribe(this); EventManager::unsubscribe(this); - EventManager::unsubscribe(this); + EventManager::unsubscribe(this); EventManager::unsubscribe(this); EventManager::unsubscribe(this); diff --git a/plugins/builtin/source/content/events.cpp b/plugins/builtin/source/content/events.cpp index 96a994668..7028fe7b8 100644 --- a/plugins/builtin/source/content/events.cpp +++ b/plugins/builtin/source/content/events.cpp @@ -44,17 +44,14 @@ namespace hex::plugin::builtin { EventManager::subscribe([](hex::prv::Provider *oldProvider, hex::prv::Provider *newProvider) { hex::unused(oldProvider); + hex::unused(newProvider); - if (newProvider != nullptr && newProvider->isAvailable()) { - EventManager::post(newProvider->getName()); - } else { - EventManager::post(""); - } + EventManager::post(); }); EventManager::subscribe([](hex::prv::Provider *provider) { if (provider != nullptr && ImHexApi::Provider::get() == provider) - EventManager::post(provider->getName()); + EventManager::post(); }); EventManager::subscribe(openFile); diff --git a/plugins/builtin/source/content/welcome_screen.cpp b/plugins/builtin/source/content/welcome_screen.cpp index eadf1fb2e..2feb039cf 100644 --- a/plugins/builtin/source/content/welcome_screen.cpp +++ b/plugins/builtin/source/content/welcome_screen.cpp @@ -500,6 +500,15 @@ namespace hex::plugin::builtin { } }); + // clear project context if we go back to the welcome screen + EventManager::subscribe([](hex::prv::Provider *oldProvider, hex::prv::Provider *newProvider) { + hex::unused(oldProvider); + if (newProvider == nullptr) { + ProjectFile::clearPath(); + EventManager::post(); + } + }); + ContentRegistry::Interface::addMenuItem("hex.builtin.menu.file", 1075, [&] { if (ImGui::BeginMenu("hex.builtin.menu.file.open_recent"_lang, !s_recentProvidersUpdating && !s_recentProviders.empty())) { // Copy to avoid changing list while iteration