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;
21 namespace Tizen.NUI.CommonUI
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.
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
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;
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;
53 /// Construct Toast with null.
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()
64 /// The constructor of the Toast class with specific Attributes.
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)
76 /// Constructor of the Toast class with special style.
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)
88 /// Gets or sets the text array of toast.
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
111 /// Gets or sets text point size in toast.
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
120 return toastAttributes.TextAttributes?.PointSize?.All ?? textPointSize;
124 CreateTextAttributes();
125 if (null == toastAttributes.TextAttributes.PointSize)
127 toastAttributes.TextAttributes.PointSize = new FloatSelector();
129 toastAttributes.TextAttributes.PointSize.All = value;
135 /// Gets or sets text font family in toast.
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
144 return toastAttributes.TextAttributes?.FontFamily;
148 CreateTextAttributes();
149 toastAttributes.TextAttributes.FontFamily = value;
155 /// Gets or sets text color in toast.
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
164 return toastAttributes.TextAttributes?.TextColor?.All;
168 CreateTextAttributes();
169 if (null == toastAttributes.TextAttributes.TextColor)
171 toastAttributes.TextAttributes.TextColor = new ColorSelector();
173 toastAttributes.TextAttributes.TextColor.All = value;
179 /// Gets or sets text horizontal alignment in toast.
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
188 return toastAttributes.TextAttributes?.HorizontalAlignment ?? HorizontalAlignment.Center;
192 CreateTextAttributes();
193 toastAttributes.TextAttributes.HorizontalAlignment = value;
199 /// Gets or sets background image resource of toast.
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
208 return toastAttributes.BackgroundImageAttributes?.ResourceURL?.All;
214 CreateBackgroundAttributes();
215 if (toastAttributes.BackgroundImageAttributes != null)
217 if (null == toastAttributes.BackgroundImageAttributes?.ResourceURL)
219 toastAttributes.BackgroundImageAttributes.ResourceURL = new StringSelector();
222 toastAttributes.BackgroundImageAttributes.ResourceURL.All = value;
223 SetToastBackground();
230 /// Gets or sets background image's border of toast.
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
239 return toastAttributes.BackgroundImageAttributes?.Border?.All;
245 CreateBackgroundAttributes();
246 if (toastAttributes.BackgroundImageAttributes != null)
248 if (null == toastAttributes.BackgroundImageAttributes.Border)
250 toastAttributes.BackgroundImageAttributes.Border = new RectangleSelector();
252 toastAttributes.BackgroundImageAttributes.Border.All = value;
253 SetToastBackground();
260 /// Gets or sets text left padding in toast.
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
269 return toastAttributes.TextAttributes?.PaddingLeft ?? textPaddingLeft;
273 CreateTextAttributes();
274 toastAttributes.TextAttributes.PaddingLeft = value;
280 /// Gets or sets text right padding in toast.
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
289 return toastAttributes.TextAttributes?.PaddingRight ?? textPaddingRight;
293 CreateTextAttributes();
294 toastAttributes.TextAttributes.PaddingRight = value;
300 /// Gets or sets text top padding in toast.
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
309 return toastAttributes.TextAttributes?.PaddingTop ?? textPaddingTop;
313 CreateTextAttributes();
314 toastAttributes.TextAttributes.PaddingTop = value;
320 /// Gets or sets text bottom padding in toast.
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
329 return toastAttributes.TextAttributes?.PaddingBottom ?? textPaddingBottom;
333 CreateTextAttributes();
334 toastAttributes.TextAttributes.PaddingBottom = value;
340 /// Gets or sets text line height in toast.
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
349 return toastAttributes.TextLineHeight ?? textLineHeight;
353 toastAttributes.TextLineHeight = value;
359 /// Gets or sets text line space in toast.
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
368 return toastAttributes.TextLineSpace ?? textLineSpace;
372 toastAttributes.TextLineSpace = value;
378 /// Gets or sets duration of toast.
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)]
387 return toastAttributes.Duration ?? duration;
391 toastAttributes.Duration = value;
392 timer.Interval = value;
397 /// Dispose ToastPopup.
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)
410 if (type == DisposeTypes.Explicit)
412 this.VisibilityChanged -= OnVisibilityChanged;
415 timer.Tick -= OnTick;
419 if (null != textLabels)
421 for (int i=0; i<textLabels.Length; i++)
423 Utility.Dispose(textLabels[i]);
432 /// Relayout control's elements
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()
439 if (null == toastAttributes)
443 if (null != toastAttributes.TextAttributes)
445 for (int i = 0; i < textLabels.Length; i++)
447 ApplyAttributes(textLabels[i], toastAttributes.TextAttributes);
454 /// LayoutChild include textLabel.
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()
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;
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);
472 _textAreaWidth = _textAreaWidth > maxTextAreaWidth ? maxTextAreaWidth : _textAreaWidth;
473 if (LayoutDirection == ViewLayoutDirectionType.LTR)
475 for (int i = 0; i < textLabels?.Length; i++)
477 textLabels[i].Position2D = new Position2D(_textPaddingLeft, _textPaddingTop + _positionY);
478 textLabels[i].Size2D = new Size2D(_textAreaWidth, _textLineHeight);
479 _positionY += _textLineHeight + _textLineSpace;
484 for (int i = 0; i < textLabels?.Length; i++)
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;
497 /// Get Toast attribues.
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()
504 return new ToastAttributes();
507 private void Initialize()
509 toastAttributes = attributes as ToastAttributes;
510 if (null == toastAttributes)
512 throw new Exception("Toast attribute parse error.");
514 ApplyAttributes(this, toastAttributes);
516 toastBackground = new NPatchVisual();
517 SetToastBackground();
519 this.VisibilityChanged += OnVisibilityChanged;
520 timer = new Timer(toastAttributes.Duration ?? duration);
521 timer.Tick += OnTick;
525 private bool OnTick(object sender, EventArgs e)
531 private void OnVisibilityChanged(object sender, VisibilityChangedEventArgs e)
533 if (true == e.Visibility)
539 private void SetToastText()
541 if (textLabels != null)
543 for (int i = 0; i < textLabels?.Length; i++)
545 if (null != textLabels[i])
547 this.Remove(textLabels[i]);
548 textLabels[i].Dispose();
549 textLabels[i] = null;
554 textLabels = new TextLabel[textArray.Length];
555 if (textLabels != null)
557 for (int i = 0; i < textArray.Length; i++)
559 textLabels[i] = new TextLabel();
560 textLabels[i].Text = textArray[i];
561 textLabels[i].BackgroundColor = Color.Blue;
562 this.Add(textLabels[i]);
567 private void SetToastBackground()
569 if (null != toastAttributes?.BackgroundImageAttributes?.ResourceURL)
571 toastBackground.URL = toastAttributes.BackgroundImageAttributes.ResourceURL.All;
573 if (null != toastAttributes?.BackgroundImageAttributes?.Border)
575 toastBackground.Border = toastAttributes.BackgroundImageAttributes.Border.All;
577 this.Background = toastBackground.OutputVisualMap;
580 private void CreateBackgroundAttributes()
582 if (null == toastAttributes.BackgroundImageAttributes)
584 toastAttributes.BackgroundImageAttributes = new ImageAttributes();
588 private void CreateTextAttributes()
590 if (null == toastAttributes.TextAttributes)
592 toastAttributes.TextAttributes = new TextAttributes();