Removing Tizen.Xamarin.Forms.Extensions
[profile/tv/apps/dotnet/mediahub.git] / TVMediaHub / TVMediaHub.Tizen / Extensions / Dialog.cs
1 using System;
2 using Xamarin.Forms;
3
4 namespace Tizen.Xamarin.Forms.Extension
5 {
6     /// <summary>
7     /// The dialog widget displays its content with buttons and title.
8     /// </summary>
9     /// <example>
10     /// <code>
11     /// var dialog = new Dialog();
12     /// dialog.Title = "Dialog"
13     ///
14     /// var positive = new Button()
15     /// {
16     ///     Text = "OK"
17     /// }
18     /// var negative = new Button()
19     /// {
20     ///     Text = "Cancel"
21     /// }
22     /// negative.Clicked += (s,e)=>
23     /// {
24     ///     dialog.Hide();
25     /// }
26     ///
27     /// dialog.Positive = positive;
28     /// dialog.Negative = negative;
29     ///
30     /// var label = new Label()
31     /// {
32     ///     Text = "New Dialog"
33     /// }
34     ///
35     /// dialog.Content = label;
36     ///
37     /// dialog.Show();
38     ///
39     /// </code>
40     /// </example>
41     public class Dialog : BindableObject
42     {
43         /// <summary>
44         /// BindableProperty. Identifies the content bindable property.
45         /// </summary>
46         public static readonly BindableProperty ContentProperty = BindableProperty.Create(nameof(Content), typeof(View), typeof(Dialog), null);
47
48         /// <summary>
49         /// BindableProperty. Identifies the positive bindable property.
50         /// </summary>
51         public static readonly BindableProperty PositiveProperty = BindableProperty.Create(nameof(Positive), typeof(Button), typeof(Dialog), null);
52
53         /// <summary>
54         /// BindableProperty. Identifies the neutral bindable property.
55         /// </summary>
56         public static readonly BindableProperty NeutralProperty = BindableProperty.Create(nameof(Neutral), typeof(Button), typeof(Dialog), null);
57
58         /// <summary>
59         /// BindableProperty. Identifies the negative bindable property.
60         /// </summary>
61         public static readonly BindableProperty NegativeProperty = BindableProperty.Create(nameof(Negative), typeof(Button), typeof(Dialog), null);
62
63         /// <summary>
64         /// BindableProperty. Identifies the title bindable property.
65         /// </summary>
66         public static readonly BindableProperty TitleProperty = BindableProperty.Create(nameof(Title), typeof(string), typeof(Dialog), null);
67
68         /// <summary>
69         /// BindableProperty. Identifies the subtitle bindable property.
70         /// </summary>
71         public static readonly BindableProperty SubtitleProperty = BindableProperty.Create(nameof(Subtitle), typeof(string), typeof(Dialog), null);
72
73         /// <summary>
74         /// BindableProperty. Identifies the HorizontalOption bindable property.
75         /// </summary>
76         public static readonly BindableProperty HorizontalOptionProperty = BindableProperty.Create(nameof(HorizontalOption), typeof(LayoutOptions), typeof(Dialog), LayoutOptions.Center);
77
78         /// <summary>
79         /// BindableProperty. Identifies the VerticalOption bindable property.
80         /// </summary>
81         public static readonly BindableProperty VerticalOptionProperty = BindableProperty.Create(nameof(VerticalOption), typeof(LayoutOptions), typeof(Dialog), LayoutOptions.End);
82
83         IDialog _dialog = null;
84
85         /// <summary>
86         /// Occurs when the dialog is hidden.
87         /// </summary>
88         public event EventHandler Hidden;
89
90         /// <summary>
91         /// Occurs when outside of the dialog is clicked.
92         /// </summary>
93         public event EventHandler OutsideClicked;
94
95         /// <summary>
96         /// Occurs when the dialog is shown on the display.
97         /// </summary>
98         public event EventHandler Shown;
99
100         /// <summary>
101         /// Occurs when the device's back button is pressed.
102         /// </summary>
103         public event EventHandler BackButtonPressed;
104
105         public Dialog()
106         {
107             _dialog = DependencyService.Get<IDialog>(DependencyFetchTarget.NewInstance);
108             if (_dialog == null)
109             {
110                 throw new Exception("Object reference not set to an instance of a Dialog.");
111             }
112
113             _dialog.Hidden += (s, e) =>
114             {
115                 Hidden?.Invoke(this, EventArgs.Empty);
116             };
117
118             _dialog.OutsideClicked += (s, e) =>
119             {
120                 OutsideClicked?.Invoke(this, EventArgs.Empty);
121             };
122
123             _dialog.Shown += (s, e) =>
124             {
125                 Shown?.Invoke(this, EventArgs.Empty);
126             };
127
128             _dialog.BackButtonPressed += (s, e) =>
129             {
130                 BackButtonPressed?.Invoke(this, EventArgs.Empty);
131             };
132
133             SetBinding(ContentProperty, new Binding(nameof(Content), mode: BindingMode.OneWayToSource, source: _dialog));
134             SetBinding(PositiveProperty, new Binding(nameof(Positive), mode: BindingMode.OneWayToSource, source: _dialog));
135             SetBinding(NeutralProperty, new Binding(nameof(Neutral), mode: BindingMode.OneWayToSource, source: _dialog));
136             SetBinding(NegativeProperty, new Binding(nameof(Negative), mode: BindingMode.OneWayToSource, source: _dialog));
137             SetBinding(TitleProperty, new Binding(nameof(Title), mode: BindingMode.OneWayToSource, source: _dialog));
138             SetBinding(SubtitleProperty, new Binding(nameof(Subtitle), mode: BindingMode.OneWayToSource, source: _dialog));
139             SetBinding(HorizontalOptionProperty, new Binding(nameof(HorizontalOption), mode: BindingMode.OneWayToSource, source: _dialog));
140             SetBinding(VerticalOptionProperty, new Binding(nameof(VerticalOption), mode: BindingMode.OneWayToSource, source: _dialog));
141         }
142
143         /// <summary>
144         /// Gets or sets content view of the dialog.
145         /// </summary>
146         public View Content
147         {
148             get { return (View)GetValue(ContentProperty); }
149             set { SetValue(ContentProperty, value); }
150         }
151
152         /// <summary>
153         /// Gets or sets positive button of the dialog.
154         /// This button is on the left.
155         /// When used alone, it is variable in size (can increase to the size of dialog).
156         /// Dialog's all buttons style is bottom
157         /// </summary>
158         public Button Positive
159         {
160             get { return (Button)GetValue(PositiveProperty); }
161             set { SetValue(PositiveProperty, value); }
162         }
163
164         /// <summary>
165         /// Gets or sets neutral button of the dialog.
166         /// This button is at the center.
167         /// When used alone or used with positive, its size is half the size of the dialog and is on the right.
168         /// </summary>
169         public Button Neutral
170         {
171             get { return (Button)GetValue(NeutralProperty); }
172             set { SetValue(NeutralProperty, value); }
173         }
174
175         /// <summary>
176         /// Gets or sets negative button of the dialog.
177         /// This button is always on the right and is displayed at a fixed size.
178         /// </summary>
179         public Button Negative
180         {
181             get { return (Button)GetValue(NegativeProperty); }
182             set { SetValue(NegativeProperty, value); }
183         }
184
185         /// <summary>
186         /// Gets or sets title of the dialog.
187         /// </summary>
188         public string Title
189         {
190             get { return (string)GetValue(TitleProperty); }
191             set { SetValue(TitleProperty, value); }
192         }
193
194         /// <summary>
195         /// Gets or sets subtitle of the dialog.
196         /// When title property value is null, subtitle is not displayed.
197         /// </summary>
198         public string Subtitle
199         {
200             get { return (string)GetValue(SubtitleProperty); }
201             set { SetValue(SubtitleProperty, value); }
202         }
203
204         /// <summary>
205         /// Gets or sets the LayoutOptions that define how the dialog gets laid in a layout cycle.
206         /// The default is center.
207         /// </summary>
208         public LayoutOptions HorizontalOption
209         {
210             get { return (LayoutOptions)GetValue(HorizontalOptionProperty); }
211             set { SetValue(HorizontalOptionProperty, value); }
212         }
213
214         /// <summary>
215         /// Gets or sets the LayoutOptions that define how the dialog gets laid in a layout cycle.
216         /// The default is end.
217         /// </summary>
218         public LayoutOptions VerticalOption
219         {
220             get { return (LayoutOptions)GetValue(VerticalOptionProperty); }
221             set { SetValue(VerticalOptionProperty, value); }
222         }
223
224         /// <summary>
225         /// Shows the dialog.
226         /// </summary>
227         public void Show()
228         {
229             _dialog.Show();
230         }
231
232         /// <summary>
233         /// Hides the dialog.
234         /// </summary>
235         public void Hide()
236         {
237             _dialog.Hide();
238         }
239     }
240 }