-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
[RDY] win32 ime #6223
Closed
Closed
[RDY] win32 ime #6223
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3c4f539
win32 ime wip
yatli 07cd28e
ime window starts tracking the cursor, but coords are wrong
yatli 168d936
fix win32 ime cursor coord
yatli b0da0ef
win32-ime lang-specific behaviors
yatli 63cd98e
track language id in WindowImpl
yatli 348a16b
lowercase dllimport
yatli 6213748
create initial ime on window creation
yatli db23339
InputMethodManager: connect to client even if im is absent at the moment
yatli 8839b00
proposal: IKeyboardDevice.NotifyInputMethodUpdated
yatli f767580
finalizing
yatli 29c05ae
ime: allow client to request active state change
yatli 68f3d2f
remove backward incompatible ActiveState.
yatli 17bacc0
InputMethodManager: NotifyInputMethodUpdated: filter the window of cu…
yatli e39e82d
merge with master.
yatli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
using Avalonia.Input.TextInput; | ||
using Avalonia.Threading; | ||
|
||
using static Avalonia.Win32.Interop.UnmanagedMethods; | ||
|
||
namespace Avalonia.Win32.Input | ||
{ | ||
/// <summary> | ||
/// A Windows input method editor based on Windows Input Method Manager (IMM32). | ||
/// </summary> | ||
class Imm32InputMethod : ITextInputMethodImpl, IDisposable | ||
{ | ||
private bool _disposedValue; | ||
private IntPtr _hwnd; | ||
private WindowImpl _parent; | ||
private bool _active; | ||
private bool _systemCaret; | ||
private bool _showCompositionWindow; | ||
private bool _showCandidateList; | ||
private ushort _langId; | ||
private const int _caretMargin = 1; | ||
|
||
public Imm32InputMethod(WindowImpl parent, IntPtr hwnd, IntPtr HKL) | ||
{ | ||
_parent = parent; | ||
_hwnd = hwnd; | ||
_disposedValue = false; | ||
_active = false; | ||
_langId = PRIMARYLANGID(LGID(HKL)); | ||
_systemCaret = (_langId == LANG_ZH || _langId == LANG_JA); | ||
_showCompositionWindow = true; | ||
_showCandidateList = true; | ||
if (_systemCaret) | ||
{ | ||
CreateCaret(_hwnd, IntPtr.Zero, 1, 1); | ||
} | ||
IsComposing = false; | ||
} | ||
|
||
public void Reset() | ||
{ | ||
if (IsComposing) | ||
{ | ||
Dispatcher.UIThread.Post(() => | ||
{ | ||
IntPtr himc = ImmGetContext(_hwnd); | ||
ImmNotifyIME(himc, NI_COMPOSITIONSTR, CPS_COMPLETE, 0); | ||
ImmReleaseContext(_hwnd, himc); | ||
}); | ||
} | ||
} | ||
|
||
public void SetActive(bool active) | ||
{ | ||
_active = active; | ||
Dispatcher.UIThread.Post(() => | ||
{ | ||
IntPtr himc = ImmGetContext(_hwnd); | ||
ImmSetActiveContext(himc, active); | ||
ImmSetOpenStatus(himc, active); | ||
ImmReleaseContext(_hwnd, himc); | ||
}); | ||
} | ||
|
||
public void SetCursorRect(Rect rect) | ||
{ | ||
var focused = GetActiveWindow() == _hwnd; | ||
if (!focused) | ||
{ | ||
return; | ||
} | ||
Dispatcher.UIThread.Post(() => | ||
{ | ||
IntPtr himc = ImmGetContext(_hwnd); | ||
if (himc == IntPtr.Zero) | ||
{ | ||
return; | ||
} | ||
|
||
// see: https://chromium.googlesource.com/experimental/chromium/src/+/bf09a5036ccfb77d2277247c66dc55daf41df3fe/chrome/browser/ime_input.cc | ||
// see: https://engine.chinmaygarde.com/window__win32_8cc_source.html | ||
|
||
var p1 = rect.TopLeft; | ||
var p2 = rect.BottomRight; | ||
var s = _parent.DesktopScaling; | ||
var (x1, y1, x2, y2) = ((int)(p1.X * s), (int)(p1.Y * s), (int)(p2.X * s), (int)(p2.Y * s)); | ||
|
||
if (_showCompositionWindow) | ||
{ | ||
var compForm = new COMPOSITIONFORM | ||
{ | ||
dwStyle = CFS_POINT, | ||
ptCurrentPos = new POINT { X = x1, Y = y1 }, | ||
}; | ||
ImmSetCompositionWindow(himc, ref compForm); | ||
} | ||
|
||
if (_showCandidateList) | ||
{ | ||
var candidateForm = new CANDIDATEFORM | ||
{ | ||
dwIndex = 0, | ||
dwStyle = CFS_CANDIDATEPOS, | ||
ptCurrentPos = new POINT { X = x2, Y = y2 } | ||
}; | ||
ImmSetCandidateWindow(himc, ref candidateForm); | ||
|
||
if (_systemCaret) | ||
{ | ||
SetCaretPos(x2, y2); | ||
} | ||
|
||
if (_langId == LANG_KO) | ||
{ | ||
y2 += _caretMargin; | ||
} | ||
|
||
candidateForm = new CANDIDATEFORM | ||
{ | ||
dwIndex = 0, | ||
dwStyle = CFS_EXCLUDE, | ||
ptCurrentPos = new POINT { X = x2, Y = y2 }, | ||
rcArea = new RECT { left = x2, top = y2, right = x2, bottom = y2 + _caretMargin} | ||
}; | ||
} | ||
|
||
ImmReleaseContext(_hwnd, himc); | ||
}); | ||
} | ||
|
||
public void SetOptions(TextInputOptionsQueryEventArgs options) | ||
{ | ||
// ??? | ||
} | ||
|
||
protected void _dispose() | ||
{ | ||
if (!_disposedValue) | ||
{ | ||
_disposedValue = true; | ||
if (_systemCaret) | ||
{ | ||
_systemCaret = false; | ||
DestroyCaret(); | ||
} | ||
} | ||
} | ||
|
||
public bool IsComposing { get; set; } | ||
|
||
~Imm32InputMethod() | ||
{ | ||
_dispose(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_dispose(); | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why exactly do we need this? Is it needed to handle keyboard layout switch between basic and IME-enabled languages?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if that should be handled on the xplat layer though. I'd expect that state to be tracked by the platform code that encapsulates such switches.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how other users do this, but for me I heavily rely on win+space to switch between en/zh on the fly.
The ime manager queries client capabilities when it gets focus (or init?), but doesn't handle IME switch when the control is focused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I'd expect the WindowImpl.cs to always have an active
Imm32InputMethod
instance (just like X11Window does) that maintains its state rather than going through the requery process. Your approach also adds more ties between input method handling and toplevels while we are kinda planning to add built-in OSK support.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WindowImpl
always have that.InputMethodManager
doesn't.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The initial idea was that InputMethodManager synchronizes the control with the native window implementation. The native window implementation is supposed to keep track of what was previously passed to it rather than requery that information on demand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Maybe it's possible to put the mutable states in IMM32InputMethod.