forked from stride3d/stride
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditText.Android.cs
More file actions
356 lines (302 loc) · 12 KB
/
EditText.Android.cs
File metadata and controls
356 lines (302 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
#if STRIDE_PLATFORM_ANDROID
using System;
using Android.Content;
using Android.Text;
using Android.Views;
using Android.Views.InputMethods;
using Android.Widget;
using Android.Text.Method;
using Stride.Core;
using Stride.Games;
using Stride.Input;
using Exception = System.Exception;
using KeyEvent = Android.Views.KeyEvent;
namespace Stride.UI.Controls
{
public partial class EditText
{
private static readonly object syncRoot = new();
private static MyAndroidEditText staticEditText;
private static EditText activeEditText;
private MyAndroidEditText editText;
private Action editTextSetMinLinesAction;
private Action editTextSetMaxLinesAction;
private Action editTextSetSelectionAction;
private Action editTextSetActivatedStateAction;
private class MyAndroidEditText : Android.Widget.EditText
{
private Action showSoftInputAction;
private Action deselectStrideEditTextAction;
public bool AutoFocus { get; set; }
public MyAndroidEditText(Context context)
: base(context)
{
showSoftInputAction = () =>
{
System.Diagnostics.Debug.Assert(Context is not null);
var inputMethodManager = Context.GetSystemService(Context.InputMethodService) as InputMethodManager;
System.Diagnostics.Debug.Assert(inputMethodManager is not null);
inputMethodManager.ShowSoftInput(this, default(ShowFlags));
};
deselectStrideEditTextAction = () =>
{
lock (syncRoot)
{
if (activeEditText is not null)
{
activeEditText.IsSelectionActive = false;
}
}
};
}
public override bool OnKeyPreIme(Keycode keyCode, KeyEvent e)
{
if (e.KeyCode == Keycode.Back)
{
if (activeEditText is not null)
{
// We use Post instead of deselecting directly because we do not want to close
// the PopupWindow of this control in the same execution as processing an input
// otherwise the control gets disposed while being active
Post(deselectStrideEditTextAction);
}
return true;
}
return base.OnKeyPreIme(keyCode, e);
}
public override void OnWindowFocusChanged(bool hasWindowFocus)
{
// Adapted from https://developer.android.com/develop/ui/views/touch-and-input/keyboard-input/visibility#ShowReliably
// Because the Android EditText will appear in a popup, the control can start in focus
// but it isn't immediately ready to show the soft input, so it must be done this way
if (AutoFocus && hasWindowFocus)
{
RequestFocus();
Post(showSoftInputAction);
}
}
}
private static void InitializeStaticImpl()
{
}
// Delay creation of static edit text to last moment when we are sure to be in Android UI thread.
// -> some Android phones crashes when native edit text is created from another thread than OS UI thread.
private void EnsureStaticEditText()
{
var gameContext = GetGameContext();
if (staticEditText is null || gameContext.RecreateEditTextPopupWindow)
{
// create and add the edit text
staticEditText = new MyAndroidEditText(PlatformAndroid.Context)
{
AutoFocus = true // Will focus & display the soft input when editing begins
};
var popupWindow = gameContext.CreateEditTextPopup(staticEditText);
popupWindow.DismissEvent += (sender, e) =>
{
if (IsSelectionActive)
{
IsSelectionActive = false;
}
};
}
}
private void InitializeImpl()
{
// Cache all the Actions that will be used in EditText.Post to reduce object allocation.
// EditText.Post is used because we are not on the same thread as the Android UI thread,
// and also need to lock on syncRoot to prevent race any condition where Stride thread
// unsets editText while we on the Android UI thread
editTextSetMinLinesAction = () =>
{
lock (syncRoot)
{
editText?.SetMinLines(MinLines);
}
};
editTextSetMaxLinesAction = () =>
{
lock (syncRoot)
{
editText?.SetMaxLines(MaxLines);
}
};
editTextSetSelectionAction = () =>
{
lock (syncRoot)
{
editText?.SetSelection(selectionStart, selectionStop);
}
};
editTextSetActivatedStateAction = () =>
{
lock (syncRoot)
{
var pendingEditText = staticEditText;
// set up the initial state of the android EditText
UpdateInputTypeFromSelf(pendingEditText);
pendingEditText.SetMinLines(MinLines);
pendingEditText.SetMaxLines(MaxLines);
pendingEditText.Text = text;
pendingEditText.SetSelection(selectionStart, selectionStop);
// add callbacks
pendingEditText.EditorAction += AndroidEditTextOnEditorAction;
pendingEditText.AfterTextChanged += AndroidEditTextOnAfterTextChanged;
editText = pendingEditText;
}
};
}
private void AndroidEditTextOnAfterTextChanged(object sender, AfterTextChangedEventArgs afterTextChangedEventArgs)
{
if (editText == null)
return;
var newText = editText.Text;
var oldStart = selectionStart;
var newStart = SelectionStart;
if (newStart <= oldStart) // we erased characters
{
SetTextInternal(newText, false);
}
else // we add or replaced characters
{
// check that new characters are correct.
builder.Clear();
var predicate = CharacterFilterPredicate;
for (int i = oldStart; i < newStart; i++)
{
var character = newText[i];
if (predicate == null || predicate(character))
builder.Append(character);
}
SetTextInternal(newText.Substring(0, oldStart) + builder + newText.Substring(newStart), false);
newStart = Math.Min(oldStart + builder.Length, text.Length);
}
UpdateTextToEditImpl();
Select(newStart, 0);
}
private void AndroidEditTextOnEditorAction(object sender, TextView.EditorActionEventArgs editorActionEventArgs)
{
if (editorActionEventArgs.ActionId == ImeAction.Done)
IsSelectionActive = false;
}
private int GetLineCountImpl()
{
if (editText == null)
return 0;
return editText.LineCount;
}
private void OnMaxLinesChangedImpl()
{
if (editText == null)
return;
editText.Post(editTextSetMaxLinesAction);
}
private void OnMinLinesChangedImpl()
{
if (editText == null)
return;
editText.Post(editTextSetMinLinesAction);
}
private void ActivateEditTextImpl(InputManager inputManager)
{
lock (syncRoot)
{
if (activeEditText is not null)
throw new Exception("Internal error: Can not activate edit text, another edit text is already active");
EnsureStaticEditText();
activeEditText = this;
// Select all text on initial focus.
// TODO: Maybe make it configurable on how caret/selection should default on activation?
SelectAll();
// Note that we do not assign 'staticEditText' to 'editText' until the Post action
// is actually invoked to ensure the control is fully ready
staticEditText.Post(editTextSetActivatedStateAction);
GetGameContext().ShowEditTextPopup();
}
}
private void DeactivateEditTextImpl(InputManager inputManager)
{
lock (syncRoot)
{
if (activeEditText == null)
throw new Exception("Internal error: Can not deactivate the EditText, it is already nullified");
// remove callbacks
editText.EditorAction -= AndroidEditTextOnEditorAction;
editText.AfterTextChanged -= AndroidEditTextOnAfterTextChanged;
editText = null;
activeEditText = null;
GetGameContext().HideEditTextPopup();
}
}
private GameContextAndroid GetGameContext()
{
if (UIElementServices.Services == null)
throw new InvalidOperationException("services");
var game = UIElementServices.Services.GetSafeServiceAs<IGame>();
return ((GameContextAndroid) game.Context);
}
private static void OnTouchMoveImpl(TouchEventArgs args)
{
}
private static void OnTouchDownImpl(TouchEventArgs args)
{
}
private void OnTouchUpImpl(TouchEventArgs args)
{
}
private void UpdateInputTypeImpl()
{
if (editText == null)
return;
UpdateInputTypeFromSelf(editText);
}
private void UpdateInputTypeFromSelf(MyAndroidEditText editText)
{
if (ShouldHideText)
{
editText.TransformationMethod = new PasswordTransformationMethod();
editText.InputType = InputTypes.ClassText | InputTypes.TextVariationPassword;
}
else
{
editText.TransformationMethod = null;
editText.InputType = InputTypes.ClassText | InputTypes.TextVariationNormal;
}
}
private void UpdateTextToEditImpl()
{
if (editText == null)
return;
// Avoid infinite text changed triggering loop.
if (editText.Text != Text)
{
editText.Post(() =>
{
lock (syncRoot)
{
if (editText is not null)
{
editText.Text = text;
}
}
});
}
}
private void UpdateSelectionFromEditImpl()
{
if (editText == null)
return;
selectionStart = editText.SelectionStart;
selectionStop = editText.SelectionEnd;
}
private void UpdateSelectionToEditImpl()
{
if (editText == null)
return;
editText.Post(editTextSetSelectionAction);
}
}
}
#endif