[NUI]Add xaml support for nui and nui xaml test sample (#230)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Xaml / FillResourceDictionariesVisitor.cs
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Reflection;
5 using Tizen.NUI.Binding;
6
7 namespace Tizen.NUI.Xaml
8 {
9         class FillResourceDictionariesVisitor : IXamlNodeVisitor
10         {
11                 public FillResourceDictionariesVisitor(HydrationContext context)
12                 {
13                         Context = context;
14                 }
15
16                 HydrationContext Context { get; }
17                 Dictionary<INode, object> Values => Context.Values;
18
19                 public TreeVisitingMode VisitingMode => TreeVisitingMode.TopDown;
20                 public bool StopOnDataTemplate => true;
21                 public bool StopOnResourceDictionary => false;
22                 public bool VisitNodeOnDataTemplate => false;
23
24                 public void Visit(ValueNode node, INode parentNode)
25                 {
26                         if (!typeof(ResourceDictionary).IsAssignableFrom(Context.Types[((IElementNode)parentNode)]))
27                                 return;
28
29                         node.Accept(new ApplyPropertiesVisitor(Context, stopOnResourceDictionary: false), parentNode);
30                 }
31
32                 public void Visit(MarkupNode node, INode parentNode)
33                 {
34                 }
35
36                 public void Visit(ElementNode node, INode parentNode)
37                 {
38                         var value = Values[node];
39                         XmlName propertyName;
40                         //Set RD to VE
41                         if (typeof(ResourceDictionary).IsAssignableFrom(Context.Types[node]) && ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out propertyName)) {
42                                 if ((propertyName.LocalName == "Resources" ||
43                                          propertyName.LocalName.EndsWith(".Resources", StringComparison.Ordinal)) && value is ResourceDictionary) {
44                                         var source = Values[parentNode];
45                                         ApplyPropertiesVisitor.SetPropertyValue(source, propertyName, value, Context.RootElement, node, Context, node);
46                                         return;
47                                 }
48                         }
49
50                         //Only proceed further if the node is a keyless RD
51                         if (   parentNode is IElementNode
52                                 && typeof(ResourceDictionary).IsAssignableFrom(Context.Types[((IElementNode)parentNode)])
53                                 && !((IElementNode)parentNode).Properties.ContainsKey(XmlName.xKey))
54                                 node.Accept(new ApplyPropertiesVisitor(Context, stopOnResourceDictionary: false), parentNode);
55                         else if (   parentNode is ListNode
56                                          && typeof(ResourceDictionary).IsAssignableFrom(Context.Types[((IElementNode)parentNode.Parent)])
57                                          && !((IElementNode)parentNode.Parent).Properties.ContainsKey(XmlName.xKey))
58                                 node.Accept(new ApplyPropertiesVisitor(Context, stopOnResourceDictionary: false), parentNode);
59                 }
60
61                 public void Visit(RootNode node, INode parentNode)
62                 {
63                 }
64
65                 public void Visit(ListNode node, INode parentNode)
66                 {
67                 }
68
69                 public bool SkipChildren(INode node, INode parentNode)
70                 {
71                         var enode = node as ElementNode;
72                         if (enode is null)
73                                 return false;
74                         if (   parentNode is IElementNode
75                             && typeof(ResourceDictionary).IsAssignableFrom(Context.Types[((IElementNode)parentNode)])
76                             && !((IElementNode)parentNode).Properties.ContainsKey(XmlName.xKey))
77                                 return true;
78                         if (   parentNode is ListNode
79                                 && typeof(ResourceDictionary).IsAssignableFrom(Context.Types[((IElementNode)parentNode.Parent)])
80                                 && !((IElementNode)parentNode.Parent).Properties.ContainsKey(XmlName.xKey))
81                                 return true;
82                         return false;
83                 }
84
85         }
86 }