[NUI] Update StyleGuide README
[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     class Program : NUIApplication
169     {
170         private Window window;
171         private Navigator navigator;
172         private CollectionView colView;
173         private ItemSelectionMode selMode;
174         private ContentPage page;
175         private SearchField field;
176         private List<ControlMenu> testSource;
177         private FocusManager focusManager;
178
179         public void OnKeyEvent(object sender, Window.KeyEventArgs e)
180         {
181             // FIXME:: Navigator should provide Back/Escape event processing.
182             if (e.Key.State == Key.StateType.Up)
183             {
184                 Log.Info("StyleGuide", $"[{e.Key.KeyPressedName}] is pressed!\n");
185                 if (e.Key.KeyPressedName == "Escape" || e.Key.KeyPressedName == "XF86Back" || e.Key.KeyPressedName == "BackSpace")
186                 {
187                     if (navigator == null) return;
188
189                     ExitSample();
190
191                     if (navigator.PageCount == 0)
192                     {
193                         Exit();
194                     }
195                 }
196             }
197         }
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             focusManager = FocusManager.Instance;
220
221             //set user customized focus indicator
222             if (!focusManager.FocusIndicator)
223             {
224                 focusManager.FocusIndicator = new View()
225                 {
226                     PositionUsesPivotPoint = true,
227                     PivotPoint = new Position(0, 0, 0),
228                     WidthResizePolicy = ResizePolicyType.FillToParent,
229                     HeightResizePolicy = ResizePolicyType.FillToParent,
230                     BorderlineColor = Color.Orange,
231                     BorderlineWidth = 4.0f,
232                     BorderlineOffset = -1f,
233                     BackgroundColor = new Color(0.2f, 0.2f, 0.2f, 0.2f),
234                 };
235             }
236
237             //enable FocusManger default algorithm
238             focusManager.EnableDefaultAlgorithm(true);
239         }
240         private void Initialize()
241         {
242             window = GetDefaultWindow();
243             window.Title = "NUI Style Guide";
244             window.KeyEvent += OnKeyEvent;
245
246             navigator = window.GetDefaultNavigator();
247             navigator.Popped += (object obj, PoppedEventArgs ev) =>
248             {
249                 Page top = navigator.Peek();
250             };
251         }
252
253         void OnSearchBtnClicked(object sender, ClickedEventArgs e)
254         {
255             var filteredSource = from filter in testSource
256                                  where filter.Name.ToLower().Contains(field.SearchTextField?.Text?.ToLower())
257                                  select filter;
258
259             colView.Header = new DefaultTitleItem()
260             {
261                 Text = "result",
262                 WidthSpecification = LayoutParamPolicies.MatchParent,
263             };
264             colView.ItemsSource = filteredSource;
265         }
266
267         private void SetMainPage()
268         {
269             var appBar = new AppBar()
270             {
271                 Title = "NUI Style Guide",
272                 AutoNavigationContent = false,
273             };
274
275             var appBarStyle = ThemeManager.GetStyle("Tizen.NUI.Components.AppBar");
276             var moreButton = new Button(((AppBarStyle)appBarStyle).BackButton);
277             moreButton.Icon.ResourceUrl = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "menu.png";
278             appBar.NavigationContent = moreButton;
279
280
281             var pageContent = new View()
282             {
283                 Layout = new LinearLayout()
284                 {
285                     LinearOrientation = LinearLayout.Orientation.Vertical,
286                 },
287                 WidthSpecification = LayoutParamPolicies.MatchParent,
288                 HeightSpecification = LayoutParamPolicies.MatchParent,
289             };
290
291             field = new SearchField()
292             {
293                 WidthSpecification = LayoutParamPolicies.MatchParent,
294             };
295             field.SearchButton.Clicked += OnSearchBtnClicked;
296
297             testSource = new ControlMenuViewModel().CreateData();
298             selMode = ItemSelectionMode.SingleAlways;
299             var myTitle = new DefaultTitleItem()
300             {
301                 Text = "TestCase",
302                 WidthSpecification = LayoutParamPolicies.MatchParent,
303             };
304
305             colView = new CollectionView()
306             {
307                 ItemsSource = testSource,
308                 ItemsLayouter = new LinearLayouter(),
309                 ItemTemplate = new DataTemplate(() =>
310                 {
311                     DefaultLinearItem item = new DefaultLinearItem()
312                     {
313                         WidthSpecification = LayoutParamPolicies.MatchParent,
314                     };
315                     item.Label.SetBinding(TextLabel.TextProperty, "ViewLabel");
316                     item.Label.HorizontalAlignment = HorizontalAlignment.Begin;
317                     item.Focusable = true; //BaseComponents' Focusable is false as a default value, true should be set to navigate key focus.
318                     return item;
319                 }),
320                 Header = myTitle,
321                 ScrollingDirection = ScrollableBase.Direction.Vertical,
322                 WidthSpecification = LayoutParamPolicies.MatchParent,
323                 HeightSpecification = LayoutParamPolicies.MatchParent,
324                 SelectionMode = selMode,
325             };
326             colView.SelectionChanged += OnSelectionChanged;
327
328             pageContent.Add(field);
329             pageContent.Add(colView);
330
331             page = new ContentPage()
332             {
333                 AppBar = appBar,
334                 Content = pageContent,
335             };
336
337             navigator.Push(page);
338         }
339
340         private void RunSample(string name)
341         {
342             IExample example = typeof(Program).Assembly?.CreateInstance(name) as IExample;
343
344
345             Console.WriteLine($"typeof(Program).Assembly={typeof(Program).Assembly}, name={name}");
346
347             if (example != null)
348             {
349                 example.Activate();
350                 if (example is Page examplePage)
351                 {
352                     navigator.Push(examplePage);
353                 }
354             }
355             else
356             {
357                 Console.WriteLine($"examle is null!");
358             }
359         }
360
361         private void ExitSample()
362         {
363             if (navigator.Peek() is IExample currentExample)
364             {
365                 currentExample.Deactivate();
366             }
367
368             navigator.Pop();
369             // FullGC();
370         }
371
372         private void FullGC()
373         {
374             global::System.GC.Collect();
375             global::System.GC.WaitForPendingFinalizers();
376             global::System.GC.Collect();
377         }
378
379         static void Main(string[] args)
380         {
381             var app = new Program();
382             app.Run(args);
383         }
384     }
385 }