Follow formatting NUI
[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     internal 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 bool IsResourceDictionary(ElementNode node) => typeof(ResourceDictionary).IsAssignableFrom(Context.Types[node]);
25
26         public void Visit(ValueNode node, INode parentNode)
27         {
28             if (!typeof(ResourceDictionary).IsAssignableFrom(Context.Types[((IElementNode)parentNode)]))
29                 return;
30
31             node.Accept(new ApplyPropertiesVisitor(Context, stopOnResourceDictionary: false), parentNode);
32         }
33
34         public void Visit(MarkupNode node, INode parentNode)
35         {
36         }
37
38         public void Visit(ElementNode node, INode parentNode)
39         {
40             var value = Values[node];
41             XmlName propertyName;
42             //Set RD to VE
43             if (typeof(ResourceDictionary).IsAssignableFrom(Context.Types[node]) && ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out propertyName))
44             {
45                 if ((propertyName.LocalName == "Resources" ||
46                      propertyName.LocalName.EndsWith(".Resources", StringComparison.Ordinal)) && value is ResourceDictionary)
47                 {
48                     var source = Values[parentNode];
49                     ApplyPropertiesVisitor.SetPropertyValue(source, propertyName, value, Context.RootElement, node, Context, node);
50                     return;
51                 }
52             }
53
54             //Only proceed further if the node is a keyless RD
55             if (parentNode is IElementNode
56                 && typeof(ResourceDictionary).IsAssignableFrom(Context.Types[((IElementNode)parentNode)])
57                 && !((IElementNode)parentNode).Properties.ContainsKey(XmlName.xKey))
58                 node.Accept(new ApplyPropertiesVisitor(Context, stopOnResourceDictionary: false), parentNode);
59             else if (parentNode is ListNode
60                      && typeof(ResourceDictionary).IsAssignableFrom(Context.Types[((IElementNode)parentNode.Parent)])
61                      && !((IElementNode)parentNode.Parent).Properties.ContainsKey(XmlName.xKey))
62                 node.Accept(new ApplyPropertiesVisitor(Context, stopOnResourceDictionary: false), parentNode);
63         }
64
65         public void Visit(RootNode node, INode parentNode)
66         {
67         }
68
69         public void Visit(ListNode node, INode parentNode)
70         {
71         }
72
73         public bool SkipChildren(INode node, INode parentNode)
74         {
75             var enode = node as ElementNode;
76             if (enode is null)
77                 return false;
78             if (parentNode is IElementNode
79                 && typeof(ResourceDictionary).IsAssignableFrom(Context.Types[((IElementNode)parentNode)])
80                 && !((IElementNode)parentNode).Properties.ContainsKey(XmlName.xKey))
81                 return true;
82             if (parentNode is ListNode
83                 && typeof(ResourceDictionary).IsAssignableFrom(Context.Types[((IElementNode)parentNode.Parent)])
84                 && !((IElementNode)parentNode.Parent).Properties.ContainsKey(XmlName.xKey))
85                 return true;
86             return false;
87         }
88     }
89 }