Skip to content

InputTextEx: do not set WantTextInputNextFrame during the frame Input… #6341

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

Closed
wants to merge 1 commit into from
Closed

Conversation

lukaasm
Copy link
Contributor

@lukaasm lukaasm commented Apr 17, 2023

…Text is deactivated

Currently, when InputText is active and is forced to be deactivated with Enter/Escape key it still requests WantTextInputNextFrame to be set for the next frame which makes WantTextInput flag to linger one frame longer than it needs to.

@ocornut
Copy link
Owner

ocornut commented Apr 17, 2023

Thanks! This looks correct but out of curiosity could you clarify how/why this was a problem for you ?

@lukaasm
Copy link
Contributor Author

lukaasm commented Apr 17, 2023

Yes, when making modal dialogs that are "acceptable" with Enter key I was looking for a way to allow/handle that even when single/multi-line InputText was active, I found a "way" where I can handle Esc/Enter keys without side-effects when WantTextInputNextFrame is 0 at the moment of key press check

//! Draw dialog content
ImGui::InputText( "##input1", ... )

...
other widgets
... 

//! Draw dialog buttons
const bool textInputRequested = ImGui::GetCurrentContext()->WantTextInputNextFrame >= 1;

auto result = DialogResult::None;
if ( ImGui::Button( "OK" ) || ( !textInputRequested && ImGui::IsKeyPressed( ImGuiKey_Enter ) ) )
    result = DialogResult::Ok;
     
if ( ImGui::Button( "Cancel" ) || ( !textInputRequested && ImGui::IsKeyPressed( ImGuiKey_Escape ) ) )
    result = DialogResult::Cancel;
     
 return result;

Without this fix, a double Enter press is needed to 'Accept' dialog.

@ocornut
Copy link
Owner

ocornut commented Apr 17, 2023

I am not sure I understand wgy you aren’t using eg GetActiveID() == 0 or GetActiveID() != GetID(“##input1”); ?

@lukaasm
Copy link
Contributor Author

lukaasm commented Apr 17, 2023

Because it is a "generic" DrawDialogButtons function that doesn't know about surrounding widgets, so checking for every possible InputText is not possible.

Pseudocode:

DrawTableFromReflection()

auto result = DrawDialogButtons(DialogButtons::YesNoCancel)

And GetActiveID() != 0 is not an option because I want to allow to accept/dismiss dialog even if user is for ex. dragging numeric slider with the mouse or has another active widget that doesn't require the capture of TextInput

@ocornut
Copy link
Owner

ocornut commented Apr 17, 2023

I see.

You can use the Input Owner aware variant of GetKeyPressed() (currently in imgui_internal.h) specifying an owner id of ImGuiKeyOwner_None and you’ll only get the key if no one is claiming it, which may be a little more correct.
In Metrics>Inputs you can see the input owner of specific key and chords.

Either way I’ll merge your PR soon (not in front of a computer atm).

@ocornut ocornut added the inputs label Apr 17, 2023
@lukaasm
Copy link
Contributor Author

lukaasm commented Apr 17, 2023

You can use the Input Owner aware variant of GetKeyPressed() (currently in imgui_internal.h) specifying an owner id of ImGuiKeyOwner_None and you’ll only get the key if no one is claiming it, which may be a little more correct.
In Metrics>Inputs you can see the input owner of specific key and chords.

Thanks, I will look into it

ocornut pushed a commit that referenced this pull request Apr 18, 2023
@ocornut
Copy link
Owner

ocornut commented Apr 18, 2023

Merged as 0948cfc, thank you!
I'd be interesting if you can use the new input-owner aware API instead to solve your problem!
(Those API are intended to become public at some point in the future)

@ocornut ocornut closed this Apr 18, 2023
ocornut added a commit that referenced this pull request Apr 22, 2025
ocornut added a commit that referenced this pull request May 23, 2025
elbadcode added a commit to elbadcode/imgui that referenced this pull request Jul 3, 2025
* origin/docking-dev: (1059 commits)
  Backends: Vulkan: Fix failing assertion for platforms where viewports are not supported (ocornut#8734)
  Backends: GLFW: Fixed not installing WndProc hook in all GLFW version, so AddMouseSourceEvent() logic was missing for some viewports.
  Backends: GLFW: Fixed crash when using GLFW 3.3 (ocornut#8713, ocornut#8676, ocornut#8239, ocornut#8069)
  Backends: warning fixes (for docking branch).
  Backends: GLFW: amend for multi-context support with multi-viewport. (ocornut#8676, ocornut#8239, ocornut#8069)
  Backends: OSX: ImGui_ImplOSX_HandleEvent() only process event for window containing our viewports. Amend 7ac99a4 for docking. (ocornut#8644)
  Fixed duplicate symbols in some compile-time configurations.
  Fonts: Misc merge fixes.
  Examples: set ConfigDpiScaleFonts / ConfigDpiScaleViewports in all examples already setup for scaling.
  Backends: GLFW, SDL2, SDL3, update for docking to use helpers.
  (Breaking) renamed/moved ImGuiConfigFlags_DpiEnableScaleFonts -> ioConfigDpiScaleFonts, ImGuiConfigFlags_DpiEnableScaleViewports -> io.ConfigDpiScaleViewports
  Backends: Win32: Viewports: handle WM_DPICHANGED in backend when ImGuiConfigFlags_DpiEnableScaleViewports flag is enabled.
  Viewports: fixed handling of simultaneous move + resize (e.g. toggling maximized) when ImGuiConfigFlags_DpiEnableScaleViewports is enabled.
  Refactor: move SetCurrentFont(), PushFont(), PopFont() to a section.
  Platform IME: Fixed multi-viewports IME support, affecting SDL backends. (ocornut#8648, ocornut#8584, ocornut#7492, ocornut#6341)
  Viewports: added per-viewport FramebufferScale, Platform_GetWindowFramebufferScale() + Backends: GLFW, SDL2, SDL3, Apple: added support. (ocornut#1065, ocornut#1542, ocornut#1676, ocornut#1786, ocornut#2826, ocornut#3757, ocornut#5081, ocornut#5580, ocornut#5592, ocornut#6465, ocornut#7273, ocornut#7779 etc.) )
  Backends: OSX: rename internal struct for consistency with other backends.
  Viewports: fallback DpiScale pulled from fallback Monitor for consistency.
  Backends: Vulkan: fixed build with VK_NO_PROTOTYPES.
  Backends: Vulkan: fixed validation errors during window detach in multi-viewport mode. [docking branch amend] (ocornut#8600, ocornut#8176)
  ...
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