[NUI] Open public apis of NUI.Components for TCSACR-248 (#1061)
[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 Tizen.NUI.BaseComponents;
19 using System.ComponentModel;
20
21 namespace Tizen.NUI.Components
22 {
23     /// <summary>
24     /// Switch is one kind of common component, it can be used as selector.
25     /// User can handle Navigation by adding/inserting/deleting NavigationItem.
26     /// </summary>
27     /// <since_tizen> 6 </since_tizen>
28     public class Switch : Button
29     {
30         private const int aniTime = 100; // will be defined in const file later
31         private ImageView switchBackgroundImage;
32         private ImageView switchHandlerImage;
33         private Animation handlerAni = null;
34         private SwitchAttributes switchAttributes;
35
36         /// <summary>
37         /// Creates a new instance of a Switch.
38         /// </summary>
39         /// <since_tizen> 6 </since_tizen>
40         public Switch() : base()
41         {
42             Initialize();
43         }
44
45         /// <summary>
46         /// Creates a new instance of a Switch with style.
47         /// </summary>
48         /// <param name="style">Create Switch by special style defined in UX.</param>
49         /// <since_tizen> 6 </since_tizen>
50         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
51         [EditorBrowsable(EditorBrowsableState.Never)]
52         public Switch(string style) : base(style)
53         {
54             Initialize();
55         }
56
57         /// <summary>
58         /// Creates a new instance of a Switch with attributes.
59         /// </summary>
60         /// <param name="attrs">Create Switch by attributes customized by user.</param>
61         /// <since_tizen> 6 </since_tizen>
62         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
63         [EditorBrowsable(EditorBrowsableState.Never)]
64         public Switch(SwitchAttributes attrs) : base(attrs)
65         {
66             Initialize();
67         }
68
69         /// <summary>
70         /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
71         /// </summary>
72         /// <since_tizen> 6 </since_tizen>
73         public event EventHandler<SelectEventArgs> SelectedEvent;
74
75         /// <summary>
76         /// Background image's resource url in Switch.
77         /// </summary>
78         /// <since_tizen> 6 </since_tizen>
79         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
80         [EditorBrowsable(EditorBrowsableState.Never)]
81         public string SwitchBackgroundImageURL
82         {
83             get
84             {
85                 return switchAttributes?.SwitchBackgroundImageAttributes?.ResourceURL?.All;
86             }
87             set
88             {
89                 if (value != null)
90                 {
91                     CreateSwitchBackgroundImageAttributes();
92                     if (switchAttributes.SwitchBackgroundImageAttributes.ResourceURL == null)
93                     {
94                         switchAttributes.SwitchBackgroundImageAttributes.ResourceURL = new StringSelector();
95                     }
96                     switchAttributes.SwitchBackgroundImageAttributes.ResourceURL.All = value;
97                     RelayoutRequest();
98                 }
99             }
100         }
101
102         /// <summary>
103         /// Background image's resource url selector in Switch.
104         /// </summary>
105         /// <since_tizen> 6 </since_tizen>
106         public StringSelector SwitchBackgroundImageURLSelector
107         {
108             get
109             {
110                 return switchAttributes?.SwitchBackgroundImageAttributes?.ResourceURL;
111             }
112             set
113             {
114                 if (value != null)
115                 {
116                     CreateSwitchBackgroundImageAttributes();
117                     switchAttributes.SwitchBackgroundImageAttributes.ResourceURL = value.Clone() as StringSelector;
118                     RelayoutRequest();
119                 }
120             }
121         }
122
123         /// <summary>
124         /// Handler image's resource url in Switch.
125         /// </summary>
126         /// <since_tizen> 6 </since_tizen>
127         public string SwitchHandlerImageURL
128         {
129             get
130             {
131                 return switchAttributes?.SwitchHandlerImageAttributes?.ResourceURL?.All;
132             }
133             set
134             {
135                 if (value != null)
136                 {
137                     CreateSwitchHandlerImageAttributes();
138                     if (switchAttributes.SwitchHandlerImageAttributes.ResourceURL == null)
139                     {
140                         switchAttributes.SwitchHandlerImageAttributes.ResourceURL = new StringSelector();
141                     }
142                     switchAttributes.SwitchHandlerImageAttributes.ResourceURL.All = value;
143                     RelayoutRequest();
144                 }
145             }
146         }
147
148         /// <summary>
149         /// Handler image's resource url selector in Switch.
150         /// </summary>
151         /// <since_tizen> 6 </since_tizen>
152         public StringSelector SwitchHandlerImageURLSelector
153         {
154             get
155             {
156                 return switchAttributes?.SwitchHandlerImageAttributes?.ResourceURL;
157             }
158             set
159             {
160                 if (value != null)
161                 {
162                     CreateSwitchHandlerImageAttributes();
163                     switchAttributes.SwitchHandlerImageAttributes.ResourceURL = value.Clone() as StringSelector;
164                     RelayoutRequest();
165                 }
166             }
167         }
168
169         /// <summary>
170         /// Handler image's size in Switch.
171         /// </summary>
172         /// <since_tizen> 6 </since_tizen>
173         public Size SwitchHandlerImageSize
174         {
175             get
176             {
177                 return switchAttributes?.SwitchHandlerImageAttributes?.Size ?? new Size(0, 0);
178             }
179             set
180             {
181                 CreateSwitchHandlerImageAttributes();
182                 switchAttributes.SwitchHandlerImageAttributes.Size = value;
183                 RelayoutRequest();
184             }
185         }
186
187         /// <summary>
188         /// Dispose Switch and all children on it.
189         /// </summary>
190         /// <param name="type">Dispose type.</param>
191         /// <since_tizen> 6 </since_tizen>
192         protected override void Dispose(DisposeTypes type)
193         {
194             if (disposed)
195             {
196                 return;
197             }
198
199             if (type == DisposeTypes.Explicit)
200             {
201                 if (handlerAni != null)
202                 {
203                     if (handlerAni.State == Animation.States.Playing)
204                     {
205                         handlerAni.Stop();
206                     }
207                     handlerAni.Dispose();
208                     handlerAni = null;
209                 }
210
211                 if (switchHandlerImage != null)
212                 {
213                     Utility.Dispose(switchHandlerImage);
214                 }
215                 if (switchBackgroundImage != null)
216                 {
217                     Utility.Dispose(switchBackgroundImage);
218                 }
219             }
220
221             base.Dispose(type);
222         }
223
224         /// <summary>
225         /// Update Switch by attributes.
226         /// </summary>
227         /// <since_tizen> 6 </since_tizen>
228         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
229         [EditorBrowsable(EditorBrowsableState.Never)]
230         protected override void OnUpdate()
231         {
232             base.OnUpdate();
233
234             if (switchAttributes.SwitchBackgroundImageAttributes != null)
235             {
236                 if (switchBackgroundImage == null)
237                 {
238                     switchBackgroundImage = new ImageView()
239                     {
240                         ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
241                         PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
242                         PositionUsesPivotPoint = true,
243                         WidthResizePolicy = ResizePolicyType.FillToParent,
244                         HeightResizePolicy = ResizePolicyType.FillToParent,
245                     };
246                     switchBackgroundImage.Name = "SwitchBackgroundImage";
247                     Add(switchBackgroundImage);
248                 }
249                 ApplyAttributes(switchBackgroundImage, switchAttributes.SwitchBackgroundImageAttributes);
250
251                 if (switchAttributes.SwitchHandlerImageAttributes != null)
252                 {
253                     if (switchHandlerImage == null)
254                     {
255                         switchHandlerImage = new ImageView()
256                         {
257                             ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
258                             PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
259                             PositionUsesPivotPoint = true,
260                         };
261                         switchHandlerImage.Name = "SwitchHandlerImage";
262                         switchBackgroundImage.Add(switchHandlerImage);
263                     }
264                     ApplyAttributes(switchHandlerImage, switchAttributes.SwitchHandlerImageAttributes);
265                 }
266             }                          
267         }
268
269         /// <summary>
270         /// Called after a key event is received by the view that has had its focus set.
271         /// </summary>
272         /// <param name="key">The key event.</param>
273         /// <returns>True if the key event should be consumed.</returns>
274         /// <since_tizen> 6 </since_tizen>
275         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
276         [EditorBrowsable(EditorBrowsableState.Never)]
277         public override bool OnKey(Key key)
278         {
279             if (IsEnabled == false)
280             {
281                 return false;
282             }
283             bool ret = base.OnKey(key);
284             if (key.State == Key.StateType.Up)
285             {
286                 if (key.KeyPressedName == "Return")
287                 {
288                     OnSelect();
289                 }
290             }
291
292             return ret;
293         }
294
295         /// <summary>
296         /// Called after a touch event is received by the owning view.<br />
297         /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
298         /// </summary>
299         /// <param name="touch">The touch event.</param>
300         /// <returns>True if the event should be consumed.</returns>
301         /// <since_tizen> 6 </since_tizen>
302         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
303         [EditorBrowsable(EditorBrowsableState.Never)]
304         public override bool OnTouch(Touch touch)
305         {
306             if(IsEnabled == false)
307             {
308                 return false;
309             }
310             PointStateType state = touch.GetState(0);
311             bool ret = base.OnTouch(touch);
312             switch (state)
313             {
314                 case PointStateType.Up:
315                     OnSelect();
316                     break;
317                 default:
318                     break;
319             }
320             return ret;
321         }
322
323         /// <summary>
324         /// Get Switch attribues.
325         /// </summary>
326         /// <since_tizen> 6 </since_tizen>
327         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
328         [EditorBrowsable(EditorBrowsableState.Never)]
329         protected override Attributes GetAttributes()
330         {
331             return new SwitchAttributes();
332         }
333
334         private void Initialize()
335         {
336             switchAttributes = attributes as SwitchAttributes;
337             if (switchAttributes == null)
338             {
339                 throw new Exception("Switch attribute parse error.");
340             }
341
342             switchAttributes.IsSelectable = true;
343             CreateHandlerAnimation();
344         }
345
346         /// <summary>
347         /// Theme change callback when theme is changed, this callback will be trigger.
348         /// </summary>
349         /// <since_tizen> 6 </since_tizen>
350         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
351         [EditorBrowsable(EditorBrowsableState.Never)]
352         protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
353         {
354             SwitchAttributes tempAttributes = StyleManager.Instance.GetAttributes(style) as SwitchAttributes;
355             if (tempAttributes != null)
356             {
357                 attributes = switchAttributes = tempAttributes;
358                 RelayoutRequest();
359             }
360         }
361
362         private void CreateSwitchBackgroundImageAttributes()
363         {
364             if (switchAttributes.SwitchBackgroundImageAttributes == null)
365             {
366                 switchAttributes.SwitchBackgroundImageAttributes = new ImageAttributes()
367                 {
368                     PositionUsesPivotPoint = true,
369                     ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
370                     PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
371                 };
372             }
373         }
374
375         private void CreateSwitchHandlerImageAttributes()
376         {
377             if (switchAttributes.SwitchHandlerImageAttributes == null)
378             {
379                 switchAttributes.SwitchHandlerImageAttributes = new ImageAttributes()
380                 {
381                     PositionUsesPivotPoint = true,
382                 };
383             }
384         }
385
386         private void CreateHandlerAnimation()
387         {
388             if (handlerAni == null)
389             {
390                 handlerAni = new Animation(aniTime);
391             }
392         }
393
394         private void OnSelect()
395         {
396             if (handlerAni.State == Animation.States.Playing)
397             {
398                 handlerAni.Stop();
399             }
400             handlerAni.Clear();
401             if (switchHandlerImage != null)
402             {
403                 handlerAni.AnimateTo(switchHandlerImage, "PositionX", Size2D.Width - switchHandlerImage.Size2D.Width - switchHandlerImage.Position2D.X);
404             }
405             if (switchBackgroundImage != null)
406             {
407                 switchBackgroundImage.Opacity = 0.5f; ///////need defined by UX
408                 handlerAni.AnimateTo(switchBackgroundImage, "Opacity", 1);
409             }
410             handlerAni.Play();
411
412             if (SelectedEvent != null)
413             {
414                 SelectEventArgs eventArgs = new SelectEventArgs();
415                 eventArgs.IsSelected = IsSelected;
416                 SelectedEvent(this, eventArgs);
417             }
418         }
419
420         /// <summary>
421         /// SelectEventArgs is a class to record item selected arguments which will sent to user.
422         /// </summary>
423         /// <since_tizen> 6 </since_tizen>
424         public class SelectEventArgs : EventArgs
425         {
426             /// <summary> Select state of Switch </summary>
427             /// <since_tizen> 6 </since_tizen>
428             public bool IsSelected;
429         }
430     }
431 }