Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Fix Ripple effect with custom background (alternative to #17821) #20412

Merged
merged 19 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
132 changes: 132 additions & 0 deletions src/Controls/samples/Controls.Sample.UITests/Issues/Issue17642.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue17642"
Title="Issue 17642">
<ContentPage.Resources>
<ResourceDictionary>

<Style TargetType="Button">
<Setter Property="Margin" Value="12" />
</Style>

</ResourceDictionary>
</ContentPage.Resources>
<ScrollView>
<StackLayout
Padding="12">
<Button
Text="No Background"/>
<Button
BorderColor="Red"
BorderWidth="4"
Text="No Background with Border"/>
<Button
Text="BackgroundColor"
TextColor="White"
BackgroundColor="Red"/>
<Button
Text="Background"
TextColor="White"
Background="Red"/>
<Button
Text="Gradient Background"
TextColor="White">
<Button.Background>
<LinearGradientBrush
StartPoint="0, 0"
EndPoint="1, 0">
<LinearGradientBrush.GradientStops>
<GradientStop Color="GreenYellow" Offset="0" />
<GradientStop Color="LawnGreen" Offset="0.25" />
<GradientStop Color="Green" Offset="0.5" />
<GradientStop Color="DarkGreen" Offset="0.75" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Button.Background>
</Button>
<Button
CornerRadius="{Binding Value, Source={x:Reference CornerSlider}}"
Text="Corner Radius Background"
TextColor="White">
<Button.Background>
<LinearGradientBrush
StartPoint="0, 0"
EndPoint="1, 0">
<LinearGradientBrush.GradientStops>
<GradientStop Color="GreenYellow" Offset="0" />
<GradientStop Color="LawnGreen" Offset="0.25" />
<GradientStop Color="Green" Offset="0.5" />
<GradientStop Color="DarkGreen" Offset="0.75" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Button.Background>
</Button>
<Label
FontSize="9"
Text="Corner Radius:"/>
<Slider
x:Name="CornerSlider"
Minimum="0"
Maximum="60"
Value="24"/>
<Button
BorderColor="Red"
BorderWidth="{Binding Value, Source={x:Reference BorderWidthSlider}}"
Text="BorderColor"
TextColor="White">
<Button.Background>
<LinearGradientBrush
StartPoint="0, 0"
EndPoint="1, 0">
<LinearGradientBrush.GradientStops>
<GradientStop Color="GreenYellow" Offset="0" />
<GradientStop Color="LawnGreen" Offset="0.25" />
<GradientStop Color="Green" Offset="0.5" />
<GradientStop Color="DarkGreen" Offset="0.75" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Button.Background>
</Button>
<Label
FontSize="9"
Text="BorderWidth:"/>
<Slider
x:Name="BorderWidthSlider"
Minimum="0"
Maximum="12"
Value="4"/>
<Button
x:Name="DynamicBackground"
BorderColor="Red"
BorderWidth="4"
CornerRadius="12"
Text="BorderColor"
TextColor="White">
<Button.Background>
<LinearGradientBrush
StartPoint="0, 0"
EndPoint="1, 0">
<LinearGradientBrush.GradientStops>
<GradientStop Color="GreenYellow" Offset="0" />
<GradientStop Color="LawnGreen" Offset="0.25" />
<GradientStop Color="Green" Offset="0.5" />
<GradientStop Color="DarkGreen" Offset="0.75" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Button.Background>
</Button>
<StackLayout
HorizontalOptions="Center"
Orientation="Horizontal">
<Button
Text="Add"
Clicked="OnAddClicked" />
<Button
Text="Remove"
Clicked="OnRemoveClicked" />
</StackLayout>
</StackLayout>
</ScrollView>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;
using Microsoft.Maui.Graphics;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 17642, "Button Click Ripple Effect Not Working In Android", PlatformAffected.Android)]
public partial class Issue17642 : ContentPage
{
readonly Random _random;

public Issue17642()
{
InitializeComponent();

_random = new Random();
}

void OnAddClicked(object sender, EventArgs args)
{
var linearGradientBrush = new LinearGradientBrush
{
StartPoint = new Point(GetRandomInt(), GetRandomInt()),
EndPoint = new Point(GetRandomInt(), GetRandomInt()),
GradientStops = new GradientStopCollection
{
new GradientStop { Color = GetRandomColor() },
new GradientStop { Color = GetRandomColor() }
}
};

DynamicBackground.Background = linearGradientBrush;
}

void OnRemoveClicked(object sender, EventArgs args)
{
DynamicBackground.Background = null;
}

double GetRandomInt()
{
return _random.NextDouble() * 1;
}

Color GetRandomColor()
{
return Color.FromRgb(_random.Next(256), _random.Next(256), _random.Next(256));
}
}
}
3 changes: 3 additions & 0 deletions src/Controls/src/Core/GradientBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public GradientStopCollection GradientStops
set => SetValue(GradientStopsProperty, value);
}

public override bool IsEmpty =>
GradientStops is null || GradientStops.Count == 0;

static void OnGradientStopsChanged(BindableObject bindable, object oldValue, object newValue)
{
(bindable as GradientBrush)?.UpdateGradientStops(oldValue as GradientStopCollection, newValue as GradientStopCollection);
Expand Down
10 changes: 1 addition & 9 deletions src/Controls/src/Core/LinearGradientBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,7 @@ public LinearGradientBrush(GradientStopCollection gradientStops, Point startPoin
EndPoint = endPoint;
}

/// <include file="../../docs/Microsoft.Maui.Controls/LinearGradientBrush.xml" path="//Member[@MemberName='IsEmpty']/Docs/*" />
public override bool IsEmpty
{
get
{
var linearGradientBrush = this;
return linearGradientBrush == null || linearGradientBrush.GradientStops.Count == 0;
}
}
public override bool IsEmpty => base.IsEmpty;

/// <summary>Bindable property for <see cref="StartPoint"/>.</summary>
public static readonly BindableProperty StartPointProperty = BindableProperty.Create(
Expand Down