Skip to content

Commit

Permalink
Add support for Shapes (#34)
Browse files Browse the repository at this point in the history
* Add support for Shapes

* Add Shapes ControlGallery page
  • Loading branch information
Dreamescaper authored Jul 23, 2022
1 parent ca5acc6 commit 3c826da
Show file tree
Hide file tree
Showing 26 changed files with 925 additions and 2 deletions.
1 change: 1 addition & 0 deletions samples/ControlGallery/AppShell.razor
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@*Present data*@
<BoxViewPage />
<LabelPage />
<Shapes />

@*Initiate commands*@
<ButtonPage />
Expand Down
75 changes: 75 additions & 0 deletions samples/ControlGallery/Views/PresentData/Shapes.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
@using BlazorBindings.Maui.Elements.Shapes
@using Microsoft.Maui.Controls.Shapes
@using System.Globalization

<ContentPage Title="Shapes">
<Grid RowDefinitions="Auto,*">
<Slider Grid.Row="0" @bind-Value="_value" Maximum="20" />

<ScrollView Grid.Row="1">
<VerticalStackLayout Padding="10" Spacing="20">

<Label>Filled ellipse</Label>
<Ellipse FillColor="Colors.Coral"
WidthRequest="EllipseWidth"
HeightRequest="50" />

<Label>Red cirle</Label>
<Ellipse StrokeColor="Colors.Red"
StrokeThickness="CircleThickness"
WidthRequest="150"
HeightRequest="150" />


<Label>Blue diagonal line</Label>
<Line X1="LineX1"
Y1="0"
X2="0"
Y2="120"
StrokeColor="Colors.DarkBlue"
StrokeDashArray="1,1"
StrokeDashOffset="6" />

<Label>Polygon that uses the Nonzero fill rule</Label>
<Polygon Points="0,48 0,144 96,150 100,0 192,0 192,96 50,96 48,192 150,200 144,48"
FillColor="Colors.Black"
FillRule="FillRule.Nonzero"
StrokeColor="Colors.Yellow"
StrokeThickness="PolygonThickness" />


<Label>Dashed polyline</Label>
<Polyline Points=@($"0,0 10,30 15,0 18,60 23,30 35,30 40,0 43,60 48,30 {PolylinePoint},30")
StrokeColor="Colors.Red"
StrokeThickness="2"
StrokeDashArray="1,1"
StrokeDashOffset="6" />

<Label>Rectangle with gradient fill</Label>
<Rectangle RadiusX="50"
RadiusY="10"
WidthRequest="200"
HeightRequest="100">
<Fill>
<RadialGradientBrush Radius="0.5">
<GradientStop Color="Colors.DarkBlue" Offset="0" />
<GradientStop Color="Colors.Red" Offset="GradientOffset" />
</RadialGradientBrush>
</Fill>
</Rectangle>

</VerticalStackLayout>
</ScrollView>
</Grid>
</ContentPage>

@code {
double _value = 10;

string PolylinePoint => (100 + 5 * _value).ToString(CultureInfo.InvariantCulture);
float GradientOffset => (float)(0.5 + _value / 10);
double PolygonThickness => 3 + _value;
double LineX1 => 40 + 5 * _value;
double CircleThickness => 4 + _value / 5;
double EllipseWidth => 150 + _value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using Microsoft.Maui.Controls;

namespace BlazorBindings.Maui.Elements
{
public partial class AttributeHelper
{
private static readonly DoubleCollectionConverter _doubleCollectionConverter = new();

public static string DoubleCollectionToString(DoubleCollection doubleCollection)
{
return _doubleCollectionConverter.ConvertToInvariantString(doubleCollection);
}

public static DoubleCollection StringToDoubleCollection(object doubleCollectionString)
{
return (DoubleCollection)_doubleCollectionConverter.ConvertFromInvariantString((string)doubleCollectionString);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Shapes;

namespace BlazorBindings.Maui.Elements
{
public partial class AttributeHelper
{
private static readonly PointCollectionConverter _pointCollectionConverter = new();

public static string PointCollectionToString(PointCollection pointCollection)
{
return _pointCollectionConverter.ConvertToInvariantString(pointCollection);
}

public static PointCollection StringToPointCollection(object pointCollectionString)
{
return (PointCollection)_pointCollectionConverter.ConvertFromInvariantString((string)pointCollectionString);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using BlazorBindings.Core;
using MC = Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;
using System;

namespace BlazorBindings.Maui.Elements.Handlers
{
public partial class RadialGradientBrushHandler : GradientBrushHandler
{
private static readonly Point CenterDefaultValue = MC.RadialGradientBrush.CenterProperty.DefaultValue is Point value ? value : default;
private static readonly double RadiusDefaultValue = MC.RadialGradientBrush.RadiusProperty.DefaultValue is double value ? value : default;

public RadialGradientBrushHandler(NativeComponentRenderer renderer, MC.RadialGradientBrush radialGradientBrushControl) : base(renderer, radialGradientBrushControl)
{
RadialGradientBrushControl = radialGradientBrushControl ?? throw new ArgumentNullException(nameof(radialGradientBrushControl));

Initialize(renderer);
}

partial void Initialize(NativeComponentRenderer renderer);

public MC.RadialGradientBrush RadialGradientBrushControl { get; }

public override void ApplyAttribute(ulong attributeEventHandlerId, string attributeName, object attributeValue, string attributeEventUpdatesAttributeName)
{
switch (attributeName)
{
case nameof(MC.RadialGradientBrush.Center):
RadialGradientBrushControl.Center = AttributeHelper.StringToPoint(attributeValue, CenterDefaultValue);
break;
case nameof(MC.RadialGradientBrush.Radius):
RadialGradientBrushControl.Radius = AttributeHelper.StringToDouble((string)attributeValue, RadiusDefaultValue);
break;
default:
base.ApplyAttribute(attributeEventHandlerId, attributeName, attributeValue, attributeEventUpdatesAttributeName);
break;
}
}
}
}
48 changes: 48 additions & 0 deletions src/BlazorBindings.Maui/Elements/RadialGradientBrush.generated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using BlazorBindings.Core;
using BlazorBindings.Maui.Elements.Handlers;
using MC = Microsoft.Maui.Controls;
using Microsoft.AspNetCore.Components;
using Microsoft.Maui.Graphics;
using System.Threading.Tasks;

namespace BlazorBindings.Maui.Elements
{
public partial class RadialGradientBrush : GradientBrush
{
static RadialGradientBrush()
{
ElementHandlerRegistry.RegisterElementHandler<RadialGradientBrush>(
renderer => new RadialGradientBrushHandler(renderer, new MC.RadialGradientBrush()));

RegisterAdditionalHandlers();
}

[Parameter] public Point? Center { get; set; }
[Parameter] public double? Radius { get; set; }

public new MC.RadialGradientBrush NativeControl => (ElementHandler as RadialGradientBrushHandler)?.RadialGradientBrushControl;

protected override void RenderAttributes(AttributesBuilder builder)
{
base.RenderAttributes(builder);

if (Center != null)
{
builder.AddAttribute(nameof(Center), AttributeHelper.PointToString(Center.Value));
}
if (Radius != null)
{
builder.AddAttribute(nameof(Radius), AttributeHelper.DoubleToString(Radius.Value));
}

RenderAdditionalAttributes(builder);
}

partial void RenderAdditionalAttributes(AttributesBuilder builder);

static partial void RegisterAdditionalHandlers();
}
}
37 changes: 37 additions & 0 deletions src/BlazorBindings.Maui/Elements/Shapes/Ellipse.generated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using BlazorBindings.Core;
using BlazorBindings.Maui.Elements;
using BlazorBindings.Maui.Elements.Shapes.Handlers;
using MCS = Microsoft.Maui.Controls.Shapes;
using Microsoft.AspNetCore.Components;
using System.Threading.Tasks;

namespace BlazorBindings.Maui.Elements.Shapes
{
public partial class Ellipse : Shape
{
static Ellipse()
{
ElementHandlerRegistry.RegisterElementHandler<Ellipse>(
renderer => new EllipseHandler(renderer, new MCS.Ellipse()));

RegisterAdditionalHandlers();
}

public new MCS.Ellipse NativeControl => (ElementHandler as EllipseHandler)?.EllipseControl;

protected override void RenderAttributes(AttributesBuilder builder)
{
base.RenderAttributes(builder);


RenderAdditionalAttributes(builder);
}

partial void RenderAdditionalAttributes(AttributesBuilder builder);

static partial void RegisterAdditionalHandlers();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using BlazorBindings.Core;
using MCS = Microsoft.Maui.Controls.Shapes;
using System;

namespace BlazorBindings.Maui.Elements.Shapes.Handlers
{
public partial class EllipseHandler : ShapeHandler
{

public EllipseHandler(NativeComponentRenderer renderer, MCS.Ellipse ellipseControl) : base(renderer, ellipseControl)
{
EllipseControl = ellipseControl ?? throw new ArgumentNullException(nameof(ellipseControl));

Initialize(renderer);
}

partial void Initialize(NativeComponentRenderer renderer);

public MCS.Ellipse EllipseControl { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using BlazorBindings.Core;
using MCS = Microsoft.Maui.Controls.Shapes;
using System;

namespace BlazorBindings.Maui.Elements.Shapes.Handlers
{
public partial class LineHandler : ShapeHandler
{
private static readonly double X1DefaultValue = MCS.Line.X1Property.DefaultValue is double value ? value : default;
private static readonly double X2DefaultValue = MCS.Line.X2Property.DefaultValue is double value ? value : default;
private static readonly double Y1DefaultValue = MCS.Line.Y1Property.DefaultValue is double value ? value : default;
private static readonly double Y2DefaultValue = MCS.Line.Y2Property.DefaultValue is double value ? value : default;

public LineHandler(NativeComponentRenderer renderer, MCS.Line lineControl) : base(renderer, lineControl)
{
LineControl = lineControl ?? throw new ArgumentNullException(nameof(lineControl));

Initialize(renderer);
}

partial void Initialize(NativeComponentRenderer renderer);

public MCS.Line LineControl { get; }

public override void ApplyAttribute(ulong attributeEventHandlerId, string attributeName, object attributeValue, string attributeEventUpdatesAttributeName)
{
switch (attributeName)
{
case nameof(MCS.Line.X1):
LineControl.X1 = AttributeHelper.StringToDouble((string)attributeValue, X1DefaultValue);
break;
case nameof(MCS.Line.X2):
LineControl.X2 = AttributeHelper.StringToDouble((string)attributeValue, X2DefaultValue);
break;
case nameof(MCS.Line.Y1):
LineControl.Y1 = AttributeHelper.StringToDouble((string)attributeValue, Y1DefaultValue);
break;
case nameof(MCS.Line.Y2):
LineControl.Y2 = AttributeHelper.StringToDouble((string)attributeValue, Y2DefaultValue);
break;
default:
base.ApplyAttribute(attributeEventHandlerId, attributeName, attributeValue, attributeEventUpdatesAttributeName);
break;
}
}
}
}
17 changes: 17 additions & 0 deletions src/BlazorBindings.Maui/Elements/Shapes/Handlers/PolygonHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace BlazorBindings.Maui.Elements.Shapes.Handlers
{
public partial class PolygonHandler
{
public override bool ApplyAdditionalAttribute(ulong attributeEventHandlerId, string attributeName, object attributeValue, string attributeEventUpdatesAttributeName)
{
switch (attributeName)
{
case nameof(Polygon.Points):
PolygonControl.Points = AttributeHelper.StringToPointCollection(attributeValue);
return true;
default:
return base.ApplyAdditionalAttribute(attributeEventHandlerId, attributeName, attributeValue, attributeEventUpdatesAttributeName);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using BlazorBindings.Core;
using MCS = Microsoft.Maui.Controls.Shapes;
using System;

namespace BlazorBindings.Maui.Elements.Shapes.Handlers
{
public partial class PolygonHandler : ShapeHandler
{
private static readonly MCS.FillRule FillRuleDefaultValue = MCS.Polygon.FillRuleProperty.DefaultValue is MCS.FillRule value ? value : default;

public PolygonHandler(NativeComponentRenderer renderer, MCS.Polygon polygonControl) : base(renderer, polygonControl)
{
PolygonControl = polygonControl ?? throw new ArgumentNullException(nameof(polygonControl));

Initialize(renderer);
}

partial void Initialize(NativeComponentRenderer renderer);

public MCS.Polygon PolygonControl { get; }

public override void ApplyAttribute(ulong attributeEventHandlerId, string attributeName, object attributeValue, string attributeEventUpdatesAttributeName)
{
switch (attributeName)
{
case nameof(MCS.Polygon.FillRule):
PolygonControl.FillRule = (MCS.FillRule)AttributeHelper.GetInt(attributeValue, (int)FillRuleDefaultValue);
break;
default:
base.ApplyAttribute(attributeEventHandlerId, attributeName, attributeValue, attributeEventUpdatesAttributeName);
break;
}
}
}
}
Loading

0 comments on commit 3c826da

Please sign in to comment.