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

Clearing InputText focus when platform window loses focus? #6467

Open
xia-i opened this issue May 28, 2023 · 5 comments
Open

Clearing InputText focus when platform window loses focus? #6467

xia-i opened this issue May 28, 2023 · 5 comments

Comments

@xia-i
Copy link

xia-i commented May 28, 2023

Version: Dear ImGui 1.89.6 WIP (18958)
Branch: master

Back-ends: imgui_impl_win32 + imgui_impl_opengl3
My Issue/Question:
In the InputText focus state, after the Win32 window loses focus, InputText will remain in the input state
How to cancel an input state

@ocornut
Copy link
Owner

ocornut commented May 28, 2023

Typically we don't want to alter ImGui state when host applications lose focus.
Why do you need to cancel the InputText() state?

@ocornut ocornut changed the title InputText bug Clearing InputText focus when platform window loses focus? May 28, 2023
@xia-i
Copy link
Author

xia-i commented May 28, 2023

I have problems with expression, English is my weakness
Current: InputText cursor blinks when window loses focus
Goal: InputText cursor not blinking when window loses focus

@cfillion
Copy link
Contributor

cfillion commented May 31, 2023

This can be done by calling ImGui::ClearActiveID (defined in imgui_internal.h) when none of your ImGui platform windows have focus. For example adding this to imgui_impl_win32's message handler:

case WM_KILLFOCUS:
  if(!ImGui::FindViewportByPlatformHandle(GetForegroundWindow()))
    ImGui::ClearActiveID();
  break;

After

@ocornut
Copy link
Owner

ocornut commented Jun 8, 2023

I do understand some application may want that, but simultaneously I am 100% sure some users will not want this. e.g. being able to Alt-Tab between two apps is often desirable. So at minimum perhaps we shouldn't clear focus but simply have option to e.g. disable blinking.

@KarlHedlund
Copy link

Another case where this is relevant:

I am currently testing out overlaying an ImGui application on top of a Electron/Tauri application by manually setting the ImGui application as the child window of the Electron/Tauri application and having no decorations. I am also taking care of position and resize so that the rendered ImGui part feels like a part of the application.

There are two different applications where only one of them can have the input focus. To get the focus to switch to the ImGui part, my current solution is adding this in the beginning of the frame:

        if(ImGui::IsMouseClicked(ImGuiMouseButton_Left))
        {
            glfwFocusWindow(window);
        }

Haven't tested it much but obviously it needs to handle all clicks and I'm sure there are edge cases where it might not work.

Now, due to the behaviour of not letting go of the internal ImGui caret, the user experience is that even though I've clicked an input field in the Electron/Tauri application, I still see a blinking caret in the ImGui part, even though that application has lost input focus.

Just felt like I could add another use case for when disabling the caret is useful.

PS.
I am aware of the WebAssembly version but can't compile all the backend to WebAssembly so it's not an option for now.

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

4 participants