[NUI.Components]Fix svace issue (#1169)
[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 trackImage;
32         private ImageView thumbImage;
33         private Animation handlerAni = null;
34
35         /// <summary>
36         /// Creates a new instance of a Switch.
37         /// </summary>
38         /// <since_tizen> 6 </since_tizen>
39         public Switch() : base()
40         {
41             Initialize();
42         }
43
44         /// <summary>
45         /// Creates a new instance of a Switch with style.
46         /// </summary>
47         /// <param name="style">Create Switch by special style defined in UX.</param>
48         /// <since_tizen> 6 </since_tizen>
49         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
50         [EditorBrowsable(EditorBrowsableState.Never)]
51         public Switch(string style) : base(style)
52         {
53             Initialize();
54         }
55
56         /// <summary>
57         /// Creates a new instance of a Switch with style.
58         /// </summary>
59         /// <param name="style">Create Switch by style customized by user.</param>
60         /// <since_tizen> 6 </since_tizen>
61         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
62         [EditorBrowsable(EditorBrowsableState.Never)]
63         public Switch(SwitchStyle style) : base(style)
64         {
65             Initialize();
66         }
67
68         /// <summary>
69         /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
70         /// </summary>
71         /// <since_tizen> 6 </since_tizen>
72         public event EventHandler<SelectEventArgs> SelectedEvent;
73
74         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
75         [EditorBrowsable(EditorBrowsableState.Never)]
76         public new SwitchStyle Style => ViewStyle as SwitchStyle;
77
78         /// <summary>
79         /// Background image's resource url in Switch.
80         /// </summary>
81         /// <since_tizen> 6 </since_tizen>
82         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
83         [EditorBrowsable(EditorBrowsableState.Never)]
84         public string SwitchBackgroundImageURL
85         {
86             get
87             {
88                 return Style?.Track?.ResourceUrl?.All;
89             }
90             set
91             {
92                 if (null != value && null != Style?.Track)
93                 {
94                     Style.Track.ResourceUrl = value;
95                 }
96             }
97         }
98
99         /// <summary>
100         /// Background image's resource url selector in Switch.
101         /// </summary>
102         /// <since_tizen> 6 </since_tizen>
103         public StringSelector SwitchBackgroundImageURLSelector
104         {
105             get
106             {
107                 return (StringSelector)Style?.Track?.ResourceUrl;
108             }
109             set
110             {
111                 if (null != value && null != Style?.Track)
112                 {
113                     Style.Track.ResourceUrl = value.Clone() as StringSelector;
114                 }
115             }
116         }
117
118         /// <summary>
119         /// Handler image's resource url in Switch.
120         /// </summary>
121         /// <since_tizen> 6 </since_tizen>
122         public string SwitchHandlerImageURL
123         {
124             get
125             {
126                 return Style?.Thumb?.ResourceUrl?.All;
127             }
128             set
129             {
130                 if (null != value && null != Style?.Thumb)
131                 {
132                     Style.Thumb.ResourceUrl = value;
133                 }
134             }
135         }
136
137         /// <summary>
138         /// Handler image's resource url selector in Switch.
139         /// </summary>
140         /// <since_tizen> 6 </since_tizen>
141         public StringSelector SwitchHandlerImageURLSelector
142         {
143             get
144             {
145                 return (StringSelector)Style?.Thumb?.ResourceUrl;
146             }
147             set
148             {
149                 if (null != value && null != Style?.Thumb)
150                 {
151                     Style.Thumb.ResourceUrl = value.Clone() as StringSelector;
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         /// Dispose Switch and all children on it.
177         /// </summary>
178         /// <param name="type">Dispose type.</param>
179         /// <since_tizen> 6 </since_tizen>
180         protected override void Dispose(DisposeTypes type)
181         {
182             if (disposed) return;
183
184             if (type == DisposeTypes.Explicit)
185             {
186                 if (null != handlerAni)
187                 {
188                     if (handlerAni.State == Animation.States.Playing)
189                     {
190                         handlerAni.Stop();
191                     }
192                     handlerAni.Dispose();
193                     handlerAni = null;
194                 }
195
196                 Utility.Dispose(thumbImage);
197                 Utility.Dispose(trackImage);
198             }
199
200             base.Dispose(type);
201         }
202
203         /// <summary>
204         /// Called after a key event is received by the view that has had its focus set.
205         /// </summary>
206         /// <param name="key">The key event.</param>
207         /// <returns>True if the key event should be consumed.</returns>
208         /// <since_tizen> 6 </since_tizen>
209         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
210         [EditorBrowsable(EditorBrowsableState.Never)]
211         public override bool OnKey(Key key)
212         {
213             if (!IsEnabled) return false;
214
215             bool ret = base.OnKey(key);
216             if (key.State == Key.StateType.Up)
217             {
218                 if (key.KeyPressedName == "Return")
219                 {
220                     OnSelect();
221                 }
222             }
223
224             return ret;
225         }
226
227         /// <summary>
228         /// Called after a touch event is received by the owning view.<br />
229         /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
230         /// </summary>
231         /// <param name="touch">The touch event.</param>
232         /// <returns>True if the event should be consumed.</returns>
233         /// <since_tizen> 6 </since_tizen>
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 override bool OnTouch(Touch touch)
237         {
238             if(!IsEnabled) return false;
239
240             PointStateType state = touch.GetState(0);
241             bool ret = base.OnTouch(touch);
242             switch (state)
243             {
244                 case PointStateType.Up:
245                     OnSelect();
246                     break;
247                 default:
248                     break;
249             }
250             return ret;
251         }
252
253         /// <summary>
254         /// Get Switch attribues.
255         /// </summary>
256         /// <since_tizen> 6 </since_tizen>
257         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
258         [EditorBrowsable(EditorBrowsableState.Never)]
259         protected override ViewStyle GetViewStyle()
260         {
261             return new SwitchStyle();
262         }
263
264         private void Initialize()
265         {
266             Style.IsSelectable = true;
267             handlerAni = new Animation(aniTime);
268             trackImage = new ImageView()
269             {
270                 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
271                 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
272                 PositionUsesPivotPoint = true,
273                 WidthResizePolicy = ResizePolicyType.FillToParent,
274                 HeightResizePolicy = ResizePolicyType.FillToParent,
275                 Name = "SwitchBackgroundImage",
276             };
277             Add(trackImage);
278             trackImage.ApplyStyle(Style.Track);
279
280             thumbImage = new ImageView()
281             {
282                 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
283                 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
284                 PositionUsesPivotPoint = true,
285                 Name = "SwitchHandlerImage",
286             };
287             trackImage.Add(thumbImage);
288             thumbImage.ApplyStyle(Style.Thumb);
289         }
290
291         /// <summary>
292         /// Theme change callback when theme is changed, this callback will be trigger.
293         /// </summary>
294         /// <since_tizen> 6 </since_tizen>
295         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
296         [EditorBrowsable(EditorBrowsableState.Never)]
297         protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
298         {
299             SwitchStyle tempAttributes = StyleManager.Instance.GetAttributes(style) as SwitchStyle;
300             if (null != tempAttributes)
301             {
302                 Style.CopyFrom(tempAttributes);
303             }
304         }
305
306         private void OnSelect()
307         {
308             if (handlerAni.State == Animation.States.Playing)
309             {
310                 handlerAni.Stop();
311             }
312             handlerAni.Clear();
313             handlerAni.AnimateTo(thumbImage, "PositionX", Size2D.Width - thumbImage.Size2D.Width - thumbImage.Position2D.X);
314             trackImage.Opacity = 0.5f; ///////need defined by UX
315             handlerAni.AnimateTo(trackImage, "Opacity", 1);
316             handlerAni.Play();
317
318             if (SelectedEvent != null)
319             {
320                 SelectEventArgs eventArgs = new SelectEventArgs();
321                 eventArgs.IsSelected = IsSelected;
322                 SelectedEvent(this, eventArgs);
323             }
324         }
325
326         /// <summary>
327         /// SelectEventArgs is a class to record item selected arguments which will sent to user.
328         /// </summary>
329         /// <since_tizen> 6 </since_tizen>
330         public class SelectEventArgs : EventArgs
331         {
332             /// <summary> Select state of Switch </summary>
333             /// <since_tizen> 6 </since_tizen>
334             public bool IsSelected;
335         }
336     }
337 }