Skip to content

Commit

Permalink
WPF - IME Only use GCS_CURSORPOS if an adjacent character is new input (
Browse files Browse the repository at this point in the history
cefsharp#3846)

* Change Wpf IME handling to only use the cursor pos when an adjacent character is new input

* Meant to change both character before and after

* Ensure the cursor pos is > 0 when checking the character before
  • Loading branch information
igandrews committed Oct 11, 2021
1 parent 544abc6 commit bfa8ccf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
24 changes: 21 additions & 3 deletions CefSharp.Wpf/Internals/IMEHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ private static void GetCompositionInfo(IntPtr hwnd, uint lParam, string text, Li

underlines.Clear();

byte[] attributes = null;
int targetStart = text.Length;
int targetEnd = text.Length;
if (IsParam(lParam, ImeNative.GCS_COMPATTR))
{
GetCompositionSelectionRange(hIMC, ref targetStart, ref targetEnd);
attributes = GetCompositionSelectionRange(hIMC, ref targetStart, ref targetEnd);
}

// Retrieve the selection range information. If CS_NOMOVECARET is specified
Expand All @@ -98,6 +99,22 @@ private static void GetCompositionInfo(IntPtr hwnd, uint lParam, string text, Li
compositionStart = 0;
}

if (attributes != null &&
// character before
((compositionStart > 0 && (compositionStart - 1) < attributes.Length && attributes[compositionStart - 1] == ImeNative.ATTR_INPUT)
||
// character after
(compositionStart >= 0 && compositionStart < attributes.Length && attributes[compositionStart] == ImeNative.ATTR_INPUT)))
{
// as MS does with their ime implementation we should only use the GCS_CURSORPOS if the character
// before or after is new input.
// https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/windows/Documents/ImmComposition.cs,1079
}
else
{
compositionStart = text.Length;
}

if (IsParam(lParam, ImeNative.GCS_COMPCLAUSE))
{
GetCompositionUnderlines(hIMC, targetStart, targetEnd, underlines);
Expand Down Expand Up @@ -158,12 +175,12 @@ private static void GetCompositionUnderlines(IntPtr hIMC, int targetStart, int t
}
}

private static void GetCompositionSelectionRange(IntPtr hIMC, ref int targetStart, ref int targetEnd)
private static byte[] GetCompositionSelectionRange(IntPtr hIMC, ref int targetStart, ref int targetEnd)
{
var attributeSize = ImeNative.ImmGetCompositionString(hIMC, ImeNative.GCS_COMPATTR, null, 0);
if (attributeSize <= 0)
{
return;
return null;
}

int start = 0;
Expand Down Expand Up @@ -191,6 +208,7 @@ private static void GetCompositionSelectionRange(IntPtr hIMC, ref int targetStar

targetStart = start;
targetEnd = end;
return attributeData;
}

private static bool IsSelectionAttribute(byte attribute)
Expand Down
1 change: 1 addition & 0 deletions CefSharp.Wpf/Internals/ImeNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static class ImeNative
internal const uint CS_NOMOVECARET = 0x4000;
internal const uint NI_COMPOSITIONSTR = 0x0015;

internal const uint ATTR_INPUT = 0x00;
internal const uint ATTR_TARGET_CONVERTED = 0x01;
internal const uint ATTR_TARGET_NOTCONVERTED = 0x03;

Expand Down

0 comments on commit bfa8ccf

Please sign in to comment.