Skip to content

Commit

Permalink
impr: Hide window on macOS when close button is pressed
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Jan 19, 2025
1 parent 04ab87c commit 7c0331d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/libimhex/source/helpers/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
15 changes: 15 additions & 0 deletions lib/libimhex/source/helpers/utils_macos.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ float getBackingScaleFactor(void) {
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);

Expand All @@ -61,6 +72,10 @@ void setupMacosWindowStyle(GLFWwindow *window, bool borderlessWindowMode) {
[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) {
Expand Down
8 changes: 6 additions & 2 deletions main/gui/source/window/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7c0331d

Please sign in to comment.