[NUI] Skip text getter when Text never setted before
[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                 Focusable = true, //BaseComponents' Focusable is false as a default value, true should be set to navigate key focus and edit text.
84             };
85         }
86
87         private View CreateUnderline()
88         {
89             return new View()
90             {
91                 BackgroundColor = new Color("#0A0E4AFF"),
92                 WidthSpecification = LayoutParamPolicies.MatchParent,
93                 HeightSpecification = 2,
94             };
95         }
96
97         private Button CreateSearchButton()
98         {
99             return new Button()
100             {
101                 Text = "Run",
102                 WidthSpecification = 120,
103                 HeightSpecification = LayoutParamPolicies.WrapContent,
104             };
105         }
106     }
107
108     public class ControlMenu
109     {
110         public ControlMenu(string name, string fullName = null)
111         {
112             Name = name;
113             FullName = fullName;
114         }
115
116         public string Name { get; set; }
117
118         public string ViewLabel
119         {
120             get
121             {
122                 return Name;
123             }
124         }
125
126         public bool Selected { get; set; }
127
128         internal string FullName { get; set; }
129     }
130
131     public class ControlMenuViewModel
132     {
133         public List<Tuple<string, string>> NamePool = new List<Tuple<string, string>>();
134
135         public ControlMenuViewModel()
136         {
137             //CreateData();
138         }
139
140         public List<ControlMenu> CreateData()
141         {
142             GetXamlPages();
143
144             List<ControlMenu> result = new List<ControlMenu>();
145             foreach (var name in NamePool)
146             {
147                 result.Add(new ControlMenu(name.Item1, name.Item2));
148             }
149             return result;
150         }
151
152         private void GetXamlPages()
153         {
154             Assembly assembly = this.GetType().Assembly;
155             Type exampleType = assembly.GetType("Tizen.NUI.StyleGuide.IExample");
156
157             foreach (Type type in assembly.GetTypes())
158             {
159                 Console.WriteLine($"type.Name={type.Name}, type.FullName={type.FullName}");
160                 if (exampleType.IsAssignableFrom(type) && type.Name != "SampleMain" && this.GetType() != type && type.IsClass)
161                 {
162                     NamePool.Add(new Tuple<string, string>(type.Name, type.FullName));
163                 }
164             }
165         }
166     }
167
168     public class CustomNavigator : Navigator
169     {
170         // Customizes how to handle back navigation.
171         // base.OnBackNavigation() pops the peek page.
172         protected override void OnBackNavigation(BackNavigationEventArgs args)
173         {
174             if (PageCount > 1)
175             {
176                 // Deactivates the peek page example before page pop.
177                 if (Peek() is IExample currentExample)
178                 {
179                     currentExample.Deactivate();
180                 }
181             }
182
183             // Pops the peek page if navigator has more than one page.
184             // If navigator has only one page, then the program is exited.
185             base.OnBackNavigation(args);
186         }
187     }
188
189     class Program : NUIApplication
190     {
191         private Window window;
192         private Navigator navigator;
193         private CollectionView colView;
194         private ItemSelectionMode selMode;
195         private ContentPage page;
196         private SearchField field;
197         private List<ControlMenu> testSource;
198
199         public void OnSelectionChanged(object sender, SelectionChangedEventArgs ev)
200         {
201             Console.WriteLine($"OnSelectionChanged() {ev.CurrentSelection}");
202
203             if (ev.CurrentSelection.Count == 0) return;
204
205             if (ev.CurrentSelection[0] is ControlMenu selItem)
206             {
207                 Console.WriteLine($"selItem.Name={selItem.Name}, selItem.FullName={selItem.FullName}");
208                 RunSample(selItem?.FullName);
209             }
210             colView.SelectedItem = null;
211         }
212
213         protected override void OnCreate()
214         {
215             base.OnCreate();
216             Initialize();
217             SetMainPage();
218
219             //enable FocusManger default algorithm
220             FocusManager.Instance.EnableDefaultAlgorithm(true);
221         }
222         private void Initialize()
223         {
224             window = GetDefaultWindow();
225             window.Title = "NUI Style Guide";
226
227             // In this example, GetDefaultNavigator() has not been called before so default navigator has not been set yet.
228             // Therefore, the following codes for unsetting and disposing the previous default navigator are not required in this example.
229             /*
230             var prevDefaultNavigator = window.GetDefaultNavigator();
231             window.Remove(prevDefaultNavigator);
232             prevDefaultNavigator.Dispose();
233             prevDefaultNavigator = null;
234             */
235
236             // Uses customized navigator to customize how to handle back navigation.
237             navigator = new CustomNavigator()
238             {
239                 WidthSpecification = LayoutParamPolicies.MatchParent,
240                 HeightSpecification = LayoutParamPolicies.MatchParent,
241             };
242
243             navigator.Popped += (object obj, PoppedEventArgs ev) =>
244             {
245                 Page top = navigator.Peek();
246             };
247
248             // Sets the customized navigator as the default navigator of the window.
249             window.SetDefaultNavigator(navigator);
250         }
251
252         void OnSearchBtnClicked(object sender, ClickedEventArgs e)
253         {
254             var filteredSource = from filter in testSource
255                                  where filter.Name.ToLower().Contains(field.SearchTextField?.Text?.ToLower())
256                                  select filter;
257
258             colView.Header = new DefaultTitleItem()
259             {
260                 Text = "result",
261                 WidthSpecification = LayoutParamPolicies.MatchParent,
262             };
263             colView.ItemsSource = filteredSource;
264         }
265
266         private void SetMainPage()
267         {
268             var appBar = new AppBar()
269             {
270                 Title = "NUI Style Guide",
271                 AutoNavigationContent = false,
272             };
273
274             var appBarStyle = ThemeManager.GetStyle("Tizen.NUI.Components.AppBar");
275             var moreButton = new Button(((AppBarStyle)appBarStyle).BackButton);
276             moreButton.Icon.ResourceUrl = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "menu.png";
277             appBar.NavigationContent = moreButton;
278
279
280             var pageContent = new View()
281             {
282                 Layout = new LinearLayout()
283                 {
284                     LinearOrientation = LinearLayout.Orientation.Vertical,
285                 },
286                 WidthSpecification = LayoutParamPolicies.MatchParent,
287                 HeightSpecification = LayoutParamPolicies.MatchParent,
288             };
289
290             field = new SearchField()
291             {
292                 WidthSpecification = LayoutParamPolicies.MatchParent,
293             };
294             field.SearchButton.Clicked += OnSearchBtnClicked;
295
296             testSource = new ControlMenuViewModel().CreateData();
297             selMode = ItemSelectionMode.SingleAlways;
298             var myTitle = new DefaultTitleItem()
299             {
300                 Text = "TestCase",
301                 WidthSpecification = LayoutParamPolicies.MatchParent,
302             };
303
304             colView = new CollectionView()
305             {
306                 ItemsSource = testSource,
307                 ItemsLayouter = new LinearLayouter(),
308                 ItemTemplate = new DataTemplate(() =>
309                 {
310                     DefaultLinearItem item = new DefaultLinearItem()
311                     {
312                         WidthSpecification = LayoutParamPolicies.MatchParent,
313                     };
314                     item.Label.SetBinding(TextLabel.TextProperty, "ViewLabel");
315                     item.Label.HorizontalAlignment = HorizontalAlignment.Begin;
316                     item.Focusable = true; //BaseComponents' Focusable is false as a default value, true should be set to navigate key focus.
317                     return item;
318                 }),
319                 Header = myTitle,
320                 ScrollingDirection = ScrollableBase.Direction.Vertical,
321                 WidthSpecification = LayoutParamPolicies.MatchParent,
322                 HeightSpecification = LayoutParamPolicies.MatchParent,
323                 SelectionMode = selMode,
324             };
325             colView.SelectionChanged += OnSelectionChanged;
326
327             pageContent.Add(field);
328             pageContent.Add(colView);
329
330             page = new ContentPage()
331             {
332                 AppBar = appBar,
333                 Content = pageContent,
334             };
335
336             navigator.Push(page);
337         }
338
339         private void RunSample(string name)
340         {
341             IExample example = typeof(Program).Assembly?.CreateInstance(name) as IExample;
342
343
344             Console.WriteLine($"typeof(Program).Assembly={typeof(Program).Assembly}, name={name}");
345
346             if (example != null)
347             {
348                 example.Activate();
349                 if (example is Page examplePage)
350                 {
351                     navigator.Push(examplePage);
352                 }
353             }
354             else
355             {
356                 Console.WriteLine($"examle is null!");
357             }
358         }
359
360         private void FullGC()
361         {
362             global::System.GC.Collect();
363             global::System.GC.WaitForPendingFinalizers();
364             global::System.GC.Collect();
365         }
366
367         static void Main(string[] args)
368         {
369             var app = new Program();
370             app.Run(args);
371         }
372     }
373 }