[NUI][EXaml] Support DataTemplate in EXaml
authorFang Xiaohui <xiaohui.fang@samsung.com>
Fri, 30 Jul 2021 01:48:06 +0000 (09:48 +0800)
committerdongsug-song <35130733+dongsug-song@users.noreply.github.com>
Tue, 3 Aug 2021 01:50:50 +0000 (10:50 +0900)
src/Tizen.NUI/src/internal/EXaml/Action/OtherActions.cs
src/Tizen.NUI/src/internal/EXaml/Block/GatherLongStringsBlock.cs [new file with mode: 0755]
src/Tizen.NUI/src/internal/EXaml/GlobalDataList.cs
src/Tizen.NUI/src/internal/EXaml/LoadEXaml.cs
src/Tizen.NUI/src/internal/EXaml/Operation/AddToCollectionInstance.cs [new file with mode: 0755]
src/Tizen.NUI/src/internal/EXaml/Operation/CreateDataTemplate.cs [new file with mode: 0755]
src/Tizen.NUI/src/internal/EXaml/Operation/CreateInstance.cs
src/Tizen.NUI/src/internal/EXaml/Operation/GatherType.cs
src/Tizen.NUI/src/public/EXaml/EXamlExtensions.cs

index b1b9f93..01a66c8 100755 (executable)
@@ -68,10 +68,30 @@ namespace Tizen.NUI.EXaml
                 switch (index)
                 {
                     case 0:
-                        int typeIndex = (int)getValues.ValueList[1];
-                        var items = getValues.ValueList[2] as List<object>;
-                        var createArrayInstanceOp = new CreateArrayInstance(globalDataList, typeIndex, items);
-                        globalDataList.Operations.Add(createArrayInstanceOp);
+                        {
+                            int typeIndex = (int)getValues.ValueList[1];
+                            var items = getValues.ValueList[2] as List<object>;
+                            var createArrayInstanceOp = new CreateArrayInstance(globalDataList, typeIndex, items);
+                            globalDataList.Operations.Add(createArrayInstanceOp);
+                        }
+                        break;
+
+                    case 1:
+                        {
+                            int typeIndex = (int)getValues.ValueList[1];
+                            int startIndex = (int)getValues.ValueList[2];
+                            int endIndex = (int)getValues.ValueList[3];
+                            var createDataTemplateOp = new CreateDataTemplate(globalDataList, typeIndex, (startIndex, endIndex));
+                            globalDataList.Operations.Add(createDataTemplateOp);
+                        }
+                        break;
+
+                    case 2:
+                        {
+                            int instanceIndex = (getValues.ValueList[1] as Instance).Index;
+                            var value = getValues.ValueList[2];
+                            globalDataList.Operations.Add(new AddToCollectionInstance(globalDataList, instanceIndex, value));
+                        }
                         break;
 
                     default:
diff --git a/src/Tizen.NUI/src/internal/EXaml/Block/GatherLongStringsBlock.cs b/src/Tizen.NUI/src/internal/EXaml/Block/GatherLongStringsBlock.cs
new file mode 100755 (executable)
index 0000000..d764917
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Copyright(c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+using System;
+using System.Collections.Generic;
+using System.Reflection;
+using System.Text;
+
+namespace Tizen.NUI.EXaml
+{
+    internal class GatherLongStringsBlock : Action
+    {
+        public GatherLongStringsBlock(GlobalDataList globalDataList, Action parent)
+        {
+            this.parent = parent;
+            this.globalDataList = globalDataList;
+        }
+
+        private Action parent;
+        private GlobalDataList globalDataList;
+
+        public Action DealChar(char c)
+        {
+            switch (c)
+            {
+                case '>':
+                    blockStartCount--;
+                    break;
+
+                case '<':
+                    blockStartCount++;
+                    break;
+            }
+
+            if (0 == blockStartCount)
+            {
+                parent?.OnActive();
+                return parent;
+            }
+            else
+            {
+                globalDataList.LongStrings += c;
+                return this;
+            }
+        }
+
+        private int blockStartCount = 1;
+
+        public void Init()
+        {
+        }
+
+        public void OnActive()
+        {
+        }
+    }
+}
index 162eeb2..057811b 100755 (executable)
@@ -103,5 +103,11 @@ namespace Tizen.NUI.EXaml
         }
 
         internal List<Assembly> GatheredAssemblies = new List<Assembly>();
+
+        internal string LongStrings
+        {
+            get;
+            set;
+        }
     }
 }
