2 * Copyright(c) 2022 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Collections;
20 using System.Collections.Generic;
21 using System.Reflection;
22 using Tizen.NUI.Binding;
24 namespace Tizen.NUI.Xaml
26 internal class FillResourceDictionariesVisitor : IXamlNodeVisitor
28 public FillResourceDictionariesVisitor(HydrationContext context)
33 HydrationContext Context { get; }
34 Dictionary<INode, object> Values => Context.Values;
36 public TreeVisitingMode VisitingMode => TreeVisitingMode.TopDown;
37 public bool StopOnDataTemplate => true;
38 public bool StopOnResourceDictionary => false;
39 public bool VisitNodeOnDataTemplate => false;
41 public bool IsResourceDictionary(ElementNode node) => typeof(ResourceDictionary).IsAssignableFrom(Context.Types[node]);
43 public void Visit(ValueNode node, INode parentNode)
45 if (!Context.Types.TryGetValue((IElementNode)parentNode, out var type) || !typeof(ResourceDictionary).IsAssignableFrom(type))
48 node.Accept(new ApplyPropertiesVisitor(Context, stopOnResourceDictionary: false), parentNode);
51 public void Visit(MarkupNode node, INode parentNode)
55 public void Visit(ElementNode node, INode parentNode)
57 if (!Values.TryGetValue(node, out var value) && Context.ExceptionHandler != null)
61 if (typeof(ResourceDictionary).IsAssignableFrom(Context.Types[node]) && ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out propertyName))
63 if ((propertyName.LocalName == "XamlResources" ||
64 propertyName.LocalName.EndsWith(".XamlResources", StringComparison.Ordinal)) && value is ResourceDictionary)
66 var source = Values[parentNode];
67 ApplyPropertiesVisitor.SetPropertyValue(source, propertyName, value, Context.RootElement, node, Context, node);
72 //Only proceed further if the node is a keyless RD
73 if (parentNode is IElementNode
74 && Context.Types.TryGetValue((IElementNode)parentNode, out var parentType)
75 && typeof(ResourceDictionary).IsAssignableFrom(parentType)
76 && !((IElementNode)parentNode).Properties.ContainsKey(XmlName.xKey))
77 node.Accept(new ApplyPropertiesVisitor(Context, stopOnResourceDictionary: false), parentNode);
78 else if (parentNode is ListNode
79 && typeof(ResourceDictionary).IsAssignableFrom(Context.Types[((IElementNode)parentNode.Parent)])
80 && !((IElementNode)parentNode.Parent).Properties.ContainsKey(XmlName.xKey))
81 node.Accept(new ApplyPropertiesVisitor(Context, stopOnResourceDictionary: false), parentNode);
84 public void Visit(RootNode node, INode parentNode)
88 public void Visit(ListNode node, INode parentNode)
92 public bool SkipChildren(INode node, INode parentNode)
94 var enode = node as ElementNode;
97 if (parentNode is IElementNode
98 && Context.Types.TryGetValue((IElementNode)parentNode, out var parentType)
99 && typeof(ResourceDictionary).IsAssignableFrom(parentType)
100 && !((IElementNode)parentNode).Properties.ContainsKey(XmlName.xKey))
102 if (parentNode is ListNode
103 && typeof(ResourceDictionary).IsAssignableFrom(Context.Types[((IElementNode)parentNode.Parent)])
104 && !((IElementNode)parentNode.Parent).Properties.ContainsKey(XmlName.xKey))