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