Release 12.0.0.18253
[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     public class CustomNavigator : Navigator
151     {
152         // Customizes how to handle back navigation.
153         // base.OnBackNavigation() pops the peek page.
154         protected override void OnBackNavigation(BackNavigationEventArgs args)
155         {
156             if (PageCount > 1)
157             {
158                 // Deactivates the peek page example before page pop.
159                 if (Peek() is IExample currentExample)
160                 {
161                     currentExample.Deactivate();
162                 }
163             }
164
165             // Pops the peek page if navigator has more than one page.
166             // If navigator has only one page, then the program is exited.
167             base.OnBackNavigation(args);
168         }
169     }
170
171     class Program : NUIApplication
172     {
173         private Window window;
174         private AppBar appBar;
175         private View pageContent;
176         private Navigator navigator;
177         private CollectionView colView;
178         private ItemSelectionMode selMode;
179         private IExample currentExample = null;
180         private ContentPage page;
181         private SearchField field;
182         private List<Gallery> testSource;
183
184         public void OnSelectionChanged(object sender, SelectionChangedEventArgs ev)
185         {
186             Console.WriteLine($"@@@ OnSelectionChanged() {ev.CurrentSelection}");
187             
188             foreach (object item in ev.CurrentSelection)
189             {
190                 if (item == null)
191                 {
192                     break;
193                 }
194
195                 var selItem = item as Gallery;
196                 Console.WriteLine($"@@@ selItem.Name={selItem.Name}, selItem.FullName={selItem.FullName}");
197                 RunSample(selItem?.FullName);
198                 colView.SelectedItem = null;
199             }
200
201             /* Use the following code when it is actually required.
202             foreach (object item in ev.PreviousSelection)
203             {
204                 if (item == null)
205                 {
206                     break;
207                 }
208
209                 var unselItem = item as Gallery;
210             }
211
212             foreach (object item in ev.CurrentSelection)
213             {
214                 if (item == null)
215                 {
216                     break;
217                 }
218
219                 var selItem = item as Gallery;
220             }
221             */
222         }
223
224         protected override void OnCreate()
225         {
226             base.OnCreate();
227             Initialize();
228             SetMainPage();
229             pageContent.SizeHeight = Window.Instance.WindowSize.Height - appBar.SizeHeight;
230         }
231         private void Initialize()
232         {
233             window = GetDefaultWindow();
234             window.Title = "NUITizenGallery";
235
236             // In this example, GetDefaultNavigator() has not been called before so default navigator has not been set yet.
237             // Therefore, the following codes for unsetting and disposing the previous default navigator are not required in this example.
238             /*
239             var prevDefaultNavigator = window.GetDefaultNavigator();
240             window.Remove(prevDefaultNavigator);
241             prevDefaultNavigator.Dispose();
242             prevDefaultNavigator = null;
243             */
244
245             // Uses customized navigator to customize how to handle back navigation.
246             navigator = new CustomNavigator()
247             {
248                 WidthSpecification = LayoutParamPolicies.MatchParent,
249                 HeightSpecification = LayoutParamPolicies.MatchParent,
250             };
251
252             // Sets the customized navigator as the default navigator of the window.
253             window.SetDefaultNavigator(navigator);
254         }
255
256         void OnSearchBtnClicked(object sender, ClickedEventArgs e)
257         {
258             var filteredSource = from filter in testSource
259                                  where filter.Name.ToLower().Contains(field.SearchTextField?.Text?.ToLower())
260                                  select filter;
261
262             colView.Header = new DefaultTitleItem()
263             {
264                 Text = "result",
265                 WidthSpecification = LayoutParamPolicies.MatchParent,
266             };
267             colView.ItemsSource = filteredSource;
268         }
269
270         private void SetMainPage()
271         {
272             appBar = new AppBar()
273             {
274                 Title = "NUI Tizen Gallery",
275                 AutoNavigationContent = false,
276             };
277
278             var appBarStyle = ThemeManager.GetStyle("Tizen.NUI.Components.AppBar");
279             var moreButton = new Button(((AppBarStyle)appBarStyle).BackButton);
280             moreButton.Icon.ResourceUrl = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "menu.png";
281             appBar.NavigationContent = moreButton;
282
283
284             pageContent = new View()
285             {
286                 Layout = new LinearLayout()
287                 {
288                     LinearOrientation = LinearLayout.Orientation.Vertical,
289                 },
290                 WidthSpecification = LayoutParamPolicies.MatchParent,
291                 HeightSpecification = LayoutParamPolicies.MatchParent,
292             };
293
294             field = new SearchField()
295             {
296                 WidthSpecification = LayoutParamPolicies.MatchParent,
297             };
298             field.SearchButton.Clicked += OnSearchBtnClicked;
299
300             testSource = new GalleryViewModel().CreateData();
301             selMode = ItemSelectionMode.SingleAlways;
302             var myTitle = new DefaultTitleItem()
303             {
304                 Text = "TestCase",
305                 WidthSpecification = LayoutParamPolicies.MatchParent,
306             };
307
308             colView = new CollectionView()
309             {
310                 ItemsSource = testSource,
311                 ItemsLayouter = new LinearLayouter(),
312                 ItemTemplate = new DataTemplate(() =>
313                 {
314                     DefaultLinearItem item = new DefaultLinearItem()
315                     {
316                         WidthSpecification = LayoutParamPolicies.MatchParent,
317                     };
318                     item.Label.SetBinding(TextLabel.TextProperty, "ViewLabel");
319                     item.Label.HorizontalAlignment = HorizontalAlignment.Begin;
320                     return item;
321                 }),
322                 Header = myTitle,
323                 ScrollingDirection = ScrollableBase.Direction.Vertical,
324                 HideScrollbar = false,
325                 WidthSpecification = LayoutParamPolicies.MatchParent,
326                 HeightSpecification = LayoutParamPolicies.MatchParent,
327                 SelectionMode = selMode,
328             };
329             colView.SelectionChanged += OnSelectionChanged;
330
331             pageContent.Add(field);
332             pageContent.Add(colView);
333
334             page = new ContentPage()
335             {
336                 AppBar = appBar,
337                 Content = pageContent,
338             };
339             navigator.Push(page);
340         }
341
342         private void RunSample(string name)
343         {
344             IExample example = typeof(Program).Assembly?.CreateInstance(name) as IExample;
345
346             Console.WriteLine($"@@@ typeof(Program).Assembly={typeof(Program).Assembly}, name={name}");
347
348             if (example != null)
349             {
350                 example.Activate();
351             }
352             else
353             {
354                 Console.WriteLine($"@@@ examle is null!");
355             }
356             currentExample = example;
357         }
358
359         private void ExitSample()
360         {
361             currentExample?.Deactivate();
362             currentExample = null;
363
364             FullGC();
365         }
366
367         private void FullGC()
368         {
369             global::System.GC.Collect();
370             global::System.GC.WaitForPendingFinalizers();
371             global::System.GC.Collect();
372         }
373
374         static void Main(string[] args)
375         {
376             string[] emptyArgs = new string[0];
377             foreach(string arg in args)
378             {
379                 Console.WriteLine(arg);
380                 if (arg.ToString() == "--implicit-scaling-factor")
381                 {
382                     Console.WriteLine("NOTE: Scaling Factor is implicitly declaired as 1.5");
383                     System.Environment.SetEnvironmentVariable("NUI_SCALING_FACTOR","1.5");
384                 }
385             }
386
387             var app = new Program();
388             app.Run(args);
389         }
390     }
391 }