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),
86 private View CreateUnderline()
90 BackgroundColor = new Color("#0A0E4AFF"),
91 WidthSpecification = LayoutParamPolicies.MatchParent,
92 HeightSpecification = 2,
96 private Button CreateSearchButton()
101 WidthSpecification = 120,
102 HeightSpecification = LayoutParamPolicies.WrapContent,
107 public class ControlMenu
109 public ControlMenu(string name, string fullName = null)
115 public string Name { get; set; }
117 public string ViewLabel
125 public bool Selected { get; set; }
127 internal string FullName { get; set; }
130 public class ControlMenuViewModel
132 public List<Tuple<string, string>> NamePool = new List<Tuple<string, string>>();
134 public ControlMenuViewModel()
139 public List<ControlMenu> CreateData()
143 List<ControlMenu> result = new List<ControlMenu>();
144 foreach (var name in NamePool)
146 result.Add(new ControlMenu(name.Item1, name.Item2));
151 private void GetXamlPages()
153 Assembly assembly = this.GetType().Assembly;
154 Type exampleType = assembly.GetType("Tizen.NUI.StyleGuide.IExample");
156 foreach (Type type in assembly.GetTypes())
158 Console.WriteLine($"@@@ type.Name={type.Name}, type.FullName={type.FullName}");
159 if (exampleType.IsAssignableFrom(type) && type.Name != "SampleMain" && this.GetType() != type && type.IsClass)
161 NamePool.Add(new Tuple<string, string>(type.Name, type.FullName));
167 class Program : NUIApplication
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;
177 public void OnKeyEvent(object sender, Window.KeyEventArgs e)
179 // FIXME:: Navigator should provide Back/Escape event processing.
180 if (e.Key.State == Key.StateType.Up)
182 Log.Info("StyleGuide", $"[{e.Key.KeyPressedName}] is pressed!\n");
183 if (e.Key.KeyPressedName == "Escape" || e.Key.KeyPressedName == "XF86Back" || e.Key.KeyPressedName == "BackSpace")
185 if (navigator == null) return;
189 if (navigator.PageCount == 0)
197 public void OnSelectionChanged(object sender, SelectionChangedEventArgs ev)
199 Console.WriteLine($"@@@ OnSelectionChanged() {ev.CurrentSelection}");
201 if (ev.CurrentSelection.Count == 0) return;
203 if (ev.CurrentSelection[0] is ControlMenu selItem)
205 Console.WriteLine($"@@@ selItem.Name={selItem.Name}, selItem.FullName={selItem.FullName}");
206 RunSample(selItem?.FullName);
208 colView.SelectedItem = null;
211 protected override void OnCreate()
217 private void Initialize()
219 window = GetDefaultWindow();
220 window.Title = "NUI Style Guide";
221 window.KeyEvent += OnKeyEvent;
223 navigator = window.GetDefaultNavigator();
226 void OnSearchBtnClicked(object sender, ClickedEventArgs e)
228 var filteredSource = from filter in testSource
229 where filter.Name.ToLower().Contains(field.SearchTextField?.Text?.ToLower())
232 colView.Header = new DefaultTitleItem()
235 WidthSpecification = LayoutParamPolicies.MatchParent,
237 colView.ItemsSource = filteredSource;
240 private void SetMainPage()
242 var appBar = new AppBar()
244 Title = "NUI Style Guide",
245 AutoNavigationContent = false,
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;
254 var pageContent = new View()
256 Layout = new LinearLayout()
258 LinearOrientation = LinearLayout.Orientation.Vertical,
260 WidthSpecification = LayoutParamPolicies.MatchParent,
261 HeightSpecification = LayoutParamPolicies.MatchParent,
264 field = new SearchField()
266 WidthSpecification = LayoutParamPolicies.MatchParent,
268 field.SearchButton.Clicked += OnSearchBtnClicked;
270 testSource = new ControlMenuViewModel().CreateData();
271 selMode = ItemSelectionMode.SingleAlways;
272 var myTitle = new DefaultTitleItem()
275 WidthSpecification = LayoutParamPolicies.MatchParent,
278 colView = new CollectionView()
280 ItemsSource = testSource,
281 ItemsLayouter = new LinearLayouter(),
282 ItemTemplate = new DataTemplate(() =>
284 DefaultLinearItem item = new DefaultLinearItem()
286 WidthSpecification = LayoutParamPolicies.MatchParent,
288 item.Label.SetBinding(TextLabel.TextProperty, "ViewLabel");
289 item.Label.HorizontalAlignment = HorizontalAlignment.Begin;
293 ScrollingDirection = ScrollableBase.Direction.Vertical,
294 WidthSpecification = LayoutParamPolicies.MatchParent,
295 HeightSpecification = LayoutParamPolicies.MatchParent,
296 SelectionMode = selMode,
298 colView.SelectionChanged += OnSelectionChanged;
300 pageContent.Add(field);
301 pageContent.Add(colView);
303 page = new ContentPage()
306 Content = pageContent,
308 navigator.Push(page);
311 private void RunSample(string name)
313 IExample example = typeof(Program).Assembly?.CreateInstance(name) as IExample;
315 Console.WriteLine($"@@@ typeof(Program).Assembly={typeof(Program).Assembly}, name={name}");
320 if (example is Page examplePage)
322 navigator.Push((examplePage));
327 Console.WriteLine($"@@@ examle is null!");
331 private void ExitSample()
333 if (navigator.Peek() is IExample currentExample)
335 currentExample.Deactivate();
342 private void FullGC()
344 global::System.GC.Collect();
345 global::System.GC.WaitForPendingFinalizers();
346 global::System.GC.Collect();
349 static void Main(string[] args)
351 var app = new Program();