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
31 private ImageView track = null;
32 private ImageView thumb = null;
37 /// Creates a new instance of a Switch.
39 /// <since_tizen> 6 </since_tizen>
40 public Switch() : base()
46 /// Creates a new instance of a Switch with style.
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)
56 /// Creates a new instance of a Switch with style.
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)
66 /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
68 /// <since_tizen> 6 </since_tizen>
69 public event EventHandler<SelectEventArgs> SelectedEvent;
72 /// Get style of switch.
74 /// <since_tizen> 8 </since_tizen>
75 public new SwitchStyle Style => ViewStyle as SwitchStyle;
78 /// Apply style to switch.
80 /// <param name="viewStyle">The style to apply.</param>
81 [EditorBrowsable(EditorBrowsableState.Never)]
82 public override void ApplyStyle(ViewStyle viewStyle)
84 base.ApplyStyle(viewStyle);
86 SwitchStyle swStyle = viewStyle as SwitchStyle;
90 if (swStyle.Track != null)
92 Track.ApplyStyle(swStyle.Track);
95 if (swStyle.Thumb != null)
97 Thumb.ApplyStyle(swStyle.Thumb);
103 /// Switch's track part.
105 [EditorBrowsable(EditorBrowsableState.Never)]
106 public ImageView Track
112 track = new ImageView()
114 PositionUsesPivotPoint = true,
115 ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
116 PivotPoint = Tizen.NUI.PivotPoint.CenterLeft,
117 WidthResizePolicy = ResizePolicyType.FillToParent,
118 HeightResizePolicy = ResizePolicyType.FillToParent
121 var extension = (SwitchExtension)Extension;
122 if (extension != null)
124 track = extension.OnCreateTrack(this, track);
137 /// Switch's thumb part.
139 [EditorBrowsable(EditorBrowsableState.Never)]
140 public ImageView Thumb
146 thumb = new ImageView()
148 PositionUsesPivotPoint = true,
149 ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
150 PivotPoint = Tizen.NUI.PivotPoint.CenterLeft,
151 WidthResizePolicy = ResizePolicyType.Fixed,
152 HeightResizePolicy = ResizePolicyType.Fixed
155 var extension = (SwitchExtension)Extension;
156 if (extension != null)
158 thumb = extension.OnCreateThumb(this, thumb);
171 /// Switch's track part.
173 /// <since_tizen> 6 </since_tizen>
174 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
175 [EditorBrowsable(EditorBrowsableState.Never)]
176 public string SwitchBackgroundImageURL
180 return Style?.Track?.ResourceUrl?.All;
184 if (null != value && null != Style?.Track)
186 Style.Track.ResourceUrl = value;
192 /// Background image's resource url selector in Switch.
194 /// <since_tizen> 6 </since_tizen>
195 public StringSelector SwitchBackgroundImageURLSelector
199 StringSelector strSl = new StringSelector();
200 strSl.Clone(Style?.Track?.ResourceUrl);
205 if (null != value && null != Style?.Track)
207 Style.Track.ResourceUrl = value;
213 /// Handler image's resource url in Switch.
215 /// <since_tizen> 6 </since_tizen>
216 public string SwitchHandlerImageURL
220 return Style?.Thumb?.ResourceUrl?.All;
224 if (null != value && null != Style?.Thumb)
226 Style.Thumb.ResourceUrl = value;
232 /// Handler image's resource url selector in Switch.
234 /// <since_tizen> 6 </since_tizen>
235 public StringSelector SwitchHandlerImageURLSelector
239 StringSelector strSl = new StringSelector();
240 strSl.Clone(Style?.Thumb?.ResourceUrl);
245 if (null != value && null != Style?.Thumb)
247 Style.Thumb.ResourceUrl = value;
253 /// Handler image's size in Switch.
255 /// <since_tizen> 6 </since_tizen>
256 public Size SwitchHandlerImageSize
260 return Style?.Thumb?.Size;
264 if (null != Style?.Thumb)
266 Style.Thumb.Size = value;
272 /// Dispose Switch and all children on it.
274 /// <param name="type">Dispose type.</param>
275 /// <since_tizen> 6 </since_tizen>
276 protected override void Dispose(DisposeTypes type)
278 if (disposed) return;
280 if (type == DisposeTypes.Explicit)
282 Utility.Dispose(Thumb);
283 Utility.Dispose(Track);
290 /// Called after a key event is received by the view that has had its focus set.
292 /// <param name="key">The key event.</param>
293 /// <returns>True if the key event should be consumed.</returns>
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 public override bool OnKey(Key key)
299 if (!IsEnabled || null == key) return false;
301 bool ret = base.OnKey(key);
302 if (key.State == Key.StateType.Up)
304 if (key.KeyPressedName == "Return")
314 /// Called after a touch event is received by the owning view.<br />
315 /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
317 /// <param name="touch">The touch event.</param>
318 /// <returns>True if the event should be consumed.</returns>
319 /// <since_tizen> 6 </since_tizen>
320 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
321 [EditorBrowsable(EditorBrowsableState.Never)]
322 public override bool OnTouch(Touch touch)
324 if(!IsEnabled || null == touch) return false;
326 PointStateType state = touch.GetState(0);
327 bool ret = base.OnTouch(touch);
330 case PointStateType.Up:
340 /// Get Switch style.
342 /// <returns>The default switch style.</returns>
343 /// <since_tizen> 8 </since_tizen>
344 protected override ViewStyle CreateViewStyle()
346 return new SwitchStyle();
349 private void Initialize()
351 Style.IsSelectable = true;
355 /// Theme change callback when theme is changed, this callback will be trigger.
357 /// <param name="sender">The sender</param>
358 /// <param name="e">The event data</param>
359 /// <since_tizen> 8 </since_tizen>
360 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
362 SwitchStyle switchStyle = StyleManager.Instance.GetViewStyle(style) as SwitchStyle;
363 if (null != switchStyle)
365 Style.CopyFrom(switchStyle);
369 private void OnSelect()
371 if (SelectedEvent != null)
373 SelectEventArgs eventArgs = new SelectEventArgs();
374 eventArgs.IsSelected = IsSelected;
375 SelectedEvent(this, eventArgs);
380 /// SelectEventArgs is a class to record item selected arguments which will sent to user.
382 /// <since_tizen> 6 </since_tizen>
383 public class SelectEventArgs : EventArgs
385 /// <summary> Select state of Switch </summary>
386 /// <since_tizen> 6 </since_tizen>
387 public bool IsSelected;
391 /// Get current track part to the attached SwitchExtension.
394 /// It returns null if the passed extension is invaild.
396 /// <param name="extension">The extension instance that is currently attached to this Switch.</param>
397 /// <return>The switch's track part.</return>
398 [EditorBrowsable(EditorBrowsableState.Never)]
399 public ImageView GetCurrentTrack(SwitchExtension extension)
401 return (extension == Extension) ? Track : null;
405 /// Get current thumb part to the attached SwitchExtension.
408 /// It returns null if the passed extension is invaild.
410 /// <param name="extension">The extension instance that is currently attached to this Switch.</param>
411 /// <return>The switch's thumb part.</return>
412 [EditorBrowsable(EditorBrowsableState.Never)]
413 public ImageView GetCurrentThumb(SwitchExtension extension)
415 return (extension == Extension) ? Thumb : null;