Skip to content

Commit

Permalink
Merge fb2033a into f14ecb6
Browse files Browse the repository at this point in the history
  • Loading branch information
amaitland committed Mar 29, 2023
2 parents f14ecb6 + fb2033a commit 04ba15b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
55 changes: 54 additions & 1 deletion CefSharp.Wpf/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ private void NoInliningConstructor()

browserSettings = Core.ObjectFactory.CreateBrowserSettings(autoDispose: true);

WpfKeyboardHandler = new WpfKeyboardHandler(this);
SetupWpfKeyboardHandler();

PresentationSource.AddSourceChangedHandler(this, PresentationSourceChangedHandler);

Expand Down Expand Up @@ -753,6 +753,13 @@ private void InternalDispose(bool disposing)
// is called.
LifeSpanHandler = null;

var inputLangManager = InputLanguageManager.Current;

if (inputLangManager != null)
{
inputLangManager.InputLanguageChanged -= OnInputLangageChanged;
}

WpfKeyboardHandler?.Dispose();
WpfKeyboardHandler = null;

Expand All @@ -769,6 +776,32 @@ private void InternalDispose(bool disposing)
Cef.RemoveDisposable(this);
}

private void SetupWpfKeyboardHandler()
{
try
{
var inputLang = InputLanguageManager.Current.CurrentInputLanguage;

var useImeKeyboardHandler = WpfImeKeyboardHandler.UseImeKeyboardHandler(inputLang.KeyboardLayoutId);

if (useImeKeyboardHandler)
{
WpfKeyboardHandler = new WpfImeKeyboardHandler(this);
}
else
{
InputLanguageManager.Current.InputLanguageChanged += OnInputLangageChanged;

WpfKeyboardHandler = new WpfKeyboardHandler(this);
}
}
catch (Exception)
{
// For now we'll ignore any errors and just use the default keyboard handler
WpfKeyboardHandler = new WpfKeyboardHandler(this);
}
}

/// <summary>
/// Gets the ScreenInfo - currently used to get the DPI scale factor.
/// </summary>
Expand Down Expand Up @@ -1719,6 +1752,26 @@ private void OnDragEnter(object sender, DragEventArgs e)
}
}

private void OnInputLangageChanged(object sender, InputLanguageEventArgs e)
{
// If we are already using the WpfImeKeyboardHandler then we'll ignore any changes
if (WpfKeyboardHandler?.GetType() == typeof(WpfImeKeyboardHandler))
{
return;
}

var useImeKeyboardHandler = WpfImeKeyboardHandler.UseImeKeyboardHandler(e.NewLanguage.KeyboardLayoutId);

if (useImeKeyboardHandler)
{
var oldKeyboardHandler = WpfKeyboardHandler;
WpfKeyboardHandler = new WpfImeKeyboardHandler(this);
oldKeyboardHandler?.Dispose();

InputLanguageManager.Current.InputLanguageChanged -= OnInputLangageChanged;
}
}

/// <summary>
/// PresentationSource changed handler.
/// </summary>
Expand Down
22 changes: 21 additions & 1 deletion CefSharp.Wpf/Experimental/WpfIMEKeyboardHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ private void CreateImeWindow(IntPtr hwnd)
}
}

private int PrimaryLangId(int lgid)
private static int PrimaryLangId(int lgid)
{
return lgid & 0x3ff;
}
Expand Down Expand Up @@ -440,5 +440,25 @@ private void UpdateCaretPosition(int index)
{
MoveImeWindow(source.Handle);
}

/// <summary>
/// Based on the <paramref name="keyboardLayoutId"/> determine if we
/// should be using IME.
/// </summary>
/// <param name="keyboardLayoutId">Keyboard Layout Id (obtained from <see cref="System.Globalization.CultureInfo.KeyboardLayoutId"/></param>
/// <returns>
/// returns true if the keyboard layout matches one of our listed that support IME, otherwise false.
/// </returns>
public static bool UseImeKeyboardHandler(int keyboardLayoutId)
{
var langId = PrimaryLangId(keyboardLayoutId);

if (langId == ImeNative.LANG_KOREAN || langId == ImeNative.LANG_JAPANESE || langId == ImeNative.LANG_CHINESE)
{
return true;
}

return false;
}
}
}

0 comments on commit 04ba15b

Please sign in to comment.