11
11
namespace ClientGUI . IME ;
12
12
public abstract class IMEHandler : IIMEHandler
13
13
{
14
- protected class XNATextBoxIMEStatus
15
- {
16
- public bool LastActionIMEChatInput = true ;
17
- public Action < char > ? HandleChatInput = null ;
18
- }
19
-
20
14
public abstract bool TextCompositionEnabled { get ; protected set ; }
21
15
22
16
private XNATextBox ? _IMEFocus = null ;
@@ -45,33 +39,19 @@ protected set
45
39
46
40
public bool CompositionEmpty => string . IsNullOrEmpty ( _composition ) ;
47
41
48
- public bool IMEEventReceived = false ;
42
+ protected bool IMEEventReceived = false ;
43
+ protected bool LastActionIMEChatInput = true ;
49
44
50
45
private void OnCompositionChanged ( string oldValue , string newValue )
51
46
{
52
47
Debug . WriteLine ( $ "IME: OnCompositionChanged: { newValue . Length - oldValue . Length } ") ;
53
48
54
49
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 ;
61
52
}
62
53
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 = [ ] ;
75
55
76
56
public virtual int CompositionCursorPosition { get ; set ; }
77
57
@@ -97,11 +77,12 @@ protected virtual void OnIMETextInput(char character)
97
77
Debug . WriteLine ( $ "IME: OnIMETextInput: { character } { ( short ) character } ; IMEFocus is null? { IMEFocus == null } ") ;
98
78
99
79
IMEEventReceived = true ;
80
+ LastActionIMEChatInput = true ;
81
+
100
82
if ( IMEFocus != null )
101
83
{
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 ) ;
105
86
}
106
87
}
107
88
@@ -147,13 +128,12 @@ public void OnSelectedChanged(XNATextBox sender)
147
128
148
129
public void RegisterXNATextBox ( XNATextBox sender , Action < char > ? handleCharInput )
149
130
{
150
- XNATextBoxIMEStatus status = GetOrNewXNATextBoxIMEStatus ( sender ) ;
151
- status . HandleChatInput = handleCharInput ;
131
+ TextBoxHandleChatInputCallbacks . Add ( sender , handleCharInput ) ;
152
132
}
153
133
154
134
public void KillXNATextBox ( XNATextBox sender )
155
135
{
156
- IMEStatus . Remove ( sender ) ;
136
+ TextBoxHandleChatInputCallbacks . Remove ( sender ) ;
157
137
}
158
138
159
139
public bool HandleScrollLeftKey ( XNATextBox sender )
@@ -168,18 +148,16 @@ public bool HandleScrollRightKey(XNATextBox sender)
168
148
169
149
public bool HandleBackspaceKey ( XNATextBox sender )
170
150
{
171
- XNATextBoxIMEStatus status = GetOrNewXNATextBoxIMEStatus ( sender ) ;
172
- bool handled = ! status . LastActionIMEChatInput ;
173
- status . LastActionIMEChatInput = true ;
151
+ bool handled = ! LastActionIMEChatInput ;
152
+ LastActionIMEChatInput = true ;
174
153
Debug . WriteLine ( $ "IME: HandleBackspaceKey: handled: { handled } ") ;
175
154
return handled ;
176
155
}
177
156
178
157
public bool HandleDeleteKey ( XNATextBox sender )
179
158
{
180
- XNATextBoxIMEStatus status = GetOrNewXNATextBoxIMEStatus ( sender ) ;
181
- bool handled = ! status . LastActionIMEChatInput ;
182
- status . LastActionIMEChatInput = true ;
159
+ bool handled = ! LastActionIMEChatInput ;
160
+ LastActionIMEChatInput = true ;
183
161
Debug . WriteLine ( $ "IME: HandleDeleteKey: handled: { handled } ") ;
184
162
return handled ;
185
163
}
@@ -210,7 +188,6 @@ public bool HandleEnterKey(XNATextBox sender)
210
188
211
189
public bool HandleEscapeKey ( XNATextBox sender )
212
190
{
213
- XNATextBoxIMEStatus status = GetOrNewXNATextBoxIMEStatus ( sender ) ;
214
191
Debug . WriteLine ( $ "IME: HandleEscapeKey: handled: { IMEEventReceived } ") ;
215
192
return IMEEventReceived ;
216
193
}
0 commit comments