index 40c262a..a15221c 100755 (executable)
@@ -44,6 +44,17 @@ namespace Tizen.NUI.EXaml
 
             Action currentOp = null;
 
+            Action[] blockActions = new Action[]
+                {
+                    new GatherAssembliesBlock(globalDataList, null),
+                    new GatherTypesBlock(globalDataList, null),
+                    new GatherPropertiesBlock(globalDataList, null),
+                    new GatherEventsBlock(globalDataList, null),
+                    new GatherMethodsBlock(globalDataList, null),
+                    new GatherBindablePropertiesBlock(globalDataList, null),
+                    new GatherLongStringsBlock(globalDataList, null)
+                };
+
             foreach (char c in xaml)
             {
                 if (null == currentOp)
@@ -51,42 +62,8 @@ namespace Tizen.NUI.EXaml
                     switch (c)
                     {
                         case '<':
-                            if (0 == index)
-                            {
-                                currentOp = new GatherAssembliesBlock(globalDataList, null);
-                                currentOp.Init();
-                                index++;
-                            }
-                            else if (1 == index)
-                            {
-                                currentOp = new GatherTypesBlock(globalDataList, null);
-                                currentOp.Init();
-                                index++;
-                            }
-                            else if (2 == index)
-                            {
-                                currentOp = new GatherPropertiesBlock(globalDataList, null);
-                                currentOp.Init();
-                                index++;
-                            }
-                            else if (3 == index)
-                            {
-                                currentOp = new GatherEventsBlock(globalDataList, null);
-                                currentOp.Init();
-                                index++;
-                            }
-                            else if (4 == index)
-                            {
-                                currentOp = new GatherMethodsBlock(globalDataList, null);
-                                currentOp.Init();
-                                index++;
-                            }
-                            else if (5 == index)
-                            {
-                                currentOp = new GatherBindablePropertiesBlock(globalDataList, null);
-                                currentOp.Init();
-                                index++;
-                            }
+                            currentOp = blockActions[index++];
+                            currentOp.Init();
                             break;
 
                         case '{':
@@ -169,7 +146,7 @@ namespace Tizen.NUI.EXaml
             return globalDataList;
         }
 
-        internal static void Load(object view, string xaml, out object xamlData)
+        internal static void Load(object view, string xaml, out GlobalDataList xamlData)
         {
             var globalDataList = GatherDataList(xaml);
 
diff --git a/src/Tizen.NUI/src/internal/EXaml/Operation/AddToCollectionInstance.cs b/src/Tizen.NUI/src/internal/EXaml/Operation/AddToCollectionInstance.cs
new file mode 100755 (executable)
index 0000000..b242355
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Copyright(c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Reflection;
+using System.Text;
+using Tizen.NUI.BaseComponents;
+using Tizen.NUI.Binding;
+using Tizen.NUI.Binding.Internals;
+using Tizen.NUI.Xaml;
+
+namespace Tizen.NUI.EXaml
+{
+    internal class AddToCollectionInstance : Operation
+    {
+        public AddToCollectionInstance(GlobalDataList globalDataList, int instanceIndex, object value)
+        {
+            this.instanceIndex = instanceIndex;
+            this.value = value;
+            this.globalDataList = globalDataList;
+        }
+
+        private GlobalDataList globalDataList;
+
+        public void Do()
+        {
+            var collection = globalDataList.GatheredInstances[instanceIndex] as IList;
+
+            if (null != collection)
+            {
+                if (value is Instance)
+                {
+                    int valueIndex = (value as Instance).Index;
+                    value = globalDataList.GatheredInstances[valueIndex];
+                }
+
+                if (value is IMarkupExtension markupExtension)
+                {
+                    value = markupExtension.ProvideValue(null);
+                }
+
+                collection.Add(value);
+            }
+        }
+
+        private int instanceIndex;
+        private object value;
+    }
+}
diff --git a/src/Tizen.NUI/src/internal/EXaml/Operation/CreateDataTemplate.cs b/src/Tizen.NUI/src/internal/EXaml/Operation/CreateDataTemplate.cs
new file mode 100755 (executable)
index 0000000..d75f245
--- /dev/null
@@ -0,0 +1,38 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Tizen.NUI.Binding;
+
+namespace Tizen.NUI.EXaml
+{
+    class CreateDataTemplate : Operation
+    {
+        public CreateDataTemplate(GlobalDataList globalDataList, int typeIndex, (int, int) indexRangeOfContent)
+        {
+            this.typeIndex = typeIndex;
+            this.indexRangeOfContent = indexRangeOfContent;
+            this.globalDataList = globalDataList;
+        }
+
+        private GlobalDataList globalDataList;
+        private int typeIndex;
+        private (int, int) indexRangeOfContent;
+
+        public void Do()
+        {
+            var content = globalDataList.LongStrings.Substring(indexRangeOfContent.Item1, indexRangeOfContent.Item2 - indexRangeOfContent.Item1 + 1);
+            var dataTemplate = new DataTemplate(() =>
+            {
+                object ret = EXamlExtensions.CreateObjectFromEXaml(content);
+                return ret;
+            });
+
+            globalDataList.GatheredInstances.Add(dataTemplate);
+
+            if (null == globalDataList.Root)
+            {
+                globalDataList.Root = dataTemplate;
+            }
+        }
+    }
+}
index 5add780..967079e 100755 (executable)
@@ -93,6 +93,11 @@ namespace Tizen.NUI.EXaml
             {
                 globalDataList.GatheredInstances.Add(obj);
 
+                if (globalDataList.Root == null)
+                {
+                    globalDataList.Root = obj;
+                }
+
                 if (obj is BindableObject bindableObject)
                 {
                     bindableObject.IsCreateByXaml = true;
index 4aa1145..f4168fa 100755 (executable)
@@ -47,7 +47,16 @@ namespace Tizen.NUI.EXaml
 
                 for (int i = 0; i < genericTypeIndexs.Count; i++)
                 {
-                    args[i] = globalDataList.GatheredTypes[genericTypeIndexs[i]];
+                    int typeIndex = genericTypeIndexs[i];
+
+                    if (typeIndex >= 0)
+                    {
+                        args[i] = globalDataList.GatheredTypes[typeIndex];
+                    }
+                    else
+                    {
+                        args[i] = GetBaseType.GetBaseTypeByIndex(typeIndex);
+                    }
                 }
 
                 type = type.MakeGenericType(args);
index 3134b22..c933798 100755 (executable)
@@ -110,7 +110,7 @@ namespace Tizen.NUI.EXaml
         [EditorBrowsable(EditorBrowsableState.Never)]
         public static object LoadFromEXamlByRelativePath<T>(this T view, string eXamlPath)
         {
-            object eXamlData = null;
+            GlobalDataList eXamlData = null;
 
             if (null == eXamlPath)
             {
@@ -163,6 +163,22 @@ namespace Tizen.NUI.EXaml
 
         /// Internal used, will never be opened.
         [EditorBrowsable(EditorBrowsableState.Never)]
+        public static object CreateObjectFromEXaml(string eXamlStr)
+        {
+            if (null == eXamlStr)
+            {
+                return null;
+            }
+
+            //MainAssembly = view.GetType().Assembly;
+            object temp = null;
+            GlobalDataList eXamlData = null;
+            LoadEXaml.Load(temp, eXamlStr, out eXamlData);
+            return eXamlData.Root;
+        }
+
+        /// Internal used, will never be opened.
+        [EditorBrowsable(EditorBrowsableState.Never)]
         public static Assembly MainAssembly
         {
             get;