diff --git a/lib/libimhex/source/api/imhex_api.cpp b/lib/libimhex/source/api/imhex_api.cpp index ff1555515..7eb451b89 100644 --- a/lib/libimhex/source/api/imhex_api.cpp +++ b/lib/libimhex/source/api/imhex_api.cpp @@ -313,13 +313,37 @@ namespace hex { return; if (!s_providers.empty()) { - if (it == s_providers.begin()) + if (it == s_providers.begin()) { + // If the first provider is being closed, select the one that's the first one now setCurrentProvider(0); - else if (std::distance(s_providers.begin(), it) < 0) + } + else if (std::distance(s_providers.begin(), it) == s_currentProvider) { + // If the current provider is being closed, select the one that's before it setCurrentProvider(s_currentProvider - 1); + } + else { + // If any other provider is being closed, find the current provider in the list again and select it again + auto currentProvider = get(); + auto currentIt = std::find(s_providers.begin(), s_providers.end(), currentProvider); + + if (currentIt != s_providers.end()) { + auto newIndex = std::distance(s_providers.begin(), currentIt); + + if (s_currentProvider == newIndex) + newIndex -= 1; + + setCurrentProvider(newIndex); + } else { + // If the current provider is not in the list anymore, select the first one + setCurrentProvider(0); + } + } } s_providers.erase(it); + if (s_currentProvider >= i64(s_providers.size())) + setCurrentProvider(0); + if (s_providers.empty()) EventManager::post(provider, nullptr);