Skip to content

Commit

Permalink
A few enhancements to the base types (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow authored Jun 19, 2022
1 parent 707125d commit e73ab52
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 98 deletions.
38 changes: 3 additions & 35 deletions samples/Forms/SkiaSharpDemo/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<views:BottomTabBarResources />
<views:OptionButtonsResources />
</ResourceDictionary.MergedDictionaries>

<converters:RoundToIntConverter x:Key="RoundToInt" />
Expand All @@ -22,41 +23,8 @@
<Setter Property="Padding" Value="0" />
</Style>

<!-- OptionButton styles -->
<ResourceDictionary>
<Style x:Key="OptionButtonStyle" TargetType="Button">
<Setter Property="CornerRadius" Value="3" />
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="Scale" Value="0.8" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectedStates">
<VisualState x:Name="Unselected" />
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Property="BorderColor" Value="Black" />
<Setter Property="BorderWidth" Value="3" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
<Style x:Key="TextOptionButtonStyle" TargetType="Button" BasedOn="{StaticResource OptionButtonStyle}">
<Setter Property="BackgroundColor" Value="Accent" />
<Setter Property="TextColor" Value="White" />
<Setter Property="Padding" Value="24,12" />
</Style>
</ResourceDictionary>
</ResourceDictionary>

<!-- Application resource dictionary -->
<!-- Application resource dictionary -->

</ResourceDictionary>
</Application.Resources>
</Application>
5 changes: 5 additions & 0 deletions samples/Forms/SkiaSharpDemo/Demos/Confetti/ConfettiPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
</controls:SKConfettiView.GestureRecognizers>
</controls:SKConfettiView>

<BoxView Color="Green" Opacity="0.5" CornerRadius="12"
WidthRequest="25" HeightRequest="24" Margin="24"
HorizontalOptions="End" VerticalOptions="Start"
IsVisible="{Binding IsComplete, Source={Reference confettiView}}" />

<views:BottomTabBar SelectedIndex="{Binding SelectedTab}" PagePadding="12,6">
<views:BottomTabCollection>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:views="clr-namespace:SkiaSharpDemo.Views"
mc:Ignorable="d"
x:Class="SkiaSharpDemo.Views.OptionButtonsResources">

<Style x:Key="OptionButtonStyle" TargetType="Button">
<Setter Property="CornerRadius" Value="3" />
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Property="Scale" Value="0.8" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectedStates">
<VisualState x:Name="Unselected" />
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Property="BorderColor" Value="Black" />
<Setter Property="BorderWidth" Value="3" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>

<Style x:Key="TextOptionButtonStyle" TargetType="Button" BasedOn="{StaticResource OptionButtonStyle}">
<Setter Property="BackgroundColor" Value="Accent" />
<Setter Property="TextColor" Value="White" />
<Setter Property="Padding" Value="24,12" />
</Style>

</ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Xamarin.Forms;

namespace SkiaSharpDemo.Views
{
public partial class OptionButtonsResources : ResourceDictionary
{
public OptionButtonsResources()
{
InitializeComponent();
}
}
}
5 changes: 5 additions & 0 deletions samples/Maui/SkiaSharpDemo/Demos/Confetti/ConfettiPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
</controls:SKConfettiView.GestureRecognizers>
</controls:SKConfettiView>

<BoxView Color="Green" Opacity="0.5" CornerRadius="12"
WidthRequest="25" HeightRequest="24" Margin="24"
HorizontalOptions="End" VerticalOptions="Start"
IsVisible="{Binding IsComplete, Source={Reference confettiView}}" />

<views:BottomTabBar SelectedIndex="{Binding SelectedTab}" PagePadding="12,6">
<views:BottomTabCollection>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace SkiaSharp.Extended.UI.Controls.Converters;

public abstract class StringTypeConverter : TypeConverter
{
internal StringTypeConverter()
{
}

public override object? ConvertFromInvariantString(string? value) =>
ConvertFromStringCore(value);

public override string? ConvertToInvariantString(object? value) =>
ConvertToStringCore(value);

protected abstract object? ConvertFromStringCore(string? value);

protected virtual string? ConvertToStringCore(object? value) => throw new NotImplementedException();
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Globalization;

namespace SkiaSharp.Extended.UI.Controls.Converters;

public abstract class StringTypeConverter : TypeConverter
{
internal StringTypeConverter()
{
}

public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) =>
sourceType == typeof(string);

public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) =>
destinationType == typeof(string);

public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) =>
ConvertFromStringCore(value?.ToString());

