Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IndicatorView control #89

Merged
merged 1 commit into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions samples/ControlGallery/Views/Collections/CarouselViewPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
<CarouselView Grid.Row="0"
ItemsSource="items"
Loop="false"
@bind-CurrentItem="currentItem"
@bind-Position="currentPosition">
IndicatorView="() => indicatorView"
@bind-CurrentItem="currentItem"
@bind-Position="currentPosition">

<ItemTemplate>
<Frame BackgroundColor="Colors.LightPink"
Expand All @@ -24,6 +25,10 @@
</CarouselView>

<VerticalStackLayout Grid.Row="1">
<IndicatorView @ref="indicatorView" HorizontalOptions="LayoutOptions.Center"
IndicatorColor="Colors.DarkGray"
SelectedIndicatorColor="Colors.LightGray" />

<Label>Current item: @currentItem?.Name</Label>
<Label>Current position: @currentPosition</Label>
<Button Text="Set last position" OnClick="() => currentPosition = 3" />
Expand All @@ -40,6 +45,7 @@
new("Gill", "https://static.wikia.nocookie.net/pixar/images/7/72/Gill.png/revision/latest/scale-to-width-down/350?cb=20210322233843")
};

IndicatorView indicatorView;
Item currentItem;
int currentPosition;

Expand Down
39 changes: 39 additions & 0 deletions src/BlazorBindings.Maui/Elements/CarouselView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Microsoft.AspNetCore.Components;
using System;
using System.Threading.Tasks;
using MC = Microsoft.Maui.Controls;

namespace BlazorBindings.Maui.Elements
{
public partial class CarouselView<T> : IHandleAfterRender
{
private MC.IndicatorView _indicatorView;

[Parameter] public Func<IndicatorView> IndicatorView { get; set; }

protected override bool HandleAdditionalParameter(string name, object value)
{
if (name == nameof(IndicatorView))
{
IndicatorView = (Func<IndicatorView>)value;
return true;
}

return base.HandleAdditionalParameter(name, value);
}

Task IHandleAfterRender.OnAfterRenderAsync()
{
if (IndicatorView != null)
{
var newIndicatorView = IndicatorView()?.NativeControl;
if (!Equals(newIndicatorView, _indicatorView))
{
_indicatorView = newIndicatorView;
NativeControl.IndicatorView = newIndicatorView;
}
}
return Task.CompletedTask;
}
}
}
140 changes: 140 additions & 0 deletions src/BlazorBindings.Maui/Elements/IndicatorView.generated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// <auto-generated>
// This code was generated by a BlazorBindings.Maui component generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>

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

namespace BlazorBindings.Maui.Elements
{
public partial class IndicatorView : TemplatedView
{
static IndicatorView()
{
ElementHandlerRegistry.RegisterPropertyContentHandler<IndicatorView>(nameof(IndicatorTemplate),
(renderer, parent, component) => new DataTemplatePropertyHandler<MC.IndicatorView>(component,
(x, dataTemplate) => x.IndicatorTemplate = dataTemplate));
RegisterAdditionalHandlers();
}

[Parameter] public int? Count { get; set; }
[Parameter] public bool? HideSingle { get; set; }
[Parameter] public Color IndicatorColor { get; set; }
[Parameter] public double? IndicatorSize { get; set; }
[Parameter] public MC.IndicatorShape? IndicatorsShape { get; set; }
[Parameter] public int? MaximumVisible { get; set; }
[Parameter] public int? Position { get; set; }
[Parameter] public Color SelectedIndicatorColor { get; set; }
[Parameter] public RenderFragment IndicatorTemplate { get; set; }
[Parameter] public EventCallback<int> PositionChanged { get; set; }

public new MC.IndicatorView NativeControl => (MC.IndicatorView)((BindableObject)this).NativeControl;

protected override MC.IndicatorView CreateNativeElement() => new();

protected override void HandleParameter(string name, object value)
{
switch (name)
{
case nameof(Count):
if (!Equals(Count, value))
{
Count = (int?)value;
NativeControl.Count = Count ?? (int)MC.IndicatorView.CountProperty.DefaultValue;
}
break;
case nameof(HideSingle):
if (!Equals(HideSingle, value))
{
HideSingle = (bool?)value;
NativeControl.HideSingle = HideSingle ?? (bool)MC.IndicatorView.HideSingleProperty.DefaultValue;
}
break;
case nameof(IndicatorColor):
if (!Equals(IndicatorColor, value))
{
IndicatorColor = (Color)value;
NativeControl.IndicatorColor = IndicatorColor;
}
break;
case nameof(IndicatorSize):
if (!Equals(IndicatorSize, value))
{
IndicatorSize = (double?)value;
NativeControl.IndicatorSize = IndicatorSize ?? (double)MC.IndicatorView.IndicatorSizeProperty.DefaultValue;
}
break;
case nameof(IndicatorsShape):
if (!Equals(IndicatorsShape, value))
{
IndicatorsShape = (MC.IndicatorShape?)value;
NativeControl.IndicatorsShape = IndicatorsShape ?? (MC.IndicatorShape)MC.IndicatorView.IndicatorsShapeProperty.DefaultValue;
}
break;
case nameof(MaximumVisible):
if (!Equals(MaximumVisible, value))
{
MaximumVisible = (int?)value;
NativeControl.MaximumVisible = MaximumVisible ?? (int)MC.IndicatorView.MaximumVisibleProperty.DefaultValue;
}
break;
case nameof(Position):
if (!Equals(Position, value))
{
Position = (int?)value;
NativeControl.Position = Position ?? (int)MC.IndicatorView.PositionProperty.DefaultValue;
}
break;
case nameof(SelectedIndicatorColor):
if (!Equals(SelectedIndicatorColor, value))
{
SelectedIndicatorColor = (Color)value;
NativeControl.SelectedIndicatorColor = SelectedIndicatorColor;
}
break;
case nameof(IndicatorTemplate):
IndicatorTemplate = (RenderFragment)value;
break;
case nameof(PositionChanged):
if (!Equals(PositionChanged, value))
{
void NativeControlPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(NativeControl.Position))
{
var value = NativeControl.Position;
Position = value;
InvokeEventCallback(PositionChanged, value);
}
}

PositionChanged = (EventCallback<int>)value;
NativeControl.PropertyChanged -= NativeControlPropertyChanged;
NativeControl.PropertyChanged += NativeControlPropertyChanged;
}
break;

default:
base.HandleParameter(name, value);
break;
}
}

