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