@ -28,6 +28,7 @@ using Math = System.Math;
using AndroidRect = Android . Graphics . Rect ;
using Window = Android . Views . Window ;
using Android.Graphics.Drawables ;
using Java.Util ;
namespace Avalonia.Android.Platform.SkiaPlatform
{
@ -410,159 +411,73 @@ namespace Avalonia.Android.Platform.SkiaPlatform
{
private readonly TopLevelImpl _ topLevel ;
private readonly IAndroidInputMethod _ inputMethod ;
private readonly InputEditable _ editable ;
public AvaloniaInputConnection ( TopLevelImpl topLevel , IAndroidInputMethod inputMethod ) : base ( inputMethod . View , true )
{
_ topLevel = topLevel ;
_ inputMethod = inputMethod ;
_ editable = new InputEditable ( _ topLevel , _ inputMethod , this ) ;
}
public TextInputMethodSurroundingText SurroundingText { get ; set ; }
public override IEditable Editable = > _ editable ;
public string ComposingText { get ; internal set ; }
public ComposingRegion ? ComposingRegion { get ; internal set ; }
public bool IsComposing = > ! string . IsNullOrEmpty ( ComposingText ) ;
public bool IsCommiting { get ; private set ; }
internal InputEditable InputEditable = > _ editable ;
public override bool SetComposingRegion ( int start , int end )
{
//System.Diagnostics.Debug.WriteLine($"Composing Region: [{start}|{end}] {SurroundingText.Text?.Substring(start, end - start)}");
var ret = base . SetComposingRegion ( start , end ) ;
ComposingRegion = new ComposingRegion ( start , end ) ;
InputEditable . RaiseCompositionChanged ( ) ;
return base . SetComposingRegion ( start , end ) ;
return ret ;
}
public override bool SetComposingText ( ICharSequence text , int newCursorPosition )
{
var composingText = text . ToString ( ) ;
ComposingText = composingText ;
_ inputMethod . Client ? . SetPreeditText ( ComposingText ) ;
return base . SetComposingText ( text , newCursorPosition ) ;
}
public override bool FinishComposingText ( )
{
if ( ! string . IsNullOrEmpty ( ComposingText ) )
if ( string . IsNullOrEmpty ( composingText ) )
{
CommitText ( ComposingText , ComposingText . Length ) ;
return CommitText ( text , newCursorPosition ) ;
}
else
{
ComposingRegion = new ComposingRegion ( SurroundingText . CursorOffset , SurroundingText . CursorOffset ) ;
}
return base . FinishComposingText ( ) ;
}
public override ICharSequence GetTextBeforeCursorFormatted ( int length , [ GeneratedEnum ] GetTextFlags flags )
{
if ( ! string . IsNullOrEmpty ( SurroundingText . Text ) & & length > 0 )
{
var start = System . Math . Max ( SurroundingText . CursorOffset - length , 0 ) ;
var ret = base . SetComposingText ( text , newCursorPosition ) ;
var end = System . Math . Min ( start + length - 1 , SurroundingText . CursorOffset ) ;
InputEditable . RaiseCompositionChanged ( ) ;
var text = SurroundingText . Text . Substring ( start , end - start ) ;
//System.Diagnostics.Debug.WriteLine($"Text Before: {text}");
return new Java . Lang . String ( text ) ;
return ret ;
}
return null ;
}
public override ICharSequence GetTextAfterCursorFormatted ( int length , [ GeneratedEnum ] GetTextFlags flags )
public override bool BeginBatchEdit ( )
{
if ( ! string . IsNullOrEmpty ( SurroundingText . Text ) )
{
var start = SurroundingText . CursorOffset ;
var end = System . Math . Min ( start + length , SurroundingText . Text . Length ) ;
var text = SurroundingText . Text . Substring ( start , end - start ) ;
_ editable . BeginBatchEdit ( ) ;
//System.Diagnostics.Debug.WriteLine($"Text After: {text}");
return new Java . Lang . String ( text ) ;
}
return null ;
return base . BeginBatchEdit ( ) ;
}
public override bool CommitText ( ICharSequence text , int newCursorPosition )
public override bool EndBatchEdit ( )
{
IsCommiting = true ;
var committedText = text . ToString ( ) ;
_ inputMethod . Client . SetPreeditText ( null ) ;
var ret = base . EndBatchEdit ( ) ;
_ editable . EndBatchEdit ( ) ;
int? start , end ;
if ( SurroundingText . CursorOffset ! = SurroundingText . AnchorOffset )
{
start = Math . Min ( SurroundingText . CursorOffset , SurroundingText . AnchorOffset ) ;
end = Math . Max ( SurroundingText . CursorOffset , SurroundingText . AnchorOffset ) ;
}
else if ( ComposingRegion ! = null )
{
start = ComposingRegion ? . Start ;
end = ComposingRegion ? . End ;
ComposingRegion = null ;
}
else
{
start = end = _ inputMethod . Client . SurroundingText . CursorOffset ;
}
_ inputMethod . Client . SelectInSurroundingText ( ( int ) start , ( int ) end ) ;
var time = DateTime . Now . TimeOfDay ;
var rawTextEvent = new RawTextInputEventArgs ( KeyboardDevice . Instance , ( ulong ) time . Ticks , _ topLevel . InputRoot , committedText ) ;
_ topLevel . Input ( rawTextEvent ) ;
ComposingText = null ;
ComposingRegion = new ComposingRegion ( newCursorPosition , newCursorPosition ) ;
return base . CommitText ( text , newCursorPosition ) ;
return ret ;
}
public override bool DeleteSurroundingText ( int beforeLength , int afterLength )
public override bool FinishComposingText ( )
{
var surroundingText = _ inputMethod . Client . SurroundingText ;
var selectionStart = surroundingText . CursorOffset ;
_ inputMethod . Client . SelectInSurroundingText ( selectionStart - beforeLength , selectionStart + afterLength ) ;
_ inputMethod . View . DispatchKeyEvent ( new KeyEvent ( KeyEventActions . Down , Keycode . ForwardDel ) ) ;
surroundingText = _ inputMethod . Client . SurroundingText ;
selectionStart = surroundingText . CursorOffset ;
ComposingRegion = new ComposingRegion ( selectionStart , selectionStart ) ;
return base . DeleteSurroundingText ( beforeLength , afterLength ) ;
var ret = base . FinishComposingText ( ) ;
InputEditable . RaiseCompositionChanged ( ) ;
return ret ;
}
public override bool SetSelection ( int star t, int end )
public override bool CommitText ( ICharSequence text , int newCursorPosition )
{
_ inputMethod . Client . SelectInSurroundingText ( start , end ) ;
ComposingRegion = new ComposingRegion ( start , end ) ;
return base . SetSelection ( start , end ) ;
var ret = base . CommitText ( text , newCursorPosition ) ;
InputEditable . RaiseCompositionChanged ( ) ;
return ret ;
}
public override bool PerformEditorAction ( [ GeneratedEnum ] ImeAction actionCode )