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 Tizen.NUI.BaseComponents;
19 using System.ComponentModel;
21 namespace Tizen.NUI.Components
24 /// Switch is one kind of common component, it can be used as selector.
25 /// User can handle Navigation by adding/inserting/deleting NavigationItem.
27 /// <since_tizen> 6 </since_tizen>
28 public class Switch : Button
30 private const int aniTime = 100; // will be defined in const file later
31 private ImageView trackImage;
32 private ImageView thumbImage;
33 private Animation handlerAni = null;
36 /// Creates a new instance of a Switch.
38 /// <since_tizen> 6 </since_tizen>
39 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> 6 </since_tizen>
49 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
50 [EditorBrowsable(EditorBrowsableState.Never)]
51 public Switch(string style) : base(style)
57 /// Creates a new instance of a Switch with style.
59 /// <param name="style">Create Switch by style customized by user.</param>
60 /// <since_tizen> 6 </since_tizen>
61 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
62 [EditorBrowsable(EditorBrowsableState.Never)]
63 public Switch(SwitchStyle style) : base(style)
69 /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
71 /// <since_tizen> 6 </since_tizen>
72 public event EventHandler<SelectEventArgs> SelectedEvent;
74 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
75 [EditorBrowsable(EditorBrowsableState.Never)]
76 public new SwitchStyle Style => ViewStyle as SwitchStyle;
79 /// Background image's resource url in Switch.
81 /// <since_tizen> 6 </since_tizen>
82 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
83 [EditorBrowsable(EditorBrowsableState.Never)]
84 public string SwitchBackgroundImageURL
88 return Style?.Track?.ResourceUrl?.All;
92 if (null != value && null != Style?.Track)
94 Style.Track.ResourceUrl = value;
99 private StringSelector switchBackgroundImageURLSelector = new StringSelector();
101 /// Background image's resource url selector in Switch.
103 /// <since_tizen> 6 </since_tizen>
104 public StringSelector SwitchBackgroundImageURLSelector
108 return switchBackgroundImageURLSelector;
112 switchBackgroundImageURLSelector.Clone(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;
135 private StringSelector switchHandlerImageURLSelector = new StringSelector();
137 /// Handler image's resource url selector in Switch.
139 /// <since_tizen> 6 </since_tizen>
140 public StringSelector SwitchHandlerImageURLSelector
144 return switchHandlerImageURLSelector;
148 switchHandlerImageURLSelector.Clone(value);
153 /// Handler image's size in Switch.
155 /// <since_tizen> 6 </since_tizen>
156 public Size SwitchHandlerImageSize
160 return Style?.Thumb?.Size;
164 if (null != Style?.Thumb)
166 Style.Thumb.Size = value;
172 /// Dispose Switch and all children on it.
174 /// <param name="type">Dispose type.</param>
175 /// <since_tizen> 6 </since_tizen>
176 protected override void Dispose(DisposeTypes type)
178 if (disposed) return;
180 if (type == DisposeTypes.Explicit)
182 if (null != handlerAni)
184 if (handlerAni.State == Animation.States.Playing)
188 handlerAni.Dispose();
192 Utility.Dispose(thumbImage);
193 Utility.Dispose(trackImage);
200 /// Called after a key event is received by the view that has had its focus set.
202 /// <param name="key">The key event.</param>
203 /// <returns>True if the key event should be consumed.</returns>
204 /// <since_tizen> 6 </since_tizen>
205 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
206 [EditorBrowsable(EditorBrowsableState.Never)]
207 public override bool OnKey(Key key)
209 if (!IsEnabled || null == key) return false;
211 bool ret = base.OnKey(key);
212 if (key.State == Key.StateType.Up)
214 if (key.KeyPressedName == "Return")
224 /// Called after a touch event is received by the owning view.<br />
225 /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
227 /// <param name="touch">The touch event.</param>
228 /// <returns>True if the event should be consumed.</returns>
229 /// <since_tizen> 6 </since_tizen>
230 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
231 [EditorBrowsable(EditorBrowsableState.Never)]
232 public override bool OnTouch(Touch touch)
234 if(!IsEnabled || null == touch) return false;
236 PointStateType state = touch.GetState(0);
237 bool ret = base.OnTouch(touch);
240 case PointStateType.Up:
250 /// Get Switch attribues.
252 /// <since_tizen> 6 </since_tizen>
253 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
254 [EditorBrowsable(EditorBrowsableState.Never)]
255 protected override ViewStyle GetViewStyle()
257 return new SwitchStyle();
260 private void Initialize()
262 Style.IsSelectable = true;
263 handlerAni = new Animation(aniTime);
264 trackImage = new ImageView()
266 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
267 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
268 PositionUsesPivotPoint = true,
269 WidthResizePolicy = ResizePolicyType.FillToParent,
270 HeightResizePolicy = ResizePolicyType.FillToParent,
271 Name = "SwitchBackgroundImage",
274 trackImage.ApplyStyle(Style.Track);
276 thumbImage = new ImageView()
278 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
279 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
280 PositionUsesPivotPoint = true,
281 Name = "SwitchHandlerImage",
283 trackImage.Add(thumbImage);
284 thumbImage.ApplyStyle(Style.Thumb);
288 /// Theme change callback when theme is changed, this callback will be trigger.
290 /// <since_tizen> 6 </since_tizen>
291 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
292 [EditorBrowsable(EditorBrowsableState.Never)]
293 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
295 SwitchStyle tempAttributes = StyleManager.Instance.GetViewStyle(style) as SwitchStyle;
296 if (null != tempAttributes)
298 Style.CopyFrom(tempAttributes);
302 private void OnSelect()
304 if (handlerAni.State == Animation.States.Playing)
309 handlerAni.AnimateTo(thumbImage, "PositionX", Size2D.Width - thumbImage.Size2D.Width - thumbImage.Position2D.X);
310 trackImage.Opacity = 0.5f; ///////need defined by UX
311 handlerAni.AnimateTo(trackImage, "Opacity", 1);
314 if (SelectedEvent != null)
316 SelectEventArgs eventArgs = new SelectEventArgs();
317 eventArgs.IsSelected = IsSelected;
318 SelectedEvent(this, eventArgs);
323 /// SelectEventArgs is a class to record item selected arguments which will sent to user.
325 /// <since_tizen> 6 </since_tizen>
326 public class SelectEventArgs : EventArgs
328 /// <summary> Select state of Switch </summary>
329 /// <since_tizen> 6 </since_tizen>
330 public bool IsSelected;