Skip to content

Commit

Permalink
fix: Update Shape.Fill when LinearGradientBrush gradient stops change
Browse files Browse the repository at this point in the history
Change GradientStopCollection to inherit from DependencyObjectCollection, to invalidate parent view when collection is changed.
  • Loading branch information
davidjohnoliver committed Sep 30, 2020
1 parent 4348caf commit 947d2dd
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using SamplesApp.UITests.TestFramework;
using Uno.UITest.Helpers;

namespace SamplesApp.UITests.Windows_UI_Xaml_Media.GradientBrushTests
{
[TestFixture]
public class LinearGradientBrush_Tests : SampleControlUITestBase
{
[Test]
[AutoRetry]
public void When_GradientStops_Changed()
{
Run("UITests.Windows_UI_Xaml_Media.GradientBrushTests.LinearGradientBrush_Change_Stops");

var rectangle = _app.Marked("GradientBrushRectangle");

_app.WaitForElement(rectangle);

var screenRect = _app.GetRect(rectangle);

var before = TakeScreenshot("Before");

_app.FastTap("ChangeBrushButton");

_app.WaitForText("StatusTextBlock", "Changed");

var after = TakeScreenshot("After");

ImageAssert.AreNotEqual(before, after, screenRect);
}
}
}
7 changes: 7 additions & 0 deletions src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -2837,6 +2837,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Media\GradientBrushTests\LinearGradientBrush_Change_Stops.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Media\ImageBrushTests\ImageBrush_WriteableBitmap.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -4945,6 +4949,9 @@
<DependentUpon>ClosedFigurePage.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Media\GradientBrushTests\GradientsPage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Media\GradientBrushTests\LinearGradientBrush_Change_Stops.xaml.cs">
<DependentUpon>LinearGradientBrush_Change_Stops.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Media\ImageBrushTests\ImageBrush_WriteableBitmap.xaml.cs">
<DependentUpon>ImageBrush_WriteableBitmap.xaml</DependentUpon>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<UserControl
x:Class="UITests.Windows_UI_Xaml_Media.GradientBrushTests.LinearGradientBrush_Change_Stops"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UITests.Windows_UI_Xaml_Media.GradientBrushTests"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<UserControl.Resources>
<Color x:Key="UnoBlueColor">#FF229DFC</Color>
<Color x:Key="UnoPurpleColor">#FF7A69F5</Color>
<Color x:Key="UnoGreenColor">#FF6CE5AE</Color>
<Color x:Key="UnoRedColor">#FFF65678</Color>
</UserControl.Resources>

<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Rectangle x:Name="GradientBrushRectangle"
Height="100"
Width="150"
HorizontalAlignment="Left">
<Rectangle.Fill>
<LinearGradientBrush x:Name="UnoGradientBrush"
StartPoint="0,0"
EndPoint="1,1">
<GradientStop Color="{StaticResource UnoGreenColor}"
Offset="0.0" />
<GradientStop Color="{StaticResource UnoGreenColor}"
Offset="0.1" />
<GradientStop Color="{StaticResource UnoBlueColor}"
Offset="0.40" />
<GradientStop Color="{StaticResource UnoPurpleColor}"
Offset="0.60" />
<GradientStop Color="{StaticResource UnoRedColor}"
Offset="0.9" />
<GradientStop Color="{StaticResource UnoRedColor}"
Offset="1" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Button x:Name="ChangeBrushButton"
Content="Change gradient stops"
Click="ChangeBrushButton_Click" />
<TextBlock x:Name="StatusTextBlock"
Text="Not started" />
</StackPanel>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Uno.UI.Samples.Controls;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236

namespace UITests.Windows_UI_Xaml_Media.GradientBrushTests
{
[Sample("GradientBrush")]
public sealed partial class LinearGradientBrush_Change_Stops : UserControl
{
public LinearGradientBrush_Change_Stops()
{
this.InitializeComponent();
}

private void ChangeBrushButton_Click(object sender, RoutedEventArgs e)
{
UnoGradientBrush.GradientStops.RemoveAt(2);
UnoGradientBrush.GradientStops.RemoveAt(2);
StatusTextBlock.Text = "Changed";
}
}
}
5 changes: 4 additions & 1 deletion src/Uno.UI/UI/Xaml/Media/GradientBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public Color FallbackColor
"GradientStops",
typeof(GradientStopCollection),
typeof(GradientBrush),
new FrameworkPropertyMetadata(null)
new FrameworkPropertyMetadata(
defaultValue: null,
options: FrameworkPropertyMetadataOptions.ValueInheritsDataContext | FrameworkPropertyMetadataOptions.LogicalChild | FrameworkPropertyMetadataOptions.AffectsArrange
)
);

public GradientStopCollection GradientStops
Expand Down
7 changes: 6 additions & 1 deletion src/Uno.UI/UI/Xaml/Media/GradientStopCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

namespace Windows.UI.Xaml.Media
{
public sealed partial class GradientStopCollection : List<GradientStop>, IList<GradientStop>, IEnumerable<GradientStop>
public sealed partial class GradientStopCollection : DependencyObjectCollection<GradientStop>, IList<GradientStop>, IEnumerable<GradientStop>
{
private protected override void OnCollectionChanged()
{
base.OnCollectionChanged();
this.InvalidateArrange();
}
}
}
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Shapes/Shape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Brush Fill
options: FrameworkPropertyMetadataOptions.ValueInheritsDataContext,
propertyChangedCallback: (s, e) => ((Shape)s).OnFillChanged((Brush)e.NewValue)
#else
options: FrameworkPropertyMetadataOptions.ValueInheritsDataContext | FrameworkPropertyMetadataOptions.AffectsArrange,
options: FrameworkPropertyMetadataOptions.ValueInheritsDataContext | FrameworkPropertyMetadataOptions.LogicalChild | FrameworkPropertyMetadataOptions.AffectsArrange,
propertyChangedCallback: (s, e) => ((Shape)s)._brushChanged.Disposable = Brush.AssignAndObserveBrush((Brush)e.NewValue, _ => s.InvalidateArrange())
#endif
)
Expand Down

0 comments on commit 947d2dd

Please sign in to comment.