Skip to content

Commit

Permalink
feat(xaml): Implement XamlCompositionBrushBase
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed605 committed Sep 5, 2023
1 parent c1db9bc commit 42c8dbe
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<UserControl
x:Class="UITests.Windows_UI_Xaml_Media.XamlCompositionBrushBase.XamlCompositionBrushBaseTests"
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.XamlCompositionBrushBase"
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" Loaded="UserControl_Loaded">

<Grid>
<Grid x:Name="testGrid" Width="200" Height="200" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Uno.UI.Samples.Controls;

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

namespace UITests.Windows_UI_Xaml_Media.XamlCompositionBrushBase
{
[Sample("Windows.UI.Xaml.Media", Name = "XamlCompositionBrushBase", Description = "Provides a base class used to create XAML brushes that paint an area with a CompositionBrush.", IsManualTest = true)]
public sealed partial class XamlCompositionBrushBaseTests : UserControl
{
public XamlCompositionBrushBaseTests()
{
this.InitializeComponent();
}

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
testGrid.Background = new TestBrush();
}
}

class TestBrush : Windows.UI.Xaml.Media.XamlCompositionBrushBase
{
protected override void OnConnected()
{
var compositor = Window.Current.Compositor;
var brush = compositor.CreateLinearGradientBrush();
brush.ColorStops.Add(compositor.CreateColorGradientStop(0.0f, Colors.Black));
brush.ColorStops.Add(compositor.CreateColorGradientStop(1.0f, Colors.White));
brush.StartPoint = new();
brush.EndPoint = new(200, 200);

CompositionBrush = brush;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,7 @@ namespace Windows.UI.Xaml.Media
public partial class XamlCompositionBrushBase : global::Windows.UI.Xaml.Media.Brush
{
// Skipping already declared property FallbackColor
#if __ANDROID__ || __IOS__ || IS_UNIT_TESTS || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "IS_UNIT_TESTS", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public global::Windows.UI.Composition.CompositionBrush CompositionBrush
{
get
{
throw new global::System.NotImplementedException("The member CompositionBrush XamlCompositionBrushBase.CompositionBrush is not implemented. For more information, visit https://aka.platform.uno/notimplemented#m=CompositionBrush%20XamlCompositionBrushBase.CompositionBrush");
}
set
{
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.UI.Xaml.Media.XamlCompositionBrushBase", "CompositionBrush XamlCompositionBrushBase.CompositionBrush");
}
}
#endif
// Skipping already declared property CompositionBrush
// Skipping already declared property FallbackColorProperty
// Skipping already declared method Windows.UI.Xaml.Media.XamlCompositionBrushBase.XamlCompositionBrushBase()
// Forced skipping of method Windows.UI.Xaml.Media.XamlCompositionBrushBase.XamlCompositionBrushBase()
Expand Down
21 changes: 12 additions & 9 deletions src/Uno.UI/UI/Xaml/Media/Brush.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ internal static IDisposable AssignAndObserveBrush(Brush brush, Compositor compos
{
return AssignAndObserveRadialGradientBrush(radialGradientBrush, compositor, brushSetter);
}
else if (brush is XamlCompositionBrushBase unimplementedCompositionBrush)
else if (brush is XamlCompositionBrushBase compositionBrush)
{
return AssignAndObserveXamlCompositionBrush(unimplementedCompositionBrush, compositor, brushSetter);
return AssignAndObserveXamlCompositionBrush(compositionBrush, compositor, brushSetter);
}
else
{
Expand Down Expand Up @@ -197,25 +197,28 @@ private static IDisposable AssignAndObserveRadialGradientBrush(RadialGradientBru
return disposables;
}

/// <summary>
/// Apply fallback colour for unimplemented <see cref="XamlCompositionBrushBase"/> types. For implemented types a more specific method
/// should be supplied.
/// </summary>
private static IDisposable AssignAndObserveXamlCompositionBrush(XamlCompositionBrushBase brush, Compositor compositor, BrushSetterHandler brushSetter)
{
var disposables = new CompositeDisposable();

var compositionBrush = compositor.CreateColorBrush(brush.FallbackColorWithOpacity);
brush.OnConnectedInternal();
var compositionBrush = brush.CompositionBrush ?? compositor.CreateColorBrush(brush.FallbackColorWithOpacity);

brush.RegisterDisposablePropertyChangedCallback(
XamlCompositionBrushBase.FallbackColorProperty,
(s, colorArg) => compositionBrush.Color = brush.FallbackColorWithOpacity
(s, colorArg) => { if (compositionBrush is CompositionColorBrush colorBrush) colorBrush.Color = brush.FallbackColorWithOpacity; }
)
.DisposeWith(disposables);

brush.RegisterDisposablePropertyChangedCallback(
XamlCompositionBrushBase.OpacityProperty,
(s, colorArg) => compositionBrush.Color = brush.FallbackColorWithOpacity
(s, colorArg) => { if (compositionBrush is CompositionColorBrush colorBrush) colorBrush.Color = brush.FallbackColorWithOpacity; }
)
.DisposeWith(disposables);

brush.RegisterDisposablePropertyChangedCallback(
XamlCompositionBrushBase.CompositionBrushProperty,
(s, brushArg) => { if (brush.CompositionBrush is CompositionBrush compBrush) brushSetter(compBrush); }
)
.DisposeWith(disposables);

Expand Down
25 changes: 25 additions & 0 deletions src/Uno.UI/UI/Xaml/Media/XamlCompositionBrushBase.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Windows.UI.Composition;
using Color = Windows.UI.Color;

namespace Windows.UI.Xaml.Media;
Expand Down Expand Up @@ -31,11 +32,35 @@ public Color FallbackColor
/// </summary>
internal Color FallbackColorWithOpacity => FallbackColor.WithOpacity(Opacity);

public CompositionBrush CompositionBrush
{
get
{
return (CompositionBrush)this.GetValue(CompositionBrushProperty);
}
set
{
this.SetValue(CompositionBrushProperty, value);
}
}

/// <summary>
/// Internal DependencyProperty used to track the CompositionBrush property.
/// </summary>
internal static DependencyProperty CompositionBrushProperty { get; } =
DependencyProperty.Register(
nameof(CompositionBrush), typeof(CompositionBrush),
typeof(XamlCompositionBrushBase),
new FrameworkPropertyMetadata(default(CompositionBrush)));

protected virtual void OnConnected()
{
}

protected virtual void OnDisconnected()
{
}

internal void OnConnectedInternal() => OnConnected();
internal void OnDisconnectedInternal() => OnDisconnected();
}

0 comments on commit 42c8dbe

Please sign in to comment.