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