Merge remote-tracking branch 'origin/master' into tizen
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / EXaml / EXamlExtensions.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 using System;
18 using System.Collections.Generic;
19 using System.ComponentModel;
20 using System.IO;
21 using System.Reflection;
22 using System.Text;
23
24 namespace Tizen.NUI.EXaml
25 {
26     /// Internal used, will never be opened.
27     [EditorBrowsable(EditorBrowsableState.Never)]
28     public static class EXamlExtensions
29     {
30         /// Internal used, will never be opened.
31         [EditorBrowsable(EditorBrowsableState.Never)]
32         public static TXaml LoadFromEXamlPath<TXaml>(this TXaml view, string path)
33         {
34             MainAssembly = view.GetType().Assembly;
35             //This EXaml file will be converted by Tizen.NUI.XamlBuild from the .xaml
36             string xamlScript = GetXamlFromPath(path);
37             LoadEXaml.Load(view, xamlScript);
38             return view;
39         }
40
41         /// Internal used, will never be opened.
42         [EditorBrowsable(EditorBrowsableState.Never)]
43         public static T LoadFromEXamlPath<T>(this T view, Type callingType)
44         {
45             if (null == callingType)
46             {
47                 return view;
48             }
49
50             MainAssembly = view.GetType().Assembly;
51
52             string resourceName = callingType.Name + ".examl";
53             string resource = Tizen.Applications.Application.Current.DirectoryInfo.Resource;
54
55             Tizen.Log.Fatal("NUI", "the resource path: " + resource);
56             int windowWidth = NUIApplication.GetDefaultWindow().Size.Width;
57             int windowHeight = NUIApplication.GetDefaultWindow().Size.Height;
58
59             string likelyResourcePath = resource + "layout/" + windowWidth.ToString() + "x" + windowHeight.ToString() + "/" + resourceName;
60             Tizen.Log.Fatal("NUI", "the resource path: " + likelyResourcePath);
61
62             if (!File.Exists(likelyResourcePath))
63             {
64                 likelyResourcePath = resource + "layout/" + resourceName;
65             }
66
67             //Find the xaml file in the layout folder
68             if (File.Exists(likelyResourcePath))
69             {
70                 StreamReader reader = new StreamReader(likelyResourcePath);
71                 var xaml = reader.ReadToEnd();
72                 reader.Close();
73                 reader.Dispose();
74
75                 LoadEXaml.Load(view, xaml);
76             }
77                 
78             return view;
79         }
80
81         /// Internal used, will never be opened.
82         [EditorBrowsable(EditorBrowsableState.Never)]
83         public static T LoadFromEXamlByRelativePath<T>(this T view, string eXamlPath)
84         {
85             if (null == eXamlPath)
86             {
87                 return view;
88             }
89
90             MainAssembly = view.GetType().Assembly;
91
92             string resource = Tizen.Applications.Application.Current.DirectoryInfo.Resource;
93
94             Tizen.Log.Fatal("NUI", "the resource path: " + resource);
95             int windowWidth = NUIApplication.GetDefaultWindow().Size.Width;
96             int windowHeight = NUIApplication.GetDefaultWindow().Size.Height;
97
98             string likelyResourcePath = resource + eXamlPath;
99
100             //Find the xaml file in the layout folder
101             if (File.Exists(likelyResourcePath))
102             {
103                 StreamReader reader = new StreamReader(likelyResourcePath);
104                 var xaml = reader.ReadToEnd();
105                 reader.Close();
106                 reader.Dispose();
107
108                 LoadEXaml.Load(view, xaml);
109             }
110             else
111             {
112                 throw new Exception($"Can't find examl file {eXamlPath}");
113             }
114
115             return view;
116         }
117
118         /// Used for TCT and TC coverage, will never be opened.
119         [EditorBrowsable(EditorBrowsableState.Never)]
120         public static T LoadFromEXaml<T>(this T view, string eXamlStr)
121         {
122             if (null == eXamlStr)
123             {
124                 return view;
125             }
126
127             MainAssembly = view.GetType().Assembly;
128
129             LoadEXaml.Load(view, eXamlStr);
130
131             return view;
132         }
133
134         /// Internal used, will never be opened.
135         [EditorBrowsable(EditorBrowsableState.Never)]
136         public static Assembly MainAssembly
137         {
138             get;
139             set;
140         }
141
142         private static string GetXamlFromPath(string path)
143         {
144             string xaml;
145
146             if (File.Exists(path))
147             {
148                 StreamReader reader = new StreamReader(path);
149                 xaml = reader.ReadToEnd();
150                 reader.Dispose();
151                 return xaml;
152             }
153             return null;
154         }
155     }
156 }