[NUI.Components] Fix BackgroundImage doesn't works issue (#1240)
[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     public class Toast : Control
31     {
32         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
33         [EditorBrowsable(EditorBrowsableState.Never)]
34         public static readonly BindableProperty MessageProperty = BindableProperty.Create(nameof(Message), typeof(string), typeof(Toast), string.Empty, propertyChanged: (bindable, oldValue, newValue) =>
35         {
36             var instance = (Toast)bindable;
37             if (newValue != null)
38             {
39                 instance.strText = (string)(newValue);
40                 instance.Style.Text.Text = instance.strText;
41             }
42         },
43         defaultValueCreator: (bindable) =>
44         {
45             var instance = (Toast)bindable;
46             return instance.strText;
47         });
48
49         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
50         [EditorBrowsable(EditorBrowsableState.Never)]
51         public static readonly BindableProperty DurationProperty = BindableProperty.Create(nameof(Duration), typeof(uint), typeof(Toast), default(uint), propertyChanged: (bindable, oldValue, newValue) =>
52         {
53             var instance = (Toast)bindable;
54             if (newValue != null)
55             {
56                 instance.Style.Duration = (uint)newValue;
57                 instance.timer.Interval = (uint)newValue;
58             }
59         },
60         defaultValueCreator: (bindable) =>
61         {
62             var instance = (Toast)bindable;
63             return instance.Style.Duration ?? instance.duration;
64         });
65
66         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
67         [EditorBrowsable(EditorBrowsableState.Never)]
68         public static Toast FromText(string text, uint duration) 
69         {
70             Toast toast = new Toast();
71             toast.Message = text;
72             toast.Duration = duration;
73             return toast;
74         }
75
76         private Window window = null;
77         /// <summary> text labels </summary>
78         protected TextLabel[] textLabels = null;
79         private TextLabel textLabel = null;
80         private string strText = null;
81         private Timer timer = null;
82         private readonly uint duration = 3000;
83
84         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
85         [EditorBrowsable(EditorBrowsableState.Never)]
86         public new ToastStyle Style => ViewStyle as ToastStyle;
87         static Toast() { }
88
89         /// <summary>
90         /// Construct Toast with null.
91         /// </summary>
92         /// <since_tizen> 6 </since_tizen>
93         public Toast() : base()
94         {
95             Initialize();
96         }
97
98         /// <summary>
99         /// The constructor of the Toast class with specific Style.
100         /// </summary>
101         /// <param name="style">Construct Style</param>
102         /// <since_tizen> 6 </since_tizen>
103         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
104         [EditorBrowsable(EditorBrowsableState.Never)]
105         public Toast(ToastStyle style) : base(style)
106         {
107             Initialize();
108         }
109
110         /// <summary>
111         /// Constructor of the Toast class with special style.
112         /// </summary>
113         /// <param name="style"> style name </param>
114         /// <since_tizen> 6 </since_tizen>
115         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
116         [EditorBrowsable(EditorBrowsableState.Never)]
117         public Toast(string style) : base(style)
118         {
119             Initialize();
120         }
121
122         /// <summary>
123         /// Gets or sets the text array of toast.
124         /// </summary>
125         /// <since_tizen> 6 </since_tizen>
126         public string[] TextArray
127         {
128             get;
129             set;
130         }
131
132         /// <summary>
133         /// Gets or sets text point size in toast.
134         /// </summary>
135         /// <since_tizen> 6 </since_tizen>
136         public float PointSize
137         {
138             get
139             {
140                 return (float)Style?.Text?.PointSize?.All;
141             }
142             set
143             {
144                 if (null != Style?.Text)
145                 {
146                     Style.Text.PointSize = value;
147                 }
148             }
149         }
150
151         /// <summary>
152         /// Gets or sets text font family in toast.
153         /// </summary>
154         /// <since_tizen> 6 </since_tizen>
155         public string FontFamily
156         {
157             get
158             {
159                 return Style?.Text?.FontFamily?.All;
160             }
161             set
162             {
163                 if (null != Style?.Text)
164                 {
165                     Style.Text.FontFamily = value;
166                 }
167             }
168         }
169
170         /// <summary>
171         /// Gets or sets text color in toast.
172         /// </summary>
173         /// <since_tizen> 6 </since_tizen>
174         public Color TextColor
175         {
176             get
177             {
178                 return Style?.Text?.TextColor?.All;
179             }
180             set
181             {
182                 if (null != Style?.Text)
183                 {
184                     Style.Text.TextColor = value;
185                 }
186             }
187         }
188
189         /// <summary>
190         /// Gets or sets text horizontal alignment in toast.
191         /// </summary>
192         /// <since_tizen> 6 </since_tizen>
193         public HorizontalAlignment TextAlignment
194         {
195             get
196             {
197                 return Style?.Text?.HorizontalAlignment ?? HorizontalAlignment.Center;
198             }
199             set
200             {
201                 if (null != Style?.Text)
202                 {
203                     Style.Text.HorizontalAlignment = value;
204                 }
205             }
206         }
207
208         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
209         [EditorBrowsable(EditorBrowsableState.Never)]
210         public void Post(Window win)
211         {
212             window = win;
213             window.Add(this);
214             this.Position.X = (window.Size.Width - this.Size.Width) / 2;
215             this.Position.Y = (window.Size.Height - this.Size.Height) / 2;
216             timer.Start();
217         }
218
219         /// <summary>
220         /// Gets or sets the text toast.
221         /// </summary>
222         /// <since_tizen> 6 </since_tizen>
223         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
224         [EditorBrowsable(EditorBrowsableState.Never)]
225         public string Message
226         {
227             get
228             {
229                 return (string)GetValue(MessageProperty);
230             }
231             set
232             {
233                 SetValue(MessageProperty, value);
234             }
235         }
236
237         /// <summary>
238         /// Gets or sets text padding in toast.
239         /// </summary>
240         /// <since_tizen> 6 </since_tizen>
241         public Extents TextPadding
242         {
243             get
244             {
245                 return Style.Text.Padding;
246             }
247             set
248             {
249                 if (null != value && null != Style.Text)
250                 {
251                     Style.Text.Padding.CopyFrom(value);
252                 }
253             }
254         }
255
256         /// <summary>
257         /// Gets or sets text line height in toast.
258         /// </summary>
259         /// <since_tizen> 6 </since_tizen>
260         public uint TextLineHeight { get; set; }
261
262         /// <summary>
263         /// Gets or sets text line space in toast.
264         /// </summary>
265         /// <since_tizen> 6 </since_tizen>
266         public uint TextLineSpace { get; set; }
267
268         /// <summary>
269         /// Gets or sets duration of toast.
270         /// </summary>
271         /// <since_tizen> 6 </since_tizen>
272         public uint Duration
273         {
274             get
275             {
276                 return (uint)GetValue(DurationProperty);
277             }
278             set
279             {
280                 SetValue(DurationProperty, value);
281             }
282         }
283
284         /// <summary>
285         /// Apply style.
286         /// </summary>
287         [EditorBrowsable(EditorBrowsableState.Never)]
288         public override void ApplyStyle(ViewStyle viewStyle)
289         {
290             base.ApplyStyle(viewStyle);
291
292             ToastStyle toastStyle = viewStyle as ToastStyle;
293
294             if (null != toastStyle)
295             {
296                 if (null == textLabel)
297                 {
298                     textLabel = new TextLabel();
299                     this.Add(textLabel);
300                 }
301                 textLabel.ApplyStyle(toastStyle.Text);
302             }
303         }
304
305         /// <summary>
306         /// Dispose ToastPopup.
307         /// </summary>
308         /// <param name="type">dispose types.</param>
309         /// <since_tizen> 6 </since_tizen>
310         protected override void Dispose(DisposeTypes type)
311         {
312             if (disposed)
313             {
314                 return;
315             }
316
317             if (type == DisposeTypes.Explicit)
318             {
319                 this.VisibilityChanged -= OnVisibilityChanged;
320                 if (null != timer)
321                 {
322                     timer.Tick -= OnTick;
323                     timer.Dispose();
324                     timer = null;
325                 }
326
327                 if (null != textLabel)
328                 {
329                     Utility.Dispose(textLabel);
330                 }
331             }
332
333             base.Dispose(type);
334         }
335
336         /// <summary>
337         /// Get Toast attribues.
338         /// </summary>
339         /// <since_tizen> 6 </since_tizen>
340         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
341         [EditorBrowsable(EditorBrowsableState.Never)]
342         protected override ViewStyle GetViewStyle()
343         {
344             return new ToastStyle();
345         }
346
347         private void Initialize()
348         {
349             if (null == textLabel)
350             {
351                 textLabel = new TextLabel();
352                 this.Add(textLabel);
353             }
354
355             this.VisibilityChanged += OnVisibilityChanged;
356             timer = new Timer(Style.Duration ?? duration);
357             timer.Tick += OnTick;
358         }
359
360         private bool OnTick(object sender, EventArgs e)
361         {
362             Hide();
363             return false;
364         }
365
366         private void OnVisibilityChanged(object sender, VisibilityChangedEventArgs e)
367         {
368             if (true == e.Visibility)
369             {
370                 window?.Add(this);
371                 timer.Start();
372             }
373             else
374             {
375                 window?.Remove(this);
376             }
377         }
378     }
379 }