imgui
imgui copied to clipboard
Clearing InputText focus when platform window loses focus?
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
Typically we don't want to alter ImGui state when host applications lose focus. Why do you need to cancel the InputText() state?
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
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;
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.
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.