Skip to content

Fix event capture in multi-window applications on macOS #8644

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

BingoXuan
Copy link

Description:
When using Dear ImGui in a macOS application with multiple windows, input events from windows without ImGui views are incorrectly captured by the global event monitor in the OSX backend.

Reproduction steps:

  1. Create a macOS application with two windows
  2. Add ImGui view to Window A only
  3. Try to interact with Window B
  4. Observe that mouse events in Window B are being captured by ImGui

Fix:
Added window check in ImGui_ImplOSX_HandleEvent to ensure events are only processed when they originate from the window containing the ImGui view:

static bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view)
{
    // Only process events from the window containing ImGui view
    if (event.window != view.window) {
        return false;
    }
    // ... rest of the event handling logic
}

Testing:

  1. Build test application with:
    • Window A containing ImGui view
    • Window B without ImGui
  2. Verify Window B receives events normally
  3. Verify ImGui functionality in Window A works correctly

[OSX] Fix event handling to prevent ImGui from capturing events from other windows
@BingoXuan BingoXuan changed the title Fix event capture in multi-window applications Fix event capture in multi-window applications on macOS May 19, 2025
@ocornut
Copy link
Owner

ocornut commented May 19, 2025

Hello,
Could you confirm the equivalent patch for multi-viewport mode (docking branch) ?

I believe it should be something like:

if (ImGui::FindViewportByPlatformHandle(view.window) == nulltr)
    return;

It would be good to test it.

BingoXuan added 2 commits May 20, 2025 09:56
convert to void pointer before comparation and ignore event when null pointer. verfied for docking branch, also work in main.
@BingoXuan
Copy link
Author

Following code has been verified in docking branch. And sync code to this pr.

static bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view)
{

    // get window of event and view
    void *event_handle = (__bridge void*)(event.window);
    void *view_handle = (__bridge void*)(view.window);
    // check if the event and view are valid
    if (event_handle == nullptr || view_handle == nullptr)
    {
        return false;
    }
    // find viewport by platform handle, if not found return false
    ImGuiViewport *viewport = ImGui::FindViewportByPlatformHandle(view_handle);
    if(viewport == nullptr)
    {
        return false;
    }
    // check if the event and view are the same, handle event from same window

    if(viewport->PlatformHandleRaw != event_handle)
    {
        return false;
    }

    ImGuiIO& io = ImGui::GetIO();
    // ... rest of the event handling logic
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants