Skip to content

Commit b3a30a8

Browse files
authored
Merge pull request #58 from muak/development
Add Gradient
2 parents 8d133ec + 3a4ab52 commit b3a30a8

13 files changed

+783
-13
lines changed

AiForms.Effects.Droid/AiForms.Effects.Droid.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<WarningLevel>4</WarningLevel>
2828
<AndroidLinkMode>None</AndroidLinkMode>
2929
<NoWarn></NoWarn>
30+
<LangVersion>8.0</LangVersion>
3031
</PropertyGroup>
3132
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3233
<DebugSymbols>true</DebugSymbols>
@@ -39,6 +40,7 @@
3940
<AndroidManagedSymbols>true</AndroidManagedSymbols>
4041
<AndroidUseSharedRuntime>false</AndroidUseSharedRuntime>
4142
<NoWarn>1591</NoWarn>
43+
<LangVersion>8.0</LangVersion>
4244
</PropertyGroup>
4345
<ItemGroup>
4446
<Reference Include="System" />
@@ -207,6 +209,7 @@
207209
<Compile Include="PlatformUtility.cs" />
208210
<Compile Include="FloatingLayoutRenderer.cs" />
209211
<Compile Include="Initialize.cs" />
212+
<Compile Include="GradientPlatformEffect.cs" />
210213
</ItemGroup>
211214
<ItemGroup>
212215
<None Include="Resources\AboutResources.txt" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System;
2+
using System.ComponentModel;
3+
using System.Linq;
4+
using AiForms.Effects;
5+
using AiForms.Effects.Droid;
6+
using Android.Graphics.Drawables;
7+
using Xamarin.Forms;
8+
using Xamarin.Forms.Platform.Android;
9+
10+
[assembly: ExportEffect(typeof(GradientPlatformEffect), nameof(Gradient))]
11+
namespace AiForms.Effects.Droid
12+
{
13+
public class GradientPlatformEffect:AiEffectBase
14+
{
15+
Android.Views.View _view;
16+
GradientDrawable _gradient;
17+
Drawable _orgDrawable;
18+
19+
protected override void OnAttachedOverride()
20+
{
21+
_view = Container ?? Control;
22+
23+
_gradient = new GradientDrawable();
24+
_orgDrawable = _view.Background;
25+
26+
UpdateGradient();
27+
}
28+
29+
protected override void OnDetachedOverride()
30+
{
31+
if(!IsDisposed)
32+
{
33+
_view.Background = _orgDrawable;
34+
_view.ClipToOutline = false;
35+
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached Disposing");
36+
}
37+
38+
_gradient?.Dispose();
39+
_gradient = null;
40+
_view = null;
41+
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");
42+
}
43+
44+
protected override void OnElementPropertyChanged(PropertyChangedEventArgs args)
45+
{
46+
base.OnElementPropertyChanged(args);
47+
48+
if (!IsSupportedByApi)
49+
return;
50+
51+
if (IsDisposed)
52+
{
53+
return;
54+
}
55+
56+
if (args.PropertyName == Gradient.ColorsProperty.PropertyName ||
57+
args.PropertyName == Gradient.OrientationProperty.PropertyName)
58+
{
59+
UpdateGradient();
60+
}
61+
}
62+
63+
void UpdateGradient()
64+
{
65+
var colors = Gradient.GetColors(Element);
66+
if(colors == null)
67+
{
68+
return;
69+
}
70+
71+
_gradient.SetColors(colors.Select(x => (int)x.ToAndroid()).ToArray());
72+
_gradient.SetOrientation(ConvertOrientation());
73+
74+
_view.ClipToOutline = true; //not to overflow children
75+
_view.SetBackground(_gradient);
76+
}
77+
78+
GradientDrawable.Orientation ConvertOrientation()
79+
{
80+
var orientation = Gradient.GetOrientation(Element);
81+
82+
switch (orientation)
83+
{
84+
case GradientOrientation.LeftRight:
85+
return GradientDrawable.Orientation.LeftRight;
86+
case GradientOrientation.BlTr:
87+
return GradientDrawable.Orientation.BlTr;
88+
case GradientOrientation.BottomTop:
89+
return GradientDrawable.Orientation.BottomTop;
90+
case GradientOrientation.BrTl:
91+
return GradientDrawable.Orientation.BrTl;
92+
case GradientOrientation.RightLeft:
93+
return GradientDrawable.Orientation.RightLeft;
94+
case GradientOrientation.TrBl:
95+
return GradientDrawable.Orientation.TrBl;
96+
case GradientOrientation.TopBottom:
97+
return GradientDrawable.Orientation.TopBottom;
98+
case GradientOrientation.TlBr:
99+
return GradientDrawable.Orientation.TlBr;
100+
default:
101+
return GradientDrawable.Orientation.LeftRight;
102+
}
103+
}
104+
}
105+
}

