[NUI] Add CommonUI Samples (#951)
[platform/core/csapi/tizenfx.git] / test / NUITestSample / CommonUI_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;
8 using Tizen.NUI.BaseComponents;
9
10 namespace NuiCommonUiSamples
11 {
12     public class DaliDemo : Tizen.FH.NUI.FHNUIApplication
13     {
14         public DaliDemo() : base()
15         {
16         }
17
18         public DaliDemo(string styleSheet) : base(styleSheet)
19         {
20         }
21
22         private IExample curExample = null;
23
24         private void FullGC()
25         {
26             global::System.GC.Collect();
27             global::System.GC.WaitForPendingFinalizers();
28             global::System.GC.Collect();
29         }
30
31         private void DeleteDaliDemo()
32         {
33             Window.Instance.Remove(demo.mRootActor);
34             demo.mRootActor.Dispose();
35             demo = null;
36
37             FullGC();
38         }
39
40         private void CreateDaliDemo()
41         {
42             demo = new DaliTableView((string name) =>
43             {
44                 RunSample(name);
45             });
46
47             Assembly assembly = this.GetType().Assembly;
48
49             Type exampleType = assembly.GetType("NuiCommonUiSamples.IExample");
50
51             foreach (Type type in assembly.GetTypes())
52             {
53                 if (exampleType.IsAssignableFrom(type) && type.Name != "SampleMain" && this.GetType() != type && true == type.IsClass)
54                 {
55                     demo.AddExample(new Example(type.FullName, type.Name));
56                 }
57             }
58             demo.SortAlphabetically(true);
59
60             demo.Initialize();
61
62             Window.Instance.GetDefaultLayer().Add(demo.mRootActor);
63         }
64
65         private void OnBackNaviTouchEvent(object source, View.TouchEventArgs e)
66         {
67             if (e.Touch.GetState(0) == PointStateType.Up)
68             {
69                 if (null != curExample)
70                 {
71                     ExitSample();
72                 }
73                 else
74                 {
75                     if (backNavigation != null)
76                     {
77                         Window.Instance.GetDefaultLayer().Remove(backNavigation);
78                         backNavigation.Dispose();
79                         backNavigation = null;
80                     }
81
82                     Exit();
83                 }
84             }
85
86             return;
87         }
88
89         private void RunSample(string name)
90         {
91             Assembly assembly = typeof(DaliDemo).Assembly;
92
93             Type exampleType = assembly?.GetType(name);
94             IExample example = assembly?.CreateInstance(name) as IExample;
95
96             curPageIndex = demo.GetCurrentPageIndex();
97
98             if (null != example)
99             {
100                 DeleteDaliDemo();
101
102                 example.Activate();
103             }
104
105             curExample = example;
106
107             backNavigation.Show();
108             backNavigation.RaiseToTop();
109         }
110
111         private int curPageIndex = -1;
112
113         private void ExitSample()
114         {
115             curExample?.Deactivate();
116             curExample = null;
117
118             FullGC();
119
120             CreateDaliDemo();
121
122             backNavigation.Hide();
123
124             if (0 <= curPageIndex)
125             {
126                 demo.ScrollTo((uint)curPageIndex);
127             }
128         }
129
130         protected override void OnCreate()
131         {
132             base.OnCreate();
133
134             CreateDaliDemo();
135
136             backNavigation = new Tizen.FH.NUI.Controls.Navigation("Back");
137             backNavigation.PositionUsesPivotPoint = true;
138             backNavigation.PivotPoint = PivotPoint.BottomLeft;
139             backNavigation.ParentOrigin = ParentOrigin.BottomLeft;
140             backNavigation.ItemTouchEvent += OnBackNaviTouchEvent;
141             backNavigation.Hide();
142
143             Tizen.FH.NUI.Controls.Navigation.NavigationItemData backItem = new Tizen.FH.NUI.Controls.Navigation.NavigationItemData("WhiteBackItem");
144             backNavigation.AddItem(backItem);
145
146             Window.Instance.GetDefaultLayer().Add(backNavigation);
147
148             Window.Instance.KeyEvent += Instance_KeyEvent;
149
150             int id = global::System.Threading.Thread.CurrentThread.ManagedThreadId;
151
152             timerForExit.Tick += TimerForExit_Tick;
153         }
154
155         private bool TimerForExit_Tick(object source, Timer.TickEventArgs e)
156         {
157             int id = global::System.Threading.Thread.CurrentThread.ManagedThreadId;
158             backNavigation?.Dispose();
159             backNavigation = null;
160
161             DeleteDaliDemo();
162             Exit();
163
164             timerForExit.Dispose();
165             return true;
166         }
167
168         private void Instance_KeyEvent(object sender, Window.KeyEventArgs e)
169         {
170             if (e.Key.State == Key.StateType.Up)
171             {
172                 if (e.Key.KeyPressedName == "Escape" || e.Key.KeyPressedName == "XF86Back" || e.Key.KeyPressedName == "BackSpace")
173                 {
174                     if (null != curExample)
175                     {
176                         ExitSample();
177                     }
178                     else
179                     {
180                         timerForExit.Start();
181                     }
182                 }
183                 else if (e.Key.KeyPressedName == "1")
184                 {
185                     FullGC();
186                 }
187             }
188         }
189
190         public void Deactivate()
191         {
192             demo = null;
193         }
194
195         private Tizen.NUI.Timer timerForExit = new Tizen.NUI.Timer(10);
196         private DaliTableView demo;
197         private Tizen.FH.NUI.Controls.Navigation backNavigation;
198     }
199 }