From 7c0331df656778f4277cd63ecbfc815f6a67edc2 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 19 Jan 2025 17:04:31 +0100 Subject: [PATCH] impr: Hide window on macOS when close button is pressed --- lib/libimhex/source/helpers/utils.cpp | 7 +++++++ lib/libimhex/source/helpers/utils_macos.m | 15 +++++++++++++++ main/gui/source/window/window.cpp | 8 ++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/libimhex/source/helpers/utils.cpp b/lib/libimhex/source/helpers/utils.cpp index ea939da31..4a109bc7c 100644 --- a/lib/libimhex/source/helpers/utils.cpp +++ b/lib/libimhex/source/helpers/utils.cpp @@ -856,4 +856,11 @@ namespace hex { return ImAlphaBlendColors(a.value(), b.value()); } + extern "C" void macOSCloseButtonPressed() { + auto windowHandle = ImHexApi::System::getMainWindowHandle(); + + glfwHideWindow(windowHandle); + glfwIconifyWindow(windowHandle); + } + } \ No newline at end of file diff --git a/lib/libimhex/source/helpers/utils_macos.m b/lib/libimhex/source/helpers/utils_macos.m index ee0294c47..37702bb44 100644 --- a/lib/libimhex/source/helpers/utils_macos.m +++ b/lib/libimhex/source/helpers/utils_macos.m @@ -48,6 +48,17 @@ return [[NSScreen mainScreen] backingScaleFactor]; } + void macOSCloseButtonPressed(void); + + @interface CloseButtonHandler : NSObject + @end + + @implementation CloseButtonHandler + - (void)pressed:(id)sender { + macOSCloseButtonPressed(); + } + @end + void setupMacosWindowStyle(GLFWwindow *window, bool borderlessWindowMode) { NSWindow* cocoaWindow = glfwGetCocoaWindow(window); @@ -61,6 +72,10 @@ [cocoaWindow setHasShadow:YES]; [cocoaWindow setBackgroundColor:[NSColor colorWithWhite: 0 alpha: 0.001f]]; } + + NSButton *closeButton = [cocoaWindow standardWindowButton:NSWindowCloseButton]; + [closeButton setAction:@selector(pressed:)]; + [closeButton setTarget:[CloseButtonHandler alloc]]; } bool isMacosFullScreenModeEnabled(GLFWwindow *window) { diff --git a/main/gui/source/window/window.cpp b/main/gui/source/window/window.cpp index d81f819ac..fa04d3d31 100644 --- a/main/gui/source/window/window.cpp +++ b/main/gui/source/window/window.cpp @@ -932,12 +932,16 @@ namespace hex { glfwSetCursorPosCallback(m_window, unlockFrameRate); glfwSetMouseButtonCallback(m_window, unlockFrameRate); glfwSetScrollCallback(m_window, unlockFrameRate); - glfwSetWindowFocusCallback(m_window, unlockFrameRate); - glfwSetWindowFocusCallback(m_window, [](GLFWwindow *, int focused) { + glfwSetWindowFocusCallback(m_window, [](GLFWwindow *window, int focused) { + unlockFrameRate(window); EventWindowFocused::post(focused == GLFW_TRUE); }); + glfwSetWindowMaximizeCallback(m_window, [](GLFWwindow *window, int) { + glfwShowWindow(window); + }); + // Register key press callback glfwSetInputMode(m_window, GLFW_LOCK_KEY_MODS, GLFW_TRUE); glfwSetKeyCallback(m_window, [](GLFWwindow *window, int key, int scanCode, int action, int mods) {