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