Skip to content

Commit e41c4a8

Browse files
committed
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
1 parent bcf338f commit e41c4a8

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

CefSharp.Wpf/Experimental/WpfIMEKeyboardHandler.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,14 @@ private IntPtr SourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, re
190190
{
191191
handled = false;
192192

193-
if (!isActive || !isSetup || owner == null || owner.IsDisposed || !owner.IsBrowserInitialized || owner.GetBrowserHost() == null)
193+
if (!isActive || !isSetup || owner == null || owner.IsDisposed || !owner.IsBrowserInitialized)
194+
{
195+
return IntPtr.Zero;
196+
}
197+
198+
var browserHost = owner.GetBrowserHost();
199+
200+
if(browserHost == null)
194201
{
195202
return IntPtr.Zero;
196203
}
@@ -212,13 +219,13 @@ private IntPtr SourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, re
212219
}
213220
case WM.IME_COMPOSITION:
214221
{
215-
OnImeComposition(hwnd, lParam.ToInt32());
222+
OnImeComposition(browserHost, hwnd, lParam.ToInt32());
216223
handled = true;
217224
break;
218225
}
219226
case WM.IME_ENDCOMPOSITION:
220227
{
221-
OnImeEndComposition(hwnd);
228+
OnImeEndComposition(browserHost, hwnd);
222229
hasImeComposition = false;
223230
handled = true;
224231
break;
@@ -239,15 +246,15 @@ private void CloseImeComposition()
239246
}
240247
}
241248

242-
private void OnImeComposition(IntPtr hwnd, int lParam)
249+
private void OnImeComposition(IBrowserHost browserHost, IntPtr hwnd, int lParam)
243250
{
244251
string text = string.Empty;
245252

246253
if (ImeHandler.GetResult(hwnd, (uint)lParam, out text))
247254
{
248-
owner.GetBrowserHost().ImeCommitText(text, new Range(int.MaxValue, int.MaxValue), 0);
249-
owner.GetBrowserHost().ImeSetComposition(text, new CompositionUnderline[0], new Range(int.MaxValue, int.MaxValue), new Range(0, 0));
250-
owner.GetBrowserHost().ImeFinishComposingText(false);
255+
browserHost.ImeCommitText(text, new Range(int.MaxValue, int.MaxValue), 0);
256+
browserHost.ImeSetComposition(text, new CompositionUnderline[0], new Range(int.MaxValue, int.MaxValue), new Range(0, 0));
257+
browserHost.ImeFinishComposingText(false);
251258
}
252259
else
253260
{
@@ -256,14 +263,14 @@ private void OnImeComposition(IntPtr hwnd, int lParam)
256263

257264
if (ImeHandler.GetComposition(hwnd, (uint)lParam, underlines, ref compositionStart, out text))
258265
{
259-
owner.GetBrowserHost().ImeSetComposition(text, underlines.ToArray(),
266+
browserHost.ImeSetComposition(text, underlines.ToArray(),
260267
new Range(int.MaxValue, int.MaxValue), new Range(compositionStart, compositionStart));
261268

262269
UpdateCaretPosition(compositionStart - 1);
263270
}
264271
else
265272
{
266-
CancelComposition(hwnd);
273+
CancelComposition(browserHost, hwnd);
267274
}
268275
}
269276
}
@@ -272,19 +279,19 @@ private void OnImeComposition(IntPtr hwnd, int lParam)
272279
/// Cancel composition.
273280
/// </summary>
274281
/// <param name="hwnd">The hwnd.</param>
275-
public void CancelComposition(IntPtr hwnd)
282+
private void CancelComposition(IBrowserHost browserHost, IntPtr hwnd)
276283
{
277-
owner.GetBrowserHost().ImeCancelComposition();
284+
browserHost.ImeCancelComposition();
278285
DestroyImeWindow(hwnd);
279286
}
280287

281-
private void OnImeEndComposition(IntPtr hwnd)
288+
private void OnImeEndComposition(IBrowserHost browserHost, IntPtr hwnd)
282289
{
283290
// Korean IMEs somehow ignore function calls to ::ImeFinishComposingText()
284291
// The same letter is commited in ::OnImeComposition()
285292
if (languageCodeId != ImeNative.LANG_KOREAN)
286293
{
287-
owner.GetBrowserHost().ImeFinishComposingText(false);
294+
browserHost.ImeFinishComposingText(false);
288295
}
289296
DestroyImeWindow(hwnd);
290297
}

0 commit comments

Comments
 (0)