2 * Copyright(c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 using System.Collections.Generic;
22 using Tizen.NUI.Components;
23 using Tizen.NUI.BaseComponents;
24 using Tizen.NUI.Binding;
25 using System.Reflection;
27 namespace Tizen.NUI.StyleGuide
29 public class SearchField : View
31 public TextField SearchTextField;
32 public Button SearchButton;
33 public SearchField() : base()
35 Layout = new LinearLayout()
37 LinearOrientation = LinearLayout.Orientation.Horizontal,
38 LinearAlignment = LinearLayout.Alignment.CenterVertical,
39 CellPadding = new Size2D(40, 0),
42 BackgroundColor = Color.White;
44 WidthSpecification = LayoutParamPolicies.MatchParent;
45 HeightSpecification = LayoutParamPolicies.WrapContent;
46 Padding = new Extents(64, 64, 0, 0);
48 var searchTextBox = CreateSearchText();
49 SearchTextField = CreateTextField();
50 var underline = CreateUnderline();
52 searchTextBox.Add(SearchTextField);
53 searchTextBox.Add(underline);
55 SearchButton = CreateSearchButton();
61 private View CreateSearchText()
65 Layout = new LinearLayout()
67 LinearOrientation = LinearLayout.Orientation.Vertical,
68 CellPadding = new Size2D(0, 20),
70 WidthSpecification = LayoutParamPolicies.MatchParent,
71 HeightSpecification = LayoutParamPolicies.WrapContent,
75 private TextField CreateTextField()
77 return new TextField()
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.
87 private View CreateUnderline()
91 BackgroundColor = new Color("#0A0E4AFF"),
92 WidthSpecification = LayoutParamPolicies.MatchParent,
93 HeightSpecification = 2,
97 private Button CreateSearchButton()
102 WidthSpecification = 120,
103 HeightSpecification = LayoutParamPolicies.WrapContent,
108 public class ControlMenu
110 public ControlMenu(string name, string fullName = null)
116 public string Name { get; set; }
118 public string ViewLabel
126 public bool Selected { get; set; }
128 internal string FullName { get; set; }
131 public class ControlMenuViewModel
133 public List<Tuple<string, string>> NamePool = new List<Tuple<string, string>>();
135 public ControlMenuViewModel()
140 public List<ControlMenu> CreateData()
144 List<ControlMenu> result = new List<ControlMenu>();
145 foreach (var name in NamePool)
147 result.Add(new ControlMenu(name.Item1, name.Item2));
152 private void GetXamlPages()
154 Assembly assembly = this.GetType().Assembly;
155 Type exampleType = assembly.GetType("Tizen.NUI.StyleGuide.IExample");
157 foreach (Type type in assembly.GetTypes())
159 Console.WriteLine($"type.Name={type.Name}, type.FullName={type.FullName}");
160 if (exampleType.IsAssignableFrom(type) && type.Name != "SampleMain" && this.GetType() != type && type.IsClass)
162 NamePool.Add(new Tuple<string, string>(type.Name, type.FullName));
168 public class CustomNavigator : Navigator
170 // Customizes how to handle back navigation.
171 // base.OnBackNavigation() pops the peek page.
172 protected override void OnBackNavigation(BackNavigationEventArgs args)
176 // Deactivates the peek page example before page pop.
177 if (Peek() is IExample currentExample)
179 currentExample.Deactivate();
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);
189 class Program : NUIApplication
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;
199 public void OnSelectionChanged(object sender, SelectionChangedEventArgs ev)
201 Console.WriteLine($"OnSelectionChanged() {ev.CurrentSelection}");
203 if (ev.CurrentSelection.Count == 0) return;
205 if (ev.CurrentSelection[0] is ControlMenu selItem)
207 Console.WriteLine($"selItem.Name={selItem.Name}, selItem.FullName={selItem.FullName}");
208 RunSample(selItem?.FullName);
210 colView.SelectedItem = null;
213 protected override void OnCreate()
219 //enable FocusManger default algorithm
220 FocusManager.Instance.EnableDefaultAlgorithm(true);
222 private void Initialize()
224 window = GetDefaultWindow();
225 window.Title = "NUI Style Guide";
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.
230 var prevDefaultNavigator = window.GetDefaultNavigator();
231 window.Remove(prevDefaultNavigator);
232 prevDefaultNavigator.Dispose();
233 prevDefaultNavigator = null;
236 // Uses customized navigator to customize how to handle back navigation.
237 navigator = new CustomNavigator()
239 WidthSpecification = LayoutParamPolicies.MatchParent,
240 HeightSpecification = LayoutParamPolicies.MatchParent,
243 navigator.Popped += (object obj, PoppedEventArgs ev) =>
245 Page top = navigator.Peek();
248 // Sets the customized navigator as the default navigator of the window.
249 window.SetDefaultNavigator(navigator);
252 void OnSearchBtnClicked(object sender, ClickedEventArgs e)
254 var filteredSource = from filter in testSource
255 where filter.Name.ToLower().Contains(field.SearchTextField?.Text?.ToLower())
258 colView.Header = new DefaultTitleItem()
261 WidthSpecification = LayoutParamPolicies.MatchParent,
263 colView.ItemsSource = filteredSource;
266 private void SetMainPage()
268 var appBar = new AppBar()
270 Title = "NUI Style Guide",
271 AutoNavigationContent = false,
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;
280 var pageContent = new View()
282 Layout = new LinearLayout()
284 LinearOrientation = LinearLayout.Orientation.Vertical,
286 WidthSpecification = LayoutParamPolicies.MatchParent,
287 HeightSpecification = LayoutParamPolicies.MatchParent,
290 field = new SearchField()
292 WidthSpecification = LayoutParamPolicies.MatchParent,
294 field.SearchButton.Clicked += OnSearchBtnClicked;
296 testSource = new ControlMenuViewModel().CreateData();
297 selMode = ItemSelectionMode.SingleAlways;
298 var myTitle = new DefaultTitleItem()
301 WidthSpecification = LayoutParamPolicies.MatchParent,
304 colView = new CollectionView()
306 ItemsSource = testSource,
307 ItemsLayouter = new LinearLayouter(),
308 ItemTemplate = new DataTemplate(() =>
310 DefaultLinearItem item = new DefaultLinearItem()
312 WidthSpecification = LayoutParamPolicies.MatchParent,
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.
320 ScrollingDirection = ScrollableBase.Direction.Vertical,
321 WidthSpecification = LayoutParamPolicies.MatchParent,
322 HeightSpecification = LayoutParamPolicies.MatchParent,
323 SelectionMode = selMode,
325 colView.SelectionChanged += OnSelectionChanged;
327 pageContent.Add(field);
328 pageContent.Add(colView);
330 page = new ContentPage()
333 Content = pageContent,
336 navigator.Push(page);
339 private void RunSample(string name)
341 IExample example = typeof(Program).Assembly?.CreateInstance(name) as IExample;
344 Console.WriteLine($"typeof(Program).Assembly={typeof(Program).Assembly}, name={name}");
349 if (example is Page examplePage)
351 navigator.Push(examplePage);
356 Console.WriteLine($"examle is null!");
360 private void FullGC()
362 global::System.GC.Collect();
363 global::System.GC.WaitForPendingFinalizers();
364 global::System.GC.Collect();
367 static void Main(string[] args)
369 var app = new Program();