protected override void RenderAdditionalElementContent(RenderTreeBuilder builder, ref int sequence)
{
base.RenderAdditionalElementContent(builder, ref sequence);
RenderTreeBuilderHelper.AddDataTemplateProperty(builder, sequence++, typeof(IndicatorView), IndicatorTemplate);
}

static partial void RegisterAdditionalHandlers();
}
}
3 changes: 3 additions & 0 deletions src/BlazorBindings.Maui/Properties/AttributeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
[assembly: GenerateComponent(typeof(HorizontalStackLayout))]
[assembly: GenerateComponent(typeof(Image))]
[assembly: GenerateComponent(typeof(ImageButton))]
[assembly: GenerateComponent(typeof(IndicatorView),
PropertyChangedEvents = new[] { nameof(IndicatorView.Position) },
Exclude = new[] { nameof(IndicatorView.ItemsSource), nameof(IndicatorView.IndicatorLayout) })]
[assembly: GenerateComponent(typeof(InputView))]
[assembly: GenerateComponent(typeof(ItemsView),
GenericProperties = new[] { nameof(ItemsView.ItemsSource), nameof(ItemsView.ItemTemplate) },
Expand Down
3 changes: 2 additions & 1 deletion src/BlazorBindings.UnitTests/BlazorBindings.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<IsPackable>false</IsPackable>
<UseMaui>true</UseMaui>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="nunit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.0" />
Expand All @@ -15,4 +15,5 @@
<ItemGroup>
<ProjectReference Include="..\BlazorBindings.Maui\BlazorBindings.Maui.csproj" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions src/BlazorBindings.UnitTests/Elements/CarouselViewTests.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@inherits ElementTestBase

@code {
[Test]
public async Task SetIndicatorViewToCarouselView()
{
var items = new[] { 1, 2, 3, 4, 5 };

IndicatorView indicatorViewRef = null;
var parent = await Render<MC.Layout>(
@<VerticalStackLayout>
<CarouselView IndicatorView="() => indicatorViewRef" ItemsSource="items" />
<IndicatorView @ref="indicatorViewRef" />
</VerticalStackLayout>
);

var carouselView = (MC.CarouselView)parent.Children[0];
var indicatorView = (MC.IndicatorView)parent.Children[1];

Assert.That(indicatorView, Is.SameAs(indicatorViewRef.NativeControl));
Assert.That(indicatorView.ItemsSource, Is.EqualTo(items));
}
}
9 changes: 0 additions & 9 deletions src/BlazorBindings.UnitTests/Elements/ElementTestBase.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@
@code {
private MauiBlazorBindingsRenderer _renderer = TestBlazorBindingsRenderer.Create();

protected override bool ShouldRender() => false;

public ElementTestBase()
{
// We need to prevent this component from rendering.
var field = typeof(ComponentBase).GetField("_hasNeverRendered", BindingFlags.NonPublic | BindingFlags.Instance);
field.SetValue(this, false);
}

protected async Task<T> Render<T>(RenderFragment renderFragment) where T : MC.BindableObject
{
var container = new RootContainerHandler();
Expand Down