Release 10.0.0.16997
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / XamlBinding / ResourcesExtensions.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 using System;
19 using System.Collections.Generic;
20
21 namespace Tizen.NUI.Binding
22 {
23     internal static class ResourcesExtensions
24     {
25         public static IEnumerable<KeyValuePair<string, object>> GetMergedResources(this IElement element)
26         {
27             Dictionary<string, object> resources = null;
28             while (element != null)
29             {
30                 var ve = element as IResourcesProvider;
31                 if (ve != null && ve.IsResourcesCreated)
32                 {
33                     resources = resources ?? new Dictionary<string, object>();
34                     if (null == resources)
35                     {
36                         return null;
37                     }
38
39                     if (ve.XamlResources != null)
40                     {
41                         foreach (KeyValuePair<string, object> res in ve.XamlResources.MergedResources)
42                             if (!resources.ContainsKey(res.Key))
43                                 resources.Add(res.Key, res.Value);
44                     }
45                 }
46
47                 var app = element as Application;
48                 if (app != null && app.SystemResources != null)
49                 {
50                     resources = resources ?? new Dictionary<string, object>(8);
51                     if (null == resources)
52                     {
53                         return null;
54                     }
55
56                     foreach (KeyValuePair<string, object> res in app.SystemResources)
57                         if (!resources.ContainsKey(res.Key))
58                             resources.Add(res.Key, res.Value);
59                 }
60                 element = element.Parent;
61             }
62             return resources;
63         }
64
65         public static bool TryGetResource(this IElement element, string key, out object value)
66         {
67             while (element != null)
68             {
69                 var ve = element as IResourcesProvider;
70                 if (ve != null && ve.IsResourcesCreated && ve.XamlResources != null && ve.XamlResources.TryGetValue(key, out value))
71                 {
72                     return true;
73                 }
74                 var app = element as Application;
75                 if (app != null && app.SystemResources != null && app.SystemResources.TryGetValue(key, out value))
76                 {
77                     return true;
78                 }
79                 element = element.Parent;
80             }
81
82             //Fallback for the XF previewer
83             if (Application.Current != null && ((IResourcesProvider)Application.Current).IsResourcesCreated && Application.Current.XamlResources.TryGetValue(key, out value))
84                 return true;
85
86             value = null;
87             return false;
88         }
89     }
90 }