AiForms.Effects.iOS/AiForms.Effects.iOS.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<MtouchLink></MtouchLink>
2929
<MtouchHttpClientHandler></MtouchHttpClientHandler>
3030
<MtouchTlsProvider></MtouchTlsProvider>
31+
<LangVersion>8.0</LangVersion>
3132
</PropertyGroup>
3233
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3334
<DebugSymbols>true</DebugSymbols>
@@ -43,6 +44,7 @@
4344
<MtouchHttpClientHandler></MtouchHttpClientHandler>
4445
<MtouchTlsProvider></MtouchTlsProvider>
4546
<NoWarn>1591</NoWarn>
47+
<LangVersion>8.0</LangVersion>
4648
</PropertyGroup>
4749
<ItemGroup>
4850
<Reference Include="System" />
@@ -89,6 +91,7 @@
8991
<Compile Include="FeedbackPlatformEffect.cs" />
9092
<Compile Include="FloatingPlatformEffect.cs" />
9193
<Compile Include="PlatformUtility.cs" />
94+
<Compile Include="GradientPlatformEffect.cs" />
9295
</ItemGroup>
9396
<ItemGroup>
9497
<ProjectReference Include="..\AiForms.Effects\AiForms.Effects.csproj">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using System;
2+
using System.ComponentModel;
3+
using System.Linq;
4+
using AiForms.Effects;
5+
using AiForms.Effects.iOS;
6+
using CoreAnimation;
7+
using CoreGraphics;
8+
using UIKit;
9+
using Xamarin.Forms;
10+
using Xamarin.Forms.Platform.iOS;
11+
12+
[assembly: ExportEffect(typeof(GradientPlatformEffect), nameof(Gradient))]
13+
namespace AiForms.Effects.iOS
14+
{
15+
public class GradientPlatformEffect:PlatformEffect
16+
{
17+
UIView _view;
18+
VisualElement _visualElement;
19+
CAGradientLayer _layer;
20+
bool _clipsToBounds;
21+
22+
protected override void OnAttached()
23+
{
24+
_view = Control ?? Container;
25+
_visualElement = Element as VisualElement;
26+
if(_visualElement == null)
27+
{
28+
Device.BeginInvokeOnMainThread(() => Gradient.SetOn(Element, false));
29+
return;
30+
}
31+
32+
if (Element is Label)
33+
{
34+
_view = Container;
35+
}
36+
37+
_clipsToBounds = _view.ClipsToBounds;
38+
_visualElement.SizeChanged += OnSizeChanged;
39+
UpdateGradient();
40+
}
41+
42+
protected override void OnDetached()
43+
{
44+
if(_visualElement != null)
45+
{
46+
_visualElement.SizeChanged -= OnSizeChanged;
47+
}
48+
49+
if(_view != null)
50+
{
51+
_view.ClipsToBounds = _clipsToBounds;
52+
}
53+
54+
_layer?.RemoveFromSuperLayer();
55+
_layer?.Dispose();
56+
_layer = null;
57+
58+
_view = null;
59+
_visualElement = null;
60+
61+
System.Diagnostics.Debug.WriteLine($"Detached {GetType().Name} from {Element.GetType().FullName}");
62+
}
63+
64+
protected override void OnElementPropertyChanged(PropertyChangedEventArgs args)
65+
{
66+
base.OnElementPropertyChanged(args);
67+
if(args.PropertyName == Gradient.ColorsProperty.PropertyName ||
68+
args.PropertyName == Gradient.OrientationProperty.PropertyName)
69+
{
70+
UpdateGradient();
71+
}
72+
}
73+
74+
private void OnSizeChanged(object sender, EventArgs e)
75+
{
76+
UpdateGradient();
77+
}
78+
79+
void UpdateGradient()
80+
{
81+
var colors = Gradient.GetColors(Element);
82+
if(colors == null)
83+
{
84+
return;
85+
}
86+
87+
_layer?.RemoveFromSuperLayer();
88+
_layer?.Dispose();
89+
90+
_layer = new CAGradientLayer();
91+
_layer.Frame = new CGRect(0, 0, _visualElement.Width, _visualElement.Height);
92+
_layer.Colors = colors.Select(x => x.ToCGColor()).ToArray();
93+
(_layer.StartPoint,_layer.EndPoint) = ConvertPoint();
94+
_view.Layer.InsertSublayer(_layer, 0);
95+
_view.ClipsToBounds = true;
96+
}
97+
98+
(CGPoint start,CGPoint end) ConvertPoint()
99+
{
100+
var orientation = Gradient.GetOrientation(Element);
101+
102+
switch(orientation)
103+
{
104+
case GradientOrientation.LeftRight:
105+
return (new CGPoint(0, 0.5), new CGPoint(1, 0.5));
106+
case GradientOrientation.BlTr:
107+
return (new CGPoint(0, 1.0), new CGPoint(1, 0));
108+
case GradientOrientation.BottomTop:
109+
return (new CGPoint(0.5, 1), new CGPoint(0.5, 0));
110+
case GradientOrientation.BrTl:
111+
return (new CGPoint(1, 1), new CGPoint(0, 0));
112+
case GradientOrientation.RightLeft:
113+
return (new CGPoint(1, 0.5), new CGPoint(0, 0.5));
114+
case GradientOrientation.TrBl:
115+
return (new CGPoint(1, 0), new CGPoint(0, 1));
116+
case GradientOrientation.TopBottom:
117+
return (new CGPoint(0.5, 0), new CGPoint(0.5, 1));
118+
case GradientOrientation.TlBr:
119+
return (new CGPoint(0, 0), new CGPoint(1, 1));
120+
default:
121+
return (new CGPoint(0, 0.5), new CGPoint(1, 0.5));
122+
}
123+
}
124+
}
125+
}

AiForms.Effects/AiForms.Effects.csproj

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<ReleaseVersion>1.1.2</ReleaseVersion>
5+
<ReleaseVersion>0.0.5-pre</ReleaseVersion>
66
</PropertyGroup>
77

88
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
99
<DebugSymbols>true</DebugSymbols>
1010
<DocumentationFile>bin\Release\netstandard2.0\AiForms.Effects.xml</DocumentationFile>
1111
<NoWarn>1701;1702;1591</NoWarn>
12+
<LangVersion>8.0</LangVersion>
13+
</PropertyGroup>
14+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
15+
<LangVersion>8.0</LangVersion>
1216
</PropertyGroup>
1317
<ItemGroup>
1418
<PackageReference Include="Xamarin.Forms" Version="4.4.0.991537" />

0 commit comments

Comments
 (0)