Skip to content

Commit

Permalink
Removes the transparent color set on Android when null (#1584)
Browse files Browse the repository at this point in the history
* Removed the transparent color that was being set on Android when the icon tint color is set to null.

* Add `null` Color to IconTintColorBehavior in Sample App

* Update Sample

---------

Co-authored-by: Brandon Minnick <13558917+brminnick@users.noreply.github.com>
  • Loading branch information
vhugogarcia and brminnick authored Nov 30, 2023
1 parent 4168e5a commit 45ca10c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
xmlns:vm="clr-namespace:CommunityToolkit.Maui.Sample.ViewModels.Behaviors"
Title="IconTintColorBehaviorPage"
x:DataType="vm:IconTintColorBehaviorViewModel"
x:TypeArguments="vm:IconTintColorBehaviorViewModel"
x:Name="this">
x:TypeArguments="vm:IconTintColorBehaviorViewModel">
<ScrollView>
<Grid
Padding="20"
Expand Down Expand Up @@ -73,23 +72,23 @@
Source="shield.png" />

<ImageButton
x:Name="UpdateImage"
Grid.Row="4"
Grid.Column="1"
Clicked="ChangeSource_Clicked"
Source="shield.png">
Command="{Binding ToggleImageButtonCommand, Mode=OneTime}"
Source="{Binding ToggleableImageSource}">
<ImageButton.Behaviors>
<mct:IconTintColorBehavior TintColor="Red" />
</ImageButton.Behaviors>
</ImageButton>

<ImageButton
x:Name="ToggleColorImageButton"
Grid.Row="4"
Grid.Column="2"
Command="{Binding ChangeCommand}"
Command="{Binding ChangeColorCommand}"
Source="shield.png">
<ImageButton.Behaviors>
<mct:IconTintColorBehavior TintColor="{Binding Source={x:Reference this}, Path=BindingContext.ChangedColor}" />
<mct:IconTintColorBehavior TintColor="{Binding Source={x:Reference ToggleColorImageButton}, Path=BindingContext.ToggleableIconTintColor}" />
</ImageButton.Behaviors>
</ImageButton>

Expand All @@ -98,13 +97,13 @@
Grid.Column="2"
FontSize="Micro"
FontAttributes="Italic"
Text="The ImageButton above uses Binding in order to change the TintColor. Click it!"/>
Text="This ImageButton uses Bindings to change the TintColor. Click it!"/>
<Label
Grid.Row="5"
Grid.Column="1"
FontSize="Micro"
FontAttributes="Italic"
Text="The ImageButton above updates the image source. Click it!"/>
Text="This ImageButton uses Bindings to change the ImageSource. Click it!"/>

</Grid>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,4 @@ public IconTintColorBehaviorPage(IconTintColorBehaviorViewModel iconTintColorBeh
{
InitializeComponent();
}

void ChangeSource_Clicked(System.Object sender, System.EventArgs e)
{
var imageSource = (ImageButton)sender;
var selectedImage = imageSource.Source as FileImageSource;

if (selectedImage is not null)
{
if (selectedImage.File == "dotnet_bot.png")
{
UpdateImage.Source = "shield.png";
}
else
{
UpdateImage.Source = "dotnet_bot.png";
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,37 @@ namespace CommunityToolkit.Maui.Sample.ViewModels.Behaviors;

public partial class IconTintColorBehaviorViewModel : BaseViewModel
{
const string dotnetBotImageFileName = "dotnet_bot.png";
const string shieldImageFileName = "shield.png";

static readonly IEnumerator<Color?> toggleableColorsEnumerator = new List<Color?> { Colors.Red, Colors.Green, null }.GetEnumerator();

[ObservableProperty]
string toggleableImageSource = shieldImageFileName;

[ObservableProperty]
Color? toggleableIconTintColor = toggleableColorsEnumerator.Current;

[RelayCommand]
void Change()
void ToggleImageButton()
{
IsChanged = !IsChanged;
ToggleableImageSource = ToggleableImageSource switch
{
dotnetBotImageFileName => shieldImageFileName,
shieldImageFileName => dotnetBotImageFileName,
_ => throw new NotSupportedException("Invalid image source")
};
}

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ChangedColor))]
bool isChanged;
[RelayCommand]
void ChangeColor()
{
if (!toggleableColorsEnumerator.MoveNext())
{
toggleableColorsEnumerator.Reset();
toggleableColorsEnumerator.MoveNext();
}

public Color ChangedColor => IsChanged ? Colors.Red : Colors.Green;
ToggleableIconTintColor = toggleableColorsEnumerator.Current;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static void SetImageViewTintColor(ImageView image, Color? color)
if (color is null)
{
image.ClearColorFilter();
color = Colors.Transparent;
return;
}

image.SetColorFilter(new PorterDuffColorFilter(color.ToPlatform(), PorterDuff.Mode.SrcIn ?? throw new InvalidOperationException("PorterDuff.Mode.SrcIn should not be null at runtime.")));
Expand Down

0 comments on commit 45ca10c

Please sign in to comment.