2 * Copyright(c) 2021 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 System.Diagnostics;
20 using Tizen.NUI.BaseComponents;
21 using Tizen.NUI.Components.Extension;
23 namespace Tizen.NUI.Components
26 /// Switch is a kind of <see cref="Button"/> component that uses icon part as a toggle shape.
27 /// The icon part consists of track and thumb.
29 /// <since_tizen> 6 </since_tizen>
30 public partial class Switch : Button
32 private ImageView thumb = null;
37 /// Creates a new instance of a Switch.
39 /// <since_tizen> 6 </since_tizen>
40 public Switch() : base()
45 /// Creates a new instance of a Switch with style.
47 /// <param name="style">Create Switch by special style defined in UX.</param>
48 /// <since_tizen> 8 </since_tizen>
49 public Switch(string style) : base(style)
54 /// Creates a new instance of a Switch with style.
56 /// <param name="switchStyle">Create Switch by style customized by user.</param>
57 /// <since_tizen> 8 </since_tizen>
58 public Switch(SwitchStyle switchStyle) : base(switchStyle)
63 /// Initialize AT-SPI object.
65 [EditorBrowsable(EditorBrowsableState.Never)]
66 public override void OnInitialize()
69 AccessibilityRole = Role.ToggleButton;
77 /// Informs AT-SPI bridge about the set of AT-SPI states associated with this object.
79 [EditorBrowsable(EditorBrowsableState.Never)]
80 protected override AccessibilityStates AccessibilityCalculateStates()
82 var states = base.AccessibilityCalculateStates();
84 states[AccessibilityState.Checked] = this.IsSelected;
90 /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
92 /// <since_tizen> 6 </since_tizen>
93 [Obsolete("Deprecated in API8; Will be removed in API10. Please use SelectedChanged event instead.")]
94 public event EventHandler<SelectEventArgs> SelectedEvent;
97 /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.
99 /// <since_tizen> 8 </since_tizen>
100 public event EventHandler<SelectedChangedEventArgs> SelectedChanged;
103 /// Return currently applied style.
106 /// Modifying contents in style may cause unexpected behaviour.
108 /// <since_tizen> 8 </since_tizen>
109 public new SwitchStyle Style => (SwitchStyle)(ViewStyle as SwitchStyle)?.Clone();
112 /// Apply style to switch.
114 /// <param name="viewStyle">The style to apply.</param>
115 [EditorBrowsable(EditorBrowsableState.Never)]
116 public override void ApplyStyle(ViewStyle viewStyle)
120 base.ApplyStyle(viewStyle);
122 if (viewStyle is SwitchStyle switchStyle)
124 if (Extension is SwitchExtension extension)
126 if (extension.ProcessThumb(this, ref thumb))
131 Icon.Relayout -= OnTrackOrThumbRelayout;
132 Icon.Relayout += OnTrackOrThumbRelayout;
134 thumb.Relayout -= OnTrackOrThumbRelayout;
135 thumb.Relayout += OnTrackOrThumbRelayout;
138 if (switchStyle.Track != null)
140 Track.ApplyStyle(switchStyle.Track);
143 if (switchStyle.Thumb != null)
145 Thumb.ApplyStyle(switchStyle.Thumb);
153 private void OnTrackOrThumbRelayout(object sender, EventArgs args)
155 if (Extension is SwitchExtension switchExtension)
157 switchExtension.OnTrackOrThumbResized(this, Icon, thumb);
162 /// Switch's track part.
164 /// <since_tizen> 8 </since_tizen>
165 public ImageView Track
175 /// Switch's thumb part.
177 /// <since_tizen> 8 </since_tizen>
178 public ImageView Thumb
188 /// Switch's track part image url selector.
190 /// <since_tizen> 6 </since_tizen>
191 public StringSelector SwitchBackgroundImageURLSelector
195 return GetValue(SwitchBackgroundImageURLSelectorProperty) as StringSelector;
199 SetValue(SwitchBackgroundImageURLSelectorProperty, value);
200 NotifyPropertyChanged();
203 private StringSelector InternalSwitchBackgroundImageURLSelector
205 get => Icon?.ResourceUrlSelector == null ? null : new StringSelector(Icon.ResourceUrlSelector);
208 Debug.Assert(Icon != null);
209 Icon.ResourceUrlSelector = value;
214 /// Handler image's resource url in Switch.
216 /// <since_tizen> 6 </since_tizen>
217 public string SwitchHandlerImageURL
221 return GetValue(SwitchHandlerImageURLProperty) as string;
225 SetValue(SwitchHandlerImageURLProperty, value);
226 NotifyPropertyChanged();
229 private string InternalSwitchHandlerImageURL
233 return Thumb.ResourceUrl;
237 Thumb.ResourceUrl = value;
242 /// Handler image's resource url selector in Switch.
243 /// Getter returns copied selector value if exist, null otherwise.
245 /// <since_tizen> 6 </since_tizen>
246 public StringSelector SwitchHandlerImageURLSelector
250 return GetValue(SwitchHandlerImageURLSelectorProperty) as StringSelector;
254 SetValue(SwitchHandlerImageURLSelectorProperty, value);
255 NotifyPropertyChanged();
258 private StringSelector InternalSwitchHandlerImageURLSelector
260 get => new StringSelector(thumb.ResourceUrlSelector);
263 Debug.Assert(thumb != null);
264 thumb.ResourceUrlSelector = value;
269 /// Handler image's size in Switch.
271 /// <since_tizen> 6 </since_tizen>
272 public Size SwitchHandlerImageSize
276 return GetValue(SwitchHandlerImageSizeProperty) as Size;
280 SetValue(SwitchHandlerImageSizeProperty, value);
281 NotifyPropertyChanged();
284 private Size InternalSwitchHandlerImageSize
297 /// Dispose Switch and all children on it.
299 /// <param name="type">Dispose type.</param>
300 /// <since_tizen> 6 </since_tizen>
301 protected override void Dispose(DisposeTypes type)
303 if (disposed) return;
305 if (type == DisposeTypes.Explicit)
309 Icon.Relayout -= OnTrackOrThumbRelayout;
313 thumb.Relayout -= OnTrackOrThumbRelayout;
314 Utility.Dispose(thumb);
322 /// Called after a key event is received by the view that has had its focus set.
324 /// <param name="key">The key event.</param>
325 /// <returns>True if the key event should be consumed.</returns>
326 /// <since_tizen> 8 </since_tizen>
327 public override bool OnKey(Key key)
329 return base.OnKey(key);
333 /// Called after a touch event is received by the owning view.<br />
334 /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
336 /// <param name="touch">The touch event.</param>
337 /// <returns>True if the event should be consumed.</returns>
338 /// <since_tizen> 8 </since_tizen>
339 [Obsolete("Deprecated in API8; Will be removed in API10. Please use OnClicked instead.")]
340 #pragma warning disable CS0809 // Obsolete member overrides non-obsolete member, It will be removed in API10
341 public override bool OnTouch(Touch touch)
342 #pragma warning restore CS0809 // Obsolete member overrides non-obsolete member, It will be removed in API10
344 return base.OnTouch(touch);
348 /// Get Switch style.
350 /// <returns>The default switch style.</returns>
351 /// <since_tizen> 8 </since_tizen>
352 protected override ViewStyle CreateViewStyle()
354 return new SwitchStyle();
358 [EditorBrowsable(EditorBrowsableState.Never)]
359 protected override ImageView CreateIcon()
361 var icon = new ImageView()
363 AccessibilityHidden = true,
364 EnableControlStatePropagation = true
367 thumb = new ImageView();
374 [EditorBrowsable(EditorBrowsableState.Never)]
375 protected override void OnControlStateChanged(ControlStateChangedEventArgs controlStateChangedInfo)
377 if (controlStateChangedInfo == null) throw new ArgumentNullException(nameof(controlStateChangedInfo));
378 base.OnControlStateChanged(controlStateChangedInfo);
382 if (controlStateChangedInfo.PreviousState.Contains(ControlState.Selected) != controlStateChangedInfo.CurrentState.Contains(ControlState.Selected))
389 private void OnSelect()
391 if (Accessibility.Accessibility.IsEnabled && IsHighlighted)
393 EmitAccessibilityStateChangedEvent(AccessibilityState.Checked, IsSelected);
396 ((SwitchExtension)Extension)?.OnSelectedChanged(this);
398 if (SelectedEvent != null)
400 SelectEventArgs eventArgs = new SelectEventArgs();
401 eventArgs.IsSelected = IsSelected;
402 SelectedEvent(this, eventArgs);
405 if (SelectedChanged != null)
407 SelectedChangedEventArgs eventArgs = new SelectedChangedEventArgs();
408 eventArgs.IsSelected = IsSelected;
409 SelectedChanged(this, eventArgs);
414 /// SelectEventArgs is a class to record item selected arguments which will sent to user.
416 /// <since_tizen> 6 </since_tizen>
417 /// It will be removed in API10
418 [Obsolete("Deprecated in API8; Will be removed in API10. Please use SelectedChangedEventArgs instead.")]
419 [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:Do not declare visible instance fields")]
420 [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
421 public class SelectEventArgs : EventArgs
423 /// <summary> Select state of Switch </summary>
424 /// <since_tizen> 6 </since_tizen>
425 public bool IsSelected;