dotnet / dotnet/wpf

Can we public the `DefaultTextStore.Current.DocumentManager` property to create custom TextEditor with IME

Open
#6,139 0 comments 1 reaction 0 assignees View on GitHub
Untriaged
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

We should use the [ITfThreadMgr.SetFocus](https://docs.microsoft.com/en-us/windows/win32/api/msctf/nf-msctf-itfthreadmgr-setfocus) to make the [IME(Input Method Editors)](https://docs.microsoft.com/en-us/windows/apps/design/input/input-method-editors) focus TextEditor in application in WPF.

But we can only do that by InputMethod.IsInputMethodEnabledProperty.

https://github.com/dotnet/wpf/blob/8e5c08117d14cc1ebfc3179fb1820bac5ffa0013/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputMethod.cs#L226-L233

https://github.com/dotnet/wpf/blob/8e5c08117d14cc1ebfc3179fb1820bac5ffa0013/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputMethod.cs#L1347-L1354

https://github.com/dotnet/wpf/blob/8e5c08117d14cc1ebfc3179fb1820bac5ffa0013/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputMethod.cs#L1360-L1376

https://github.com/dotnet/wpf/blob/8e5c08117d14cc1ebfc3179fb1820bac5ffa0013/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/TextServicesContext.cs#L242-L245

https://github.com/dotnet/wpf/blob/8e5c08117d14cc1ebfc3179fb1820bac5ffa0013/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/TextServicesContext.cs#L346-L354

As you can see, we can not call the `TextServicesContext.SetFocusOnDefaultTextStore` when the `InputMethod.IsInputMethodEnabledProperty` is true.

The other third part TextEditor, such as [AvalonEdit](https://github.com/icsharpcode/AvalonEdit), should call the `ITfThreadMgr.SetFocus(IntPtr.Zero)` to do that.

https://github.com/icsharpcode/AvalonEdit/blob/395ef8166870e2c6e1f63a7d97ac22e5e646e790/ICSharpCode.AvalonEdit/Editing/ImeSupport.cs#L117-L122

After we call the `ITfThreadMgr.SetFocus(IntPtr.Zero)`, we can not switch the IME mode when the application is the sub-window in other process application.

My solution is calling the `InputMethod.EnableOrDisableInputMethod` by Reflection.

```csharp
var type = typeof(InputMethod);
var methodInfo = type.GetMethod("EnableOrDisableInputMethod",BindingFlags.Instance|BindingFlags.NonPublic);
if (methodInfo != null)
{
methodInfo.Invoke(InputMethod.Current, new object[] { true });
}
```

Can we make the `DefaultTextStore.Current.DocumentManager` property as public property or make the `InputMethod.EnableOrDisableInputMethod` as public?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.