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