Skip to content

Commit

Permalink
Merge pull request #58 from muak/development
Browse files Browse the repository at this point in the history
Add Gradient
  • Loading branch information
muak authored Feb 8, 2020
2 parents 8d133ec + 3a4ab52 commit b3a30a8
Show file tree
Hide file tree
Showing 13 changed files with 783 additions and 13 deletions.
3 changes: 3 additions & 0 deletions AiForms.Effects.Droid/AiForms.Effects.Droid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<WarningLevel>4</WarningLevel>
<AndroidLinkMode>None</AndroidLinkMode>
<NoWarn></NoWarn>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -39,6 +40,7 @@
<AndroidManagedSymbols>true</AndroidManagedSymbols>
<AndroidUseSharedRuntime>false</AndroidUseSharedRuntime>
<NoWarn>1591</NoWarn>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down Expand Up @@ -207,6 +209,7 @@
<Compile Include="PlatformUtility.cs" />
<Compile Include="FloatingLayoutRenderer.cs" />
<Compile Include="Initialize.cs" />
<Compile Include="GradientPlatformEffect.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\AboutResources.txt" />
Expand Down
105 changes: 105 additions & 0 deletions AiForms.Effects.Droid/GradientPlatformEffect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
using System.ComponentModel;
using System.Linq;
using AiForms.Effects;
using AiForms.Effects.Droid;
using Android.Graphics.Drawables;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportEffect(typeof(GradientPlatformEffect), nameof(Gradient))]
namespace AiForms.Effects.Droid
{
public class GradientPlatformEffect:AiEffectBase
{
Android.Views.View _view;
GradientDrawable _gradient;
Drawable _orgDrawable;

protected override void OnAttachedOverride()
{
_view = Container ?? Control;

_gradient = new GradientDrawable();
_orgDrawable = _view.Background;

UpdateGradient();
}

protected override void OnDetachedOverride()
{
if(!IsDisposed)
{
_view.Background = _orgDrawable;
_view.ClipToOutline = false;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached Disposing");
}

_gradient?.Dispose();
_gradient = null;
_view = null;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");
}

protected override void OnElementPropertyChanged(PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged(args);

if (!IsSupportedByApi)
return;

if (IsDisposed)
{
return;
}

if (args.PropertyName == Gradient.ColorsProperty.PropertyName ||
args.PropertyName == Gradient.OrientationProperty.PropertyName)
{
UpdateGradient();
}
}

void UpdateGradient()
{
var colors = Gradient.GetColors(Element);
if(colors == null)
{
return;
}

_gradient.SetColors(colors.Select(x => (int)x.ToAndroid()).ToArray());
_gradient.SetOrientation(ConvertOrientation());

_view.ClipToOutline = true; //not to overflow children
_view.SetBackground(_gradient);
}

GradientDrawable.Orientation ConvertOrientation()
{
var orientation = Gradient.GetOrientation(Element);

switch (orientation)
{
case GradientOrientation.LeftRight:
return GradientDrawable.Orientation.LeftRight;
case GradientOrientation.BlTr:
return GradientDrawable.Orientation.BlTr;
case GradientOrientation.BottomTop:
return GradientDrawable.Orientation.BottomTop;
case GradientOrientation.BrTl:
return GradientDrawable.Orientation.BrTl;
case GradientOrientation.RightLeft:
return GradientDrawable.Orientation.RightLeft;
case GradientOrientation.TrBl:
return GradientDrawable.Orientation.TrBl;
case GradientOrientation.TopBottom:
return GradientDrawable.Orientation.TopBottom;
case GradientOrientation.TlBr:
return GradientDrawable.Orientation.TlBr;
default:
return GradientDrawable.Orientation.LeftRight;
}
}
}
}
3 changes: 3 additions & 0 deletions AiForms.Effects.iOS/AiForms.Effects.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<MtouchLink></MtouchLink>
<MtouchHttpClientHandler></MtouchHttpClientHandler>
<MtouchTlsProvider></MtouchTlsProvider>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -43,6 +44,7 @@
<MtouchHttpClientHandler></MtouchHttpClientHandler>
<MtouchTlsProvider></MtouchTlsProvider>
<NoWarn>1591</NoWarn>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down Expand Up @@ -89,6 +91,7 @@
<Compile Include="FeedbackPlatformEffect.cs" />
<Compile Include="FloatingPlatformEffect.cs" />
<Compile Include="PlatformUtility.cs" />
<Compile Include="GradientPlatformEffect.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AiForms.Effects\AiForms.Effects.csproj">
Expand Down
125 changes: 125 additions & 0 deletions AiForms.Effects.iOS/GradientPlatformEffect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using System;
using System.ComponentModel;
using System.Linq;
using AiForms.Effects;
using AiForms.Effects.iOS;
using CoreAnimation;
using CoreGraphics;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportEffect(typeof(GradientPlatformEffect), nameof(Gradient))]
namespace AiForms.Effects.iOS
{
public class GradientPlatformEffect:PlatformEffect
{
UIView _view;
VisualElement _visualElement;
CAGradientLayer _layer;
bool _clipsToBounds;

protected override void OnAttached()
{
_view = Control ?? Container;
_visualElement = Element as VisualElement;
if(_visualElement == null)
{
Device.BeginInvokeOnMainThread(() => Gradient.SetOn(Element, false));
return;
}

if (Element is Label)
{
_view = Container;
}

_clipsToBounds = _view.ClipsToBounds;
_visualElement.SizeChanged += OnSizeChanged;
UpdateGradient();
}

protected override void OnDetached()
{
if(_visualElement != null)
{
_visualElement.SizeChanged -= OnSizeChanged;
}

if(_view != null)
{
_view.ClipsToBounds = _clipsToBounds;
}

_layer?.RemoveFromSuperLayer();
_layer?.Dispose();
_layer = null;

_view = null;
_visualElement = null;

System.Diagnostics.Debug.WriteLine($"Detached {GetType().Name} from {Element.GetType().FullName}");
}

protected override void OnElementPropertyChanged(PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged(args);
if(args.PropertyName == Gradient.ColorsProperty.PropertyName ||
args.PropertyName == Gradient.OrientationProperty.PropertyName)
{
UpdateGradient();
}
}

private void OnSizeChanged(object sender, EventArgs e)
{
UpdateGradient();
}

void UpdateGradient()
{
var colors = Gradient.GetColors(Element);
if(colors == null)
{
return;
}

_layer?.RemoveFromSuperLayer();
_layer?.Dispose();

_layer = new CAGradientLayer();
_layer.Frame = new CGRect(0, 0, _visualElement.Width, _visualElement.Height);
_layer.Colors = colors.Select(x => x.ToCGColor()).ToArray();
(_layer.StartPoint,_layer.EndPoint) = ConvertPoint();
_view.Layer.InsertSublayer(_layer, 0);
_view.ClipsToBounds = true;
}

(CGPoint start,CGPoint end) ConvertPoint()
{
var orientation = Gradient.GetOrientation(Element);

switch(orientation)
{
case GradientOrientation.LeftRight:
return (new CGPoint(0, 0.5), new CGPoint(1, 0.5));
case GradientOrientation.BlTr:
return (new CGPoint(0, 1.0), new CGPoint(1, 0));
case GradientOrientation.BottomTop:
return (new CGPoint(0.5, 1), new CGPoint(0.5, 0));
case GradientOrientation.BrTl:
return (new CGPoint(1, 1), new CGPoint(0, 0));
case GradientOrientation.RightLeft:
return (new CGPoint(1, 0.5), new CGPoint(0, 0.5));
case GradientOrientation.TrBl:
return (new CGPoint(1, 0), new CGPoint(0, 1));
case GradientOrientation.TopBottom:
return (new CGPoint(0.5, 0), new CGPoint(0.5, 1));
case GradientOrientation.TlBr:
return (new CGPoint(0, 0), new CGPoint(1, 1));
default:
return (new CGPoint(0, 0.5), new CGPoint(1, 0.5));
}
}
}
}
6 changes: 5 additions & 1 deletion AiForms.Effects/AiForms.Effects.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<ReleaseVersion>1.1.2</ReleaseVersion>
<ReleaseVersion>0.0.5-pre</ReleaseVersion>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DocumentationFile>bin\Release\netstandard2.0\AiForms.Effects.xml</DocumentationFile>
<NoWarn>1701;1702;1591</NoWarn>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="4.4.0.991537" />
Expand Down
Loading

0 comments on commit b3a30a8

Please sign in to comment.