summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Renderers/FrameRenderer.cs
blob: 414553f7ddab83ab78aa945b20099a974aa610c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
´╗┐using ElmSharp;
using EColor = ElmSharp.Color;

namespace Xamarin.Forms.Platform.Tizen
{
	public class FrameRenderer : ViewRenderer<Frame, Native.Canvas>
	{
		const int _thickness = 2;
		const int _shadow_shift = 2;
		const int _shadow_thickness = _thickness + 2;

		static readonly EColor s_DefaultColor = EColor.Black;
		static readonly EColor s_ShadowColor = EColor.FromRgba(80, 80, 80, 50);

		Polygon _shadow = null;
		Polygon _frame = null;

		static FrameRenderer()
		{
			RegisterPropertyHandler(Frame.OutlineColorProperty, (r) => (r as FrameRenderer).UpdateColor());
			RegisterPropertyHandler(Frame.HasShadowProperty, (r) => (r as FrameRenderer).UpdateShadowVisibility());
		}

		protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
		{
			if (Control == null)
			{
				SetNativeControl(new Native.Canvas(Forms.Context.MainWindow));

				_shadow = new Polygon(NativeView);
				_shadow.Color = s_ShadowColor;
				Control.Children.Add(_shadow);

				_frame = new Polygon(NativeView);
				_frame.Show();
				Control.Children.Add(_frame);
				Control.LayoutUpdated += OnLayoutUpdated;
			}
			base.OnElementChanged(e);
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (Control != null)
				{
					Control.LayoutUpdated -= OnLayoutUpdated;
				}
			}
			base.Dispose(disposing);
		}

		static void DrawFrame(Polygon frame, int left, int top, int right, int bottom, int thickness)
		{
			frame.ClearPoints();
			if (left + thickness >= right || top + thickness >= bottom)
			{
				if (left >= right || top >= bottom)
					return;
				// shape reduces to a rectangle
				frame.AddPoint(left, top);
				frame.AddPoint(right, top);
				frame.AddPoint(right, bottom);
				frame.AddPoint(left, bottom);
				return;
			}
			// outside edge
			frame.AddPoint(left, top);
			frame.AddPoint(right, top);
			frame.AddPoint(right, bottom);
			frame.AddPoint(left, bottom);
			frame.AddPoint(left, top + thickness);
			// and inside edge
			frame.AddPoint(left + thickness, top + thickness);
			frame.AddPoint(left + thickness, bottom - thickness);
			frame.AddPoint(right - thickness, bottom - thickness);
			frame.AddPoint(right - thickness, top + thickness);
			frame.AddPoint(left, top + thickness);
		}

		void OnLayoutUpdated(object sender, Native.LayoutEventArgs e)
		{
			UpdateGeometry();
		}

		void UpdateGeometry()
		{
			var geometry = NativeView.Geometry;
			DrawFrame(_frame,
				geometry.X,
				geometry.Y,
				geometry.Right,
				geometry.Bottom,
				_thickness
			);
			DrawFrame(_shadow,
				geometry.X + _shadow_shift,
				geometry.Y + _shadow_shift,
				geometry.Right - _thickness + _shadow_shift + _shadow_thickness,
				geometry.Bottom - _thickness + _shadow_shift + _shadow_thickness,
				_shadow_thickness
			);
		}

		void UpdateColor()
		{
			if (Element.OutlineColor.IsDefault)
				_frame.Color = s_DefaultColor;
			else
				_frame.Color = Element.OutlineColor.ToNative();
		}

		void UpdateShadowVisibility()
		{
			if (Element.HasShadow)
				_shadow.Show();
			else
				_shadow.Hide();
		}
	}
}