11using System ;
22using Android . Content ;
33using Android . Runtime ;
4+ using Android . Text ;
45using Android . Views ;
56using Android . Views . InputMethods ;
67using Avalonia . Android . Platform . SkiaPlatform ;
7- using Avalonia . Controls . Presenters ;
88using Avalonia . Input . TextInput ;
99
1010namespace Avalonia . Android
@@ -13,7 +13,7 @@ internal interface IAndroidInputMethod
1313 {
1414 public View View { get ; }
1515
16- public ITextInputMethodClient Client { get ; }
16+ public TextInputMethodClient Client { get ; }
1717
1818 public bool IsActive { get ; }
1919
@@ -36,7 +36,7 @@ internal class AndroidInputMethod<TView> : ITextInputMethodImpl, IAndroidInputMe
3636 {
3737 private readonly TView _host ;
3838 private readonly InputMethodManager _imm ;
39- private ITextInputMethodClient _client ;
39+ private TextInputMethodClient _client ;
4040 private AvaloniaInputConnection _inputConnection ;
4141
4242 public AndroidInputMethod ( TView host )
@@ -56,7 +56,7 @@ public AndroidInputMethod(TView host)
5656
5757 public bool IsActive => Client != null ;
5858
59- public ITextInputMethodClient Client => _client ;
59+ public TextInputMethodClient Client => _client ;
6060
6161 public InputMethodManager IMM => _imm ;
6262
@@ -65,7 +65,7 @@ public void Reset()
6565
6666 }
6767
68- public void SetClient ( ITextInputMethodClient client )
68+ public void SetClient ( TextInputMethodClient client )
6969 {
7070 _client = client ;
7171
@@ -77,16 +77,55 @@ public void SetClient(ITextInputMethodClient client)
7777
7878 _imm . ShowSoftInput ( _host , ShowFlags . Implicit ) ;
7979
80- var surroundingText = Client . SurroundingText ;
80+ var selection = Client . Selection ;
8181
82- _imm . UpdateSelection ( _host , surroundingText . AnchorOffset , surroundingText . CursorOffset , surroundingText . AnchorOffset , surroundingText . CursorOffset ) ;
82+ _imm . UpdateSelection ( _host , selection . Start , selection . End , selection . Start , selection . End ) ;
83+
84+ var surroundingText = _client . SurroundingText ?? "" ;
85+
86+ var extractedText = new ExtractedText
87+ {
88+ Text = new Java . Lang . String ( surroundingText ) ,
89+ SelectionStart = selection . Start ,
90+ SelectionEnd = selection . End ,
91+ PartialEndOffset = surroundingText . Length
92+ } ;
93+
94+ _imm . UpdateExtractedText ( _host , _inputConnection ? . ExtractedTextToken ?? 0 , extractedText ) ;
95+
96+ _client . SurroundingTextChanged += _client_SurroundingTextChanged ;
97+ _client . SelectionChanged += _client_SelectionChanged ;
8398 }
8499 else
85100 {
86101 _imm . HideSoftInputFromWindow ( _host . WindowToken , HideSoftInputFlags . ImplicitOnly ) ;
87102 }
88103 }
89104
105+ private void _client_SelectionChanged ( object sender , EventArgs e )
106+ {
107+ var selection = Client . Selection ;
108+
109+ _imm . UpdateSelection ( _host , selection . Start , selection . End , selection . Start , selection . End ) ;
110+
111+ _inputConnection . SetSelection ( selection . Start , selection . End ) ;
112+ }
113+
114+ private void _client_SurroundingTextChanged ( object sender , EventArgs e )
115+ {
116+ var surroundingText = _client . SurroundingText ?? "" ;
117+
118+ _inputConnection . EditableWrapper . IgnoreChange = true ;
119+
120+ _inputConnection . Editable . Replace ( 0 , _inputConnection . Editable . Length ( ) , surroundingText ) ;
121+
122+ _inputConnection . EditableWrapper . IgnoreChange = false ;
123+
124+ var selection = Client . Selection ;
125+
126+ _imm . UpdateSelection ( _host , selection . Start , selection . End , selection . Start , selection . End ) ;
127+ }
128+
90129 public void SetCursorRect ( Rect rect )
91130 {
92131
@@ -134,25 +173,8 @@ public void SetOptions(TextInputOptions options)
134173
135174 outAttrs . ImeOptions |= ImeFlags . NoFullscreen | ImeFlags . NoExtractUi ;
136175
137- _client . TextEditable = _inputConnection . InputEditable ;
138-
139176 return _inputConnection ;
140177 } ) ;
141178 }
142179 }
143-
144- internal readonly record struct ComposingRegion
145- {
146- private readonly int _start = - 1 ;
147- private readonly int _end = - 1 ;
148-
149- public ComposingRegion ( int start , int end )
150- {
151- _start = start ;
152- _end = end ;
153- }
154-
155- public int Start => _start ;
156- public int End => _end ;
157- }
158180}
0 commit comments