Follow formatting NUI
[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 using Tizen.NUI.Binding;
21
22 namespace Tizen.NUI.Components
23 {
24     /// <summary>
25     /// Use a toast to provide simple messages when the user does not need to make an additional action or confirmation.
26     /// Unlike other popups, a toast only has the body field as it is just used for providing simple feedback to user actions.
27     /// A toast will automatically disappear after a certain time.
28     /// </summary>
29     /// <since_tizen> 6 </since_tizen>
30     [Obsolete("Deprecated in API8; Will be removed in API10")]
31     public class Toast : Control
32     {
33         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
34         [EditorBrowsable(EditorBrowsableState.Never)]
35         public static readonly BindableProperty MessageProperty = BindableProperty.Create(nameof(Message), typeof(string), typeof(Toast), string.Empty, propertyChanged: (bindable, oldValue, newValue) =>
36         {
37             var instance = (Toast)bindable;
38             if (newValue != null)
39             {
40                 instance.strText = (string)(newValue);
41                 if (null != instance.textLabel)
42                 {
43                     instance.textLabel.Text = instance.strText;
44                 }
45             }
46         },
47         defaultValueCreator: (bindable) =>
48         {
49             var instance = (Toast)bindable;
50             return instance.strText;
51         });
52
53         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
54         [EditorBrowsable(EditorBrowsableState.Never)]
55         public static readonly BindableProperty DurationProperty = BindableProperty.Create(nameof(Duration), typeof(uint), typeof(Toast), default(uint), propertyChanged: (bindable, oldValue, newValue) =>
56         {
57             var instance = (Toast)bindable;
58             if (newValue != null)
59             {
60                 instance.toastStyle.Duration = (uint)newValue;
61                 if (instance.timer == null)
62                 {
63                     instance.timer = new Timer(instance.duration);
64                 }
65                 instance.timer.Interval = (uint)newValue;
66             }
67         },
68         defaultValueCreator: (bindable) =>
69         {
70             var instance = (Toast)bindable;
71             return instance.toastStyle.Duration ?? instance.duration;
72         });
73
74         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
75         [EditorBrowsable(EditorBrowsableState.Never)]
76         public static Toast FromText(string text, uint duration)
77         {
78             Toast toast = new Toast();
79             toast.Message = text;
80             toast.Duration = duration;
81             return toast;
82         }
83
84         private Window window = null;
85         /// <summary> text labels </summary>
86         protected TextLabel[] textLabels = null;
87         private TextLabel textLabel = null;
88         private string strText = null;
89         private Timer timer = null;
90         private readonly uint duration = 3000;
91         private ToastStyle toastStyle => ViewStyle as ToastStyle;
92
93         /// <summary>
94         /// Return a copied Style instance of Toast
95         /// </summary>
96         /// <remarks>
97         /// It returns copied Style instance and changing it does not effect to the Toast.
98         /// Style setting is possible by using constructor or the function of ApplyStyle(ViewStyle viewStyle)
99         /// </remarks>
100         [EditorBrowsable(EditorBrowsableState.Never)]
101         public new ToastStyle Style
102         {
103             get
104             {
105                 var result = new ToastStyle(toastStyle);
106                 result.CopyPropertiesFromView(this);
107                 result.Text.CopyPropertiesFromView(textLabel);
108                 return result;
109             }
110         }
111         static Toast() { }
112
113         /// <summary>
114         /// Construct Toast with null.
115         /// </summary>
116         /// <since_tizen> 6 </since_tizen>
117         [Obsolete("Deprecated in API8; Will be removed in API10")]
118         public Toast() : base()
119         {
120             Initialize();
121         }
122
123         /// <summary>
124         /// The constructor of the Toast class with specific Style.
125         /// </summary>
126         /// <param name="toastStyle">Construct Style</param>
127         [EditorBrowsable(EditorBrowsableState.Never)]
128         public Toast(ToastStyle toastStyle) : base(toastStyle)
129         {
130             Initialize();
131         }
132
133         /// <summary>
134         /// Constructor of the Toast class with special style.
135         /// </summary>
136         /// <param name="style"> style name </param>
137         [EditorBrowsable(EditorBrowsableState.Never)]
138         public Toast(string style) : base(style)
139         {
140             Initialize();
141         }
142
143         /// <summary>
144         /// Gets or sets the text array of toast.
145         /// </summary>
146         /// <since_tizen> 6 </since_tizen>
147         [Obsolete("Deprecated in API8; Will be removed in API10")]
148         public string[] TextArray
149         {
150             get;
151             set;
152         }
153
154         /// <summary>
155         /// Gets or sets text point size in toast.
156         /// </summary>
157         /// <since_tizen> 6 </since_tizen>
158         [Obsolete("Deprecated in API8; Will be removed in API10")]
159         public float PointSize
160         {
161             get
162             {
163                 return (float)textLabel?.PointSize;
164             }
165             set
166             {
167                 if (null != textLabel)
168                 {
169                     textLabel.PointSize = value;
170                 }
171             }
172         }
173
174         /// <summary>
175         /// Gets or sets text font family in toast.
176         /// </summary>
177         /// <since_tizen> 6 </since_tizen>
178         [Obsolete("Deprecated in API8; Will be removed in API10")]
179         public string FontFamily
180         {
181             get
182             {
183                 return textLabel?.FontFamily;
184             }
185             set
186             {
187                 if (null != textLabel)
188                 {
189                     textLabel.FontFamily = value;
190                 }
191             }
192         }
193
194         /// <summary>
195         /// Gets or sets text color in toast.
196         /// </summary>
197         /// <since_tizen> 6 </since_tizen>
198         [Obsolete("Deprecated in API8; Will be removed in API10")]
199         public Color TextColor
200         {
201             get
202             {
203                 return textLabel?.TextColor;
204             }
205             set
206             {
207                 if (null != textLabel)
208                 {
209                     textLabel.TextColor = value;
210                 }
211             }
212         }
213
214         /// <summary>
215         /// Gets or sets text horizontal alignment in toast.
216         /// </summary>
217         /// <since_tizen> 6 </since_tizen>
218         [Obsolete("Deprecated in API8; Will be removed in API10")]
219         public HorizontalAlignment TextAlignment
220         {
221             get
222             {
223                 return textLabel?.HorizontalAlignment ?? HorizontalAlignment.Center;
224             }
225             set
226             {
227                 if (null != textLabel)
228                 {
229                     textLabel.HorizontalAlignment = value;
230                 }
231             }
232         }
233
234         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
235         [EditorBrowsable(EditorBrowsableState.Never)]
236         public void Post(Window win)
237         {
238             window = win;
239             window.Add(this);
240             this.Position.X = (window.Size.Width - this.Size.Width) / 2;
241             this.Position.Y = (window.Size.Height - this.Size.Height) / 2;
242             timer.Start();
243         }
244
245         /// <summary>
246         /// Gets or sets the text toast.
247         /// </summary>
248         /// <since_tizen> 6 </since_tizen>
249         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
250         [EditorBrowsable(EditorBrowsableState.Never)]
251         public string Message
252         {
253             get
254             {
255                 return (string)GetValue(MessageProperty);
256             }
257             set
258             {
259                 SetValue(MessageProperty, value);
260             }
261         }
262
263         /// <summary>
264         /// Gets or sets text padding in toast.
265         /// </summary>
266         /// <since_tizen> 6 </since_tizen>
267         [Obsolete("Deprecated in API8; Will be removed in API10")]
268         public Extents TextPadding
269         {
270             get
271             {
272                 return textLabel?.Padding;
273             }
274             set
275             {
276                 if (null != value && null != textLabel)
277                 {
278                     textLabel.Padding.CopyFrom(value);
279                 }
280             }
281         }
282
283         /// <summary>
284         /// Gets or sets text line height in toast.
285         /// </summary>
286         /// <since_tizen> 6 </since_tizen>
287         [Obsolete("Deprecated in API8; Will be removed in API10")]
288         public uint TextLineHeight { get; set; }
289
290         /// <summary>
291         /// Gets or sets text line space in toast.
292         /// </summary>
293         /// <since_tizen> 6 </since_tizen>
294         [Obsolete("Deprecated in API8; Will be removed in API10")]
295         public uint TextLineSpace { get; set; }
296
297         /// <summary>
298         /// Gets or sets duration of toast.
299         /// </summary>
300         /// <since_tizen> 6 </since_tizen>
301         [Obsolete("Deprecated in API8; Will be removed in API10")]
302         public uint Duration
303         {
304             get
305             {
306                 return (uint)GetValue(DurationProperty);
307             }
308             set
309             {
310                 SetValue(DurationProperty, value);
311             }
312         }
313
314         /// <summary>
315         /// Apply style to toast.
316         /// </summary>
317         /// <param name="viewStyle">The style to apply.</param>
318         [EditorBrowsable(EditorBrowsableState.Never)]
319         public override void ApplyStyle(ViewStyle viewStyle)
320         {
321             WidthResizePolicy = ResizePolicyType.FitToChildren;
322             HeightResizePolicy = ResizePolicyType.FitToChildren;
323
324             base.ApplyStyle(viewStyle);
325
326             ToastStyle toastStyle = viewStyle as ToastStyle;
327
328             if (null != toastStyle)
329             {
330                 if (null == textLabel)
331                 {
332                     textLabel = new TextLabel()
333                     {
334                         PositionUsesPivotPoint = true,
335                         ParentOrigin = Tizen.NUI.ParentOrigin.Center,
336                         PivotPoint = Tizen.NUI.PivotPoint.Center,
337                         WidthResizePolicy = ResizePolicyType.UseNaturalSize,
338                         HeightResizePolicy = ResizePolicyType.UseNaturalSize,
339                         HorizontalAlignment = HorizontalAlignment.Center,
340                         VerticalAlignment = VerticalAlignment.Center,
341                         TextColor = Color.White,
342                     };
343                     this.Add(textLabel);
344                 }
345                 textLabel.ApplyStyle(toastStyle.Text);
346             }
347         }
348
349         /// <summary>
350         /// Dispose ToastPopup.
351         /// </summary>
352         /// <param name="type">dispose types.</param>
353         /// <since_tizen> 6 </since_tizen>
354         [Obsolete("Deprecated in API8; Will be removed in API10")]
355         protected override void Dispose(DisposeTypes type)
356         {
357             if (disposed)
358             {
359                 return;
360             }
361
362             if (type == DisposeTypes.Explicit)
363             {
364                 this.VisibilityChanged -= OnVisibilityChanged;
365                 if (null != timer)
366                 {
367                     timer.Tick -= OnTick;
368                     timer.Dispose();
369                     timer = null;
370                 }
371
372                 if (null != textLabel)
373                 {
374                     Utility.Dispose(textLabel);
375                 }
376             }
377
378             base.Dispose(type);
379         }
380
381         /// <summary>
382         /// Get Toast style.
383         /// </summary>
384         /// <returns>The default toast style.</returns>
385         [EditorBrowsable(EditorBrowsableState.Never)]
386         protected override ViewStyle CreateViewStyle()
387         {
388             return new ToastStyle();
389         }
390
391         private void Initialize()
392         {
393             if (null == textLabel)
394             {
395                 textLabel = new TextLabel()
396                 {
397                     PositionUsesPivotPoint = true,
398                     ParentOrigin = Tizen.NUI.ParentOrigin.Center,
399                     PivotPoint = Tizen.NUI.PivotPoint.Center,
400                     WidthResizePolicy = ResizePolicyType.UseNaturalSize,
401                     HeightResizePolicy = ResizePolicyType.UseNaturalSize,
402                     HorizontalAlignment = HorizontalAlignment.Center,
403                     VerticalAlignment = VerticalAlignment.Center,
404                     TextColor = Color.White,
405                 };
406                 this.Add(textLabel);
407             }
408
409             this.VisibilityChanged += OnVisibilityChanged;
410             timer = new Timer(toastStyle.Duration ?? duration);
411             timer.Tick += OnTick;
412         }
413
414         private bool OnTick(object sender, EventArgs e)
415         {
416             Hide();
417             return false;
418         }
419
420         private void OnVisibilityChanged(object sender, VisibilityChangedEventArgs e)
421         {
422             if (true == e.Visibility)
423             {
424                 window?.Add(this);
425                 timer.Start();
426             }
427             else
428             {
429                 window?.Remove(this);
430             }
431         }
432     }
433 }