From 9f9c5abf35a61775e567ce35e09a9d98b1447c56 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 1 Jan 2025 16:45:11 +0100 Subject: [PATCH] impr: Prevent window from being moved while hovering over items on macOS --- .../include/hex/helpers/utils_macos.hpp | 1 + lib/libimhex/source/helpers/utils_macos.m | 6 ++++++ .../source/content/window_decoration.cpp | 17 +++++++++++++---- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/utils_macos.hpp b/lib/libimhex/include/hex/helpers/utils_macos.hpp index 5c025356e..16cd7931e 100644 --- a/lib/libimhex/include/hex/helpers/utils_macos.hpp +++ b/lib/libimhex/include/hex/helpers/utils_macos.hpp @@ -17,6 +17,7 @@ void enumerateFontsMacos(); void macosHandleTitlebarDoubleClickGesture(GLFWwindow *window); + void macosSetWindowMovable(GLFWwindow *window, bool movable); bool macosIsWindowBeingResizedByUser(GLFWwindow *window); void macosMarkContentEdited(GLFWwindow *window, bool edited = true); } diff --git a/lib/libimhex/source/helpers/utils_macos.m b/lib/libimhex/source/helpers/utils_macos.m index 4436232b9..eb1c974cf 100644 --- a/lib/libimhex/source/helpers/utils_macos.m +++ b/lib/libimhex/source/helpers/utils_macos.m @@ -112,6 +112,12 @@ } } + void macosSetWindowMovable(GLFWwindow *window, bool movable) { + NSWindow* cocoaWindow = glfwGetCocoaWindow(window); + + [cocoaWindow setMovable:movable]; + } + bool macosIsWindowBeingResizedByUser(GLFWwindow *window) { NSWindow* cocoaWindow = glfwGetCocoaWindow(window); diff --git a/plugins/builtin/source/content/window_decoration.cpp b/plugins/builtin/source/content/window_decoration.cpp index 0dbfb2a15..3c6209dc8 100644 --- a/plugins/builtin/source/content/window_decoration.cpp +++ b/plugins/builtin/source/content/window_decoration.cpp @@ -416,10 +416,19 @@ namespace hex::plugin::builtin { const auto menuUnderlaySize = ImVec2(windowSize.x, ImGui::GetCurrentWindowRead()->MenuBarHeight); 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); + + // Prevent window from being moved unless title bar is hovered + + if (!ImGui::IsAnyItemHovered()) { + const auto cursorPos = ImGui::GetCursorScreenPos(); + if (ImGui::IsMouseHoveringRect(cursorPos, cursorPos + menuUnderlaySize) && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) { + macosHandleTitlebarDoubleClickGesture(window); + } + + macosSetWindowMovable(window, true); + } else { + macosSetWindowMovable(window, false); + } } #endif