[NUI] Add ThemeManager (#2034)
[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         private ImageView track = null;
32         private ImageView thumb = null;
33
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         [Obsolete("Deprecated in API8; Will be removed in API10. Please use SelectedChanged event instead.")]
70         public event EventHandler<SelectEventArgs> SelectedEvent;
71
72         /// <summary>
73         /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.
74         /// </summary>
75         /// <since_tizen> 8 </since_tizen>
76         public event EventHandler<SelectedChangedEventArgs> SelectedChanged;
77
78         /// <summary>
79         /// Return a copied Style instance of Switch
80         /// </summary>
81         /// <remarks>
82         /// It returns copied Style instance and changing it does not effect to the Switch.
83         /// Style setting is possible by using constructor or the function of ApplyStyle(ViewStyle viewStyle)
84         /// </remarks>
85         /// <since_tizen> 8 </since_tizen>
86         public new SwitchStyle Style
87         {
88             get
89             {
90                 var result = new SwitchStyle(ViewStyle as SwitchStyle);
91                 result.CopyPropertiesFromView(this);
92                 result.Track.CopyPropertiesFromView(Track);
93                 result.Thumb.CopyPropertiesFromView(Thumb);
94                 return result;
95             }
96         }
97
98         /// <summary>
99         /// Apply style to switch.
100         /// </summary>
101         /// <param name="viewStyle">The style to apply.</param>
102         [EditorBrowsable(EditorBrowsableState.Never)]
103         public override void ApplyStyle(ViewStyle viewStyle)
104         {
105             base.ApplyStyle(viewStyle);
106
107             SwitchStyle swStyle = viewStyle as SwitchStyle;
108
109             if (null != swStyle)
110             {
111                 if (swStyle.Track != null)
112                 {
113                     Track.ApplyStyle(swStyle.Track);
114                 }
115
116                 if (swStyle.Thumb != null)
117                 {
118                     Thumb.ApplyStyle(swStyle.Thumb);
119                 }
120             }
121         }
122
123         /// <summary>
124         /// Switch's track part.
125         /// </summary>
126         /// <since_tizen> 8 </since_tizen>
127         public ImageView Track
128         {
129             get
130             {
131                 if (track == null)
132                 {
133                     track = new ImageView()
134                     {
135                         PositionUsesPivotPoint = true,
136                         ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
137                         PivotPoint = Tizen.NUI.PivotPoint.CenterLeft,
138                         WidthResizePolicy = ResizePolicyType.FillToParent,
139                         HeightResizePolicy = ResizePolicyType.FillToParent
140                     };
141
142                     var extension = (SwitchExtension)Extension;
143                     if (extension != null)
144                     {
145                         track = extension.OnCreateTrack(this, track);
146                     }
147                     Add(track);
148                 }
149                 return track;
150             }
151             internal set
152             {
153                 track = value;
154             }
155         }
156
157         /// <summary>
158         /// Switch's thumb part.
159         /// </summary>
160         /// <since_tizen> 8 </since_tizen>
161         public ImageView Thumb
162         {
163             get
164             {
165                 if (thumb == null)
166                 {
167                     thumb = new ImageView()
168                     {
169                         PositionUsesPivotPoint = true,
170                         ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
171                         PivotPoint = Tizen.NUI.PivotPoint.CenterLeft,
172                         WidthResizePolicy = ResizePolicyType.Fixed,
173                         HeightResizePolicy = ResizePolicyType.Fixed
174                     };
175
176                     var extension = (SwitchExtension)Extension;
177                     if (extension != null)
178                     {
179                         thumb = extension.OnCreateThumb(this, thumb);
180                     }
181                     Add(thumb);
182                 }
183                 return thumb;
184             }
185             internal set
186             {
187                 thumb = value;
188             }
189         }
190
191         /// <summary>
192         /// Background image's resource url selector in Switch.
193         /// </summary>
194         /// <since_tizen> 6 </since_tizen>
195         public StringSelector SwitchBackgroundImageURLSelector
196         {
197             get => track == null ? null : new StringSelector((Selector<string>)track.GetValue(ImageView.ResourceUrlSelectorProperty));
198             set => track?.SetValue(ImageView.ResourceUrlSelectorProperty, value);
199         }
200
201         /// <summary>
202         /// Handler image's resource url in Switch.
203         /// </summary>
204         /// <since_tizen> 6 </since_tizen>
205         public string SwitchHandlerImageURL
206         {
207             get
208             {
209                 return Thumb.ResourceUrl;
210             }
211             set
212             {
213                 Thumb.ResourceUrl = value;
214             }
215         }
216
217         /// <summary>
218         /// Handler image's resource url selector in Switch.
219         /// Getter returns copied selector value if exist, null otherwise.
220         /// </summary>
221         /// <since_tizen> 6 </since_tizen>
222         public StringSelector SwitchHandlerImageURLSelector
223         {
224             get => thumb == null ? null : new StringSelector((Selector<string>)thumb.GetValue(ImageView.ResourceUrlSelectorProperty));
225             set => thumb?.SetValue(ImageView.ResourceUrlSelectorProperty, value);
226         }
227
228         /// <summary>
229         /// Handler image's size in Switch.
230         /// </summary>
231         /// <since_tizen> 6 </since_tizen>
232         public Size SwitchHandlerImageSize
233         {
234             get
235             {
236                 return Thumb.Size;
237             }
238             set
239             {
240                 Thumb.Size = value;
241             }
242         }
243
244         /// <summary>
245         /// Dispose Switch and all children on it.
246         /// </summary>
247         /// <param name="type">Dispose type.</param>
248         /// <since_tizen> 6 </since_tizen>
249         protected override void Dispose(DisposeTypes type)
250         {
251             if (disposed) return;
252
253             if (type == DisposeTypes.Explicit)
254             {
255                 Utility.Dispose(Thumb);
256                 Utility.Dispose(Track);
257             }
258
259             base.Dispose(type);
260         }
261
262         /// <summary>
263         /// Called after a key event is received by the view that has had its focus set.
264         /// </summary>
265         /// <param name="key">The key event.</param>
266         /// <returns>True if the key event should be consumed.</returns>
267         /// <since_tizen> 8 </since_tizen>
268         public override bool OnKey(Key key)
269         {
270             return base.OnKey(key);
271         }
272
273         /// <summary>
274         /// Called after a touch event is received by the owning view.<br />
275         /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
276         /// </summary>
277         /// <param name="touch">The touch event.</param>
278         /// <returns>True if the event should be consumed.</returns>
279         /// <since_tizen> 8 </since_tizen>
280         [Obsolete("Deprecated in API8; Will be removed in API10. Please use OnClicked instead.")]
281         public override bool OnTouch(Touch touch)
282         {
283             return base.OnTouch(touch);
284         }
285
286         /// <summary>
287         /// Get Switch style.
288         /// </summary>
289         /// <returns>The default switch style.</returns>
290         /// <since_tizen> 8 </since_tizen>
291         protected override ViewStyle CreateViewStyle()
292         {
293             return new SwitchStyle();
294         }
295
296         /// <inheritdoc/>
297         [EditorBrowsable(EditorBrowsableState.Never)]
298         protected override void OnControlStateChanged(ControlStateChangedEventArgs controlStateChangedInfo)
299         {
300             base.OnControlStateChanged(controlStateChangedInfo);
301
302             if (!IsSelectable)
303             {
304                 return;
305             }
306
307             bool previousSelected = controlStateChangedInfo.PreviousState.Contains(ControlState.Selected);
308
309             if (previousSelected != IsSelected)
310             {
311                 OnSelect();
312             }
313         }
314
315         private void Initialize()
316         {
317             IsSelectable = true;
318         }
319
320         private void OnSelect()
321         {
322             ((SwitchExtension)Extension)?.OnSelectedChanged(this);
323
324             if (SelectedEvent != null)
325             {
326                 SelectEventArgs eventArgs = new SelectEventArgs();
327                 eventArgs.IsSelected = IsSelected;
328                 SelectedEvent(this, eventArgs);
329             }
330
331             if (SelectedChanged != null)
332             {
333                 SelectedChangedEventArgs eventArgs = new SelectedChangedEventArgs();
334                 eventArgs.IsSelected = IsSelected;
335                 SelectedChanged(this, eventArgs);
336             }
337         }
338
339         /// <summary>
340         /// SelectEventArgs is a class to record item selected arguments which will sent to user.
341         /// </summary>
342         /// <since_tizen> 6 </since_tizen>
343         [Obsolete("Deprecated in API8; Will be removed in API10. Please use SelectedChangedEventArgs instead.")]
344         public class SelectEventArgs : EventArgs
345         {
346             /// <summary> Select state of Switch </summary>
347             /// <since_tizen> 6 </since_tizen>
348             public bool IsSelected;
349         }
350     }
351 }