2 * Copyright(c) 2019 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.ComponentModel;
19 using Tizen.NUI.BaseComponents;
20 using Tizen.NUI.Components.Extension;
22 namespace Tizen.NUI.Components
25 /// Switch is one kind of common component, it can be used as selector.
26 /// User can handle Navigation by adding/inserting/deleting NavigationItem.
28 /// <since_tizen> 6 </since_tizen>
29 public class Switch : Button
34 /// Creates a new instance of a Switch.
36 /// <since_tizen> 6 </since_tizen>
37 public Switch() : base()
43 /// Creates a new instance of a Switch with style.
45 /// <param name="style">Create Switch by special style defined in UX.</param>
46 /// <since_tizen> 8 </since_tizen>
47 public Switch(string style) : base(style)
53 /// Creates a new instance of a Switch with style.
55 /// <param name="switchStyle">Create Switch by style customized by user.</param>
56 /// <since_tizen> 8 </since_tizen>
57 public Switch(SwitchStyle switchStyle) : base(switchStyle)
63 /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
65 /// <since_tizen> 6 </since_tizen>
66 public event EventHandler<SelectEventArgs> SelectedEvent;
69 /// Get style of switch.
71 /// <since_tizen> 8 </since_tizen>
72 public new SwitchStyle Style => ViewStyle as SwitchStyle;
75 /// Background image's resource url in Switch.
77 /// <since_tizen> 6 </since_tizen>
78 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
79 [EditorBrowsable(EditorBrowsableState.Never)]
80 public string SwitchBackgroundImageURL
84 return Style?.Track?.ResourceUrl?.All;
88 if (null != value && null != Style?.Track)
90 Style.Track.ResourceUrl = value;
96 /// Background image's resource url selector in Switch.
98 /// <since_tizen> 6 </since_tizen>
99 public StringSelector SwitchBackgroundImageURLSelector
103 StringSelector strSl = new StringSelector();
104 strSl.Clone(Style?.Track?.ResourceUrl);
109 if (null != value && null != Style?.Track)
111 Style.Track.ResourceUrl = value;
117 /// Handler image's resource url in Switch.
119 /// <since_tizen> 6 </since_tizen>
120 public string SwitchHandlerImageURL
124 return Style?.Thumb?.ResourceUrl?.All;
128 if (null != value && null != Style?.Thumb)
130 Style.Thumb.ResourceUrl = value;
136 /// Handler image's resource url selector in Switch.
138 /// <since_tizen> 6 </since_tizen>
139 public StringSelector SwitchHandlerImageURLSelector
143 StringSelector strSl = new StringSelector();
144 strSl.Clone(Style?.Thumb?.ResourceUrl);
149 if (null != value && null != Style?.Thumb)
151 Style.Thumb.ResourceUrl = value;
157 /// Handler image's size in Switch.
159 /// <since_tizen> 6 </since_tizen>
160 public Size SwitchHandlerImageSize
164 return Style?.Thumb?.Size;
168 if (null != Style?.Thumb)
170 Style.Thumb.Size = value;
176 /// Switch's track part.
178 [EditorBrowsable(EditorBrowsableState.Never)]
179 protected ImageView Track { get; set; }
182 /// Switch's thumb part.
184 [EditorBrowsable(EditorBrowsableState.Never)]
185 protected ImageView Thumb { get; set; }
188 /// Creates Switch's track part.
190 /// <return>The created Button's icon part.</return>
191 [EditorBrowsable(EditorBrowsableState.Never)]
192 protected virtual ImageView CreateTrack()
194 return new ImageView();
198 /// Creates Switch's overlay thumb part.
200 /// <return>The created Button's overlay image part.</return>
201 [EditorBrowsable(EditorBrowsableState.Never)]
202 protected virtual ImageView CreateThumb()
204 return new ImageView();
208 /// Dispose Switch and all children on it.
210 /// <param name="type">Dispose type.</param>
211 /// <since_tizen> 6 </since_tizen>
212 protected override void Dispose(DisposeTypes type)
214 if (disposed) return;
216 if (type == DisposeTypes.Explicit)
218 Utility.Dispose(Thumb);
219 Utility.Dispose(Track);
226 /// Called after a key event is received by the view that has had its focus set.
228 /// <param name="key">The key event.</param>
229 /// <returns>True if the key event should be consumed.</returns>
230 /// <since_tizen> 6 </since_tizen>
231 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
232 [EditorBrowsable(EditorBrowsableState.Never)]
233 public override bool OnKey(Key key)
235 if (!IsEnabled || null == key) return false;
237 bool ret = base.OnKey(key);
238 if (key.State == Key.StateType.Up)
240 if (key.KeyPressedName == "Return")
250 /// Called after a touch event is received by the owning view.<br />
251 /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
253 /// <param name="touch">The touch event.</param>
254 /// <returns>True if the event should be consumed.</returns>
255 /// <since_tizen> 6 </since_tizen>
256 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
257 [EditorBrowsable(EditorBrowsableState.Never)]
258 public override bool OnTouch(Touch touch)
260 if(!IsEnabled || null == touch) return false;
262 PointStateType state = touch.GetState(0);
263 bool ret = base.OnTouch(touch);
266 case PointStateType.Up:
276 /// Get Switch style.
278 /// <returns>The default switch style.</returns>
279 /// <since_tizen> 8 </since_tizen>
280 protected override ViewStyle GetViewStyle()
282 return new SwitchStyle();
285 private void Initialize()
287 Style.IsSelectable = true;
294 Track.ApplyStyle(Style.Track);
300 Thumb.ApplyStyle(Style.Thumb);
304 private void CreateComponents()
306 Track = CreateTrack();
307 Thumb = CreateThumb();
309 if (Extension as SwitchExtension == null)
314 // Update component with extension
315 var extension = (SwitchExtension)Extension;
316 Track = extension.OnCreateTrack(this, Track);
317 Thumb = extension.OnCreateThumb(this, Thumb);
321 /// Theme change callback when theme is changed, this callback will be trigger.
323 /// <param name="sender">The sender</param>
324 /// <param name="e">The event data</param>
325 /// <since_tizen> 8 </since_tizen>
326 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
328 SwitchStyle switchStyle = StyleManager.Instance.GetViewStyle(style) as SwitchStyle;
329 if (null != switchStyle)
331 Style.CopyFrom(switchStyle);
335 private void OnSelect()
337 if (SelectedEvent != null)
339 SelectEventArgs eventArgs = new SelectEventArgs();
340 eventArgs.IsSelected = IsSelected;
341 SelectedEvent(this, eventArgs);
346 /// SelectEventArgs is a class to record item selected arguments which will sent to user.
348 /// <since_tizen> 6 </since_tizen>
349 public class SelectEventArgs : EventArgs
351 /// <summary> Select state of Switch </summary>
352 /// <since_tizen> 6 </since_tizen>
353 public bool IsSelected;
357 /// Get current track part to the attached SwitchExtension.
360 /// It returns null if the passed extension is invaild.
362 /// <param name="extension">The extension instance that is currently attached to this Switch.</param>
363 /// <return>The switch's track part.</return>
364 [EditorBrowsable(EditorBrowsableState.Never)]
365 public ImageView GetCurrentTrack(SwitchExtension extension)
367 return (extension == Extension) ? Track : null;
371 /// Get current thumb part to the attached SwitchExtension.
374 /// It returns null if the passed extension is invaild.
376 /// <param name="extension">The extension instance that is currently attached to this Switch.</param>
377 /// <return>The switch's thumb part.</return>
378 [EditorBrowsable(EditorBrowsableState.Never)]
379 public ImageView GetCurrentThumb(SwitchExtension extension)
381 return (extension == Extension) ? Thumb : null;