[NUI] Change the name of Tizen.NUI.CommonUI as Tizen.NUI.Components (#958)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Toast.cs
1 /*
2  * Copyright(c) 2019 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 using System;
18 using Tizen.NUI.BaseComponents;
19 using System.ComponentModel;
20
21 namespace Tizen.NUI.Components
22 {
23     /// <summary>
24     /// Use a toast to provide simple messages when the user does not need to make an additional action or confirmation.
25     /// Unlike other popups, a toast only has the body field as it is just used for providing simple feedback to user actions.
26     /// A toast will automatically disappear after a certain time.
27     /// </summary>
28     /// <since_tizen> 6 </since_tizen>
29     /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
30     [EditorBrowsable(EditorBrowsableState.Never)]
31     public class Toast : Control
32     {
33         /// <summary>
34         /// textLabels.
35         /// </summary>
36         protected TextLabel[] textLabels = null;
37         private ToastAttributes toastAttributes = null;
38         private string[] textArray = null;
39         private NPatchVisual toastBackground = null;
40         private Timer timer = null;
41
42         private readonly int maxTextAreaWidth = 808;
43         private readonly uint textLineHeight = 56;
44         private readonly uint textLineSpace = 4;
45         private readonly float textPointSize = 38;
46         private readonly int textPaddingLeft = 96;
47         private readonly int textPaddingRight = 96;
48         private readonly int textPaddingTop = 38;
49         private readonly int textPaddingBottom = 38;
50         private readonly uint duration = 3000;
51
52         /// <summary>
53         /// Construct Toast with null.
54         /// </summary>
55         /// <since_tizen> 6 </since_tizen>
56         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
57         [EditorBrowsable(EditorBrowsableState.Never)]
58         public Toast() : base()
59         {
60             Initialize();
61         }
62
63         /// <summary>
64         /// The constructor of the Toast class with specific Attributes.
65         /// </summary>
66         /// <param name="attributes">Construct Attributes</param>
67         /// <since_tizen> 6 </since_tizen>
68         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
69         [EditorBrowsable(EditorBrowsableState.Never)]
70         public Toast(ToastAttributes attributes) : base(attributes)
71         {
72             Initialize();
73         }
74
75         /// <summary>
76         /// Constructor of the Toast class with special style.
77         /// </summary>
78         /// <param name="style"> style name </param>
79         /// <since_tizen> 6 </since_tizen>
80         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
81         [EditorBrowsable(EditorBrowsableState.Never)]
82         public Toast(string style) : base(style)
83         {
84             Initialize();
85         }
86
87         /// <summary>
88         /// Gets or sets the text array of toast.
89         /// </summary>
90         /// <since_tizen> 6 </since_tizen>
91         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
92         [EditorBrowsable(EditorBrowsableState.Never)]
93         public string[] TextArray
94         {
95             get
96             {
97                 return textArray;
98             }
99             set
100             {
101                 if (null != value)
102                 {
103                     textArray = value;
104                     SetToastText();
105                     RelayoutRequest();
106                 }
107             }
108         }
109
110         /// <summary>
111         /// Gets or sets text point size in toast.
112         /// </summary>
113         /// <since_tizen> 6 </since_tizen>
114         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
115         [EditorBrowsable(EditorBrowsableState.Never)]
116         public float PointSize
117         {
118             get
119             {
120                 return toastAttributes.TextAttributes?.PointSize?.All ?? textPointSize;
121             }
122             set
123             {
124                 CreateTextAttributes();
125                 if (null == toastAttributes.TextAttributes.PointSize)
126                 {
127                     toastAttributes.TextAttributes.PointSize = new FloatSelector();
128                 }
129                 toastAttributes.TextAttributes.PointSize.All = value;
130                 RelayoutRequest();
131             }
132         }
133
134         /// <summary>
135         /// Gets or sets text font family in toast.
136         /// </summary>
137         /// <since_tizen> 6 </since_tizen>
138         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
139         [EditorBrowsable(EditorBrowsableState.Never)]
140         public string FontFamily
141         {
142             get
143             {
144                 return toastAttributes.TextAttributes?.FontFamily;
145             }
146             set
147             {
148                 CreateTextAttributes();
149                 toastAttributes.TextAttributes.FontFamily = value;
150                 RelayoutRequest();
151             }
152         }
153
154         /// <summary>
155         /// Gets or sets text color in toast.
156         /// </summary>
157         /// <since_tizen> 6 </since_tizen>
158         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
159         [EditorBrowsable(EditorBrowsableState.Never)]
160         public Color TextColor
161         {
162             get
163             {
164                 return toastAttributes.TextAttributes?.TextColor?.All;
165             }
166             set
167             {
168                 CreateTextAttributes();
169                 if (null == toastAttributes.TextAttributes.TextColor)
170                 {
171                     toastAttributes.TextAttributes.TextColor = new ColorSelector();
172                 }
173                 toastAttributes.TextAttributes.TextColor.All = value;
174                 RelayoutRequest();
175             }
176         }
177
178         /// <summary>
179         /// Gets or sets text horizontal alignment in toast.
180         /// </summary>
181         /// <since_tizen> 6 </since_tizen>
182         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
183         [EditorBrowsable(EditorBrowsableState.Never)]
184         public HorizontalAlignment TextAlignment
185         {
186             get
187             {
188                 return toastAttributes.TextAttributes?.HorizontalAlignment ?? HorizontalAlignment.Center;
189             }
190             set
191             {
192                 CreateTextAttributes();
193                 toastAttributes.TextAttributes.HorizontalAlignment = value;
194                 RelayoutRequest();
195             }
196         }
197
198         /// <summary>
199         /// Gets or sets background image resource of toast.
200         /// </summary>
201         /// <since_tizen> 6 </since_tizen>
202         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
203         [EditorBrowsable(EditorBrowsableState.Never)]
204         public string BackgroundImageURL
205         {
206             get
207             {
208                 return toastAttributes.BackgroundImageAttributes?.ResourceURL?.All;
209             }
210             set
211             {
212                 if (null != value)
213                 {
214                     CreateBackgroundAttributes();
215                                         if (toastAttributes.BackgroundImageAttributes != null)
216                                         {
217                         if (null == toastAttributes.BackgroundImageAttributes?.ResourceURL)
218                         {
219                             toastAttributes.BackgroundImageAttributes.ResourceURL = new StringSelector();
220                         }
221     
222                         toastAttributes.BackgroundImageAttributes.ResourceURL.All = value;
223                         SetToastBackground();
224                                         }
225                 }
226             }
227         }
228
229         /// <summary>
230         /// Gets or sets background image's border of toast.
231         /// </summary>
232         /// <since_tizen> 6 </since_tizen>
233         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
234         [EditorBrowsable(EditorBrowsableState.Never)]
235         public Rectangle BackgroundImageBorder
236         {
237             get
238             {
239                 return toastAttributes.BackgroundImageAttributes?.Border?.All;
240             }
241             set
242             {
243                 if (null != value)
244                 {
245                     CreateBackgroundAttributes();
246                     if (toastAttributes.BackgroundImageAttributes != null)
247                     {
248                         if (null == toastAttributes.BackgroundImageAttributes.Border)
249                         {
250                             toastAttributes.BackgroundImageAttributes.Border = new RectangleSelector();
251                         }
252                         toastAttributes.BackgroundImageAttributes.Border.All = value;
253                         SetToastBackground();
254                     }
255                 }
256             }
257         }
258
259         /// <summary>
260         /// Gets or sets text left padding in toast.
261         /// </summary>
262         /// <since_tizen> 6 </since_tizen>
263         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
264         [EditorBrowsable(EditorBrowsableState.Never)]
265         public int TextPaddingLeft
266         {
267             get
268             {
269                 return toastAttributes.TextAttributes?.PaddingLeft ?? textPaddingLeft;
270             }
271             set
272             {
273                 CreateTextAttributes();
274                 toastAttributes.TextAttributes.PaddingLeft = value;
275                 RelayoutRequest();
276             }
277         }
278
279         /// <summary>
280         /// Gets or sets text right padding in toast.
281         /// </summary>
282         /// <since_tizen> 6 </since_tizen>
283         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
284         [EditorBrowsable(EditorBrowsableState.Never)]
285         public int TextPaddingRight
286         {
287             get
288             {
289                 return toastAttributes.TextAttributes?.PaddingRight ?? textPaddingRight;
290             }
291             set
292             {
293                 CreateTextAttributes();
294                 toastAttributes.TextAttributes.PaddingRight = value;
295                 RelayoutRequest();
296             }
297         }
298
299         /// <summary>
300         /// Gets or sets text top padding in toast.
301         /// </summary>
302         /// <since_tizen> 6 </since_tizen>
303         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
304         [EditorBrowsable(EditorBrowsableState.Never)]
305         public int TextPaddingTop
306         {
307             get
308             {
309                 return toastAttributes.TextAttributes?.PaddingTop ?? textPaddingTop;
310             }
311             set
312             {
313                 CreateTextAttributes();
314                 toastAttributes.TextAttributes.PaddingTop = value;
315                 RelayoutRequest();
316             }
317         }
318
319         /// <summary>
320         /// Gets or sets text bottom padding in toast.
321         /// </summary>
322         /// <since_tizen> 6 </since_tizen>
323         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
324         [EditorBrowsable(EditorBrowsableState.Never)]
325         public int TextPaddingBottom
326         {
327             get
328             {
329                 return toastAttributes.TextAttributes?.PaddingBottom ?? textPaddingBottom;
330             }
331             set
332             {
333                 CreateTextAttributes();
334                 toastAttributes.TextAttributes.PaddingBottom = value;
335                 RelayoutRequest();
336             }
337         }
338
339         /// <summary>
340         /// Gets or sets text line height in toast.
341         /// </summary>
342         /// <since_tizen> 6 </since_tizen>
343         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
344         [EditorBrowsable(EditorBrowsableState.Never)]
345         public uint TextLineHeight
346         {
347             get
348             {
349                 return toastAttributes.TextLineHeight ?? textLineHeight;
350             }
351             set
352             {
353                 toastAttributes.TextLineHeight = value;
354                 RelayoutRequest();
355             }
356         }
357
358         /// <summary>
359         /// Gets or sets text line space in toast.
360         /// </summary>
361         /// <since_tizen> 6 </since_tizen>
362         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
363         [EditorBrowsable(EditorBrowsableState.Never)]
364         public uint TextLineSpace
365         {
366             get
367             {
368                 return toastAttributes.TextLineSpace ?? textLineSpace;
369             }
370             set
371             {
372                 toastAttributes.TextLineSpace = value;
373                 RelayoutRequest();
374             }
375         }
376
377         /// <summary>
378         /// Gets or sets duration of toast.
379         /// </summary>
380         /// <since_tizen> 6 </since_tizen>
381         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
382         [EditorBrowsable(EditorBrowsableState.Never)]
383         public uint Duration
384         {
385             get
386             {
387                 return toastAttributes.Duration ?? duration;
388             }
389             set
390             {
391                 toastAttributes.Duration = value;
392                 timer.Interval = value;
393             }
394         }
395
396         /// <summary>
397         /// Dispose ToastPopup.
398         /// </summary>
399         /// <param name="type">dispose types.</param>
400         /// <since_tizen> 6 </since_tizen>
401         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
402         [EditorBrowsable(EditorBrowsableState.Never)]
403         protected override void Dispose(DisposeTypes type)
404         {
405             if (disposed)
406             {
407                 return;
408             }
409
410             if (type == DisposeTypes.Explicit)
411             {
412                 this.VisibilityChanged -= OnVisibilityChanged;
413                 if (null != timer)
414                 {
415                     timer.Tick -= OnTick;
416                     timer.Dispose();
417                     timer = null;
418                 }
419                 if (null != textLabels)
420                 {
421                     for (int i=0; i<textLabels.Length; i++)
422                     {
423                         Utility.Dispose(textLabels[i]);
424                     }
425                 }
426             }
427
428             base.Dispose(type);
429         }
430
431         /// <summary>
432         /// Relayout control's elements
433         /// </summary>
434         /// <since_tizen> 6 </since_tizen>
435         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
436         [EditorBrowsable(EditorBrowsableState.Never)]
437         protected override void OnUpdate()
438         {
439             if (null == toastAttributes)
440             {
441                 return;
442             }
443             if (null != toastAttributes.TextAttributes)
444             {
445                 for (int i = 0; i < textLabels.Length; i++)
446                 {
447                     ApplyAttributes(textLabels[i], toastAttributes.TextAttributes);
448                 }
449             }
450             LayoutChild();
451         }
452
453         /// <summary>
454         /// LayoutChild include textLabel.
455         /// </summary>
456         /// <since_tizen> 6 </since_tizen>
457         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
458         [EditorBrowsable(EditorBrowsableState.Never)]
459         protected virtual void LayoutChild()
460         {
461             int _textPaddingLeft = toastAttributes.TextAttributes?.PaddingLeft ?? textPaddingLeft;
462             int _textPaddingRight = toastAttributes.TextAttributes?.PaddingRight ?? _textPaddingLeft;
463             int _textPaddingTop = toastAttributes.TextAttributes?.PaddingTop ?? textPaddingTop;
464             int _textPaddingBottom = toastAttributes.TextAttributes?.PaddingBottom ?? _textPaddingTop;
465
466             int _textAreaWidth = this.Size2D.Width - _textPaddingLeft - _textPaddingRight;
467             int _textAreaHeight = this.Size2D.Height - _textPaddingTop - _textPaddingBottom;
468             int _textLineSpace = (int)(toastAttributes.TextLineSpace ?? textLineSpace);
469             int _textLineHeight = (int)(toastAttributes.TextLineHeight ?? textLineHeight);
470             int _positionY = 0;
471
472             _textAreaWidth = _textAreaWidth > maxTextAreaWidth ? maxTextAreaWidth : _textAreaWidth;
473             if (LayoutDirection == ViewLayoutDirectionType.LTR)
474             {
475                 for (int i = 0; i < textLabels?.Length; i++)
476                 {
477                     textLabels[i].Position2D = new Position2D(_textPaddingLeft, _textPaddingTop + _positionY);
478                     textLabels[i].Size2D = new Size2D(_textAreaWidth, _textLineHeight);
479                     _positionY += _textLineHeight + _textLineSpace;
480                 }
481             }
482             else
483             {
484                 for (int i = 0; i < textLabels?.Length; i++)
485                 {
486                     textLabels[i].ParentOrigin = Tizen.NUI.ParentOrigin.TopRight;
487                     textLabels[i].PivotPoint = Tizen.NUI.PivotPoint.TopRight;
488                     textLabels[i].PositionUsesPivotPoint = true;
489                     textLabels[i].Position2D = new Position2D(-_textPaddingLeft, _textPaddingTop + _positionY);
490                     textLabels[i].Size2D = new Size2D(_textAreaWidth, _textLineHeight);
491                     _positionY += _textLineHeight + _textLineSpace;
492                 }
493             }
494         }
495
496         /// <summary>
497         /// Get Toast attribues.
498         /// </summary>
499         /// <since_tizen> 6 </since_tizen>
500         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
501         [EditorBrowsable(EditorBrowsableState.Never)]
502         protected override Attributes GetAttributes()
503         {
504             return new ToastAttributes();
505         }
506
507         private void Initialize()
508         {
509             toastAttributes = attributes as ToastAttributes;
510             if (null == toastAttributes)
511             {
512                 throw new Exception("Toast attribute parse error.");
513             }
514             ApplyAttributes(this, toastAttributes);
515
516             toastBackground = new NPatchVisual();
517             SetToastBackground();
518
519             this.VisibilityChanged += OnVisibilityChanged;
520             timer = new Timer(toastAttributes.Duration ?? duration);
521             timer.Tick += OnTick;
522             timer.Start();
523         }
524
525         private bool OnTick(object sender, EventArgs e)
526         {
527             Hide();
528             return false;
529         }
530
531         private void OnVisibilityChanged(object sender, VisibilityChangedEventArgs e)
532         {
533             if (true == e.Visibility)
534             {
535                 timer.Start();
536             }
537         }
538
539         private void SetToastText()
540         {
541             if (textLabels != null)
542             {
543                 for (int i = 0; i < textLabels?.Length; i++)
544                 {
545                     if (null != textLabels[i])
546                     {
547                         this.Remove(textLabels[i]);
548                         textLabels[i].Dispose();
549                         textLabels[i] = null;
550                     }
551                 }
552             }
553
554             textLabels = new TextLabel[textArray.Length];
555             if (textLabels != null)
556             {
557                 for (int i = 0; i < textArray.Length; i++)
558                 {
559                     textLabels[i] = new TextLabel();
560                     textLabels[i].Text = textArray[i];
561                     textLabels[i].BackgroundColor = Color.Blue;
562                     this.Add(textLabels[i]);
563                 }
564             }
565         }
566
567         private void SetToastBackground()
568         {
569             if (null != toastAttributes?.BackgroundImageAttributes?.ResourceURL)
570             {
571                 toastBackground.URL = toastAttributes.BackgroundImageAttributes.ResourceURL.All;
572             }
573             if (null != toastAttributes?.BackgroundImageAttributes?.Border)
574             {
575                 toastBackground.Border = toastAttributes.BackgroundImageAttributes.Border.All;
576             }
577             this.Background = toastBackground.OutputVisualMap;
578         }
579
580         private void CreateBackgroundAttributes()
581         {
582             if (null == toastAttributes.BackgroundImageAttributes)
583             {
584                 toastAttributes.BackgroundImageAttributes = new ImageAttributes();
585             }
586         }
587
588         private void CreateTextAttributes()
589         {
590             if (null == toastAttributes.TextAttributes)
591             {
592                 toastAttributes.TextAttributes = new TextAttributes();
593             }
594         }
595     }
596 }