Skip to content

Commit

Permalink
Add GradientStops support to GradientBrush
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamescaper authored and Eilon committed Jun 7, 2022
1 parent 2e98968 commit 9976e74
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Microsoft.MobileBlazorBindings/Elements/GradientBrush.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using Microsoft.AspNetCore.Components;

namespace Microsoft.MobileBlazorBindings.Elements
{
public abstract partial class GradientBrush : Brush
{
#pragma warning disable CA1721 // Property names should not match get methods
[Parameter] public RenderFragment ChildContent { get; set; }
#pragma warning restore CA1721 // Property names should not match get methods

protected override RenderFragment GetChildContent() => ChildContent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using System;
using System.Diagnostics;
using XF = Xamarin.Forms;

namespace Microsoft.MobileBlazorBindings.Elements.Handlers
{
public abstract partial class GradientBrushHandler : BrushHandler, IXamarinFormsContainerElementHandler
{
public void AddChild(XF.Element child, int physicalSiblingIndex)
{
if (!(child is XF.GradientStop gradientStopChild))
{
throw new ArgumentException($"GradientBrush support GradientStop child elements only, but {child?.GetType()} found instead.", nameof(child));
}

if (physicalSiblingIndex <= GradientBrushControl.GradientStops.Count)
{
GradientBrushControl.GradientStops.Insert(physicalSiblingIndex, gradientStopChild);
}
else
{
Debug.WriteLine($"WARNING: {nameof(AddChild)} called with {nameof(physicalSiblingIndex)}={physicalSiblingIndex}, but GradientBrushControl.GradientStops.Count={GradientBrushControl.GradientStops}");
GradientBrushControl.GradientStops.Add(gradientStopChild);
}
}

public int GetChildIndex(XF.Element child)
{
if (!(child is XF.GradientStop gradientStopChild))
{
throw new ArgumentException($"GradientBrush support GradientStop child elements only, but {child?.GetType()} found instead.", nameof(child));
}

return GradientBrushControl.GradientStops.IndexOf(gradientStopChild);
}

public void RemoveChild(XF.Element child)
{
if (!(child is XF.GradientStop gradientStopChild))
{
throw new ArgumentException($"GradientBrush support GradientStop child elements only, but {child?.GetType()} found instead.", nameof(child));
}

GradientBrushControl.GradientStops.Remove(gradientStopChild);
}
}
}

0 comments on commit 9976e74

Please sign in to comment.