Skip to content

Commit

Permalink
fix: fixes TextBox cursor color for Android 29+
Browse files Browse the repository at this point in the history
Addresses #6240
  • Loading branch information
Hudney committed Sep 2, 2021
1 parent 67b3a9f commit 375db65
Showing 1 changed file with 57 additions and 37 deletions.
94 changes: 57 additions & 37 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,11 +149,12 @@ private static void PrepareFields(Context? context)
{
_cursorDrawableField = _editorField.Get(editText)?.Class.GetDeclaredField("mCursorDrawable");
}
else
else if ((int)Build.VERSION.SdkInt == 28)
{
// set differently in Android P (API 28) and higher
// set differently in Android P (API 28)
_cursorDrawableField = _editorField.Get(editText)?.Class.GetDeclaredField("mDrawableForCursor");
}
// Android versions 29+ are handled directly through SetColorFilter API

if (_cursorDrawableField != null)
{
Expand All @@ -167,51 +171,67 @@ 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__
#pragma warning disable 618 // SetColorFilter is deprecated
if ((int)Build.VERSION.SdkInt < 28) // 28 means BuildVersionCodes.P
if ((int)Build.VERSION.SdkInt >= 29)
{
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);
var drawable = editText.TextCursorDrawable;
if (BlendMode.SrcAtop != null)
{
drawable?.SetColorFilter(new BlendModeColorFilter(color, BlendMode.SrcAtop));
}
}
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
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(new BlendModeColorFilterCompat(color, BlendModeCompat.SrcIn));
drawables[1].SetColorFilter(new BlendModeColorFilterCompat(color, BlendModeCompat.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(new BlendModeColorFilterCompat(color, BlendModeCompat.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);
}
#endif
}
}
catch (Exception)
{
Expand Down

0 comments on commit 375db65

Please sign in to comment.