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 System.Collections.Generic;
19 using Tizen.NUI.BaseComponents;
20 using Tizen.NUI.Binding;
21 using System.ComponentModel;
23 namespace Tizen.NUI.Components
26 /// Popup is one kind of common component, it can be used as popup window.
27 /// User can handle Popup button count, head title and content area.
29 /// <since_tizen> 6 </since_tizen>
30 public class Popup : 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 ButtonHeightProperty = BindableProperty.Create(nameof(ButtonHeight), typeof(int), typeof(Popup), default(int), propertyChanged: (bindable, oldValue, newValue) =>
36 var instance = (Popup)bindable;
37 if (newValue != null && instance?.Style?.Buttons?.Size != null )
39 instance.Style.Buttons.Size.Height = (int)newValue;
40 instance.btGroup.Itemheight = (int)newValue;
41 instance.UpdateView();
44 defaultValueCreator: (bindable) =>
46 var instance = (Popup)bindable;
47 return (int)(instance.Style?.Buttons?.Size?.Height ?? 0);
50 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
51 [EditorBrowsable(EditorBrowsableState.Never)]
52 public static readonly BindableProperty ButtonTextPointSizeProperty = BindableProperty.Create(nameof(ButtonTextPointSize), typeof(float), typeof(Popup), default(float), propertyChanged: (bindable, oldValue, newValue) =>
54 var instance = (Popup)bindable;
57 if (instance.Style?.Buttons?.Text != null)
59 instance.Style.Buttons.Text.PointSize = (float)newValue;
61 instance.btGroup.ItemPointSize = (float)newValue;
64 defaultValueCreator: (bindable) =>
66 var instance = (Popup)bindable;
67 return instance.Style?.Buttons?.Text?.PointSize?.All ?? 0;
70 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
71 [EditorBrowsable(EditorBrowsableState.Never)]
72 public static readonly BindableProperty ButtonFontFamilyProperty = BindableProperty.Create(nameof(ButtonFontFamily), typeof(string), typeof(Popup), string.Empty, propertyChanged: (bindable, oldValue, newValue) =>
74 var instance = (Popup)bindable;
77 instance.Style.Buttons.Text.FontFamily = (string)newValue;
78 instance.btGroup.ItemFontFamily = (string)newValue;
81 defaultValueCreator: (bindable) =>
83 var instance = (Popup)bindable;
84 return instance.Style?.Buttons?.Text?.FontFamily.All;
87 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
88 [EditorBrowsable(EditorBrowsableState.Never)]
89 public static readonly BindableProperty ButtonTextColorProperty = BindableProperty.Create(nameof(ButtonTextColor), typeof(Color), typeof(Popup), Color.Transparent, propertyChanged: (bindable, oldValue, newValue) =>
91 var instance = (Popup)bindable;
94 if (instance.Style?.Buttons?.Text != null)
96 instance.Style.Buttons.Text.TextColor = (Color)newValue;
98 instance.btGroup.ItemTextColor = (Color)newValue;
101 defaultValueCreator: (bindable) =>
103 var instance = (Popup)bindable;
104 return instance.Style?.Buttons?.Text?.TextColor?.All;
107 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
108 [EditorBrowsable(EditorBrowsableState.Never)]
109 public static readonly BindableProperty ButtonOverLayBackgroundColorSelectorProperty = BindableProperty.Create(nameof(ButtonOverLayBackgroundColorSelector), typeof(Selector<Color>), typeof(Popup), new Selector<Color>(), propertyChanged: (bindable, oldValue, newValue) =>
111 var instance = (Popup)bindable;
112 if (newValue != null)
114 instance.Style.Buttons.Overlay.BackgroundColor = (Selector<Color>)newValue;
115 instance.btGroup.OverLayBackgroundColorSelector = (Selector<Color>)newValue;
118 defaultValueCreator: (bindable) =>
120 var instance = (Popup)bindable;
121 return instance.Style?.Buttons?.Overlay?.BackgroundColor;
124 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
125 [EditorBrowsable(EditorBrowsableState.Never)]
126 public static readonly BindableProperty ButtonTextAlignmentProperty = BindableProperty.Create(nameof(ButtonTextAlignment), typeof(HorizontalAlignment), typeof(Popup), new HorizontalAlignment(), propertyChanged: (bindable, oldValue, newValue) =>
128 var instance = (Popup)bindable;
129 if (newValue != null)
131 instance.Style.Buttons.Text.HorizontalAlignment = (HorizontalAlignment)newValue;
132 instance.btGroup.ItemTextAlignment = (HorizontalAlignment)newValue;
135 defaultValueCreator: (bindable) =>
137 var instance = (Popup)bindable;
138 return instance.Style?.Buttons?.Text?.HorizontalAlignment ?? HorizontalAlignment.Center;
141 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
142 [EditorBrowsable(EditorBrowsableState.Never)]
143 public static readonly BindableProperty ButtonBackgroundProperty = BindableProperty.Create(nameof(ButtonBackground), typeof(string), typeof(Popup), string.Empty, propertyChanged: (bindable, oldValue, newValue) =>
145 var instance = (Popup)bindable;
146 if (newValue != null)
148 if (instance.Style.Buttons.BackgroundImage == null)
150 instance.Style.Buttons.BackgroundImage = new Selector<string>();
152 instance.btGroup.ItemBackgroundImageUrl = (string)newValue;
153 instance.Style.Buttons.BackgroundImage = (string)newValue;
156 defaultValueCreator: (bindable) =>
158 var instance = (Popup)bindable;
159 return instance.Style?.Buttons?.BackgroundImage?.All;
162 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
163 [EditorBrowsable(EditorBrowsableState.Never)]
164 public static readonly BindableProperty ButtonBackgroundBorderProperty = BindableProperty.Create(nameof(ButtonBackgroundBorder), typeof(Rectangle), typeof(Popup), new Rectangle(0, 0, 0, 0), propertyChanged: (bindable, oldValue, newValue) =>
166 var instance = (Popup)bindable;
167 if (newValue != null)
169 if (instance.Style.Buttons.BackgroundImageBorder == null)
171 instance.Style.Buttons.BackgroundImageBorder = new Selector<Rectangle>();
173 instance.Style.Buttons.BackgroundImageBorder = (Rectangle)newValue;
174 instance.btGroup.ItemBackgroundBorder = (Rectangle)newValue;
177 defaultValueCreator: (bindable) =>
179 var instance = (Popup)bindable;
180 return instance.Style?.Buttons?.BackgroundImageBorder?.All;
183 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
184 [EditorBrowsable(EditorBrowsableState.Never)]
185 public static readonly BindableProperty ButtonImageShadowProperty = BindableProperty.Create(nameof(ButtonImageShadow), typeof(ImageShadow), typeof(Popup), null, propertyChanged: (bindable, oldValue, newValue) =>
187 var instance = (Popup)bindable;
188 ImageShadow shadow = (ImageShadow)newValue;
189 instance.btGroup.ItemImageShadow = new ImageShadow(shadow);
190 instance.Style.Buttons.ImageShadow = new ImageShadow(shadow);
192 defaultValueCreator: (bindable) =>
194 var instance = (Popup)bindable;
195 return instance.Style?.Buttons?.ImageShadow?.All;
199 private TextLabel titleText;
200 private ButtonGroup btGroup = null;
201 private Window window = null;
202 private Layer container = new Layer();
206 /// Creates a new instance of a Popup.
208 /// <since_tizen> 6 </since_tizen>
209 public Popup() : base()
215 /// Creates a new instance of a Popup with style.
217 /// <param name="style">Create Popup by special style defined in UX.</param>
218 /// <since_tizen> 8 </since_tizen>
219 public Popup(string style) : base(style)
225 /// Creates a new instance of a Popup with style.
227 /// <param name="popupStyle">Create Popup by style customized by user.</param>
228 /// <since_tizen> 8 </since_tizen>
229 public Popup(PopupStyle popupStyle) : base(popupStyle)
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 virtual void Post(Window targetWindow)
238 if (targetWindow == null)
243 window = targetWindow;
244 window.AddLayer(container);
245 container.RaiseToTop();
249 /// Dismiss the dialog
251 [EditorBrowsable(EditorBrowsableState.Never)]
252 public virtual void Dismiss()
259 window.RemoveLayer(container);
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 void AddButton(string buttonText)
267 if (Style.Buttons != null)
269 Button btn = new Button(Style.Buttons);
270 btn.Style.Text.Text = buttonText;
271 btn.ClickEvent += ButtonClickEvent;
272 btGroup.AddItem(btn);
278 /// Add button by style's name.
280 /// <since_tizen> 8 </since_tizen>
281 public void AddButton(string buttonText, string style)
283 AddButton(buttonText);
287 /// Add button by style.
289 /// <since_tizen> 8 </since_tizen>
290 public void AddButton(string buttonText, ButtonStyle style)
292 if (Style.Buttons != null && style != null)
294 Style.Buttons.CopyFrom(style);
295 AddButton(buttonText);
299 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
300 [EditorBrowsable(EditorBrowsableState.Never)]
301 public Button GetButton(int index)
303 return btGroup.GetItem(index);
306 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
307 [EditorBrowsable(EditorBrowsableState.Never)]
308 public void RemoveButton(int index)
310 btGroup.RemoveItem(index);
313 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
314 [EditorBrowsable(EditorBrowsableState.Never)]
315 public void AddContentText(View childView)
317 if (null != ContentView)
319 ContentView.Add(childView);
325 /// An event for the button clicked signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
327 /// <since_tizen> 6 </since_tizen>
328 public event EventHandler<ButtonClickEventArgs> PopupButtonClickEvent;
331 /// Get style of popup.
333 /// <since_tizen> 8 </since_tizen>
334 public new PopupStyle Style => ViewStyle as PopupStyle;
339 [EditorBrowsable(EditorBrowsableState.Never)]
340 public TextLabel Title
344 if (null == titleText)
346 titleText = new TextLabel();
358 /// Title text string in Popup.
360 /// <since_tizen> 6 </since_tizen>
361 public string TitleText
365 return Style?.Title?.Text?.All;
371 if (Style?.Title != null)
373 Style.Title.Text = value;
380 /// Title text point size in Popup.
382 /// <since_tizen> 6 </since_tizen>
383 public float TitlePointSize
387 return Style?.Title?.PointSize?.All ?? 0;
391 if (Style?.Title != null)
393 Style.Title.PointSize = value;
399 /// Title text color in Popup.
401 /// <since_tizen> 6 </since_tizen>
402 public Color TitleTextColor
406 return Style?.Title?.TextColor?.All;
410 if (Style?.Title != null)
412 Style.Title.TextColor = value;
418 /// Title text horizontal alignment in Popup.
420 /// <since_tizen> 6 </since_tizen>
421 public HorizontalAlignment TitleTextHorizontalAlignment
425 return Style?.Title?.HorizontalAlignment ?? HorizontalAlignment.Center;
429 Style.Title.HorizontalAlignment = value;
434 /// Title text's position in Popup.
436 /// <since_tizen> 6 </since_tizen>
437 public Position TitleTextPosition
441 return Style?.Title?.Position ?? new Position(0, 0, 0);
445 Style.Title.Position = value;
450 /// Title text's height in Popup.
452 /// <since_tizen> 6 </since_tizen>
453 public int TitleHeight
457 return (int)(Style?.Title?.Size?.Height ?? 0);
461 if (Style?.Title?.Size != null)
463 Style.Title.Size.Height = value;
469 /// Content view in Popup, only can be gotten.
471 /// <since_tizen> 6 </since_tizen>
472 public View ContentView
479 /// Button count in Popup.
481 /// <since_tizen> 6 </since_tizen>
482 public int ButtonCount
489 /// Button height in Popup.
491 /// <since_tizen> 6 </since_tizen>
492 public int ButtonHeight
496 return (int)GetValue(ButtonHeightProperty);
500 SetValue(ButtonHeightProperty, value);
505 /// Button text point size in Popup.
507 /// <since_tizen> 6 </since_tizen>
508 public float ButtonTextPointSize
512 return (float)GetValue(ButtonTextPointSizeProperty);
516 SetValue(ButtonTextPointSizeProperty, value);
521 /// Button text font family in Popup.
523 /// <since_tizen> 6 </since_tizen>
524 public string ButtonFontFamily
528 return (string)GetValue(ButtonFontFamilyProperty);
532 SetValue(ButtonFontFamilyProperty, value);
537 /// Button text color in Popup.
539 /// <since_tizen> 6 </since_tizen>
540 public Color ButtonTextColor
544 return (Color)GetValue(ButtonTextColorProperty);
548 SetValue(ButtonTextColorProperty, value);
553 /// Button overlay background color selector in Popup.
555 /// <since_tizen> 6 </since_tizen>
556 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
557 [EditorBrowsable(EditorBrowsableState.Never)]
558 public Selector<Color> ButtonOverLayBackgroundColorSelector
562 return (Selector<Color>)GetValue(ButtonOverLayBackgroundColorSelectorProperty);
566 SetValue(ButtonOverLayBackgroundColorSelectorProperty, value);
571 /// Button text horizontal alignment in Popup.
573 /// <since_tizen> 6 </since_tizen>
574 public HorizontalAlignment ButtonTextAlignment
578 return (HorizontalAlignment)GetValue(ButtonTextAlignmentProperty);
582 SetValue(ButtonTextAlignmentProperty, value);
587 /// Button background image's resource url in Popup.
589 /// <since_tizen> 6 </since_tizen>
590 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
591 [EditorBrowsable(EditorBrowsableState.Never)]
592 public string ButtonBackground
596 return (string)GetValue(ButtonBackgroundProperty);
600 SetValue(ButtonBackgroundProperty, value);
605 /// Button background image's border in Popup.
607 /// <since_tizen> 6 </since_tizen>
608 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
609 [EditorBrowsable(EditorBrowsableState.Never)]
610 public Rectangle ButtonBackgroundBorder
615 return (Rectangle)GetValue(ButtonBackgroundBorderProperty);
619 SetValue(ButtonBackgroundBorderProperty, value);
624 /// Button's image shadow in Popup.
626 /// <since_tizen> 6 </since_tizen>
627 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
628 [EditorBrowsable(EditorBrowsableState.Never)]
629 public ImageShadow ButtonImageShadow
631 get => (ImageShadow)GetValue(ButtonImageShadowProperty);
632 set => SetValue(ButtonImageShadowProperty, value);
637 /// Set button text by index.
639 /// <param name="index">Button index.</param>
640 /// <param name="text">Button text string.</param>
641 /// <since_tizen> 6 </since_tizen>
642 public void SetButtonText(int index, string text)
648 /// Dispose Popup and all children on it.
650 /// <param name="type">Dispose type.</param>
651 /// <since_tizen> 6 </since_tizen>
652 protected override void Dispose(DisposeTypes type)
659 if (type == DisposeTypes.Explicit)
661 if (titleText != null)
667 if (ContentView != null)
670 ContentView.Dispose();
685 /// Focus gained callback.
687 /// <since_tizen> 6 </since_tizen>
688 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
689 [EditorBrowsable(EditorBrowsableState.Never)]
690 public override void OnFocusGained()
692 base.OnFocusGained();
696 /// Focus lost callback.
698 /// <since_tizen> 6 </since_tizen>
699 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
700 [EditorBrowsable(EditorBrowsableState.Never)]
701 public override void OnFocusLost()
707 /// Apply style to popup.
709 /// <param name="viewStyle">The style to apply.</param>
710 /// <since_tizen> 8 </since_tizen>
711 public override void ApplyStyle(ViewStyle viewStyle)
713 base.ApplyStyle(viewStyle);
714 PopupStyle ppStyle = viewStyle as PopupStyle;
717 Title.ApplyStyle(ppStyle.Title);
725 /// <returns>The default popup style.</returns>
726 /// <since_tizen> 8 </since_tizen>
727 protected override ViewStyle CreateViewStyle()
729 return new PopupStyle();
733 /// Theme change callback when theme is changed, this callback will be trigger.
735 /// <param name="sender">The sender</param>
736 /// <param name="e">The event data</param>
737 /// <since_tizen> 8 </since_tizen>
738 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
740 PopupStyle popupStyle = StyleManager.Instance.GetViewStyle(style) as PopupStyle;
741 if (popupStyle != null)
743 string strSaveTitleText = TitleText;
744 Style.CopyFrom(popupStyle);
745 Style.Title.Text = strSaveTitleText;
750 private void Initialize()
753 container.SetTouchConsumed(true);
754 container.SetHoverConsumed(true);
756 LeaveRequired = true;
757 PropertyChanged += PopupStylePropertyChanged;
760 ContentView = new View()
762 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
763 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
764 PositionUsesPivotPoint = true
767 ContentView.RaiseToTop();
770 btGroup = new ButtonGroup(this);
773 private void UpdateView()
775 btGroup.UpdateButton(Style.Buttons);
780 private void ButtonClickEvent(object sender, Button.ClickEventArgs e)
782 if (PopupButtonClickEvent != null && btGroup.Count > 0)
784 Button button = sender as Button;
785 for (int i = 0; i < btGroup.Count; i++)
787 if (button == GetButton(i))
789 ButtonClickEventArgs args = new ButtonClickEventArgs();
790 args.ButtonIndex = i;
791 PopupButtonClickEvent(this, args);
797 private void PopupStylePropertyChanged(object sender, PropertyChangedEventArgs e)
799 if (e.PropertyName.Equals("LayoutDirection"))
801 btGroup.UpdateButton(Style.Buttons);
805 private void UpdateContentView()
811 string strText = Style.Title.Text.All;
812 if (!string.IsNullOrEmpty(strText) && Style.Title.Size != null)
814 titleH = (int)titleText.Size.Height;
817 if (!string.IsNullOrEmpty(strText) && Style.Title.Position != null)
819 titleX = (int)Style.Title.Position.X;
820 titleY = (int)Style.Title.Position.Y;
823 if (btGroup.Count != 0 && Style?.Buttons?.Size != null )
825 buttonH = (int)Style.Buttons.Size.Height;
827 ContentView.Size = new Size(Size.Width - titleX * 2, Size.Height - titleY - titleH - buttonH);
828 ContentView.Position = new Position(titleX, titleY + titleH);
829 ContentView.RaiseToTop();
832 private void UpdateTitle()
834 if (titleText != null && string.IsNullOrEmpty(Style.Title.Text.All) && Style.Title.Size != null)
836 titleText.RaiseToTop();
840 /// ButtonClickEventArgs is a class to record button click event arguments which will sent to user.
842 /// <since_tizen> 6 </since_tizen>
843 public class ButtonClickEventArgs : EventArgs
845 /// <summary> Button index which is clicked in Popup </summary>
846 /// <since_tizen> 6 </since_tizen>
847 public int ButtonIndex;