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

Implement SetPlatformImeDataFn for SDL backend to locate IME at caret while typing. #6071

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

imkzh
Copy link

@imkzh imkzh commented Jan 12, 2023

Implement SetPlatformImeDataFn for SDL backend to properly locate IME candidate panel while typing in utf-8 enabled ImGui::InputText.

From SDL Wiki:

SDL supports "on-the-spot" mode.

One important thing to notice is that the application can enable and disable text input arbitrarily with SDL_StartTextInput() and SDL_StopTextInput(). SDL_SetTextInputRect() controls where the Candidate List will open, if supported.

Screenshots

Before patch:
before_patch

After patch:
after_patch

Tests

Tested example_sdl_sdlrenderer, example_sdl_opengl2, example_sdl_opengl3 on Ubuntu 16.04(X11 + IBus)

CJK text will only appear if the font was loaded with the appropriate CJK character ranges.

For testing, I use the following code to load a Chinese TTF font:

io.Fonts->AddFontFromFileTTF("/usr/share/fonts/truetype/wqy/wqy-microhei.ttc", 18.0f, NULL, io.Fonts->GetGlyphRangesChineseFull());
@ocornut
Copy link
Owner

ocornut commented Jan 12, 2023

Thank you for this PR.

My understanding until now was that SDL essentially broke IME under Windows, at least I could not get it to work. (See in particular #1953 which was never merged.)

Are you using a recent version of SDL?

Related changes in SDL: From https://www.libsdl.org/tmp/SDL/WhatsNew.txt

2.0.22

  • Added SDL_TEXTEDITING_EXT event for handling long composition text, and a hint SDL_HINT_IME_SUPPORT_EXTENDED_TEXT to enable it

2.0.18:

  • Added the hint SDL_HINT_IME_SHOW_UI to show native UI components instead of hiding them (defaults off)

Are you using SDL_HINT_IME_SHOW_UI by by chance?

@imkzh
Copy link
Author

imkzh commented Jan 12, 2023

I'm using 2.0.18 at the time of creating this pull request. And I just upgraded it to 2.26.0 at this moment. I have not tested this on windows yet (since I have no access to windows machine with proper dev-env at this moment)

I think programs which do not activate SDL_HINT_IME_SHOW_UI (on SDL>=2.0.18) should render IME panel themselves (of course Dear ImGui can implement this for users, but I think this introduce extra complexity, and is (probably?) way too far for a library targeting "content creation tools and visualization / debug tools" ), SDL just tells the program what are in "candidate list" and the content of "pre-edit text" (through events SDL_TextInputEvent).

Without SDL_HINT_IME_SHOW_UI activated, the SDL_SetTextInputRect call simply does nothing visible to user (it just tells OS to update IME location, since no IME is shown by OS, nothing is visible to user) (maybe it's just because my platform does not support UI-Less IME, so IME panel will always be shown).

From SDL Wiki SDL_SetTextInputRect:

Note: If you want to use the system native IME window, try setting hint SDL_HINT_IME_SHOW_UI to 1, otherwise this function won't give you any feedback.

@RT2Code
Copy link

RT2Code commented Feb 7, 2023

I tested this PR with SDL 2.26.2 on Windows 11, and it seems to work fine :
ezgif-3-a80e138252
Backspace and enter key press are not forwarded to imgui while the candidate window is active, which is the expected behavior.

The only missing thing is the composition string, which, If I got it right, should be handled by imgui via the SDL_TEXTEDITING event.

@ocornut
Copy link
Owner

ocornut commented Feb 7, 2023

I tested this PR with SDL 2.26.2 on Windows 11, and it seems to work fine :

I now understand why I wasn't seeing anything
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1"); needs to be called BEFORE SDL_CreateWindow(). It therefore cannot be done by the backend.

Merged your PR as 734c6af + various amends and added hint to all examples + equivalent in SDL3 38a6c70

However

  • I notice the position doesn't always update correctly. Namely, when clicking to close instead of validating with Escape.
  • SDL3 correct is mysteriously broken.
  • It's ambiguous how the SDL_SetTextInputRect() API expect positions to work with multiple SDL windows.

I tried to let it use your own implementation embedded in imgui.cpp, which can be used once viewport->PlatformHandleRaw is set by the backend, however it seems like there's a scaling issue as without hi-dpi the scales between our coordinates and raw IME coordinates are mismatching.

@ocornut
Copy link
Owner

ocornut commented Feb 7, 2023

The only missing thing is the composition string, which, If I got it right, should be handled by imgui via the SDL_TEXTEDITING event.

Right, this is the topic of #5878 I'll answer there.

kjblanchard pushed a commit to kjblanchard/imgui that referenced this pull request May 5, 2023
kjblanchard pushed a commit to kjblanchard/imgui that referenced this pull request May 5, 2023
kjblanchard pushed a commit to kjblanchard/imgui that referenced this pull request May 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants