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;
337 /// Title text string in Popup.
339 /// <since_tizen> 6 </since_tizen>
340 public string TitleText
344 return Style?.Title?.Text?.All;
350 if (Style?.Title != null)
352 Style.Title.Text = value;
359 /// Title text point size in Popup.
361 /// <since_tizen> 6 </since_tizen>
362 public float TitlePointSize
366 return Style?.Title?.PointSize?.All ?? 0;
370 if (Style?.Title != null)
372 Style.Title.PointSize = value;
378 /// Title text color in Popup.
380 /// <since_tizen> 6 </since_tizen>
381 public Color TitleTextColor
385 return Style?.Title?.TextColor?.All;
389 if (Style?.Title != null)
391 Style.Title.TextColor = value;
397 /// Title text horizontal alignment in Popup.
399 /// <since_tizen> 6 </since_tizen>
400 public HorizontalAlignment TitleTextHorizontalAlignment
404 return Style?.Title?.HorizontalAlignment ?? HorizontalAlignment.Center;
408 Style.Title.HorizontalAlignment = value;
413 /// Title text's position in Popup.
415 /// <since_tizen> 6 </since_tizen>
416 public Position TitleTextPosition
420 return Style?.Title?.Position ?? new Position(0, 0, 0);
424 Style.Title.Position = value;
429 /// Title text's height in Popup.
431 /// <since_tizen> 6 </since_tizen>
432 public int TitleHeight
436 return (int)(Style?.Title?.Size?.Height ?? 0);
440 if (Style?.Title?.Size != null)
442 Style.Title.Size.Height = value;
448 /// Content view in Popup, only can be gotten.
450 /// <since_tizen> 6 </since_tizen>
451 public View ContentView
458 /// Button count in Popup.
460 /// <since_tizen> 6 </since_tizen>
461 public int ButtonCount
468 /// Button height in Popup.
470 /// <since_tizen> 6 </since_tizen>
471 public int ButtonHeight
475 return (int)GetValue(ButtonHeightProperty);
479 SetValue(ButtonHeightProperty, value);
484 /// Button text point size in Popup.
486 /// <since_tizen> 6 </since_tizen>
487 public float ButtonTextPointSize
491 return (float)GetValue(ButtonTextPointSizeProperty);
495 SetValue(ButtonTextPointSizeProperty, value);
500 /// Button text font family in Popup.
502 /// <since_tizen> 6 </since_tizen>
503 public string ButtonFontFamily
507 return (string)GetValue(ButtonFontFamilyProperty);
511 SetValue(ButtonFontFamilyProperty, value);
516 /// Button text color in Popup.
518 /// <since_tizen> 6 </since_tizen>
519 public Color ButtonTextColor
523 return (Color)GetValue(ButtonTextColorProperty);
527 SetValue(ButtonTextColorProperty, value);
532 /// Button overlay background color selector in Popup.
534 /// <since_tizen> 6 </since_tizen>
535 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
536 [EditorBrowsable(EditorBrowsableState.Never)]
537 public Selector<Color> ButtonOverLayBackgroundColorSelector
541 return (Selector<Color>)GetValue(ButtonOverLayBackgroundColorSelectorProperty);
545 SetValue(ButtonOverLayBackgroundColorSelectorProperty, value);
550 /// Button text horizontal alignment in Popup.
552 /// <since_tizen> 6 </since_tizen>
553 public HorizontalAlignment ButtonTextAlignment
557 return (HorizontalAlignment)GetValue(ButtonTextAlignmentProperty);
561 SetValue(ButtonTextAlignmentProperty, value);
566 /// Button background image's resource url in Popup.
568 /// <since_tizen> 6 </since_tizen>
569 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
570 [EditorBrowsable(EditorBrowsableState.Never)]
571 public string ButtonBackground
575 return (string)GetValue(ButtonBackgroundProperty);
579 SetValue(ButtonBackgroundProperty, value);
584 /// Button background image's border in Popup.
586 /// <since_tizen> 6 </since_tizen>
587 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
588 [EditorBrowsable(EditorBrowsableState.Never)]
589 public Rectangle ButtonBackgroundBorder
594 return (Rectangle)GetValue(ButtonBackgroundBorderProperty);
598 SetValue(ButtonBackgroundBorderProperty, value);
603 /// Button's image shadow in Popup.
605 /// <since_tizen> 6 </since_tizen>
606 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
607 [EditorBrowsable(EditorBrowsableState.Never)]
608 public ImageShadow ButtonImageShadow
610 get => (ImageShadow)GetValue(ButtonImageShadowProperty);
611 set => SetValue(ButtonImageShadowProperty, value);
616 /// Set button text by index.
618 /// <param name="index">Button index.</param>
619 /// <param name="text">Button text string.</param>
620 /// <since_tizen> 6 </since_tizen>
621 public void SetButtonText(int index, string text)
627 /// Dispose Popup and all children on it.
629 /// <param name="type">Dispose type.</param>
630 /// <since_tizen> 6 </since_tizen>
631 protected override void Dispose(DisposeTypes type)
638 if (type == DisposeTypes.Explicit)
640 if (titleText != null)
646 if (ContentView != null)
649 ContentView.Dispose();
664 /// Focus gained callback.
666 /// <since_tizen> 6 </since_tizen>
667 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
668 [EditorBrowsable(EditorBrowsableState.Never)]
669 public override void OnFocusGained()
671 base.OnFocusGained();
675 /// Focus lost callback.
677 /// <since_tizen> 6 </since_tizen>
678 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
679 [EditorBrowsable(EditorBrowsableState.Never)]
680 public override void OnFocusLost()
686 /// Apply style to popup.
688 /// <param name="viewStyle">The style to apply.</param>
689 /// <since_tizen> 8 </since_tizen>
690 public override void ApplyStyle(ViewStyle viewStyle)
692 base.ApplyStyle(viewStyle);
693 PopupStyle ppStyle = viewStyle as PopupStyle;
696 if (null == titleText)
698 titleText = new TextLabel();
701 titleText.RaiseToTop();
702 titleText.ApplyStyle(ppStyle.Title);
709 /// <returns>The default popup style.</returns>
710 /// <since_tizen> 8 </since_tizen>
711 protected override ViewStyle GetViewStyle()
713 return new PopupStyle();
717 /// Theme change callback when theme is changed, this callback will be trigger.
719 /// <param name="sender">The sender</param>
720 /// <param name="e">The event data</param>
721 /// <since_tizen> 8 </since_tizen>
722 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
724 PopupStyle popupStyle = StyleManager.Instance.GetViewStyle(style) as PopupStyle;
725 if (popupStyle != null)
727 string strSaveTitleText = TitleText;
728 Style.CopyFrom(popupStyle);
729 Style.Title.Text = strSaveTitleText;
734 private void Initialize()
737 container.SetTouchConsumed(true);
738 container.SetHoverConsumed(true);
740 LeaveRequired = true;
741 PropertyChanged += PopupStylePropertyChanged;
744 ContentView = new View()
746 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
747 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
748 PositionUsesPivotPoint = true
751 ContentView.RaiseToTop();
754 if (null == titleText)
756 titleText = new TextLabel();
757 titleText.RaiseToTop();
762 btGroup = new ButtonGroup(this);
765 private void UpdateView()
767 btGroup.UpdateButton(Style.Buttons);
772 private void ButtonClickEvent(object sender, Button.ClickEventArgs e)
774 if (PopupButtonClickEvent != null && btGroup.Count > 0)
776 Button button = sender as Button;
777 for (int i = 0; i < btGroup.Count; i++)
779 if (button == GetButton(i))
781 ButtonClickEventArgs args = new ButtonClickEventArgs();
782 args.ButtonIndex = i;
783 PopupButtonClickEvent(this, args);
789 private void PopupStylePropertyChanged(object sender, PropertyChangedEventArgs e)
791 if (e.PropertyName.Equals("LayoutDirection"))
793 btGroup.UpdateButton(Style.Buttons);
797 private void UpdateContentView()
803 string strText = Style.Title.Text.All;
804 if (!string.IsNullOrEmpty(strText) && Style.Title.Size != null)
806 titleH = (int)titleText.Size.Height;
809 if (!string.IsNullOrEmpty(strText) && Style.Title.Position != null)
811 titleX = (int)Style.Title.Position.X;
812 titleY = (int)Style.Title.Position.Y;
815 if (btGroup.Count != 0 && Style?.Buttons?.Size != null )
817 buttonH = (int)Style.Buttons.Size.Height;
819 ContentView.Size = new Size(Size.Width - titleX * 2, Size.Height - titleY - titleH - buttonH);
820 ContentView.Position = new Position(titleX, titleY + titleH);
821 ContentView.RaiseToTop();
824 private void UpdateTitle()
826 if (titleText != null && string.IsNullOrEmpty(Style.Title.Text.All) && Style.Title.Size != null)
828 titleText.RaiseToTop();
832 /// ButtonClickEventArgs is a class to record button click event arguments which will sent to user.
834 /// <since_tizen> 6 </since_tizen>
835 public class ButtonClickEventArgs : EventArgs
837 /// <summary> Button index which is clicked in Popup </summary>
838 /// <since_tizen> 6 </since_tizen>
839 public int ButtonIndex;