Skip to content

Commit

Permalink
fix(revealbrush): Use RevealBrush FallbackColor
Browse files Browse the repository at this point in the history
When a RevealBrush is set, use its FallbackColor for Panel.Background and Border.Background, since the actual reveal effect is not implemented.

This fixes TreeView selection background not being displayed (because, surprise, it's using RevealBrush).
  • Loading branch information
davidjohnoliver committed May 26, 2021
1 parent e9ff8cc commit 035c614
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using SamplesApp.UITests.TestFramework;

namespace SamplesApp.UITests.Windows_UI_Xaml_Media.RevealBrushTests
{
[TestFixture]
class RevealBrush_Tests : SampleControlUITestBase
{
[Test]
[AutoRetry]
public void When_FallbackColor_Set()
{
Run("UITests.Windows_UI_Xaml_Media.BrushesTests.RevealBrush_Fallback");

_app.WaitForElement("StatusTextBlock");

var views = new[]
{
"RevealGrid",
"RevealGridCR",
"RevealBorder",
"RevealBorderCR",
};

using var initial = TakeScreenshot("Initial");
foreach (var view in views)
{
AssertHasColor(view, initial, Color.Orange);
}

_app.FastTap("ChangeColorButton");
_app.WaitForText("StatusTextBlock", "Color changed");

using var after = TakeScreenshot("After");
foreach (var view in views)
{
AssertHasColor(view, after, Color.ForestGreen);
}

void AssertHasColor(string view, ScreenshotInfo screenshot, Color expectedColor)
{
var rect = _app.GetPhysicalRect(view);
ImageAssert.HasColorAt(screenshot, rect.CenterX, rect.CenterY, expectedColor);
}
}
}
}
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 @@ -3413,6 +3413,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Media\BrushesTests\RevealBrush_Fallback.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Media\BrushesTests\SolidColorBrush_Color_Changed.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -6074,6 +6078,9 @@
<DependentUpon>Brushes_ImplicitConvert.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Media\BrushesTests\ColorPresenter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Media\BrushesTests\RevealBrush_Fallback.xaml.cs">
<DependentUpon>RevealBrush_Fallback.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Media\BrushesTests\SolidColorBrush_Color_Changed.xaml.cs">
<DependentUpon>SolidColorBrush_Color_Changed.xaml</DependentUpon>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<UserControl x:Class="UITests.Windows_UI_Xaml_Media.BrushesTests.RevealBrush_Fallback"
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.BrushesTests"
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>
<RevealBackgroundBrush x:Name="OrangeCrush"
Color="Orange"
FallbackColor="Orange" />
</UserControl.Resources>

<StackPanel>
<Button x:Name="ChangeColorButton"
Content="Change color"
Click="ChangeColor" />
<TextBlock x:Name="StatusTextBlock"
Text="Initial" />

<Grid Background="{StaticResource OrangeCrush}"
x:Name="RevealGrid"
Margin="10"
Width="120"
Height="55">
</Grid>
<Grid CornerRadius="10"
Background="{StaticResource OrangeCrush}"
x:Name="RevealGridCR"
Margin="10"
Width="120"
Height="55">
</Grid>

<Border Background="{StaticResource OrangeCrush}"
x:Name="RevealBorder"
Margin="10"
Width="120"
Height="55">
</Border>
<Border CornerRadius="10"
Background="{StaticResource OrangeCrush}"
x:Name="RevealBorderCR"
Margin="10"
Width="120"
Height="55">
</Border>

</StackPanel>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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;
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.BrushesTests
{
[Sample]
public sealed partial class RevealBrush_Fallback : UserControl
{
public RevealBrush_Fallback()
{
this.InitializeComponent();
}

private void ChangeColor(object sender, object args)
{
OrangeCrush.Color = Colors.ForestGreen;
OrangeCrush.FallbackColor = Colors.ForestGreen;
StatusTextBlock.Text = "Color changed";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ private class LayoutState : IEquatable<LayoutState>
public readonly Thickness BorderThickness;
public readonly CornerRadius CornerRadius;
public readonly Thickness Padding;
public readonly Color? BackgroundFallbackColor;

public LayoutState(Windows.Foundation.Rect area, Brush background, Thickness borderThickness, Brush borderBrush, CornerRadius cornerRadius, Thickness padding)
{
Expand All @@ -474,6 +475,8 @@ public LayoutState(Windows.Foundation.Rect area, Brush background, Thickness bor

BackgroundColor = (Background as SolidColorBrush)?.Color;
BorderBrushColor = (BorderBrush as SolidColorBrush)?.Color;

BackgroundFallbackColor = (Background as XamlCompositionBrushBase)?.FallbackColor;
}

public bool Equals(LayoutState other)
Expand All @@ -487,7 +490,8 @@ public bool Equals(LayoutState other)
&& other.BorderBrushColor == BorderBrushColor
&& other.BorderThickness == BorderThickness
&& other.CornerRadius == CornerRadius
&& other.Padding == Padding;
&& other.Padding == Padding
&& other.BackgroundFallbackColor == BackgroundFallbackColor;
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/Border/BorderLayerRenderer.iOSmacOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ private static IDisposable InnerCreateLayer(UIElement owner, CALayer parent, Lay
acrylicBrush.Subscribe(owner, area, adjustedArea, parent, sublayers, ref insertionIndex, fillMask)
.DisposeWith(disposables);
}
else if (background is XamlCompositionBrushBase unsupportedCompositionBrush)
{
Brush.AssignAndObserveBrush(unsupportedCompositionBrush, color => innerLayer.FillColor = color)
.DisposeWith(disposables);
}
else
{
innerLayer.FillColor = Colors.Transparent;
Expand Down Expand Up @@ -268,6 +273,16 @@ private static IDisposable InnerCreateLayer(UIElement owner, CALayer parent, Lay

acrylicBrush.Subscribe(owner, fullArea, insideArea, parent, sublayers, ref insertionIndex, fillMask: null);
}
else if (background is XamlCompositionBrushBase unsupportedCompositionBrush)
{
Brush.AssignAndObserveBrush(unsupportedCompositionBrush, color => parent.BackgroundColor = color)
.DisposeWith(disposables);

// This is required because changing the CornerRadius changes the background drawing
// implementation and we don't want a rectangular background behind a rounded background.
Disposable.Create(() => parent.BackgroundColor = null)
.DisposeWith(disposables);
}
else
{
parent.BackgroundColor = Colors.Transparent;
Expand Down
15 changes: 15 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/Border/BorderLayerRenderer.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ private static IDisposable InnerCreateLayer(UIElement owner, LayoutState state)
Brush.AssignAndObserveBrush(acrylicBrush, color => backgroundShape.FillBrush = compositor.CreateColorBrush(color))
.DisposeWith(disposables);
}
else if (background is XamlCompositionBrushBase unsupportedCompositionBrush)
{
Brush.AssignAndObserveBrush(unsupportedCompositionBrush, color => backgroundShape.FillBrush = compositor.CreateColorBrush(color))
.DisposeWith(disposables);
}
else
{
backgroundShape.FillBrush = null;
Expand Down Expand Up @@ -220,6 +225,16 @@ private static IDisposable InnerCreateLayer(UIElement owner, LayoutState state)
Disposable.Create(() => backgroundShape.FillBrush = null)
.DisposeWith(disposables);
}
else if (background is XamlCompositionBrushBase unsupportedCompositionBrush)
{
Brush.AssignAndObserveBrush(unsupportedCompositionBrush, c => backgroundShape.FillBrush = compositor.CreateColorBrush(c))
.DisposeWith(disposables);

// This is required because changing the CornerRadius changes the background drawing
// implementation and we don't want a rectangular background behind a rounded background.
Disposable.Create(() => backgroundShape.FillBrush = null)
.DisposeWith(disposables);
}
else
{
backgroundShape.FillBrush = null;
Expand Down
5 changes: 5 additions & 0 deletions src/Uno.UI/UI/Xaml/FrameworkElement.Interface.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ private void SetBackgroundBrush(Brush brush)
WindowManagerInterop.SetElementBackgroundGradient(HtmlId, gradientBrush.ToCssString(RenderSize));
RecalculateBrushOnSizeChanged(true);
break;
case XamlCompositionBrushBase unsupportedCompositionBrush:
var fallbackColor = unsupportedCompositionBrush.FallbackColorWithOpacity;
WindowManagerInterop.SetElementBackgroundColor(HtmlId, fallbackColor);
RecalculateBrushOnSizeChanged(false);
break;
default:
WindowManagerInterop.ResetElementBackground(HtmlId);
RecalculateBrushOnSizeChanged(false);
Expand Down
18 changes: 18 additions & 0 deletions src/Uno.UI/UI/Xaml/Media/Brush.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,24 @@ internal static IDisposable AssignAndObserveBrush(Brush b, ColorSetterHandler co

return disposables;
}
else if (b is XamlCompositionBrushBase unsupportedCompositionBrush)
{
var disposables = new CompositeDisposable(2);

colorSetter(unsupportedCompositionBrush.FallbackColorWithOpacity);

unsupportedCompositionBrush.RegisterDisposablePropertyChangedCallback(
XamlCompositionBrushBase.FallbackColorProperty,
(s, args) => colorSetter((s as XamlCompositionBrushBase).FallbackColorWithOpacity))
.DisposeWith(disposables);

unsupportedCompositionBrush.RegisterDisposablePropertyChangedCallback(
OpacityProperty,
(s, args) => colorSetter((s as XamlCompositionBrushBase).FallbackColorWithOpacity))
.DisposeWith(disposables);

return disposables;
}
else
{
colorSetter(SolidColorBrushHelper.Transparent.Color);
Expand Down
16 changes: 16 additions & 0 deletions src/Uno.UI/UI/Xaml/Media/Brush.Skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ internal static IDisposable AssignAndObserveBrush(Brush b, ColorSetterHandler co

return disposables;
}
else if (b is XamlCompositionBrushBase unsupportedCompositionBrush)
{
colorSetter(unsupportedCompositionBrush.FallbackColorWithOpacity);

unsupportedCompositionBrush.RegisterDisposablePropertyChangedCallback(
XamlCompositionBrushBase.FallbackColorProperty,
(s, colorArg) => colorSetter((s as XamlCompositionBrushBase).FallbackColorWithOpacity)
)
.DisposeWith(disposables);

unsupportedCompositionBrush.RegisterDisposablePropertyChangedCallback(
OpacityProperty,
(s, colorArg) => colorSetter((s as XamlCompositionBrushBase).FallbackColorWithOpacity)
)
.DisposeWith(disposables);
}
else
{
colorSetter(SolidColorBrushHelper.Transparent.Color);
Expand Down
19 changes: 19 additions & 0 deletions src/Uno.UI/UI/Xaml/Media/Brush.iOSmacOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,25 @@ internal static IDisposable AssignAndObserveBrush(Brush b, ColorSetterHandler co

return disposables;
}
else if (b is XamlCompositionBrushBase unsupportedCompositionBrush)
{
var disposables = new CompositeDisposable(2);
colorSetter(unsupportedCompositionBrush.FallbackColorWithOpacity);

unsupportedCompositionBrush.RegisterDisposablePropertyChangedCallback(
XamlCompositionBrushBase.FallbackColorProperty,
(s, colorArg) => colorSetter((s as XamlCompositionBrushBase).FallbackColorWithOpacity)
)
.DisposeWith(disposables);

unsupportedCompositionBrush.RegisterDisposablePropertyChangedCallback(
OpacityProperty,
(s, colorArg) => colorSetter((s as XamlCompositionBrushBase).FallbackColorWithOpacity)
)
.DisposeWith(disposables);

return disposables;
}
else
{
colorSetter(SolidColorBrushHelper.Transparent.Color);
Expand Down
21 changes: 21 additions & 0 deletions src/Uno.UI/UI/Xaml/Media/Brush.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ internal static IDisposable AssignAndObserveBrush(Brush b, ColorSetterHandler co
return Disposable.Empty;
}

if (b is XamlCompositionBrushBase unsupportedCompositionBrush)
{
//
var disposables = new CompositeDisposable(2);
colorSetter(unsupportedCompositionBrush.FallbackColorWithOpacity);

unsupportedCompositionBrush.RegisterDisposablePropertyChangedCallback(
XamlCompositionBrushBase.FallbackColorProperty,
(s, colorArg) => colorSetter((s as XamlCompositionBrushBase).FallbackColorWithOpacity)
)
.DisposeWith(disposables);

unsupportedCompositionBrush.RegisterDisposablePropertyChangedCallback(
OpacityProperty,
(s, colorArg) => colorSetter((s as XamlCompositionBrushBase).FallbackColorWithOpacity)
)
.DisposeWith(disposables);

return disposables;
}

colorSetter(SolidColorBrushHelper.Transparent.Color);
return Disposable.Empty;
}
Expand Down

0 comments on commit 035c614

Please sign in to comment.