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 class Program : NUIApplication
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;
179 public void OnKeyEvent(object sender, Window.KeyEventArgs e)
181 // FIXME:: Navigator should provide Back/Escape event processing.
182 if (e.Key.State == Key.StateType.Up)
184 Log.Info("StyleGuide", $"[{e.Key.KeyPressedName}] is pressed!\n");
185 if (e.Key.KeyPressedName == "Escape" || e.Key.KeyPressedName == "XF86Back" || e.Key.KeyPressedName == "BackSpace")
187 if (navigator == null) return;
191 if (navigator.PageCount == 0)
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 focusManager = FocusManager.Instance;
221 //set user customized focus indicator
222 if (!focusManager.FocusIndicator)
224 focusManager.FocusIndicator = new View()
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),
237 //enable FocusManger default algorithm
238 focusManager.EnableDefaultAlgorithm(true);
240 private void Initialize()
242 window = GetDefaultWindow();
243 window.Title = "NUI Style Guide";
244 window.KeyEvent += OnKeyEvent;
246 navigator = window.GetDefaultNavigator();
247 navigator.Popped += (object obj, PoppedEventArgs ev) =>
249 Page top = navigator.Peek();
253 void OnSearchBtnClicked(object sender, ClickedEventArgs e)
255 var filteredSource = from filter in testSource
256 where filter.Name.ToLower().Contains(field.SearchTextField?.Text?.ToLower())
259 colView.Header = new DefaultTitleItem()
262 WidthSpecification = LayoutParamPolicies.MatchParent,
264 colView.ItemsSource = filteredSource;
267 private void SetMainPage()
269 var appBar = new AppBar()
271 Title = "NUI Style Guide",
272 AutoNavigationContent = false,
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;
281 var pageContent = new View()
283 Layout = new LinearLayout()
285 LinearOrientation = LinearLayout.Orientation.Vertical,
287 WidthSpecification = LayoutParamPolicies.MatchParent,
288 HeightSpecification = LayoutParamPolicies.MatchParent,
291 field = new SearchField()
293 WidthSpecification = LayoutParamPolicies.MatchParent,
295 field.SearchButton.Clicked += OnSearchBtnClicked;
297 testSource = new ControlMenuViewModel().CreateData();
298 selMode = ItemSelectionMode.SingleAlways;
299 var myTitle = new DefaultTitleItem()
302 WidthSpecification = LayoutParamPolicies.MatchParent,
305 colView = new CollectionView()
307 ItemsSource = testSource,
308 ItemsLayouter = new LinearLayouter(),
309 ItemTemplate = new DataTemplate(() =>
311 DefaultLinearItem item = new DefaultLinearItem()
313 WidthSpecification = LayoutParamPolicies.MatchParent,
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.
321 ScrollingDirection = ScrollableBase.Direction.Vertical,
322 WidthSpecification = LayoutParamPolicies.MatchParent,
323 HeightSpecification = LayoutParamPolicies.MatchParent,
324 SelectionMode = selMode,
326 colView.SelectionChanged += OnSelectionChanged;
328 pageContent.Add(field);
329 pageContent.Add(colView);
331 page = new ContentPage()
334 Content = pageContent,
337 navigator.Push(page);
340 private void RunSample(string name)
342 IExample example = typeof(Program).Assembly?.CreateInstance(name) as IExample;
345 Console.WriteLine($"typeof(Program).Assembly={typeof(Program).Assembly}, name={name}");
350 if (example is Page examplePage)
352 navigator.Push(examplePage);
357 Console.WriteLine($"examle is null!");
361 private void ExitSample()
363 if (navigator.Peek() is IExample currentExample)
365 currentExample.Deactivate();
372 private void FullGC()
374 global::System.GC.Collect();
375 global::System.GC.WaitForPendingFinalizers();
376 global::System.GC.Collect();
379 static void Main(string[] args)
381 var app = new Program();