summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Native/Canvas.cs
blob: 12ca2a1519675594f1ddd10073c947864638bd3a (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
using System.Collections.Generic;
using System.Collections.Specialized;
using ElmSharp;

namespace Xamarin.Forms.Platform.Tizen.Native
{
	/// <summary>
	/// A Canvas provides a class which can be a container for other controls.
	/// </summary>
	/// <remarks>
	/// This class is used as a container view for Layouts from Xamarin.Forms.Platform.Tizen framework.
	/// It is used for implementing xamarin pages and layouts.
	/// </remarks>
	public class Canvas : Box, IContainable<EvasObject>
	{
		/// <summary>
		/// The list of Views.
		/// </summary>
		readonly ObservableCollection<EvasObject> _children = new ObservableCollection<EvasObject>();

		/// <summary>
		/// Initializes a new instance of the <see cref="Xamarin.Forms.Platform.Tizen.Native.Canvas"/> class.
		/// </summary>
		/// <remarks>Canvas doesn't support replacing its children, this will be ignored.</remarks>
		/// <param name="parent">Parent of this instance.</param>
		public Canvas(EvasObject parent) : base(parent)
		{
			_children.CollectionChanged += (o, e) =>
			{
				if (e.Action == NotifyCollectionChangedAction.Add)
				{
					foreach (var v in e.NewItems)
					{
						var view = v as EvasObject;
						if (null != view)
						{
							OnAdd(view);
						}
					}
				}
				else if (e.Action == NotifyCollectionChangedAction.Remove)
				{
					foreach (var v in e.OldItems)
					{
						var view = v as EvasObject;
						if (null != view)
						{
							OnRemove(view);
						}
					}
				}
				else if (e.Action == NotifyCollectionChangedAction.Reset)
				{
					OnRemoveAll();
				}
			};
		}

		/// <summary>
		/// Gets list of native elements that are placed in the canvas.
		/// </summary>
		public new IList<EvasObject> Children
		{
			get
			{
				return _children;
			}
		}

		/// <summary>
		/// Provides destruction for native element and contained elements.
		/// </summary>
		protected override void OnUnrealize()
		{
			foreach (var child in _children)
			{
				child.Unrealize();
			}

			base.OnUnrealize();
		}

		/// <summary>
		/// Adds a new child to a container.
		/// </summary>
		/// <param name="view">Native element which will be added</param>
		void OnAdd(EvasObject view)
		{
			PackEnd(view);
		}

		/// <summary>
		/// Removes a child from a container.
		/// </summary>
		/// <param name="view">Child element to be removed from canvas</param>
		void OnRemove(EvasObject view)
		{
			UnPack(view);
		}

		/// <summary>
		/// Removes all children from a canvas.
		/// </summary>
		void OnRemoveAll()
		{
			UnPackAll();
		}
	}
}