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

Disable IME when not needed #2589

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,7 @@ static void SettingsHandlerWindow_WriteAll(ImGuiContext* imgui_ctx,
// Platform Dependents default implementation for IO functions
static const char* GetClipboardTextFn_DefaultImpl(void* user_data);
static void SetClipboardTextFn_DefaultImpl(void* user_data, const char* text);
static void ImeSetOpenStatusFn_DefaultImpl(bool open);
static void ImeSetInputScreenPosFn_DefaultImpl(int x, int y);

namespace ImGui
Expand Down Expand Up @@ -1235,6 +1236,7 @@ ImGuiIO::ImGuiIO()
GetClipboardTextFn = GetClipboardTextFn_DefaultImpl; // Platform dependent default implementations
SetClipboardTextFn = SetClipboardTextFn_DefaultImpl;
ClipboardUserData = NULL;
ImeSetOpenStatusFn = ImeSetOpenStatusFn_DefaultImpl;
ImeSetInputScreenPosFn = ImeSetInputScreenPosFn_DefaultImpl;
ImeWindowHandle = NULL;

Expand Down Expand Up @@ -3583,6 +3585,7 @@ void ImGui::NewFrame()
g.MouseCursor = ImGuiMouseCursor_Arrow;
g.WantCaptureMouseNextFrame = g.WantCaptureKeyboardNextFrame = g.WantTextInputNextFrame = -1;
g.PlatformImePos = ImVec2(1.0f, 1.0f); // OS Input Method Editor showing on top-left of our window by default
g.PlatformImeStatus = false;

// Mouse wheel scrolling, scale
UpdateMouseWheel();
Expand Down Expand Up @@ -3877,7 +3880,13 @@ void ImGui::EndFrame()
IM_ASSERT(g.FrameScopeActive && "Forgot to call ImGui::NewFrame()?");

// Notify OS when our Input Method Editor cursor has moved (e.g. CJK inputs using Microsoft IME)
if (g.IO.ImeSetInputScreenPosFn && (g.PlatformImeLastPos.x == FLT_MAX || ImLengthSqr(g.PlatformImeLastPos - g.PlatformImePos) > 0.0001f))
if (g.IO.ImeSetOpenStatusFn && g.PlatformImeLastStatus != g.PlatformImeStatus)
{
g.IO.ImeSetOpenStatusFn(g.PlatformImeStatus);
g.PlatformImeLastStatus = g.PlatformImeStatus;
}

if (g.PlatformImeStatus && g.IO.ImeSetInputScreenPosFn && (g.PlatformImeLastPos.x == FLT_MAX || ImLengthSqr(g.PlatformImeLastPos - g.PlatformImePos) > 0.0001f))
{
g.IO.ImeSetInputScreenPosFn((int)g.PlatformImePos.x, (int)g.PlatformImePos.y);
g.PlatformImeLastPos = g.PlatformImePos;
Expand Down Expand Up @@ -9703,6 +9712,12 @@ static void SetClipboardTextFn_DefaultImpl(void*, const char* text)
#pragma comment(lib, "imm32")
#endif

static void ImeSetOpenStatusFn_DefaultImpl(bool open)
{
if (HWND hwnd = (HWND)GImGui->IO.ImeWindowHandle)
::ImmAssociateContextEx(hwnd, NULL, open? IACE_DEFAULT: 0);
}

static void ImeSetInputScreenPosFn_DefaultImpl(int x, int y)
{
// Notify OS Input Method Editor of text input position
Expand All @@ -9720,6 +9735,8 @@ static void ImeSetInputScreenPosFn_DefaultImpl(int x, int y)

#else

static void ImeSetOpenStatusFn_DefaultImpl(bool) {}

static void ImeSetInputScreenPosFn_DefaultImpl(int, int) {}

#endif
Expand Down
1 change: 1 addition & 0 deletions imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,7 @@ struct ImGuiIO

// Optional: Notify OS Input Method Editor of the screen position of your cursor for text input position (e.g. when using Japanese/Chinese IME on Windows)
// (default to use native imm32 api on Windows)
void (*ImeSetOpenStatusFn)(bool open);
void (*ImeSetInputScreenPosFn)(int x, int y);
void* ImeWindowHandle; // = NULL // (Windows) Set this to your HWND to get automatic IME cursor positioning.

Expand Down
3 changes: 3 additions & 0 deletions imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,8 @@ struct ImGuiContext
// Platform support
ImVec2 PlatformImePos; // Cursor position request & last passed to the OS Input Method Editor
ImVec2 PlatformImeLastPos;
bool PlatformImeStatus;
bool PlatformImeLastStatus;

// Settings
bool SettingsLoaded;
Expand Down Expand Up @@ -1133,6 +1135,7 @@ struct ImGuiContext
MultiSelectScopeId = 0;

PlatformImePos = PlatformImeLastPos = ImVec2(FLT_MAX, FLT_MAX);
PlatformImeStatus = PlatformImeLastStatus = true;

SettingsLoaded = false;
SettingsDirtyTimer = 0.0f;
Expand Down
3 changes: 3 additions & 0 deletions imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4040,7 +4040,10 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_

// Notify OS of text input position for advanced IME (-1 x offset so that Windows IME can cover our cursor. Bit of an extra nicety.)
if (!is_readonly)
{
g.PlatformImePos = ImVec2(cursor_screen_pos.x - 1.0f, cursor_screen_pos.y - g.FontSize);
g.PlatformImeStatus = true;
}
}
}
else
Expand Down