Skip to content

Commit 6e7fed8

Browse files
committed
Update IMEHandler.cs
1 parent ce86185 commit 6e7fed8

File tree

1 file changed

+15
-38
lines changed

1 file changed

+15
-38
lines changed

ClientGUI/IME/IMEHandler.cs

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111
namespace ClientGUI.IME;
1212
public abstract class IMEHandler : IIMEHandler
1313
{
14-
protected class XNATextBoxIMEStatus
15-
{
16-
public bool LastActionIMEChatInput = true;
17-
public Action<char>? HandleChatInput = null;
18-
}
19-
2014
public abstract bool TextCompositionEnabled { get; protected set; }
2115

2216
private XNATextBox? _IMEFocus = null;
@@ -45,33 +39,19 @@ protected set
4539

4640
public bool CompositionEmpty => string.IsNullOrEmpty(_composition);
4741

48-
public bool IMEEventReceived = false;
42+
protected bool IMEEventReceived = false;
43+
protected bool LastActionIMEChatInput = true;
4944

5045
private void OnCompositionChanged(string oldValue, string newValue)
5146
{
5247
Debug.WriteLine($"IME: OnCompositionChanged: {newValue.Length - oldValue.Length}");
5348

5449
IMEEventReceived = true;
55-
if (IMEFocus != null)
56-
{
57-
XNATextBoxIMEStatus status = GetOrNewXNATextBoxIMEStatus(IMEFocus);
58-
// It seems that OnIMETextInput() is always triggered after OnCompositionChanged(). We expect such a behavior.
59-
status.LastActionIMEChatInput = false;
60-
}
50+
// It seems that OnIMETextInput() is always triggered after OnCompositionChanged(). We expect such a behavior.
51+
LastActionIMEChatInput = false;
6152
}
6253

63-
protected Dictionary<XNATextBox, XNATextBoxIMEStatus> IMEStatus = [];
64-
65-
protected XNATextBoxIMEStatus GetOrNewXNATextBoxIMEStatus(XNATextBox textBox)
66-
{
67-
if (textBox == null)
68-
throw new ArgumentNullException(nameof(textBox));
69-
70-
if (!IMEStatus.ContainsKey(textBox))
71-
IMEStatus[textBox] = new XNATextBoxIMEStatus();
72-
73-
return IMEStatus[textBox];
74-
}
54+
protected Dictionary<XNATextBox, Action<char>?> TextBoxHandleChatInputCallbacks = [];
7555

7656
public virtual int CompositionCursorPosition { get; set; }
7757

@@ -97,11 +77,12 @@ protected virtual void OnIMETextInput(char character)
9777
Debug.WriteLine($"IME: OnIMETextInput: {character} {(short)character}; IMEFocus is null? {IMEFocus == null}");
9878

9979
IMEEventReceived = true;
80+
LastActionIMEChatInput = true;
81+
10082
if (IMEFocus != null)
10183
{
102-
var status = GetOrNewXNATextBoxIMEStatus(IMEFocus);
103-
status.LastActionIMEChatInput = true;
104-
status.HandleChatInput?.Invoke(character);
84+
TextBoxHandleChatInputCallbacks.TryGetValue(IMEFocus, out var handleChatInput);
85+
handleChatInput?.Invoke(character);
10586
}
10687
}
10788

@@ -147,13 +128,12 @@ public void OnSelectedChanged(XNATextBox sender)
147128

148129
public void RegisterXNATextBox(XNATextBox sender, Action<char>? handleCharInput)
149130
{
150-
XNATextBoxIMEStatus status = GetOrNewXNATextBoxIMEStatus(sender);
151-
status.HandleChatInput = handleCharInput;
131+
TextBoxHandleChatInputCallbacks.Add(sender, handleCharInput);
152132
}
153133

154134
public void KillXNATextBox(XNATextBox sender)
155135
{
156-
IMEStatus.Remove(sender);
136+
TextBoxHandleChatInputCallbacks.Remove(sender);
157137
}
158138

159139
public bool HandleScrollLeftKey(XNATextBox sender)
@@ -168,18 +148,16 @@ public bool HandleScrollRightKey(XNATextBox sender)
168148

169149
public bool HandleBackspaceKey(XNATextBox sender)
170150
{
171-
XNATextBoxIMEStatus status = GetOrNewXNATextBoxIMEStatus(sender);
172-
bool handled = !status.LastActionIMEChatInput;
173-
status.LastActionIMEChatInput = true;
151+
bool handled = !LastActionIMEChatInput;
152+
LastActionIMEChatInput = true;
174153
Debug.WriteLine($"IME: HandleBackspaceKey: handled: {handled}");
175154
return handled;
176155
}
177156

178157
public bool HandleDeleteKey(XNATextBox sender)
179158
{
180-
XNATextBoxIMEStatus status = GetOrNewXNATextBoxIMEStatus(sender);
181-
bool handled = !status.LastActionIMEChatInput;
182-
status.LastActionIMEChatInput = true;
159+
bool handled = !LastActionIMEChatInput;
160+
LastActionIMEChatInput = true;
183161
Debug.WriteLine($"IME: HandleDeleteKey: handled: {handled}");
184162
return handled;
185163
}
@@ -210,7 +188,6 @@ public bool HandleEnterKey(XNATextBox sender)
210188

211189
public bool HandleEscapeKey(XNATextBox sender)
212190
{
213-
XNATextBoxIMEStatus status = GetOrNewXNATextBoxIMEStatus(sender);
214191
Debug.WriteLine($"IME: HandleEscapeKey: handled: {IMEEventReceived}");
215192
return IMEEventReceived;
216193
}

0 commit comments

Comments
 (0)