From 66cd9ef4b8098a3658a55c47bdfdff0845359bc1 Mon Sep 17 00:00:00 2001 From: Alex Maitland Date: Thu, 22 Jul 2021 10:29:22 +1000 Subject: [PATCH] WPF - Improve WpfIMEKeyboardHandler browserHost null checks CancelComposition is now private as it's not used outside the scope of the class. Technically this is a breaking change though it's incredibly unlikely this method was used outside the scope of WpfImeKeyboardHandler as it requires the Hwnd which is obtained from the SourceHook which is private. We can and probably should make most of the private methods protected virtual to allow for overriding of just a small portion of the class, you currently can copy and paste the whole class into your own code base if small changes are required. #3690 --- .../Experimental/WpfIMEKeyboardHandler.cs | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/CefSharp.Wpf/Experimental/WpfIMEKeyboardHandler.cs b/CefSharp.Wpf/Experimental/WpfIMEKeyboardHandler.cs index 81bd1aca6..d433d7d0a 100644 --- a/CefSharp.Wpf/Experimental/WpfIMEKeyboardHandler.cs +++ b/CefSharp.Wpf/Experimental/WpfIMEKeyboardHandler.cs @@ -190,7 +190,14 @@ namespace CefSharp.Wpf.Experimental { handled = false; - if (!isActive || !isSetup || owner == null || owner.IsDisposed || !owner.IsBrowserInitialized || owner.GetBrowserHost() == null) + if (!isActive || !isSetup || owner == null || owner.IsDisposed || !owner.IsBrowserInitialized) + { + return IntPtr.Zero; + } + + var browserHost = owner.GetBrowserHost(); + + if(browserHost == null) { return IntPtr.Zero; } @@ -212,13 +219,13 @@ namespace CefSharp.Wpf.Experimental } case WM.IME_COMPOSITION: { - OnImeComposition(hwnd, lParam.ToInt32()); + OnImeComposition(browserHost, hwnd, lParam.ToInt32()); handled = true; break; } case WM.IME_ENDCOMPOSITION: { - OnImeEndComposition(hwnd); + OnImeEndComposition(browserHost, hwnd); hasImeComposition = false; handled = true; break; @@ -239,15 +246,15 @@ namespace CefSharp.Wpf.Experimental } } - private void OnImeComposition(IntPtr hwnd, int lParam) + private void OnImeComposition(IBrowserHost browserHost, IntPtr hwnd, int lParam) { string text = string.Empty; if (ImeHandler.GetResult(hwnd, (uint)lParam, out text)) { - owner.GetBrowserHost().ImeCommitText(text, new Range(int.MaxValue, int.MaxValue), 0); - owner.GetBrowserHost().ImeSetComposition(text, new CompositionUnderline[0], new Range(int.MaxValue, int.MaxValue), new Range(0, 0)); - owner.GetBrowserHost().ImeFinishComposingText(false); + browserHost.ImeCommitText(text, new Range(int.MaxValue, int.MaxValue), 0); + browserHost.ImeSetComposition(text, new CompositionUnderline[0], new Range(int.MaxValue, int.MaxValue), new Range(0, 0)); + browserHost.ImeFinishComposingText(false); } else { @@ -256,14 +263,14 @@ namespace CefSharp.Wpf.Experimental if (ImeHandler.GetComposition(hwnd, (uint)lParam, underlines, ref compositionStart, out text)) { - owner.GetBrowserHost().ImeSetComposition(text, underlines.ToArray(), + browserHost.ImeSetComposition(text, underlines.ToArray(), new Range(int.MaxValue, int.MaxValue), new Range(compositionStart, compositionStart)); UpdateCaretPosition(compositionStart - 1); } else { - CancelComposition(hwnd); + CancelComposition(browserHost, hwnd); } } } @@ -272,19 +279,19 @@ namespace CefSharp.Wpf.Experimental /// Cancel composition. /// /// The hwnd. - public void CancelComposition(IntPtr hwnd) + private void CancelComposition(IBrowserHost browserHost, IntPtr hwnd) { - owner.GetBrowserHost().ImeCancelComposition(); + browserHost.ImeCancelComposition(); DestroyImeWindow(hwnd); } - private void OnImeEndComposition(IntPtr hwnd) + private void OnImeEndComposition(IBrowserHost browserHost, IntPtr hwnd) { // Korean IMEs somehow ignore function calls to ::ImeFinishComposingText() // The same letter is commited in ::OnImeComposition() if (languageCodeId != ImeNative.LANG_KOREAN) { - owner.GetBrowserHost().ImeFinishComposingText(false); + browserHost.ImeFinishComposingText(false); } DestroyImeWindow(hwnd); }