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;
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> 6 </since_tizen>
50 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
51 [EditorBrowsable(EditorBrowsableState.Never)]
52 public Switch(string style) : base(style)
58 /// Creates a new instance of a Switch with style.
60 /// <param name="style">Create Switch by style customized by user.</param>
61 /// <since_tizen> 6 </since_tizen>
62 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
63 [EditorBrowsable(EditorBrowsableState.Never)]
64 public Switch(SwitchStyle style) : base(style)
70 /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
72 /// <since_tizen> 6 </since_tizen>
73 public event EventHandler<SelectEventArgs> SelectedEvent;
75 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
76 [EditorBrowsable(EditorBrowsableState.Never)]
77 public new SwitchStyle Style => ViewStyle as SwitchStyle;
80 /// Background image's resource url in Switch.
82 /// <since_tizen> 6 </since_tizen>
83 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
84 [EditorBrowsable(EditorBrowsableState.Never)]
85 public string SwitchBackgroundImageURL
89 return Style?.Track?.ResourceUrl?.All;
93 if (null != value && null != Style?.Track)
95 Style.Track.ResourceUrl = value;
101 /// Background image's resource url selector in Switch.
103 /// <since_tizen> 6 </since_tizen>
104 public StringSelector SwitchBackgroundImageURLSelector
108 StringSelector strSl = new StringSelector();
109 strSl.Clone(Style?.Track?.ResourceUrl);
114 if (null != value && null != Style?.Track)
116 Style.Track.ResourceUrl = value;
122 /// Handler image's resource url in Switch.
124 /// <since_tizen> 6 </since_tizen>
125 public string SwitchHandlerImageURL
129 return Style?.Thumb?.ResourceUrl?.All;
133 if (null != value && null != Style?.Thumb)
135 Style.Thumb.ResourceUrl = value;
141 /// Handler image's resource url selector in Switch.
143 /// <since_tizen> 6 </since_tizen>
144 public StringSelector SwitchHandlerImageURLSelector
148 StringSelector strSl = new StringSelector();
149 strSl.Clone(Style?.Thumb?.ResourceUrl);
154 if (null != value && null != Style?.Thumb)
156 Style.Thumb.ResourceUrl = value;
162 /// Handler image's size in Switch.
164 /// <since_tizen> 6 </since_tizen>
165 public Size SwitchHandlerImageSize
169 return Style?.Thumb?.Size;
173 if (null != Style?.Thumb)
175 Style.Thumb.Size = value;
181 /// Dispose Switch and all children on it.
183 /// <param name="type">Dispose type.</param>
184 /// <since_tizen> 6 </since_tizen>
185 protected override void Dispose(DisposeTypes type)
187 if (disposed) return;
189 if (type == DisposeTypes.Explicit)
191 if (null != handlerAni)
193 if (handlerAni.State == Animation.States.Playing)
197 handlerAni.Dispose();
201 Utility.Dispose(thumbImage);
202 Utility.Dispose(trackImage);
209 /// Called after a key event is received by the view that has had its focus set.
211 /// <param name="key">The key event.</param>
212 /// <returns>True if the key event should be consumed.</returns>
213 /// <since_tizen> 6 </since_tizen>
214 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
215 [EditorBrowsable(EditorBrowsableState.Never)]
216 public override bool OnKey(Key key)
218 if (!IsEnabled || null == key) return false;
220 bool ret = base.OnKey(key);
221 if (key.State == Key.StateType.Up)
223 if (key.KeyPressedName == "Return")
233 /// Called after a touch event is received by the owning view.<br />
234 /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
236 /// <param name="touch">The touch event.</param>
237 /// <returns>True if the event should be consumed.</returns>
238 /// <since_tizen> 6 </since_tizen>
239 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
240 [EditorBrowsable(EditorBrowsableState.Never)]
241 public override bool OnTouch(Touch touch)
243 if(!IsEnabled || null == touch) return false;
245 PointStateType state = touch.GetState(0);
246 bool ret = base.OnTouch(touch);
249 case PointStateType.Up:
259 /// Get Switch attribues.
261 /// <since_tizen> 6 </since_tizen>
262 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
263 [EditorBrowsable(EditorBrowsableState.Never)]
264 protected override ViewStyle GetViewStyle()
266 return new SwitchStyle();
269 private void Initialize()
271 Style.IsSelectable = true;
272 handlerAni = new Animation(aniTime);
273 trackImage = new ImageView()
275 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
276 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
277 PositionUsesPivotPoint = true,
278 WidthResizePolicy = ResizePolicyType.FillToParent,
279 HeightResizePolicy = ResizePolicyType.FillToParent,
280 Name = "SwitchBackgroundImage",
283 trackImage.ApplyStyle(Style.Track);
285 thumbImage = new ImageView()
287 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
288 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
289 PositionUsesPivotPoint = true,
290 Name = "SwitchHandlerImage",
292 trackImage.Add(thumbImage);
293 thumbImage.ApplyStyle(Style.Thumb);
297 /// Theme change callback when theme is changed, this callback will be trigger.
299 /// <since_tizen> 6 </since_tizen>
300 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
301 [EditorBrowsable(EditorBrowsableState.Never)]
302 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
304 SwitchStyle tempAttributes = StyleManager.Instance.GetViewStyle(style) as SwitchStyle;
305 if (null != tempAttributes)
307 Style.CopyFrom(tempAttributes);
311 private void OnSelect()
313 if (handlerAni.State == Animation.States.Playing)
318 handlerAni.AnimateTo(thumbImage, "PositionX", Size2D.Width - thumbImage.Size2D.Width - thumbImage.Position2D.X);
319 trackImage.Opacity = 0.5f; ///////need defined by UX
320 handlerAni.AnimateTo(trackImage, "Opacity", 1);
323 if (SelectedEvent != null)
325 SelectEventArgs eventArgs = new SelectEventArgs();
326 eventArgs.IsSelected = IsSelected;
327 SelectedEvent(this, eventArgs);
332 /// SelectEventArgs is a class to record item selected arguments which will sent to user.
334 /// <since_tizen> 6 </since_tizen>
335 public class SelectEventArgs : EventArgs
337 /// <summary> Select state of Switch </summary>
338 /// <since_tizen> 6 </since_tizen>
339 public bool IsSelected;