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

Right-clicking in the void won't disable focus #1738

Closed
ldecarufel opened this issue Apr 9, 2018 · 6 comments
Closed

Right-clicking in the void won't disable focus #1738

ldecarufel opened this issue Apr 9, 2018 · 6 comments

Comments

@ldecarufel
Copy link

ldecarufel commented Apr 9, 2018

In our editor, right-clicking in the 3D viewport opens a context menu handled by WPF (not ImGui). Also, dragging around the 3D viewport while right-clicking rotates the camera. While rotating the camera, we can also use the WASD keys to move the camera around the 3D world.

However, when an ImGui text field is active, right-clicking outside ImGui windows will not make the window lose its focus. WantTextInput is still active and keys are not sent to our viewport. WASD keys are sent to the ImGui text field instead of controlling the camera position.

According to the comments in imgui.cpp this behavior appears to be by design, but I wonder if it would be possible to add a flag to control this behavior.

I have locally modified our version of ImGui v1.60 like so:

In ImGui::EndFrame():

if (g.IO.MouseClicked[1])
{
    // ...         
    if ( g.HoveredRootWindow == NULL && modal == NULL && g.NavWindow != NULL )
    {
        // Right-clicking on void disable focus
        FocusWindow(NULL);
    }
}

In ImGui::InputTextEx():

if (focus_requested || user_clicked || user_scrolled || user_nav_input_start)
{
// ...
}
else if (io.MouseClicked[0] || io.MouseClicked[1]) // Also check right-click
{
    // Release focus when we click outside
    clear_active_id = true;
}

This seems to work fine, even with global popups created with ImGui::BeginPopupContextVoid().

ocornut added a commit that referenced this issue Apr 10, 2018
@ocornut
Copy link
Owner

ocornut commented Apr 10, 2018

Hello Louis,

Correct, right-click not stealing focus is indeed by design, as commented in NewFrame(),
// With right mouse button we close popups without changing focus

Your patch would work, my suggestion is that you may just call that code yourself after EndFrame?
If you decide to clear the active id yourself you don't need to modify the code in InputText either. The user may want to have a context menu on a InputText so the second part of your patch would break this.

So, something like this, in your own code, called after EndFrame() may work:

if (g.HoveredWindow == NULL && GetFrontMostPopupModal() == NULL && g.NavWindow != NULL && io.MouseClicked[1])
{
  FocusWindow();
  ClearActiveID();
}

So there's no need for additional options on either side, and that small bit of code using imgui_internal.h won't be very difficult to maintain.

PS: I have renamed GetFrontMostModalRootWindow() which was a static function of imgui.cpp to GetFrontMostPopupModal() (more correct/consistent) and declared in imgui_internal.h.

@ocornut ocornut added the popups label Apr 10, 2018
@ocornut
Copy link
Owner

ocornut commented Apr 10, 2018

Btw, I would want to evaluate actually clearing focus on right-click on void, it's just a little more work to do that carefully (I think the FocusWindow(NULL) code in EndFrame should be moved outside the if (g.ActiveId == 0 && g.HoveredId == 0) block and run for either mouse button. The way InputText() releases focus is a bit of an anomaly at the moment so this ideally needs to be reworked as well at the same time. I'll probably want to this keep topic open for that reason.

@ldecarufel
Copy link
Author

Doing it in my own code after the call to ImGui::EndFrame() is perfectly acceptable! In fact I prefer not to modify any of the ImGui files to facilitate future integrations.

If you're going to clear focus on right-click on void, then please also do it for the middle mouse button!

@afraidofdark
Copy link

afraidofdark commented Apr 3, 2020

I am living the same problem aswell. I have different windows for different views; perspective, top, etc .. I want to activate those windows with right or middle mouse buttons. I managed to do activation. However if there is an InputText on these windows, inputfocus remeans there and i couldnt kill the focus of that item. So intecting with keyboard in the next window affects previous InputText field.

It would be very nice to activate windows with the mouse clicks we wishes. like 0 1 2
io.ImGuiActivatesWindowsWith = MOUSE_RIGHT | MOUSE_LEFT | MOUSE_MIDDLE;
Sort of code would help me alot. Also ImGui::SetKeyboardFocusHere(-2); Might kill the Keyboard focus of the current window. This works too.

Version 1.75 WIP

Thanks.

@ocornut
Copy link
Owner

ocornut commented May 8, 2020

Hello @afraidofdark,

I confirmed there was a bug where the code path for FocusWindow(NULL) wouldn't clear active id properly. This is now fixed by 7b3d379

If anyone needs to clear window focus based on those condition they can use code such as:

if (!ImGui::GetIO().WantCaptureMouse)
    if (ImGui::IsMouseClicked(ImGuiMouseButton_Right) || ImGui::IsMouseClicked(ImGuiMouseButton_Middle))
        ImGui::FocusWindow(NULL);

Anywhere in their app.

The reason I am not making this the default is that it would be inconsistent with how you can close popups with the right-mouse without altering focus, which seems like a semi-standard behavior. Because we have a 3-lines workaround above I consider this solved for now.

(cc: #3200 for later reference)

@ocornut
Copy link
Owner

ocornut commented May 18, 2020

I confirmed there was a bug where the code path for FocusWindow(NULL) wouldn't clear active id properly. This is now fixed by 7b3d379

Fixed Docking regression caused by 7b3d379.

Somehow I knowingly removed this line while moving the block

if (focus_front_window->Flags & ImGuiWindowFlags_Popup) // FIXME: This statement may be unnecessary? Need further testing before removing it..

And I forgot to mention that in the commit message. I knew it would be a bit agressive/risky but somehow against all odd the way our anti-regression tests was setup didn't detect the bug in (#3243). We are now amending testing code to avoid that.

ocornut added a commit that referenced this issue Aug 10, 2020
ocornut added a commit that referenced this issue Oct 26, 2020
…tip) when opening a TreeNode() or CollapsingHeader() while dragging. (#1738)

Amend 7b3d379, 8241cd6 etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants