impr: Make close button on macOS close providers if any are open instead of closing ImHex

This commit is contained in:
WerWolv
2025-07-24 21:37:49 +02:00
parent 2e200a2ab8
commit a9ce71c481
5 changed files with 47 additions and 9 deletions

View File

@@ -12,6 +12,13 @@ namespace hex {
*/
EVENT_DEF(EventImHexStartupFinished);
/**
* @brief Called when the user presses the close button on the main window
*
* This is currently only used and implemented on macOS
*/
EVENT_DEF(EventCloseButtonPressed);
/**
* @brief Called when ImHex is closing, to trigger the last shutdown hooks
*

View File

@@ -822,10 +822,7 @@ namespace hex {
}
extern "C" void macOSCloseButtonPressed() {
auto windowHandle = ImHexApi::System::getMainWindowHandle();
glfwHideWindow(windowHandle);
glfwIconifyWindow(windowHandle);
EventCloseButtonPressed::post();
}
extern "C" void macosEventDataReceived(const u8 *data, size_t length) {

View File

@@ -103,7 +103,8 @@ namespace hex {
}
void Window::beginNativeWindowFrame() {
if (!ImHexApi::Provider::isValid())
macosMarkContentEdited(m_window, false);
}
void Window::endNativeWindowFrame() {

View File

@@ -10,8 +10,9 @@ namespace hex::plugin::builtin {
class PopupTasksWaiting : public Popup<PopupTasksWaiting> {
public:
PopupTasksWaiting()
: hex::Popup<PopupTasksWaiting>("hex.builtin.popup.waiting_for_tasks.title", false) { }
PopupTasksWaiting(std::function<void()> onFinish)
: hex::Popup<PopupTasksWaiting>("hex.builtin.popup.waiting_for_tasks.title", false),
m_onFinish(std::move(onFinish)){ }
void drawContent() override {
ImGui::TextUnformatted("hex.builtin.popup.waiting_for_tasks.desc"_lang);
@@ -26,13 +27,16 @@ namespace hex::plugin::builtin {
if (TaskManager::getRunningTaskCount() == 0 && TaskManager::getRunningBackgroundTaskCount() == 0) {
ImGui::CloseCurrentPopup();
ImHexApi::System::closeImHex();
m_onFinish();
}
}
[[nodiscard]] ImGuiWindowFlags getFlags() const override {
return ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove;
}
private:
std::function<void()> m_onFinish;
};
}

View File

@@ -95,11 +95,40 @@ namespace hex::plugin::builtin {
TaskManager::doLater([] {
for (auto &task : TaskManager::getRunningTasks())
task->interrupt();
PopupTasksWaiting::open();
PopupTasksWaiting::open([]() {
ImHexApi::System::closeImHex();
});
});
}
});
EventCloseButtonPressed::subscribe([]() {
if (ImHexApi::Provider::isValid()) {
if (ImHexApi::Provider::isDirty()) {
ui::PopupQuestion::open("hex.builtin.popup.exit_application.desc"_lang,
[] {
for (const auto &provider : ImHexApi::Provider::getProviders())
ImHexApi::Provider::remove(provider);
},
[] { }
);
} else if (TaskManager::getRunningTaskCount() > 0 || TaskManager::getRunningBackgroundTaskCount() > 0) {
TaskManager::doLater([] {
for (auto &task : TaskManager::getRunningTasks())
task->interrupt();
PopupTasksWaiting::open([]() {
EventCloseButtonPressed::post();
});
});
} else {
for (const auto &provider : ImHexApi::Provider::getProviders())
ImHexApi::Provider::remove(provider);
}
} else {
ImHexApi::System::closeImHex();
}
});
EventProviderClosing::subscribe([](const prv::Provider *provider, bool *shouldClose) {
if (provider->isDirty()) {
*shouldClose = false;