Skip to content

Commit

Permalink
WPF - Improve WpfIMEKeyboardHandler browserHost null checks
Browse files Browse the repository at this point in the history
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
  • Loading branch information
amaitland committed Jul 22, 2021
1 parent bcf338f commit e41c4a8
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions CefSharp.Wpf/Experimental/WpfIMEKeyboardHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,14 @@ private IntPtr SourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, re
{
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;
}
Expand All @@ -212,13 +219,13 @@ private IntPtr SourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, re
}
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;
Expand All @@ -239,15 +246,15 @@ private void CloseImeComposition()
}
}

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
{
Expand All @@ -256,14 +263,14 @@ private void OnImeComposition(IntPtr hwnd, int lParam)

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);
}
}
}
Expand All @@ -272,19 +279,19 @@ private void OnImeComposition(IntPtr hwnd, int lParam)
/// Cancel composition.
/// </summary>
/// <param name="hwnd">The hwnd.</param>
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);
}
Expand Down

0 comments on commit e41c4a8

Please sign in to comment.