Skip to content

Commit 90746b8

Browse files
committed
fix(wasm): Keep the TextBox in sync with Android soft keyboard input
Port of unoplatform#24502 (closes unoplatform#22230) to servicing/6.8. Differences from the master version: - Master's TextBoxCore.IME.cs changes are applied to TextBox.IME.skia.cs (6.8 has no TextBoxCore): the same ResetComposition / RaiseTextCompositionEnded helpers, StartIndex and TextChangePending handling and pending-flag resets, raising the TextBox's own events instead of going through the host. - ImeCompositionEventArgs.StartIndex / TextChangePending are added to ImeCompositionEventArgs.skia.cs (master: ImeCompositionEventArgs.cs). - IsReadOnly is forwarded to the view from TextBox.skia.cs's OnIsReadonlyChangedPartial rather than from TextBoxCore. - BrowserInvisibleTextBoxViewExtension uses the TextBox directly (master uses the ITextBoxHost core); the TypeScript, the IME extension and the WASM-only Given_TextBox.BrowserSoftKeyboard tests are verbatim.
1 parent 6e25166 commit 90746b8

7 files changed

Lines changed: 1545 additions & 130 deletions

File tree

‎src/Uno.UI.Runtime.Skia.WebAssembly.Browser/UI/Xaml/Controls/TextBox/BrowserInvisibleTextBoxViewExtension.cs‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ internal partial class BrowserInvisibleTextBoxViewExtension : IOverlayTextBoxVie
1717
private readonly TextBoxView _view;
1818
private bool _isNativeInputActive;
1919

20+
// Selection the TextBox had when the input last reported a change, which is where a rejected
21+
// change (BeforeTextChanging, coercion) puts the caret back.
22+
private static (int start, int length) _selectionBeforeInput;
23+
2024
public BrowserInvisibleTextBoxViewExtension(TextBoxView view)
2125
{
2226
_view = view;
@@ -32,8 +36,21 @@ private static void OnInputTextChanged(string text, int selectionStart, int sele
3236
// We are expecting this to be called only when the TextBox is focused, as it's the result of an interaction with the native HTML input.
3337
if (FocusManager.GetFocusedElement(xamlRoot!) is TextBox textBox)
3438
{
39+
_selectionBeforeInput = (textBox.SelectionStart, textBox.SelectionLength);
40+
// Applied by the text change itself, so the caret moves straight to where the input has it
41+
// rather than through the start of the text first.
42+
textBox.SetPendingSelection(selectionStart, selectionLength);
3543
textBox.TextBoxView.UpdateTextFromNative(text);
36-
textBox.SelectInternal(selectionStart, selectionLength);
44+
if (textBox.Text == text.Replace('\n', '\r'))
45+
{
46+
textBox.SelectInternal(selectionStart, selectionLength);
47+
}
48+
else
49+
{
50+
// The change was rejected or coerced and the TextBox's text written back to the input, whose
51+
// caret then has to follow the TextBox's; an unchanged selection would not be pushed on its own.
52+
textBox.TextBoxView.Select(textBox.SelectionStart, textBox.SelectionLength);
53+
}
3754
}
3855
}
3956

@@ -114,7 +131,8 @@ public void StartEntry()
114131
_view.TextBox?.Text,
115132
_view.TextBox?.AcceptsReturn ?? false,
116133
GetInputModeValue(),
117-
GetEnterKeyHintValue());
134+
GetEnterKeyHintValue(),
135+
_view.TextBox?.IsReadOnly ?? false);
118136

119137
if (_isNativeInputActive)
120138
{
@@ -244,12 +262,13 @@ public void UpdateProperties()
244262
{
245263
NativeMethods.SetEnterKeyHint(enterKeyHintValue);
246264
}
265+
NativeMethods.SetReadOnly(_view.TextBox?.IsReadOnly ?? false);
247266
}
248267

