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)
118 styleApplying = true;
120 base.ApplyStyle(viewStyle);
122 if (viewStyle is SwitchStyle switchStyle)
124 if (Extension != null) Extension.OnDispose(this);
126 if ((Extension = switchStyle.CreateExtension()) != null && Extension is SwitchExtension extension)
130 TextLabel.Unparent();
131 Icon = extension.OnCreateTrack(this, Icon);
132 thumb = extension.OnCreateThumb(this, thumb);
136 Icon.Relayout -= OnTrackOrThumbRelayout;
137 Icon.Relayout += OnTrackOrThumbRelayout;
139 thumb.Relayout -= OnTrackOrThumbRelayout;
140 thumb.Relayout += OnTrackOrThumbRelayout;
143 if (switchStyle.Track != null)
145 Track.ApplyStyle(switchStyle.Track);
148 if (switchStyle.Thumb != null)
150 Thumb.ApplyStyle(switchStyle.Thumb);
153 if (switchStyle.Text != null)
155 TextLabel.ThemeChangeSensitive = false;
156 TextLabel.ApplyStyle(switchStyle.Text);
159 styleApplying = false;
164 private void OnTrackOrThumbRelayout(object sender, EventArgs args)
166 if (Extension is SwitchExtension switchExtension)
168 switchExtension.OnTrackOrThumbResized(this, Icon, thumb);
173 /// Switch's track part.
175 /// <since_tizen> 8 </since_tizen>
176 public ImageView Track
186 /// Switch's thumb part.
188 /// <since_tizen> 8 </since_tizen>
189 public ImageView Thumb
199 /// Switch's track part image url selector.
201 /// <since_tizen> 6 </since_tizen>
202 public StringSelector SwitchBackgroundImageURLSelector
206 return GetValue(SwitchBackgroundImageURLSelectorProperty) as StringSelector;
210 SetValue(SwitchBackgroundImageURLSelectorProperty, value);
211 NotifyPropertyChanged();
214 private StringSelector InternalSwitchBackgroundImageURLSelector
216 get => Icon?.ResourceUrlSelector == null ? null : new StringSelector(Icon.ResourceUrlSelector);
219 Debug.Assert(Icon != null);
220 Icon.ResourceUrlSelector = value;
225 /// Handler image's resource url in Switch.
227 /// <since_tizen> 6 </since_tizen>
228 public string SwitchHandlerImageURL
232 return GetValue(SwitchHandlerImageURLProperty) as string;
236 SetValue(SwitchHandlerImageURLProperty, value);
237 NotifyPropertyChanged();
240 private string InternalSwitchHandlerImageURL
244 return Thumb.ResourceUrl;
248 Thumb.ResourceUrl = value;
253 /// Handler image's resource url selector in Switch.
254 /// Getter returns copied selector value if exist, null otherwise.
256 /// <since_tizen> 6 </since_tizen>
257 public StringSelector SwitchHandlerImageURLSelector
261 return GetValue(SwitchHandlerImageURLSelectorProperty) as StringSelector;
265 SetValue(SwitchHandlerImageURLSelectorProperty, value);
266 NotifyPropertyChanged();
269 private StringSelector InternalSwitchHandlerImageURLSelector
271 get => new StringSelector(thumb.ResourceUrlSelector);
274 Debug.Assert(thumb != null);
275 thumb.ResourceUrlSelector = value;
280 /// Handler image's size in Switch.
282 /// <since_tizen> 6 </since_tizen>
283 public Size SwitchHandlerImageSize
287 return GetValue(SwitchHandlerImageSizeProperty) as Size;
291 SetValue(SwitchHandlerImageSizeProperty, value);
292 NotifyPropertyChanged();
295 private Size InternalSwitchHandlerImageSize
308 /// Dispose Switch and all children on it.
310 /// <param name="type">Dispose type.</param>
311 /// <since_tizen> 6 </since_tizen>
312 protected override void Dispose(DisposeTypes type)
314 if (disposed) return;
316 if (type == DisposeTypes.Explicit)
320 Icon.Relayout -= OnTrackOrThumbRelayout;
324 thumb.Relayout -= OnTrackOrThumbRelayout;
325 Utility.Dispose(thumb);
333 /// Called after a key event is received by the view that has had its focus set.
335 /// <param name="key">The key event.</param>
336 /// <returns>True if the key event should be consumed.</returns>
337 /// <since_tizen> 8 </since_tizen>
338 public override bool OnKey(Key key)
340 return base.OnKey(key);
344 /// Called after a touch event is received by the owning view.<br />
345 /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
347 /// <param name="touch">The touch event.</param>
348 /// <returns>True if the event should be consumed.</returns>
349 /// <since_tizen> 8 </since_tizen>
350 [Obsolete("Deprecated in API8; Will be removed in API10. Please use OnClicked instead.")]
351 #pragma warning disable CS0809 // Obsolete member overrides non-obsolete member, It will be removed in API10
352 public override bool OnTouch(Touch touch)
353 #pragma warning restore CS0809 // Obsolete member overrides non-obsolete member, It will be removed in API10
355 return base.OnTouch(touch);
359 /// Get Switch style.
361 /// <returns>The default switch style.</returns>
362 /// <since_tizen> 8 </since_tizen>
363 protected override ViewStyle CreateViewStyle()
365 return new SwitchStyle();
369 [EditorBrowsable(EditorBrowsableState.Never)]
370 protected override ImageView CreateIcon()
372 var icon = new ImageView()
374 AccessibilityHidden = true,
375 EnableControlStatePropagation = true
378 thumb = new ImageView();
385 [EditorBrowsable(EditorBrowsableState.Never)]
386 protected override void OnControlStateChanged(ControlStateChangedEventArgs controlStateChangedInfo)
388 if (controlStateChangedInfo == null) throw new ArgumentNullException(nameof(controlStateChangedInfo));
389 base.OnControlStateChanged(controlStateChangedInfo);
393 if (controlStateChangedInfo.PreviousState.Contains(ControlState.Selected) != controlStateChangedInfo.CurrentState.Contains(ControlState.Selected))
400 private void OnSelect()
402 if (Accessibility.Accessibility.IsEnabled && IsHighlighted)
404 EmitAccessibilityStateChangedEvent(AccessibilityState.Checked, IsSelected);
407 ((SwitchExtension)Extension)?.OnSelectedChanged(this);
409 if (SelectedEvent != null)
411 SelectEventArgs eventArgs = new SelectEventArgs();
412 eventArgs.IsSelected = IsSelected;
413 SelectedEvent(this, eventArgs);
416 if (SelectedChanged != null)
418 SelectedChangedEventArgs eventArgs = new SelectedChangedEventArgs();
419 eventArgs.IsSelected = IsSelected;
420 SelectedChanged(this, eventArgs);
425 /// SelectEventArgs is a class to record item selected arguments which will sent to user.
427 /// <since_tizen> 6 </since_tizen>
428 /// It will be removed in API10
429 [Obsolete("Deprecated in API8; Will be removed in API10. Please use SelectedChangedEventArgs instead.")]
430 [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:Do not declare visible instance fields")]
431 [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
432 public class SelectEventArgs : EventArgs
434 /// <summary> Select state of Switch </summary>
435 /// <since_tizen> 6 </since_tizen>
436 public bool IsSelected;