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.
18 using System.Collections;
24 namespace Tizen.NUI.Xaml.Build.Tasks
26 class SetResourcesVisitor : IXamlNodeVisitor
28 public SetResourcesVisitor(ILContext context)
31 Module = context.Body.Method.Module;
34 public ILContext Context { get; }
35 ModuleDefinition Module { get; }
36 public TreeVisitingMode VisitingMode => TreeVisitingMode.TopDown;
37 public bool StopOnDataTemplate => true;
38 public bool StopOnResourceDictionary => false;
39 public bool VisitNodeOnDataTemplate => false;
41 public void Visit(ValueNode node, INode parentNode)
43 if (!IsResourceDictionary((IElementNode)parentNode))
46 node.Accept(new SetPropertiesVisitor(Context, stopOnResourceDictionary: false), parentNode);
49 public void Visit(MarkupNode node, INode parentNode)
54 public void Visit(ElementNode node, INode parentNode)
57 //Set ResourcesDictionaries to their parents
58 if (IsResourceDictionary(node) && SetPropertiesVisitor.TryGetPropertyName(node, parentNode, out propertyName)) {
59 if ((propertyName.LocalName == "XamlResources" || propertyName.LocalName.EndsWith(".XamlResources", StringComparison.Ordinal))) {
60 Context.IL.Append(SetPropertiesVisitor.SetPropertyValue(Context.Variables[(IElementNode)parentNode], propertyName, node, Context, node));
65 //Only proceed further if the node is a keyless RD
66 if ( parentNode is IElementNode
67 && IsResourceDictionary((IElementNode)parentNode)
68 && !((IElementNode)parentNode).Properties.ContainsKey(XmlName.xKey))
69 node.Accept(new SetPropertiesVisitor(Context, stopOnResourceDictionary: false), parentNode);
70 else if ( parentNode is ListNode
71 && IsResourceDictionary((IElementNode)parentNode.Parent)
72 && !((IElementNode)parentNode.Parent).Properties.ContainsKey(XmlName.xKey))
73 node.Accept(new SetPropertiesVisitor(Context, stopOnResourceDictionary: false), parentNode);
76 public void Visit(RootNode node, INode parentNode)
80 public void Visit(ListNode node, INode parentNode)
84 public bool IsResourceDictionary(ElementNode node) => IsResourceDictionary((IElementNode)node);
86 bool IsResourceDictionary(IElementNode node)
88 var parentVar = Context.Variables[(IElementNode)node];
89 return parentVar.VariableType.FullName == "Tizen.NUI.Binding.ResourceDictionary"
90 || parentVar.VariableType.ResolveCached().BaseType?.FullName == "Tizen.NUI.Binding.ResourceDictionary";
93 public bool SkipChildren(INode node, INode parentNode)
95 var enode = node as ElementNode;
98 if ( parentNode is IElementNode
99 && IsResourceDictionary((IElementNode)parentNode)
100 && !((IElementNode)parentNode).Properties.ContainsKey(XmlName.xKey))
102 if ( parentNode is ListNode
103 && IsResourceDictionary((IElementNode)parentNode.Parent)
104 && !((IElementNode)parentNode.Parent).Properties.ContainsKey(XmlName.xKey))