Skip to content

Commit

Permalink
Use the TextColor value to set the ClearButton color on the Entry (#1…
Browse files Browse the repository at this point in the history
…3475)

* Use the TextColor value to set the ClearButton color on the Entry

* Auto-format source code

* Fix iOS issue

---------

Co-authored-by: GitHub Actions Autoformatter <[email protected]>
  • Loading branch information
jsuarezruiz and GitHub Actions Autoformatter authored Feb 24, 2023
1 parent af6bdec commit 04bfb27
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
<Entry
Text="Text"
TextColor="Purple"/>
<Label
Text="Using TextColor (for the text color and the clear button color)"
Style="{StaticResource Headline}" />
<Entry
ClearButtonVisibility="WhileEditing"
Text="Text"
TextColor="Red"/>
<Label
Text="With Placeholder"
Style="{StaticResource Headline}" />
Expand Down
9 changes: 5 additions & 4 deletions src/Core/src/Handlers/Entry/EntryHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,15 @@ internal void ShowClearButton()

var drawable = GetClearButtonDrawable();

if (VirtualView?.TextColor is not null)
drawable?.SetColorFilter(VirtualView.TextColor.ToPlatform(), FilterMode.SrcIn);
else
drawable?.ClearColorFilter();

if (PlatformView.LayoutDirection == LayoutDirection.Rtl)
{
PlatformView.SetCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
}
else
{
PlatformView.SetCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
}

_clearButtonVisible = true;
}
Expand Down
57 changes: 56 additions & 1 deletion src/Core/src/Platform/iOS/TextFieldExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using CoreGraphics;
using Foundation;
using Microsoft.Maui.Graphics;
using ObjCRuntime;
Expand Down Expand Up @@ -176,7 +177,61 @@ static UITextPosition GetSelectionEnd(UITextField textField, IEntry entry, UITex

public static void UpdateClearButtonVisibility(this UITextField textField, IEntry entry)
{
textField.ClearButtonMode = entry.ClearButtonVisibility == ClearButtonVisibility.WhileEditing ? UITextFieldViewMode.WhileEditing : UITextFieldViewMode.Never;
if (entry.ClearButtonVisibility == ClearButtonVisibility.WhileEditing)
{
textField.ClearButtonMode = UITextFieldViewMode.WhileEditing;
textField.UpdateClearButtonColor(entry);
}
else
textField.ClearButtonMode = UITextFieldViewMode.Never;
}

internal static void UpdateClearButtonColor(this UITextField textField, IEntry entry)
{
if (textField.ValueForKey(new NSString("clearButton")) is UIButton clearButton)
{
UIImage defaultClearImage = clearButton.ImageForState(UIControlState.Highlighted);

if (entry.TextColor is null)
{
clearButton.SetImage(defaultClearImage, UIControlState.Normal);
clearButton.SetImage(defaultClearImage, UIControlState.Highlighted);
}
else
{
clearButton.TintColor = entry.TextColor.ToPlatform();

var tintedClearImage = GetClearButtonTintImage(defaultClearImage, entry.TextColor.ToPlatform());
clearButton.SetImage(tintedClearImage, UIControlState.Normal);
clearButton.SetImage(tintedClearImage, UIControlState.Highlighted);
}
}
}

internal static UIImage? GetClearButtonTintImage(UIImage image, UIColor color)
{
var size = image.Size;

UIGraphics.BeginImageContextWithOptions(size, false, UIScreen.MainScreen.Scale);

if (UIGraphics.GetCurrentContext() == null)
return null;

var context = UIGraphics.GetCurrentContext();

image.Draw(CGPoint.Empty, CGBlendMode.Normal, 1.0f);
context?.SetFillColor(color.CGColor);
context?.SetBlendMode(CGBlendMode.SourceIn);
context?.SetAlpha(1.0f);

var rect = new CGRect(CGPoint.Empty.X, CGPoint.Empty.Y, image.Size.Width, image.Size.Height);
context?.FillRect(rect);

var tintedImage = UIGraphics.GetImageFromCurrentImageContext();

UIGraphics.EndImageContext();

return tintedImage;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ namespace Microsoft.Maui.DeviceTests
{
public partial class EntryHandlerTests
{
[Theory(DisplayName = "ClearButton Color Initializes Correctly")]
[InlineData(0xFFFF0000)]
[InlineData(0xFF00FF00)]
[InlineData(0xFF0000FF)]
public async Task ClearButtonColorInitializesCorrectly(uint color)
{
var expected = Color.FromUint(color);

var entry = new EntryStub()
{
Text = "Test",
TextColor = expected,
ClearButtonVisibility = ClearButtonVisibility.WhileEditing
};

entry.Focus();

await ValidateHasColor(entry, expected);
}

[Fact(DisplayName = "Padding is the same after background changes")]
public async Task PaddingDoesntChangeAfterBackground()
Expand Down
8 changes: 7 additions & 1 deletion src/Core/tests/DeviceTests/Stubs/StubBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,16 @@ public void InvalidateMeasure()
{
}

public bool Focus() => false;
public bool Focus()
{
IsFocused = true;

return true;
}

public void Unfocus()
{
IsFocused = false;
}

public Size Measure(double widthConstraint, double heightConstraint)
Expand Down

0 comments on commit 04bfb27

Please sign in to comment.