Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impr: Restore native macOS title bar double click gesture in borderless mode #1689

Merged
merged 5 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/libimhex/include/hex/helpers/utils_macos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
void setupMacosWindowStyle(GLFWwindow *window, bool borderlessWindowMode);

void enumerateFontsMacos();

void macosHandleTitlebarDoubleClickGesture(GLFWwindow *window);
bool macosIsWindowBeingResizedByUser(GLFWwindow *window);
}

#endif
#endif
30 changes: 30 additions & 0 deletions lib/libimhex/source/helpers/utils_macos.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,36 @@ void enumerateFontsMacos(void) {
CFRelease(fontDescriptors);
}

void macosHandleTitlebarDoubleClickGesture(GLFWwindow *window) {
NSWindow* cocoaWindow = glfwGetCocoaWindow(window);

// Consult user preferences: "System Settings -> Desktop & Dock -> Double-click a window's title bar to"
NSString* action = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleActionOnDoubleClick"];

if (action == nil || [action isEqualToString:@"None"]) {
// Nothing to do
} else if ([action isEqualToString:@"Minimize"]) {
if ([cocoaWindow isMiniaturizable]) {
[cocoaWindow miniaturize:nil];
}
} else if ([action isEqualToString:@"Maximize"]) {
// `[NSWindow zoom:_ sender]` takes over pumping the main runloop for the duration of the resize,
// and would interfere with our renderer's frame logic. Schedule it for the next frame

CFRunLoopPerformBlock(CFRunLoopGetMain(), kCFRunLoopCommonModes, ^{
if ([cocoaWindow isZoomable]) {
[cocoaWindow zoom:nil];
}
});
}
}

bool macosIsWindowBeingResizedByUser(GLFWwindow *window) {
NSWindow* cocoaWindow = glfwGetCocoaWindow(window);

return cocoaWindow.inLiveResize;
}

@interface HexDocument : NSDocument

@end
Expand Down
29 changes: 20 additions & 9 deletions main/gui/source/window/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,14 +683,20 @@ namespace hex {
glfwMakeContextCurrent(backupContext);

if (shouldRender) {
int displayWidth, displayHeight;
glfwGetFramebufferSize(m_window, &displayWidth, &displayHeight);
glViewport(0, 0, displayWidth, displayHeight);
glClearColor(0.00F, 0.00F, 0.00F, 0.00F);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

glfwSwapBuffers(m_window);
auto* drawData = ImGui::GetDrawData();

// Avoid accidentally clearing the viewport when the application is minimized,
// otherwise the OS will display an empty frame during deminimization on macOS
if (drawData->DisplaySize.x != 0 && drawData->DisplaySize.y != 0) {
int displayWidth, displayHeight;
glfwGetFramebufferSize(m_window, &displayWidth, &displayHeight);
glViewport(0, 0, displayWidth, displayHeight);
glClearColor(0.00F, 0.00F, 0.00F, 0.00F);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

glfwSwapBuffers(m_window);
}

m_unlockFrameRate = true;
}
Expand Down Expand Up @@ -820,7 +826,12 @@ namespace hex {
auto win = static_cast<Window *>(glfwGetWindowUserPointer(window));
win->m_unlockFrameRate = true;

#if !defined(OS_MACOS)
#if defined(OS_MACOS)
// Stop widgets registering hover effects while the window is being resized
if (macosIsWindowBeingResizedByUser(window)) {
ImGui::GetIO().MousePos = ImVec2();
}
#else
win->fullFrame();
#endif
});
Expand Down
14 changes: 14 additions & 0 deletions plugins/builtin/source/content/window_decoration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,20 @@ namespace hex::plugin::builtin {
drawMenu();
drawTitleBar();

#if defined(OS_MACOS)
if (ImHexApi::System::isBorderlessWindowModeEnabled()) {
const auto windowSize = ImHexApi::System::getMainWindowSize();
const auto menuUnderlaySize = ImVec2(windowSize.x, ImGui::GetCurrentWindowRead()->MenuBarHeight() * 1.5F);

ImGui::SetCursorPos(ImVec2());

// Drawing this button late allows widgets rendered before it to grab click events, forming an "input underlay"
if (ImGui::InvisibleButton("##mainMenuUnderlay", menuUnderlaySize, ImGuiButtonFlags_PressedOnDoubleClick)) {
macosHandleTitlebarDoubleClickGesture(window);
}
}
#endif

ImGui::EndMainMenuBar();
} else {
ImGui::PopStyleVar(2);
Expand Down
Loading