From 5aa2d87bc82e081ecea4234cb5a34a00fdb988b9 Mon Sep 17 00:00:00 2001 From: actboy168 Date: Wed, 29 May 2019 16:40:23 +0800 Subject: [PATCH 1/2] Disable IME when not needed --- imgui.cpp | 19 ++++++++++++++++++- imgui.h | 1 + imgui_internal.h | 3 +++ imgui_widgets.cpp | 3 +++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/imgui.cpp b/imgui.cpp index 4ff123e32bed..20366ce7f14b 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -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 @@ -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; @@ -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(); @@ -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; @@ -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 @@ -9720,6 +9735,8 @@ static void ImeSetInputScreenPosFn_DefaultImpl(int x, int y) #else +static void ImeSetOpenStatusFn_DefaultImpl(int, int) {} + static void ImeSetInputScreenPosFn_DefaultImpl(int, int) {} #endif diff --git a/imgui.h b/imgui.h index 188372f9eff0..92163f823b5f 100644 --- a/imgui.h +++ b/imgui.h @@ -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. diff --git a/imgui_internal.h b/imgui_internal.h index 8afa9206dc60..cf076c1bc547 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -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; @@ -1133,6 +1135,7 @@ struct ImGuiContext MultiSelectScopeId = 0; PlatformImePos = PlatformImeLastPos = ImVec2(FLT_MAX, FLT_MAX); + PlatformImeStatus = PlatformImeLastStatus = true; SettingsLoaded = false; SettingsDirtyTimer = 0.0f; diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index cbf7ff725ecd..1a0fc9eea78d 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -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 From bca58fee3cd837877152c2b2136d567916c3ad07 Mon Sep 17 00:00:00 2001 From: actboy168 Date: Wed, 29 May 2019 17:14:48 +0800 Subject: [PATCH 2/2] Fixes --- imgui.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imgui.cpp b/imgui.cpp index 20366ce7f14b..d5e3fabfa809 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -9735,7 +9735,7 @@ static void ImeSetInputScreenPosFn_DefaultImpl(int x, int y) #else -static void ImeSetOpenStatusFn_DefaultImpl(int, int) {} +static void ImeSetOpenStatusFn_DefaultImpl(bool) {} static void ImeSetInputScreenPosFn_DefaultImpl(int, int) {}