Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WPF - Use WpfImeKeyboardHandler for supported KeyboardLayoutIds #4439

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this protected and add xml doc.

{
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;
}
}
}