[NUI] Introduce Button extentions and styles (#1515)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Switch.cs
1 /*
2  * Copyright(c) 2019 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17 using System;
18 using System.ComponentModel;
19 using Tizen.NUI.BaseComponents;
20 using Tizen.NUI.Components.Extension;
21
22 namespace Tizen.NUI.Components
23 {
24     /// <summary>
25     /// Switch is one kind of common component, it can be used as selector.
26     /// User can handle Navigation by adding/inserting/deleting NavigationItem.
27     /// </summary>
28     /// <since_tizen> 6 </since_tizen>
29     public class Switch : Button
30     {
31         static Switch() { }
32
33         /// <summary>
34         /// Creates a new instance of a Switch.
35         /// </summary>
36         /// <since_tizen> 6 </since_tizen>
37         public Switch() : base()
38         {
39             Initialize();
40         }
41
42         /// <summary>
43         /// Creates a new instance of a Switch with style.
44         /// </summary>
45         /// <param name="style">Create Switch by special style defined in UX.</param>
46         /// <since_tizen> 8 </since_tizen>
47         public Switch(string style) : base(style)
48         {
49             Initialize();
50         }
51
52         /// <summary>
53         /// Creates a new instance of a Switch with style.
54         /// </summary>
55         /// <param name="switchStyle">Create Switch by style customized by user.</param>
56         /// <since_tizen> 8 </since_tizen>
57         public Switch(SwitchStyle switchStyle) : base(switchStyle)
58         {
59             Initialize();
60         }
61
62         /// <summary>
63         /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
64         /// </summary>
65         /// <since_tizen> 6 </since_tizen>
66         public event EventHandler<SelectEventArgs> SelectedEvent;
67
68         /// <summary>
69         /// Get style of switch.
70         /// </summary>
71         /// <since_tizen> 8 </since_tizen>
72         public new SwitchStyle Style => ViewStyle as SwitchStyle;
73
74         /// <summary>
75         /// Background image's resource url in Switch.
76         /// </summary>
77         /// <since_tizen> 6 </since_tizen>
78         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
79         [EditorBrowsable(EditorBrowsableState.Never)]
80         public string SwitchBackgroundImageURL
81         {
82             get
83             {
84                 return Style?.Track?.ResourceUrl?.All;
85             }
86             set
87             {
88                 if (null != value && null != Style?.Track)
89                 {
90                     Style.Track.ResourceUrl = value;
91                 }
92             }
93         }
94
95         /// <summary>
96         /// Background image's resource url selector in Switch.
97         /// </summary>
98         /// <since_tizen> 6 </since_tizen>
99         public StringSelector SwitchBackgroundImageURLSelector
100         {
101             get
102             {
103                 StringSelector strSl = new StringSelector();
104                 strSl.Clone(Style?.Track?.ResourceUrl);
105                 return strSl;
106             }
107             set
108             {
109                 if (null != value && null != Style?.Track)
110                 {
111                     Style.Track.ResourceUrl = value;
112                 }
113             }
114         }
115
116         /// <summary>
117         /// Handler image's resource url in Switch.
118         /// </summary>
119         /// <since_tizen> 6 </since_tizen>
120         public string SwitchHandlerImageURL
121         {
122             get
123             {
124                 return Style?.Thumb?.ResourceUrl?.All;
125             }
126             set
127             {
128                 if (null != value && null != Style?.Thumb)
129                 {
130                     Style.Thumb.ResourceUrl = value;
131                 }
132             }
133         }
134
135         /// <summary>
136         /// Handler image's resource url selector in Switch.
137         /// </summary>
138         /// <since_tizen> 6 </since_tizen>
139         public StringSelector SwitchHandlerImageURLSelector
140         {
141             get
142             {
143                 StringSelector strSl = new StringSelector();
144                 strSl.Clone(Style?.Thumb?.ResourceUrl);
145                 return strSl;
146             }
147             set
148             {
149                 if (null != value && null != Style?.Thumb)
150                 {
151                     Style.Thumb.ResourceUrl = value;
152                 }
153             }
154         }
155
156         /// <summary>
157         /// Handler image's size in Switch.
158         /// </summary>
159         /// <since_tizen> 6 </since_tizen>
160         public Size SwitchHandlerImageSize
161         {
162             get
163             {
164                 return Style?.Thumb?.Size;
165             }
166             set
167             {
168                 if (null != Style?.Thumb)
169                 {
170                     Style.Thumb.Size = value;
171                 }
172             }
173         }
174
175         /// <summary>
176         /// Switch's track part.
177         /// </summary>
178         [EditorBrowsable(EditorBrowsableState.Never)]
179         protected ImageView Track { get; set; }
180
181         /// <summary>
182         /// Switch's thumb part.
183         /// </summary>
184         [EditorBrowsable(EditorBrowsableState.Never)]
185         protected ImageView Thumb { get; set; }
186
187         /// <summary>
188         /// Creates Switch's track part.
189         /// </summary>
190         /// <return>The created Button's icon part.</return>
191         [EditorBrowsable(EditorBrowsableState.Never)]
192         protected virtual ImageView CreateTrack()
193         {
194             return new ImageView();
195         }
196
197         /// <summary>
198         /// Creates Switch's overlay thumb part.
199         /// </summary>
200         /// <return>The created Button's overlay image part.</return>
201         [EditorBrowsable(EditorBrowsableState.Never)]
202         protected virtual ImageView CreateThumb()
203         {
204             return new ImageView();
205         }
206
207         /// <summary>
208         /// Dispose Switch and all children on it.
209         /// </summary>
210         /// <param name="type">Dispose type.</param>
211         /// <since_tizen> 6 </since_tizen>
212         protected override void Dispose(DisposeTypes type)
213         {
214             if (disposed) return;
215
216             if (type == DisposeTypes.Explicit)
217             {
218                 Utility.Dispose(Thumb);
219                 Utility.Dispose(Track);
220             }
221
222             base.Dispose(type);
223         }
224
225         /// <summary>
226         /// Called after a key event is received by the view that has had its focus set.
227         /// </summary>
228         /// <param name="key">The key event.</param>
229         /// <returns>True if the key event should be consumed.</returns>
230         /// <since_tizen> 6 </since_tizen>
231         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
232         [EditorBrowsable(EditorBrowsableState.Never)]
233         public override bool OnKey(Key key)
234         {
235             if (!IsEnabled || null == key) return false;
236
237             bool ret = base.OnKey(key);
238             if (key.State == Key.StateType.Up)
239             {
240                 if (key.KeyPressedName == "Return")
241                 {
242                     OnSelect();
243                 }
244             }
245
246             return ret;
247         }
248
249         /// <summary>
250         /// Called after a touch event is received by the owning view.<br />
251         /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
252         /// </summary>
253         /// <param name="touch">The touch event.</param>
254         /// <returns>True if the event should be consumed.</returns>
255         /// <since_tizen> 6 </since_tizen>
256         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
257         [EditorBrowsable(EditorBrowsableState.Never)]
258         public override bool OnTouch(Touch touch)
259         {
260             if(!IsEnabled || null == touch) return false;
261
262             PointStateType state = touch.GetState(0);
263             bool ret = base.OnTouch(touch);
264             switch (state)
265             {
266                 case PointStateType.Up:
267                     OnSelect();
268                     break;
269                 default:
270                     break;
271             }
272             return ret;
273         }
274
275         /// <summary>
276         /// Get Switch style.
277         /// </summary>
278         /// <returns>The default switch style.</returns>
279         /// <since_tizen> 8 </since_tizen>
280         protected override ViewStyle GetViewStyle()
281         {
282             return new SwitchStyle();
283         }
284
285         private void Initialize()
286         {
287             Style.IsSelectable = true;
288
289             CreateComponents();
290
291             if (null != Track)
292             {
293                 Add(Track);
294                 Track.ApplyStyle(Style.Track);
295             }
296
297             if (null != Thumb)
298             {
299                 Add(Thumb);
300                 Thumb.ApplyStyle(Style.Thumb);
301             }
302         }
303
304         private void CreateComponents()
305         {
306             Track = CreateTrack();
307             Thumb = CreateThumb();
308
309             if (Extension as SwitchExtension == null)
310             {
311                 return;
312             }
313
314             // Update component with extension
315             var extension = (SwitchExtension)Extension;
316             Track = extension.OnCreateTrack(this, Track);
317             Thumb = extension.OnCreateThumb(this, Thumb);
318         }
319
320         /// <summary>
321         /// Theme change callback when theme is changed, this callback will be trigger.
322         /// </summary>
323         /// <param name="sender">The sender</param>
324         /// <param name="e">The event data</param>
325         /// <since_tizen> 8 </since_tizen>
326         protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
327         {
328             SwitchStyle switchStyle = StyleManager.Instance.GetViewStyle(style) as SwitchStyle;
329             if (null != switchStyle)
330             {
331                 Style.CopyFrom(switchStyle);
332             }
333         }
334
335         private void OnSelect()
336         {
337             if (SelectedEvent != null)
338             {
339                 SelectEventArgs eventArgs = new SelectEventArgs();
340                 eventArgs.IsSelected = IsSelected;
341                 SelectedEvent(this, eventArgs);
342             }
343         }
344
345         /// <summary>
346         /// SelectEventArgs is a class to record item selected arguments which will sent to user.
347         /// </summary>
348         /// <since_tizen> 6 </since_tizen>
349         public class SelectEventArgs : EventArgs
350         {
351             /// <summary> Select state of Switch </summary>
352             /// <since_tizen> 6 </since_tizen>
353             public bool IsSelected;
354         }
355
356         /// <summary>
357         /// Get current track part to the attached SwitchExtension.
358         /// </summary>
359         /// <remarks>
360         /// It returns null if the passed extension is invaild.
361         /// </remarks>
362         /// <param name="extension">The extension instance that is currently attached to this Switch.</param>
363         /// <return>The switch's track part.</return>
364         [EditorBrowsable(EditorBrowsableState.Never)]
365         public ImageView GetCurrentTrack(SwitchExtension extension)
366         {
367             return (extension == Extension) ? Track : null;
368         }
369
370          /// <summary>
371         /// Get current thumb part to the attached SwitchExtension.
372         /// </summary>
373         /// <remarks>
374         /// It returns null if the passed extension is invaild.
375         /// </remarks>
376         /// <param name="extension">The extension instance that is currently attached to this Switch.</param>
377         /// <return>The switch's thumb part.</return>
378         [EditorBrowsable(EditorBrowsableState.Never)]
379         public ImageView GetCurrentThumb(SwitchExtension extension)
380         {
381             return (extension == Extension) ? Thumb : null;
382         }
383     }
384 }