public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType) =>
ConvertToStringCore(value);

protected abstract object? ConvertFromStringCore(string? value);

protected virtual string? ConvertToStringCore(object? value) => throw new NotImplementedException();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#if XAMARIN_FORMS
[TypeConversion(typeof(SKConfettiColorCollection))]
#endif
public class SKConfettiColorCollectionTypeConverter : ExtendedTypeConverter
public class SKConfettiColorCollectionTypeConverter : StringTypeConverter
{
protected override object? Convert(string? value)
protected override object? ConvertFromStringCore(string? value)
{
if (value == null)
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#if XAMARIN_FORMS
[TypeConversion(typeof(SKConfettiEmitterBounds))]
#endif
public class SKConfettiEmitterBoundsTypeConverter : ExtendedTypeConverter
public class SKConfettiEmitterBoundsTypeConverter : StringTypeConverter
{
protected override object? Convert(string? value)
protected override object? ConvertFromStringCore(string? value)
{
if (value == null)
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#if XAMARIN_FORMS
[TypeConversion(typeof(SKConfettiPhysics))]
#endif
public class SKConfettiPhysicsTypeConverter : ExtendedTypeConverter
public class SKConfettiPhysicsTypeConverter : StringTypeConverter
{
protected override object? Convert(string? value)
protected override object? ConvertFromStringCore(string? value)
{
if (value == null)
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected override void OnPaintSurface(SKCanvas canvas, SKSize size)
{
var particles = 0;

if (Systems is not null)
if (Systems?.Count > 0)
{
foreach (var system in Systems)
{
Expand All @@ -93,7 +93,7 @@ private void OnSizeChanged(object? sender, EventArgs e)
}
}

private void OnSystemsChanged(object? sender, NotifyCollectionChangedEventArgs e)
private void OnSystemsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems is not null)
{
Expand Down Expand Up @@ -126,10 +126,10 @@ private static void OnSystemsPropertyChanged(BindableObject bindable, object? ol
return;

if (oldValue is SKConfettiSystemCollection oldCollection)
oldCollection.CollectionChanged -= cv.OnSystemsChanged;
oldCollection.CollectionChanged -= cv.OnSystemsCollectionChanged;

if (newValue is SKConfettiSystemCollection newCollection)
newCollection.CollectionChanged += cv.OnSystemsChanged;
newCollection.CollectionChanged += cv.OnSystemsCollectionChanged;

cv.UpdateIsComplete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,6 @@ public SKConfettiViewResources()
InitializeComponent();
}

internal static void EnsureRegistered()
{
if (registered)
return;

var merged = Application.Current?.Resources?.MergedDictionaries;
if (merged != null)
{
foreach (var dic in merged)
{
if (dic.GetType() == typeof(SKConfettiViewResources))
{
registered = true;
break;
}
}

if (!registered)
{
merged.Add(new SKConfettiViewResources());
registered = true;
}
}
}
internal static void EnsureRegistered() =>
ResourceLoader.EnsureRegistered<SKConfettiViewResources>(ref registered);
}
30 changes: 30 additions & 0 deletions source/SkiaSharp.Extended.UI/Utils/ResourceLoader.shared.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace SkiaSharp.Extended.UI;

internal static class ResourceLoader
{
internal static void EnsureRegistered<T>(ref bool registered)
where T : ResourceDictionary, new()
{
if (registered)
return;

var merged = Application.Current?.Resources?.MergedDictionaries;
if (merged != null)
{
foreach (var dic in merged)
{
if (dic.GetType() == typeof(T))
{
registered = true;
break;
}
}

if (!registered)
{
merged.Add(new T());
registered = true;
}
}
}
}

0 comments on commit e73ab52

Please sign in to comment.