2 * Copyright(c) 2019 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using Tizen.NUI.BaseComponents;
19 using System.ComponentModel;
20 using Tizen.NUI.Binding;
22 namespace Tizen.NUI.Components
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.
29 /// <since_tizen> 6 </since_tizen>
30 public class Toast : Control
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) =>
36 var instance = (Toast)bindable;
39 instance.strText = (string)(newValue);
40 instance.Style.Text.Text = instance.strText;
43 defaultValueCreator: (bindable) =>
45 var instance = (Toast)bindable;
46 return instance.strText;
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) =>
53 var instance = (Toast)bindable;
56 instance.Style.Duration = (uint)newValue;
57 instance.timer.Interval = (uint)newValue;
60 defaultValueCreator: (bindable) =>
62 var instance = (Toast)bindable;
63 return instance.Style.Duration ?? instance.duration;
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)
70 Toast toast = new Toast();
72 toast.Duration = duration;
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;
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;
90 /// Construct Toast with null.
92 /// <since_tizen> 6 </since_tizen>
93 public Toast() : base()
99 /// The constructor of the Toast class with specific Style.
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)
111 /// Constructor of the Toast class with special style.
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)
123 /// Gets or sets the text array of toast.
125 /// <since_tizen> 6 </since_tizen>
126 public string[] TextArray
133 /// Gets or sets text point size in toast.
135 /// <since_tizen> 6 </since_tizen>
136 public float PointSize
140 return (float)Style?.Text?.PointSize?.All;
144 if (null != Style?.Text)
146 Style.Text.PointSize = value;
152 /// Gets or sets text font family in toast.
154 /// <since_tizen> 6 </since_tizen>
155 public string FontFamily
159 return Style?.Text?.FontFamily?.All;
163 if (null != Style?.Text)
165 Style.Text.FontFamily = value;
171 /// Gets or sets text color in toast.
173 /// <since_tizen> 6 </since_tizen>
174 public Color TextColor
178 return Style?.Text?.TextColor?.All;
182 if (null != Style?.Text)
184 Style.Text.TextColor = value;
190 /// Gets or sets text horizontal alignment in toast.
192 /// <since_tizen> 6 </since_tizen>
193 public HorizontalAlignment TextAlignment
197 return Style?.Text?.HorizontalAlignment ?? HorizontalAlignment.Center;
201 if (null != Style?.Text)
203 Style.Text.HorizontalAlignment = value;
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)
214 this.Position.X = (window.Size.Width - this.Size.Width) / 2;
215 this.Position.Y = (window.Size.Height - this.Size.Height) / 2;
220 /// Gets or sets the text toast.
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
229 return (string)GetValue(MessageProperty);
233 SetValue(MessageProperty, value);
238 /// Gets or sets text padding in toast.
240 /// <since_tizen> 6 </since_tizen>
241 public Extents TextPadding
245 return Style.Text.Padding;
249 if (null != value && null != Style.Text)
251 Style.Text.Padding.CopyFrom(value);
257 /// Gets or sets text line height in toast.
259 /// <since_tizen> 6 </since_tizen>
260 public uint TextLineHeight { get; set; }
263 /// Gets or sets text line space in toast.
265 /// <since_tizen> 6 </since_tizen>
266 public uint TextLineSpace { get; set; }
269 /// Gets or sets duration of toast.
271 /// <since_tizen> 6 </since_tizen>
276 return (uint)GetValue(DurationProperty);
280 SetValue(DurationProperty, value);
287 [EditorBrowsable(EditorBrowsableState.Never)]
288 public override void ApplyStyle(ViewStyle viewStyle)
290 base.ApplyStyle(viewStyle);
292 ToastStyle toastStyle = viewStyle as ToastStyle;
294 if (null != toastStyle)
296 if (null == textLabel)
298 textLabel = new TextLabel();
301 textLabel.ApplyStyle(toastStyle.Text);
306 /// Dispose ToastPopup.
308 /// <param name="type">dispose types.</param>
309 /// <since_tizen> 6 </since_tizen>
310 protected override void Dispose(DisposeTypes type)
317 if (type == DisposeTypes.Explicit)
319 this.VisibilityChanged -= OnVisibilityChanged;
322 timer.Tick -= OnTick;
327 if (null != textLabel)
329 Utility.Dispose(textLabel);
337 /// Get Toast attribues.
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()
344 return new ToastStyle();
347 private void Initialize()
349 if (null == textLabel)
351 textLabel = new TextLabel();
355 this.VisibilityChanged += OnVisibilityChanged;
356 timer = new Timer(Style.Duration ?? duration);
357 timer.Tick += OnTick;
360 private bool OnTick(object sender, EventArgs e)
366 private void OnVisibilityChanged(object sender, VisibilityChangedEventArgs e)
368 if (true == e.Visibility)
375 window?.Remove(this);