1.[NUI 276] c# nui visual high level class refactorying.
[platform/core/csapi/nui.git] / NUISamples / NUISamples / NUISamples.TizenTV / examples / control-dashboard.cs
1 /*
2  * Copyright (c) 2017 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.Runtime.InteropServices;
20 using Tizen.NUI;
21 using Tizen.NUI.UIComponents;
22 using Tizen.NUI.Constants;
23
24 namespace ControlDashboard
25 {
26     class Example : NUIApplication
27     {
28         // This is simple structure to contain Control name and implement state at once
29         // name : control name
30         // isImplemented : the state which the control is implemented in public or not
31         private struct Item
32         {
33             public String name;
34             public bool isImplemented;
35
36             public Item(String name, bool isImplemented)
37             {
38                 this.name = name;
39                 this.isImplemented = isImplemented;
40             }
41         }
42
43         private TableView _contentContainer;
44         private Timer _timer;
45         private Stage _stage;
46         private Popup _popup;
47         private ProgressBar _progressBar;
48         private const string _resPath = "/home/owner/apps_rw/NUISamples.TizenTV/res";
49
50         // List of items
51         private Item[] mViewList = {
52       new Item("PushButton", true),  new Item("DropDown", false),    new Item("Toggle", true),
53       new Item("InputField", false),  new Item("AnimateGif", false),  new Item("Loading", false),
54       new Item("ProgressBar", true), new Item("CheckBox", false),    new Item("RadioButton", true),
55       new Item("Tooltip", true),     new Item("Popup", true),       new Item("Toast", true),
56       new Item("ItemView", false),    new Item("CheckBox", true)
57     };
58
59         public Example() : base()
60         {
61         }
62
63         public Example(string stylesheet) : base(stylesheet)
64         {
65         }
66
67         public Example(string stylesheet, NUIApplication.WindowMode windowMode) : base(stylesheet, windowMode)
68         {
69         }
70
71         protected override void OnCreate()
72         {
73             base.OnCreate();
74             Initialize();
75         }
76
77         public void Initialize()
78         {
79             Tizen.Log.Debug("NUI", "Customized Application Initialize event handler");
80             _stage = Stage.Instance;
81             _stage.BackgroundColor = Color.White;
82
83             // Top label
84             TextLabel topLabel = new TextLabel();
85             topLabel.WidthResizePolicy = ResizePolicyType.FillToParent;
86             topLabel.HeightResizePolicy = ResizePolicyType.SizeRelativeToParent;
87             topLabel.AnchorPoint = AnchorPoint.TopCenter;
88             topLabel.SetSizeModeFactor(new Vector3(0.0f, 0.1f, 0.0f));
89             topLabel.BackgroundColor = new Color(43.0f / 255.0f, 145.0f / 255.0f, 175.0f / 255.0f, 1.0f);
90             topLabel.TextColor = Color.White;
91             topLabel.Text = " DALi Views";
92             topLabel.HorizontalAlignment = HorizontalAlignment.Begin;
93             topLabel.VerticalAlignment = VerticalAlignment.Center;
94             topLabel.PointSize = 42.0f;
95             _stage.GetDefaultLayer().Add(topLabel);
96             //StyleManager.Get().ApplyStyle(topLabel, _resPath + "/json/control-dashboard-theme.json", "TextFieldFontSize4");
97             topLabel.SetStyleName("TextFieldFontSize4");
98
99             // Grid container to contain items. Use tableView because FlexContainer support focus navigation just two direction ( up/down or left/right )
100             _contentContainer = new TableView(6, 5);
101             _contentContainer.WidthResizePolicy = ResizePolicyType.FillToParent;
102             _contentContainer.HeightResizePolicy = ResizePolicyType.SizeRelativeToParent;
103             _contentContainer.SetSizeModeFactor(new Vector3(0.0f, 0.9f, 0.0f));
104             _contentContainer.AnchorPoint = AnchorPoint.BottomCenter;
105             _contentContainer.Position = new Position(0, _stage.Size.Height * 0.1f, 0);
106             _contentContainer.SetRelativeHeight(0, 0.07f);
107             _contentContainer.SetRelativeHeight(1, 0.26f);
108             _contentContainer.SetRelativeHeight(2, 0.07f);
109             _contentContainer.SetRelativeHeight(3, 0.26f);
110             _contentContainer.SetRelativeHeight(4, 0.07f);
111             _contentContainer.SetRelativeHeight(5, 0.26f);
112             _contentContainer.Focusable = (true);
113             _stage.GetDefaultLayer().Add(_contentContainer);
114
115             CreateContent();
116
117             FocusManager.Instance.PreFocusChange += OnPreFocusChange;
118         }
119
120         // Callback for KeyboardFocusManager
121         private View OnPreFocusChange(object source, FocusManager.PreFocusChangeEventArgs e)
122         {
123             if (!e.ProposedView && !e.CurrentView)
124             {
125                 e.ProposedView = View.DownCast(_contentContainer.GetChildAt(1));
126             }
127             return e.ProposedView;
128         }
129
130         private void CreateContent()
131         {
132             for (int i = 0; i < mViewList.Length; i++)
133             {
134                 CreateItem(mViewList[i], i);
135             }
136         }
137
138         private void CreateItem(Item item, int idx)
139         {
140             // Make label for item
141             TextLabel itemLabel = new TextLabel("    " + item.name);
142             itemLabel.Size = new Vector3(_stage.Size.Width * 0.2f, _stage.Size.Height * 0.05f, 0.0f);
143             itemLabel.HorizontalAlignment = HorizontalAlignment.Begin;
144             itemLabel.VerticalAlignment = VerticalAlignment.Bottom;
145             //itemLabel.PointSize = 18.0f;
146             _contentContainer.AddChild(itemLabel, new TableView.CellPosition(((uint)idx / 5) * 2, (uint)idx % 5));
147
148             // If item is implemented in public, attach it on stage
149             if (item.isImplemented)
150             {
151                 if (item.name.CompareTo("PushButton") == 0)
152                 {
153                     PushButton pushButton = new PushButton();
154                     pushButton.LabelText = "Push Button";
155                     pushButton.WidthResizePolicy = ResizePolicyType.FillToParent;
156                     pushButton.HeightResizePolicy = ResizePolicyType.FillToParent;
157                     pushButton.UnselectedColor = new Vector4(1.0f, 0.0f, 0.0f, 1.0f);
158                     pushButton.SelectedColor = new Vector4(0.0f, 1.0f, 0.0f, 1.0f);
159                     pushButton.Clicked += (obj, e) =>
160                     {
161                         Button sender = obj as Button;
162                         sender.LabelText = "Click Me";
163                         sender.UnselectedColor = new Vector4(0.0f, 0.0f, 1.0f, 1.0f);
164                         return true;
165                     };
166
167                     _contentContainer.AddChild(pushButton, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
168                 }
169                 if (item.name.CompareTo("DropDown") == 0)
170                 {
171
172                 }
173                 if (item.name.CompareTo("Toggle") == 0)
174                 {
175                     ToggleButton toggleButton = new ToggleButton();
176                     PropertyArray array = new PropertyArray();
177                     array.Add(new PropertyValue(_resPath + "/images/star-highlight.png"));
178                     array.Add(new PropertyValue(_resPath + "/images/star-mod.png"));
179                     array.Add(new PropertyValue(_resPath + "/images/star-dim.png"));
180                     toggleButton.StateVisuals = array;
181
182                     PropertyArray tooltips = new PropertyArray();
183                     tooltips.Add(new PropertyValue("State A"));
184                     tooltips.Add(new PropertyValue("State B"));
185                     tooltips.Add(new PropertyValue("State C"));
186                     toggleButton.Tooltips = tooltips;
187
188                     toggleButton.WidthResizePolicy = ResizePolicyType.FillToParent;
189                     toggleButton.HeightResizePolicy = ResizePolicyType.FillToParent;
190                     toggleButton.Clicked += (obj, e) =>
191                     {
192                         Tizen.Log.Debug("NUI", "Toggle button state changed.");
193                         return true;
194                     };
195
196                     _contentContainer.AddChild(toggleButton, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
197                 }
198                 if (item.name.CompareTo("InputField") == 0)
199                 {
200
201                 }
202                 if (item.name.CompareTo("AnimateGif") == 0)
203                 {
204
205                 }
206                 if (item.name.CompareTo("Loading") == 0)
207                 {
208
209                 }
210                 if (item.name.CompareTo("ProgressBar") == 0)
211                 {
212                     _progressBar = new ProgressBar();
213                     _progressBar.WidthResizePolicy = ResizePolicyType.FillToParent;
214                     _progressBar.HeightResizePolicy = ResizePolicyType.Fixed;
215                     _progressBar.Size2D = new Size2D(0, 100);
216
217                     _progressBar.ValueChanged += OnProgressBarValueChanged;
218
219                     _timer = new Timer(100);
220                     _timer.Tick += (obj, e) =>
221                     {
222                         float progress = (float)Math.Round(_progressBar.ProgressValue, 2);
223
224                         if (progress == 1.0f)
225                         {
226                             _progressBar.ProgressValue = 0.0f;
227                             _progressBar.SecondaryProgressValue = 0.01f;
228                         }
229                         else
230                         {
231                             _progressBar.ProgressValue = progress + 0.01f;
232                             _progressBar.SecondaryProgressValue = progress + 0.21f;
233                         }
234                         return true;
235                     };
236                     _timer.Start();
237
238                     _contentContainer.AddChild(_progressBar, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
239                 }
240                 if (item.name.CompareTo("ScrollBar") == 0)
241                 {
242
243                 }
244                 if (item.name.CompareTo("CheckBox") == 0)
245                 {
246                     CheckBoxButton checkBoxButton = new CheckBoxButton();
247                     checkBoxButton.LabelText = "Yes";
248
249                     _contentContainer.AddChild(checkBoxButton, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
250                 }
251                 if (item.name.CompareTo("RadioButton") == 0)
252                 {
253                     TableView tableView = new TableView(2, 1);
254                     tableView.WidthResizePolicy = ResizePolicyType.FillToParent;
255                     tableView.HeightResizePolicy = ResizePolicyType.FillToParent;
256
257                     RadioButton rButton = new RadioButton();
258                     rButton.LabelText = "Yes";
259                     rButton.Selected = true;
260                     tableView.AddChild(rButton, new TableView.CellPosition(0, 0));
261
262                     rButton = new RadioButton();
263                     rButton.LabelText = "No";
264
265                     tableView.AddChild(rButton, new TableView.CellPosition(1, 0));
266
267                     _contentContainer.AddChild(tableView, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
268                 }
269                 if (item.name.CompareTo("Tooltip") == 0)
270                 {
271                     TableView tableView = new TableView(2, 1);
272                     tableView.WidthResizePolicy = ResizePolicyType.FillToParent;
273                     tableView.HeightResizePolicy = ResizePolicyType.FillToParent;
274
275                     // Create two push buttons and add them to a table view
276                     PushButton buttonWithSimpleTooltip = new PushButton();
277                     buttonWithSimpleTooltip.LabelText = "Tooltip with text only";
278                     buttonWithSimpleTooltip.UnselectedColor = new Vector4(0.5f, 0.5f, 0.7f, 1.0f);
279                     buttonWithSimpleTooltip.SelectedColor = new Vector4(0.7f, 0.5f, 0.7f, 1.0f);
280                     buttonWithSimpleTooltip.WidthResizePolicy = ResizePolicyType.FillToParent;
281                     tableView.AddChild(buttonWithSimpleTooltip, new TableView.CellPosition(0, 0));
282
283                     PushButton buttonWithIconTooltip = new PushButton();
284                     buttonWithIconTooltip.LabelText = "Tooltip with Text and Icon";
285                     buttonWithIconTooltip.WidthResizePolicy = ResizePolicyType.FillToParent;
286                     buttonWithIconTooltip.UnselectedColor = new Vector4(0.5f, 0.5f, 0.7f, 1.0f);
287                     buttonWithIconTooltip.SelectedColor = new Vector4(0.7f, 0.5f, 0.7f, 1.0f);
288                     tableView.AddChild(buttonWithIconTooltip, new TableView.CellPosition(1, 0));
289
290                     // Add a simple text only tooltip to the first push button
291                     buttonWithSimpleTooltip.TooltipText = "Simple Tooltip";
292
293                     // Create a property map for a tooltip with one icon and one text
294                     PropertyArray iconTooltipContent = new PropertyArray();
295
296                     PropertyMap iconVisual = new PropertyMap();
297                     iconVisual.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Image))
298                       .Add(ImageVisualProperty.URL, new PropertyValue(_resPath + "/images/star-highlight.png"));
299                     iconTooltipContent.Add(new PropertyValue(iconVisual));
300
301                     PropertyMap textVisual = new PropertyMap();
302                     textVisual.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Text))
303                       .Add(TextVisualProperty.Text, new PropertyValue("Tooltip with Icon"));
304                     iconTooltipContent.Add(new PropertyValue(textVisual));
305
306                     PropertyMap iconTooltip = new PropertyMap();
307                     iconTooltip.Add(Tizen.NUI.Constants.Tooltip.Property.Content, new PropertyValue(iconTooltipContent))
308                       .Add(Tizen.NUI.Constants.Tooltip.Property.Tail, new PropertyValue(true));
309
310                     // Add the tooltip with icon and text to the second push button
311                     buttonWithIconTooltip.Tooltip = iconTooltip;
312
313                     _contentContainer.AddChild(tableView, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
314                 }
315                 if (item.name.CompareTo("Popup") == 0)
316                 {
317                     PushButton button = new PushButton();
318                     button.LabelText = "Popup";
319                     button.AnchorPoint = AnchorPoint.Center;
320                     button.MaximumSize = new Size2D(150, 100);
321                     _popup = CreatePopup();
322                     _popup.SetTitle(CreateTitle("Popup"));
323
324                     TextLabel text = new TextLabel("This will erase the file permanently. Are you sure?");
325                     text.BackgroundColor = Color.White;
326                     text.MultiLine = true;
327                     text.WidthResizePolicy = ResizePolicyType.FillToParent;
328                     text.HeightResizePolicy = ResizePolicyType.DimensionDependency;
329                     text.SetPadding(new PaddingType(10.0f, 10.0f, 20.0f, 0.0f));
330                     _popup.SetContent(text);
331                     _popup.Focusable = (true);
332                     _popup.SetDisplayState(Popup.DisplayStateType.Hidden);
333
334                     button.Clicked += (obj, ee) =>
335                     {
336                         _stage.GetDefaultLayer().Add(_popup);
337                         _popup.SetDisplayState(Popup.DisplayStateType.Shown);
338                         FocusManager.Instance.SetCurrentFocusView(View.DownCast((_popup.FindChildByName("Footer")).FindChildByName("OKButton")));
339                         return true;
340                     };
341                     _contentContainer.AddChild(button, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
342                 }
343                 if (item.name.CompareTo("Toast") == 0)
344                 {
345                     PushButton button = new PushButton();
346                     button.LabelText = "Toast";
347                     button.AnchorPoint = AnchorPoint.Center;
348                     button.Clicked += (obj, ee) =>
349                     {
350                         TypeInfo typeInfo = new TypeInfo(TypeRegistry.Get().GetTypeInfo("PopupToast"));
351                         if (typeInfo)
352                         {
353                             BaseHandle baseHandle = typeInfo.CreateInstance();
354                             if (baseHandle)
355                             {
356                                 Popup toast = Popup.DownCast(baseHandle);
357                                 TextLabel text = new TextLabel("This is a Toast.\nIt will auto-hide itself");
358                                 text.TextColor = Color.White;
359                                 text.MultiLine = true;
360                                 text.HorizontalAlignment = HorizontalAlignment.Center;
361                                 toast.SetTitle(text);
362                                 _stage.GetDefaultLayer().Add(toast);
363                                 toast.SetDisplayState(Popup.DisplayStateType.Shown);
364                             }
365                         }
366                         return true;
367                     };
368                     _contentContainer.AddChild(button, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
369                 }
370                 if (item.name.CompareTo("ItemView") == 0)
371                 {
372
373                 }
374             }
375             else
376             {
377                 ImageView notSupportView = new ImageView(_resPath + "/images/not_yet_sign.png");
378                 notSupportView.Size = new Vector3(_stage.Size.Width * 0.2f, _stage.Size.Height * 0.25f, 0.0f);
379                 notSupportView.Focusable = (true);
380                 _contentContainer.AddChild(notSupportView, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
381             }
382         }
383         Popup CreatePopup()
384         {
385             Popup confirmationPopup = new Popup();
386
387             Actor footer = new Actor();
388             footer.Name = ("Footer");
389             footer.WidthResizePolicy = ResizePolicyType.FillToParent;
390             footer.HeightResizePolicy = ResizePolicyType.Fixed;
391             footer.Size = new Size(0.0f, 80.0f, 0.0f);
392             footer.AnchorPoint = AnchorPoint.Center;
393
394             PushButton okButton = CreateOKButton();
395             okButton.AnchorPoint = AnchorPoint.Center;
396             okButton.WidthResizePolicy = ResizePolicyType.SizeFixedOffsetFromParent;
397             okButton.HeightResizePolicy = ResizePolicyType.SizeFixedOffsetFromParent;
398             okButton.SetSizeModeFactor(new Vector3(-20.0f, -20.0f, 0.0f));
399
400             PushButton cancelButton = CreateCancelButton();
401             cancelButton.AnchorPoint = AnchorPoint.Center;
402             cancelButton.WidthResizePolicy = ResizePolicyType.SizeFixedOffsetFromParent;
403             cancelButton.HeightResizePolicy = ResizePolicyType.SizeFixedOffsetFromParent;
404             cancelButton.SetSizeModeFactor(new Vector3(-20.0f, -20.0f, 0.0f));
405
406             TableView controlLayout = new TableView(1, 2);
407             controlLayout.AnchorPoint = AnchorPoint.Center;
408             controlLayout.WidthResizePolicy = ResizePolicyType.FillToParent;
409             controlLayout.HeightResizePolicy = ResizePolicyType.FillToParent;
410             controlLayout.SetCellPadding(new Size2D(10, 10));
411             controlLayout.SetRelativeWidth(0, 0.5f);
412             controlLayout.SetRelativeWidth(1, 0.5f);
413             controlLayout.SetCellAlignment(new TableView.CellPosition(0, 0), HorizontalAlignmentType.Center, VerticalAlignmentType.Center);
414             controlLayout.SetCellAlignment(new TableView.CellPosition(0, 1), HorizontalAlignmentType.Center, VerticalAlignmentType.Center);
415             controlLayout.AddChild(okButton, new TableView.CellPosition(0, 0));
416             controlLayout.AddChild(cancelButton, new TableView.CellPosition(0, 1));
417
418             footer.Add(controlLayout);
419
420             confirmationPopup.SetFooter(footer);
421             return confirmationPopup;
422         }
423         Actor CreateTitle(string title)
424         {
425             TextLabel titleActor = new TextLabel(title);
426             titleActor.TextColor = Color.White;
427             titleActor.MultiLine = true;
428             titleActor.HorizontalAlignment = HorizontalAlignment.Center;
429             return titleActor;
430         }
431
432         PushButton CreateOKButton()
433         {
434             PushButton okayButton = new PushButton();
435             okayButton.Name = ("OKButton");
436             okayButton.LabelText = "OK";
437             okayButton.Focusable = (true);
438             okayButton.Clicked += (obj, ee) =>
439             {
440                 _popup.SetDisplayState(Popup.DisplayStateType.Hidden);
441                 return true;
442             };
443             return okayButton;
444         }
445         PushButton CreateCancelButton()
446         {
447             PushButton cancelButton = new PushButton();
448             cancelButton.LabelText = "Cancel";
449             cancelButton.Focusable = (true);
450             cancelButton.Clicked += (obj, ee) =>
451             {
452                 _popup.SetDisplayState(Popup.DisplayStateType.Hidden);
453                 return true;
454             };
455             return cancelButton;
456         }
457
458         void OnProgressBarValueChanged(object source, ProgressBar.ValueChangedEventArgs e)
459         {
460             PropertyMap labelVisual = new PropertyMap();
461             labelVisual.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Text))
462               .Add(TextVisualProperty.Text, new PropertyValue(Math.Round(e.ProgressBar.ProgressValue, 2) + " / " + Math.Round(e.ProgressBar.SecondaryProgressValue, 2)))
463               .Add(TextVisualProperty.PointSize, new PropertyValue(10.0f));
464             e.ProgressBar.LabelVisual = labelVisual;
465             return;
466         }
467
468
469         /// <summary>
470         /// The main entry point for the application.
471         /// </summary>
472
473         [STAThread]
474         static void _Main(string[] args)
475         {
476             Tizen.Log.Debug("NUI", "Hello Mono World");
477
478             Example example = new Example("/home/owner/apps_rw/NUISamples.TizenTV/res/json/control-dashboard-theme.json");
479             example.Run(args);
480         }
481     }
482 }