249268
public int GetSelectionStart() => 0;
250269
public int GetSelectionLength() => 0;
251-
public int GetSelectionStartBeforeKeyDown() => 0;
252-
public int GetSelectionLengthBeforeKeyDown() => 0;
270+
public int GetSelectionStartBeforeKeyDown() => _selectionBeforeInput.start;
271+
public int GetSelectionLengthBeforeKeyDown() => _selectionBeforeInput.length;
253272

254273
private string GetEnterKeyHintValue()
255274
{
@@ -279,7 +298,7 @@ private static partial class NativeMethods
279298
public static partial void SetText(string text);
280299

281300
[JSImport("globalThis.Uno.UI.Runtime.Skia.BrowserInvisibleTextBoxViewExtension.focus")]
282-
public static partial bool Focus(IntPtr handle, bool isPassword, string? text, bool acceptsReturn, string inputMode, string enterKeyHint);
301+
public static partial bool Focus(IntPtr handle, bool isPassword, string? text, bool acceptsReturn, string inputMode, string enterKeyHint, bool isReadOnly);
283302

284303
[JSImport("globalThis.Uno.UI.Runtime.Skia.BrowserInvisibleTextBoxViewExtension.blur")]
285304
public static partial void Blur(IntPtr handle);
@@ -302,6 +321,9 @@ private static partial class NativeMethods
302321
[JSImport("globalThis.Uno.UI.Runtime.Skia.BrowserInvisibleTextBoxViewExtension.setEnterKeyHint")]
303322
public static partial void SetEnterKeyHint(string setEnterKeyHint);
304323

324+
[JSImport("globalThis.Uno.UI.Runtime.Skia.BrowserInvisibleTextBoxViewExtension.setReadOnly")]
325+
public static partial void SetReadOnly(bool isReadOnly);
326+
305327
[JSImport("globalThis.Uno.UI.Runtime.Skia.BrowserInvisibleTextBoxViewExtension.setInputMode")]
306328
public static partial void SetInputMode(string inputMode);
307329
}

‎src/Uno.UI.Runtime.Skia.WebAssembly.Browser/UI/Xaml/Controls/TextBox/WasmImeTextBoxExtension.cs‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ namespace Uno.UI.Runtime.Skia;
1111
/// WASM browser implementation of <see cref="IImeTextBoxExtension"/>.
1212
/// Bridges browser CompositionEvent APIs (compositionstart/compositionupdate/compositionend)
1313
/// to the managed TextBox composition event lifecycle (Started → Updated → Completed → Ended).
14+
/// The hidden input applies the text itself (synced through the input event), so composition
15+
/// events only carry the preedit and where it sits in the text, and a completion is only reported
16+
/// once the committed text is in the input.
1417
/// </summary>
1518
internal sealed partial class WasmImeTextBoxExtension : IImeTextBoxExtension
1619
{
@@ -50,16 +53,22 @@ private static void OnCompositionStarted()
5053
}
5154

5255
[JSExport]
53-
private static void OnCompositionUpdated(string text, int cursorPosition)
56+
private static void OnCompositionUpdated(string text, int startIndex, bool textChangePending)
5457
{
55-
Instance.CompositionUpdated?.Invoke(Instance, new ImeCompositionEventArgs(text, cursorPosition));
58+
Instance.CompositionUpdated?.Invoke(Instance, new ImeCompositionEventArgs(text, textAlreadyApplied: true, startIndex: startIndex, textChangePending: textChangePending));
5659
}
5760

5861
[JSExport]
5962
private static void OnCompositionCompleted(string text)
6063
{
64+
// A compositionend for a composition that a managed text change already ended has nothing left to commit.
65+
if (!Instance._isComposing)
66+
{
67+
return;
68+
}
69+
6170
Instance._isComposing = false;
62-
Instance.CompositionCompleted?.Invoke(Instance, new ImeCompositionEventArgs(text));
71+
Instance.CompositionCompleted?.Invoke(Instance, new ImeCompositionEventArgs(text, textAlreadyApplied: true));
6372
Instance.CompositionEnded?.Invoke(Instance, EventArgs.Empty);
6473
}
6574

0 commit comments

Comments
 (0)