Release 9.0.0.16908
[platform/core/csapi/tizenfx.git] / test / NUITizenGallery / NUITizenGallery.cs
1 using System;
2 using System.Linq;
3 using System.Collections.Generic;
4 using Tizen.NUI;
5 using Tizen.NUI.Components;
6 using Tizen.NUI.BaseComponents;
7 using Tizen.NUI.Binding;
8 using System.Reflection;
9
10 namespace NUITizenGallery
11 {
12     public class SearchField : View
13     {
14         public TextField SearchTextField;
15         public Button SearchButton;
16         public SearchField() : base()
17         {
18             Layout = new LinearLayout()
19             {
20                 LinearOrientation = LinearLayout.Orientation.Horizontal,
21                 LinearAlignment = LinearLayout.Alignment.CenterVertical,
22                 CellPadding = new Size2D(40, 0),
23             };
24
25             BackgroundColor = Color.White;
26
27             WidthSpecification = LayoutParamPolicies.MatchParent;
28             HeightSpecification = LayoutParamPolicies.WrapContent;
29             Padding = new Extents(64, 64, 0, 0);
30
31             var searchTextBox = CreateSearchText();
32             SearchTextField = CreateTextField();
33             var underline = CreateUnderline();
34
35             searchTextBox.Add(SearchTextField);
36             searchTextBox.Add(underline);
37
38             SearchButton = CreateSearchButton();
39
40             Add(searchTextBox);
41             Add(SearchButton);
42         }
43
44         private View CreateSearchText()
45         {
46             return new View()
47             {
48                 Layout = new LinearLayout()
49                 {
50                     LinearOrientation = LinearLayout.Orientation.Vertical,
51                     CellPadding = new Size2D(0, 20),
52                 },
53                 WidthSpecification = LayoutParamPolicies.MatchParent,
54                 HeightSpecification = LayoutParamPolicies.WrapContent,
55             };
56         }
57
58         private TextField CreateTextField()
59         {
60             return new TextField()
61             {
62                 PlaceholderText = "Search",
63                 WidthSpecification = LayoutParamPolicies.MatchParent,
64                 HeightSpecification = LayoutParamPolicies.WrapContent,
65                 MinimumSize = new Size2D(0, 40),
66             };
67         }
68
69         private View CreateUnderline()
70         {
71             return new View()
72             {
73                 BackgroundColor = new Color("#0A0E4AFF"),
74                 WidthSpecification = LayoutParamPolicies.MatchParent,
75                 HeightSpecification = 2,
76             };
77         }
78
79         private Button CreateSearchButton()
80         {
81             return new Button()
82             {
83                 Text = "Run",
84                 WidthSpecification = 120,
85                 HeightSpecification = LayoutParamPolicies.WrapContent,
86             };
87         }
88     }
89
90     public class Gallery
91     {
92         public Gallery(string name, string fullName = null)
93         {
94             Name = name;
95             FullName = fullName;
96         }
97
98         public string Name { get; set; }
99
100         public string ViewLabel
101         {
102             get
103             {
104                 return Name;
105             }
106         }
107
108         public bool Selected { get; set; }
109
110         internal string FullName { get; set; }
111     }
112
113     public class GalleryViewModel
114     {
115         public List<Tuple<string, string>> NamePool = new List<Tuple<string, string>>();
116
117         public GalleryViewModel()
118         {
119             //CreateData();
120         }
121
122         public List<Gallery> CreateData()
123         {
124             GetXamlPages();
125
126             List<Gallery> result = new List<Gallery>();
127             foreach (var name in NamePool)
128             {
129                 result.Add(new Gallery(name.Item1, name.Item2));
130             }
131             return result;
132         }
133
134         private void GetXamlPages()
135         {
136             Assembly assembly = this.GetType().Assembly;
137             Type exampleType = assembly.GetType("NUITizenGallery.IExample");
138
139             foreach (Type type in assembly.GetTypes())
140             {
141                 Console.WriteLine($"@@@ type.Name={type.Name}, type.FullName={type.FullName}");
142                 if (exampleType.IsAssignableFrom(type) && type.Name != "SampleMain" && this.GetType() != type && type.IsClass)
143                 {
144                     NamePool.Add(new Tuple<string, string>(type.Name, type.FullName));
145                 }
146             }
147         }
148     }
149
150     class Program : NUIApplication
151     {
152         private Window window;
153         private Navigator navigator;
154         private CollectionView colView;
155         private ItemSelectionMode selMode;
156         private IExample currentExample = null;
157         private ContentPage page;
158         private SearchField field;
159         private List<Gallery> testSource;
160
161         public void OnKeyEvent(object sender, Window.KeyEventArgs e)
162         {
163             if (e.Key.State == Key.StateType.Up)
164             {
165                 if (e.Key.KeyPressedName == "Escape" || e.Key.KeyPressedName == "XF86Back" || e.Key.KeyPressedName == "BackSpace")
166                 {
167                     if (null != currentExample)
168                     {
169                         ExitSample();
170                     }
171                     else
172                     {
173                         Exit();
174                     }
175                 }
176             }
177         }
178
179         public void OnSelectionChanged(object sender, SelectionChangedEventArgs ev)
180         {
181             Console.WriteLine($"@@@ OnSelectionChanged() {ev.CurrentSelection}");
182             
183             foreach (object item in ev.CurrentSelection)
184             {
185                 if (item == null)
186                 {
187                     break;
188                 }
189
190                 var selItem = item as Gallery;
191                 Console.WriteLine($"@@@ selItem.Name={selItem.Name}, selItem.FullName={selItem.FullName}");
192                 RunSample(selItem?.FullName);
193                 colView.SelectedItem = null;
194             }
195
196             /* Use the following code when it is actually required.
197             foreach (object item in ev.PreviousSelection)
198             {
199                 if (item == null)
200                 {
201                     break;
202                 }
203
204                 var unselItem = item as Gallery;
205             }
206
207             foreach (object item in ev.CurrentSelection)
208             {
209                 if (item == null)
210                 {
211                     break;
212                 }
213
214                 var selItem = item as Gallery;
215             }
216             */
217         }
218
219         protected override void OnCreate()
220         {
221             base.OnCreate();
222             Initialize();
223             SetMainPage();
224         }
225         private void Initialize()
226         {
227             window = GetDefaultWindow();
228             window.Title = "NUITizenGallery";
229             window.KeyEvent += OnKeyEvent;
230
231             navigator = window.GetDefaultNavigator();
232         }
233
234         void OnSearchBtnClicked(object sender, ClickedEventArgs e)
235         {
236             var filteredSource = from filter in testSource
237                                  where filter.Name.ToLower().Contains(field.SearchTextField?.Text?.ToLower())
238                                  select filter;
239
240             colView.Header = new DefaultTitleItem()
241             {
242                 Text = "result",
243                 WidthSpecification = LayoutParamPolicies.MatchParent,
244             };
245             colView.ItemsSource = filteredSource;
246         }
247
248         private void SetMainPage()
249         {
250             var appBar = new AppBar()
251             {
252                 Title = "NUI Tizen Gallery",
253                 AutoNavigationContent = false,
254             };
255
256             var appBarStyle = ThemeManager.GetStyle("Tizen.NUI.Components.AppBar");
257             var moreButton = new Button(((AppBarStyle)appBarStyle).BackButton);
258             moreButton.Icon.ResourceUrl = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "menu.png";
259             appBar.NavigationContent = moreButton;
260
261
262             var pageContent = new View()
263             {
264                 Layout = new LinearLayout()
265                 {
266                     LinearOrientation = LinearLayout.Orientation.Vertical,
267                 },
268                 WidthSpecification = LayoutParamPolicies.MatchParent,
269                 HeightSpecification = LayoutParamPolicies.MatchParent,
270             };
271
272             field = new SearchField()
273             {
274                 WidthSpecification = LayoutParamPolicies.MatchParent,
275             };
276             field.SearchButton.Clicked += OnSearchBtnClicked;
277
278             testSource = new GalleryViewModel().CreateData();
279             selMode = ItemSelectionMode.SingleAlways;
280             var myTitle = new DefaultTitleItem()
281             {
282                 Text = "TestCase",
283                 WidthSpecification = LayoutParamPolicies.MatchParent,
284             };
285
286             colView = new CollectionView()
287             {
288                 ItemsSource = testSource,
289                 ItemsLayouter = new LinearLayouter(),
290                 ItemTemplate = new DataTemplate(() =>
291                 {
292                     DefaultLinearItem item = new DefaultLinearItem()
293                     {
294                         WidthSpecification = LayoutParamPolicies.MatchParent,
295                     };
296                     item.Label.SetBinding(TextLabel.TextProperty, "ViewLabel");
297                     item.Label.HorizontalAlignment = HorizontalAlignment.Begin;
298                     return item;
299                 }),
300                 Header = myTitle,
301                 ScrollingDirection = ScrollableBase.Direction.Vertical,
302                 WidthSpecification = LayoutParamPolicies.MatchParent,
303                 HeightSpecification = LayoutParamPolicies.MatchParent,
304                 SelectionMode = selMode,
305             };
306             colView.SelectionChanged += OnSelectionChanged;
307
308             pageContent.Add(field);
309             pageContent.Add(colView);
310
311             page = new ContentPage()
312             {
313                 AppBar = appBar,
314                 Content = pageContent,
315             };
316             navigator.Push(page);
317         }
318
319         private void RunSample(string name)
320         {
321             IExample example = typeof(Program).Assembly?.CreateInstance(name) as IExample;
322
323             Console.WriteLine($"@@@ typeof(Program).Assembly={typeof(Program).Assembly}, name={name}");
324
325             if (example != null)
326             {
327                 example.Activate();
328             }
329             else
330             {
331                 Console.WriteLine($"@@@ examle is null!");
332             }
333             currentExample = example;
334         }
335
336         private void ExitSample()
337         {
338             currentExample?.Deactivate();
339             currentExample = null;
340
341             FullGC();
342         }
343
344         private void FullGC()
345         {
346             global::System.GC.Collect();
347             global::System.GC.WaitForPendingFinalizers();
348             global::System.GC.Collect();
349         }
350
351         static void Main(string[] args)
352         {
353             var app = new Program();
354             app.Run(args);
355         }
356     }
357 }