Skip to content

Commit

Permalink
fix: fixes TextBox cursor color for Android 29+
Browse files Browse the repository at this point in the history
  • Loading branch information
Hudney committed Aug 27, 2021
1 parent 52d23a3 commit 21a8389
Showing 1 changed file with 53 additions and 35 deletions.
88 changes: 53 additions & 35 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBoxView.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,11 @@ private static void PrepareFields(Context? context)
}
var editText = new EditText(context);

_cursorDrawableResField = textViewClass.GetDeclaredField("mCursorDrawableRes");
_cursorDrawableResField.Accessible = true;
if ((int)Build.VERSION.SdkInt < 29)
{
_cursorDrawableResField = textViewClass.GetDeclaredField("mCursorDrawableRes");
_cursorDrawableResField.Accessible = true;
}

_editorField = textViewClass.GetDeclaredField("mEditor");
_editorField.Accessible = true;
Expand All @@ -146,7 +149,7 @@ private static void PrepareFields(Context? context)
{
_cursorDrawableField = _editorField.Get(editText)?.Class.GetDeclaredField("mCursorDrawable");
}
else
else if((int)Build.VERSION.SdkInt < 29)
{
// set differently in Android P (API 28) and higher
_cursorDrawableField = _editorField.Get(editText)?.Class.GetDeclaredField("mDrawableForCursor");
Expand All @@ -167,51 +170,66 @@ public static void SetCursorColor(EditText editText, Color color)
PrepareFields(editText.Context);
}

if (_cursorDrawableField == null || _cursorDrawableResField == null || _editorField == null || PorterDuff.Mode.SrcIn == null)
if (PorterDuff.Mode.SrcIn == null)
{
// PrepareFields() failed, give up now
editText.Log().WarnIfEnabled(() => "Failed to change the cursor color. Some devices don't support this.");
return;
}

var mCursorDrawableRes = _cursorDrawableResField.GetInt(editText);
var editor = _editorField.Get(editText);

#if __ANDROID_28__
if ((int)Build.VERSION.SdkInt >= 29)
{
#pragma warning disable 618 // SetColorFilter is deprecated
if ((int)Build.VERSION.SdkInt < 28) // 28 means BuildVersionCodes.P
var drawable = editText.TextCursorDrawable;
drawable?.SetColorFilter(color, PorterDuff.Mode.SrcIn);
#pragma warning restore 618 // SetColorFilter is deprecated
}
else if (_cursorDrawableField == null || _cursorDrawableResField == null || _editorField == null || PorterDuff.Mode.SrcIn == null)
{
var drawables = new Drawable[2];
drawables[0] = ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
drawables[1] = ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
drawables[0].SetColorFilter(color, PorterDuff.Mode.SrcIn);
drawables[1].SetColorFilter(color, PorterDuff.Mode.SrcIn);
_cursorDrawableField.Set(editor, drawables);
// PrepareFields() failed, give up now
editText.Log().WarnIfEnabled(() => "Failed to change the cursor color. Some devices don't support this.");
return;
}
else
{
var drawable = ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
drawable.SetColorFilter(color, PorterDuff.Mode.SrcIn);
_cursorDrawableField.Set(editor, drawable);
}
var mCursorDrawableRes = _cursorDrawableResField.GetInt(editText);
var editor = _editorField.Get(editText);

#if __ANDROID_28__
#pragma warning disable 618 // SetColorFilter is deprecated
if ((int)Build.VERSION.SdkInt < 28) // 28 means BuildVersionCodes.P
{
var drawables = new Drawable[2];
drawables[0] = ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
drawables[1] = ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
drawables[0].SetColorFilter(color, PorterDuff.Mode.SrcIn);
drawables[1].SetColorFilter(color, PorterDuff.Mode.SrcIn);
_cursorDrawableField.Set(editor, drawables);
}
else
{
var drawable = ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
drawable.SetColorFilter(color, PorterDuff.Mode.SrcIn);
_cursorDrawableField.Set(editor, drawable);
}
#pragma warning restore 618 // SetColorFilter is deprecated
#else
if ((int)Build.VERSION.SdkInt < 28) // 28 means BuildVersionCodes.P
{
var drawables = new Drawable[2];
drawables[0] = ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
drawables[1] = ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
drawables[0].SetColorFilter(new BlendModeColorFilterCompat(color, BlendModeCompat.SrcIn));
drawables[1].SetColorFilter(new BlendModeColorFilterCompat(color, BlendModeCompat.SrcIn));
_cursorDrawableField.Set(editor, drawables);
}
else
{
var drawable = ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
drawable.SetColorFilter(new BlendModeColorFilterCompat(color, BlendModeCompat.SrcIn));
_cursorDrawableField.Set(editor, drawable);
}
if ((int)Build.VERSION.SdkInt < 28) // 28 means BuildVersionCodes.P
{
var drawables = new Drawable[2];
drawables[0] = ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
drawables[1] = ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
drawables[0].SetColorFilter(new BlendModeColorFilterCompat(color, BlendModeCompat.SrcIn));
drawables[1].SetColorFilter(new BlendModeColorFilterCompat(color, BlendModeCompat.SrcIn));
_cursorDrawableField.Set(editor, drawables);
}
else
{
var drawable = ContextCompat.GetDrawable(editText.Context, mCursorDrawableRes);
drawable.SetColorFilter(new BlendModeColorFilterCompat(color, BlendModeCompat.SrcIn));
_cursorDrawableField.Set(editor, drawable);
}
#endif
}
}
catch (Exception)
{
Expand Down

0 comments on commit 21a8389

Please sign in to comment.