temporary fix of crash problem
[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
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.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 = "BEGIN";
144             itemLabel.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                         Console.WriteLine("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.ParentOrigin = ParentOrigin.Center;
320                     button.AnchorPoint = AnchorPoint.Center;
321                     button.MaximumSize = new Size2D(150, 100);
322                     _popup = CreatePopup();
323                     _popup.SetTitle(CreateTitle("Popup"));
324
325                     TextLabel text = new TextLabel("This will erase the file permanently. Are you sure?");
326                     text.BackgroundColor = Color.White;
327                     text.MultiLine = true;
328                     text.WidthResizePolicy = ResizePolicyType.FillToParent;
329                     text.HeightResizePolicy = ResizePolicyType.DimensionDependency;
330                     text.SetPadding(new PaddingType(10.0f, 10.0f, 20.0f, 0.0f));
331                     _popup.SetContent(text);
332                     _popup.Focusable = (true);
333                     _popup.SetDisplayState(Popup.DisplayStateType.Hidden);
334
335                     button.Clicked += (obj, ee) =>
336                     {
337                         _stage.GetDefaultLayer().Add(_popup);
338                         _popup.SetDisplayState(Popup.DisplayStateType.Shown);
339                         FocusManager.Instance.SetCurrentFocusView(View.DownCast((_popup.FindChildByName("Footer")).FindChildByName("OKButton")));
340                         return true;
341                     };
342                     _contentContainer.AddChild(button, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
343                 }
344                 if (item.name.CompareTo("Toast") == 0)
345                 {
346                     PushButton button = new PushButton();
347                     button.LabelText = "Toast";
348                     button.ParentOrigin = ParentOrigin.Center;
349                     button.AnchorPoint = AnchorPoint.Center;
350                     button.Clicked += (obj, ee) =>
351                     {
352                         TypeInfo typeInfo = new TypeInfo(TypeRegistry.Get().GetTypeInfo("PopupToast"));
353                         if (typeInfo)
354                         {
355                             BaseHandle baseHandle = typeInfo.CreateInstance();
356                             if (baseHandle)
357                             {
358                                 Popup toast = Popup.DownCast(baseHandle);
359                                 TextLabel text = new TextLabel("This is a Toast.\nIt will auto-hide itself");
360                                 text.TextColor = Color.White;
361                                 text.MultiLine = true;
362                                 text.HorizontalAlignment = "center";
363                                 toast.SetTitle(text);
364                                 _stage.GetDefaultLayer().Add(toast);
365                                 toast.SetDisplayState(Popup.DisplayStateType.Shown);
366                             }
367                         }
368                         return true;
369                     };
370                     _contentContainer.AddChild(button, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
371                 }
372                 if (item.name.CompareTo("ItemView") == 0)
373                 {
374
375                 }
376             }
377             else
378             {
379                 ImageView notSupportView = new ImageView(_resPath + "/images/not_yet_sign.png");
380                 notSupportView.Size = new Vector3(_stage.Size.Width * 0.2f, _stage.Size.Height * 0.25f, 0.0f);
381                 notSupportView.Focusable = (true);
382                 _contentContainer.AddChild(notSupportView, new TableView.CellPosition(((uint)idx / 5) * 2 + 1, (uint)idx % 5));
383             }
384         }
385         Popup CreatePopup()
386         {
387             Popup confirmationPopup = new Popup();
388
389             Actor footer = new Actor();
390             footer.Name = ("Footer");
391             footer.WidthResizePolicy = ResizePolicyType.FillToParent;
392             footer.HeightResizePolicy = ResizePolicyType.Fixed;
393             footer.Size = new Size(0.0f, 80.0f, 0.0f);
394             footer.ParentOrigin = ParentOrigin.Center;
395             footer.AnchorPoint = AnchorPoint.Center;
396
397             PushButton okButton = CreateOKButton();
398             okButton.ParentOrigin = ParentOrigin.Center;
399             okButton.AnchorPoint = AnchorPoint.Center;
400             okButton.WidthResizePolicy = ResizePolicyType.SizeFixedOffsetFromParent;
401             okButton.HeightResizePolicy = ResizePolicyType.SizeFixedOffsetFromParent;
402             okButton.SetSizeModeFactor(new Vector3(-20.0f, -20.0f, 0.0f));
403
404             PushButton cancelButton = CreateCancelButton();
405             cancelButton.ParentOrigin = ParentOrigin.Center;
406             cancelButton.AnchorPoint = AnchorPoint.Center;
407             cancelButton.WidthResizePolicy = ResizePolicyType.SizeFixedOffsetFromParent;
408             cancelButton.HeightResizePolicy = ResizePolicyType.SizeFixedOffsetFromParent;
409             cancelButton.SetSizeModeFactor(new Vector3(-20.0f, -20.0f, 0.0f));
410
411             TableView controlLayout = new TableView(1, 2);
412             controlLayout.ParentOrigin = ParentOrigin.Center;
413             controlLayout.AnchorPoint = AnchorPoint.Center;
414             controlLayout.WidthResizePolicy = ResizePolicyType.FillToParent;
415             controlLayout.HeightResizePolicy = ResizePolicyType.FillToParent;
416             controlLayout.SetCellPadding(new Size2D(10, 10));
417             controlLayout.SetRelativeWidth(0, 0.5f);
418             controlLayout.SetRelativeWidth(1, 0.5f);
419             controlLayout.SetCellAlignment(new TableView.CellPosition(0, 0), HorizontalAlignmentType.Center, VerticalAlignmentType.Center);
420             controlLayout.SetCellAlignment(new TableView.CellPosition(0, 1), HorizontalAlignmentType.Center, VerticalAlignmentType.Center);
421             controlLayout.AddChild(okButton, new TableView.CellPosition(0, 0));
422             controlLayout.AddChild(cancelButton, new TableView.CellPosition(0, 1));
423
424             footer.Add(controlLayout);
425
426             confirmationPopup.SetFooter(footer);
427             return confirmationPopup;
428         }
429         Actor CreateTitle(string title)
430         {
431             TextLabel titleActor = new TextLabel(title);
432             titleActor.TextColor = Color.White;
433             titleActor.MultiLine = true;
434             titleActor.HorizontalAlignment = "center";
435             return titleActor;
436         }
437
438         PushButton CreateOKButton()
439         {
440             PushButton okayButton = new PushButton();
441             okayButton.Name = ("OKButton");
442             okayButton.LabelText = "OK";
443             okayButton.Focusable = (true);
444             okayButton.Clicked += (obj, ee) =>
445             {
446                 _popup.SetDisplayState(Popup.DisplayStateType.Hidden);
447                 return true;
448             };
449             return okayButton;
450         }
451         PushButton CreateCancelButton()
452         {
453             PushButton cancelButton = new PushButton();
454             cancelButton.LabelText = "Cancel";
455             cancelButton.Focusable = (true);
456             cancelButton.Clicked += (obj, ee) =>
457             {
458                 _popup.SetDisplayState(Popup.DisplayStateType.Hidden);
459                 return true;
460             };
461             return cancelButton;
462         }
463
464         void OnProgressBarValueChanged(object source, ProgressBar.ValueChangedEventArgs e)
465         {
466             PropertyMap labelVisual = new PropertyMap();
467             labelVisual.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Text))
468               .Add(TextVisualProperty.Text, new PropertyValue(Math.Round(e.ProgressBar.ProgressValue, 2) + " / " + Math.Round(e.ProgressBar.SecondaryProgressValue, 2)))
469               .Add(TextVisualProperty.PointSize, new PropertyValue(10.0f));
470             e.ProgressBar.LabelVisual = labelVisual;
471             return;
472         }
473
474
475         /// <summary>
476         /// The main entry point for the application.
477         /// </summary>
478
479         [STAThread]
480         static void _Main(string[] args)
481         {
482             Console.WriteLine("Hello Mono World");
483
484             Example example = new Example("/home/owner/apps_rw/NUISamples.TizenTV/res/json/control-dashboard-theme.json");
485             example.Run(args);
486         }
487     }
488 }