Skip to content

Commit

Permalink
impr: Prevent window from being moved while hovering over items on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Jan 1, 2025
1 parent 6a3b101 commit 9f9c5ab
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/libimhex/include/hex/helpers/utils_macos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 6 additions & 0 deletions lib/libimhex/source/helpers/utils_macos.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ void macosHandleTitlebarDoubleClickGesture(GLFWwindow *window) {
}
}

void macosSetWindowMovable(GLFWwindow *window, bool movable) {
NSWindow* cocoaWindow = glfwGetCocoaWindow(window);

[cocoaWindow setMovable:movable];
}

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

Expand Down
17 changes: 13 additions & 4 deletions plugins/builtin/source/content/window_decoration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9f9c5ab

Please sign in to comment.