[NUI] Fix Tizen.NUI.Samples crash error when it is terminated (#1353)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / DaliDemo / DaliDemo.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Reflection;
5 using System.Text;
6 using System.Threading.Tasks;
7 using Tizen.NUI.BaseComponents;
8
9 namespace Tizen.NUI.Samples
10 {
11     public class DaliDemo : NUIApplication
12     {
13         public DaliDemo(string styleSheet) : base(styleSheet)
14         {
15         }
16
17         private IExample curExample = null;
18
19         private void FullGC()
20         {
21             global::System.GC.Collect();
22             global::System.GC.WaitForPendingFinalizers();
23             global::System.GC.Collect();
24         }
25
26         private void DeleteDaliDemo()
27         {
28             Window.Instance.Remove(demo.mRootActor);
29             demo.mRootActor.Dispose();
30             demo = null;
31
32             FullGC();
33         }
34
35         private void CreateDaliDemo()
36         {
37             demo = new DaliTableView((string name) =>
38             {
39                 string fileName = global::System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
40
41                 //global::System.Diagnostics.Process.Start(fileName, name);
42
43                 RunSample(name);
44             });
45
46             Assembly assembly = this.GetType().Assembly;
47
48             Type exampleType = assembly.GetType("Tizen.NUI.Samples.IExample");
49
50             foreach (Type type in assembly.GetTypes())
51             {
52                 if (exampleType.IsAssignableFrom(type) && type.Name != "SampleMain" && this.GetType() != type && true == type.IsClass)
53                 {
54                     demo.AddExample(new Example(type.FullName, type.Name));
55                 }
56             }
57
58             demo.SortAlphabetically(true);
59
60             demo.Initialize();
61
62             Window.Instance.GetDefaultLayer().Add(demo.mRootActor);
63         }
64
65         private void RunSample(string name)
66         {
67             Assembly assembly = typeof(DaliDemo).Assembly;
68
69             Type exampleType = assembly?.GetType(name);
70             IExample example = assembly?.CreateInstance(name) as IExample;
71
72             if (null != example)
73             {
74                 DeleteDaliDemo();
75
76                 example.Activate();
77             }
78
79             curExample = example;
80         }
81
82         private void ExitSample()
83         {
84             curExample?.Deactivate();
85             curExample = null;
86
87             FullGC();
88
89             CreateDaliDemo();
90         }
91
92         protected override void OnCreate()
93         {
94             base.OnCreate();
95             CreateDaliDemo();
96
97             Window.Instance.KeyEvent += Instance_KeyEvent;
98             Window.Instance.BackgroundColor = Color.White;
99         }
100
101         private void Instance_KeyEvent(object sender, Window.KeyEventArgs e)
102         {
103             if (e.Key.State == Key.StateType.Up)
104             {
105                 if (e.Key.KeyPressedName == "Escape" || e.Key.KeyPressedName == "XF86Back" || e.Key.KeyPressedName == "BackSpace")
106                 {
107                     if (null != curExample)
108                     {
109                         ExitSample();
110                     }
111                     else
112                     {
113                         Exit();
114                     }
115                 }
116             }
117         }
118
119         public void Deactivate()
120         {
121             demo = null;
122         }
123
124         private DaliTableView demo;
125     }
126 }