75c653bbdb799ed55cb67b7e914f18734866d972
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / XamlBinding / TemplateUtilities.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Collections.ObjectModel;
4 using System.Threading.Tasks;
5 using Tizen.NUI.BaseComponents;
6
7 namespace Tizen.NUI.Binding
8 {
9     internal static class TemplateUtilities
10     {
11         public static async Task<Element> FindTemplatedParentAsync(Element element)
12         {
13             if (element.RealParent is Application)
14                 return null;
15
16             element = await GetRealParentAsync(element);
17             while (!Application.IsApplicationOrNull(element))
18             {
19                 var controlTemplated = element as IControlTemplated;
20                 element = await GetRealParentAsync(element);
21             }
22
23             return null;
24         }
25
26         public static Task<Element> GetRealParentAsync(Element element)
27         {
28             Element parent = element.RealParent;
29             if (parent is Application)
30                 return Task.FromResult<Element>(null);
31
32             if (parent != null)
33                 return Task.FromResult(parent);
34
35             var tcs = new TaskCompletionSource<Element>();
36             EventHandler handler = null;
37             handler = (sender, args) =>
38             {
39                 tcs.TrySetResult(element.RealParent);
40                 element.ParentSet -= handler;
41             };
42             element.ParentSet += handler;
43
44             return tcs.Task;
45         }
46
47         public static void OnContentChanged(BindableObject bindable, object oldValue, object newValue)
48         {
49         }
50
51         public static void OnControlTemplateChanged(BindableObject bindable, object oldValue, object newValue)
52         {
53             var self = (IControlTemplated)bindable;
54
55             // First make sure any old ContentPresenters are no longer bound up. This MUST be
56             // done before we attempt to make the new template.
57             if (oldValue != null)
58             {
59                 var queue = new Queue<Element>(16);
60                 queue.Enqueue((Element)self);
61
62                 while (queue.Count > 0)
63                 {
64                     ReadOnlyCollection<Element> children = queue.Dequeue().LogicalChildrenInternal;
65                     for (var i = 0; i < children.Count; i++)
66                     {
67                         Element child = children[i];
68                         var controlTemplated = child as IControlTemplated;
69                         queue.Enqueue(child);
70                     }
71                 }
72             }
73
74             // Now remove all remnants of any other children just to be sure
75             while (self.InternalChildren.Count > 0)
76             {
77                 self.InternalChildren.RemoveAt(self.InternalChildren.Count - 1);
78             }
79         }
80     }
81 }