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;
94 if (Style.Track.ResourceUrl == null)
96 Style.Track.ResourceUrl = new StringSelector();
98 Style.Track.ResourceUrl.All = value;
104 /// Background image's resource url selector in Switch.
106 /// <since_tizen> 6 </since_tizen>
107 public StringSelector SwitchBackgroundImageURLSelector
111 return (StringSelector)Style.Track?.ResourceUrl;
117 Style.Track.ResourceUrl = value.Clone() as StringSelector;
123 /// Handler image's resource url in Switch.
125 /// <since_tizen> 6 </since_tizen>
126 public string SwitchHandlerImageURL
130 return Style.Thumb?.ResourceUrl?.All;
136 if (Style.Thumb.ResourceUrl == null)
138 Style.Thumb.ResourceUrl = new StringSelector();
140 Style.Thumb.ResourceUrl.All = value;
146 /// Handler image's resource url selector in Switch.
148 /// <since_tizen> 6 </since_tizen>
149 public StringSelector SwitchHandlerImageURLSelector
153 return (StringSelector)Style.Thumb?.ResourceUrl;
159 Style.Thumb.ResourceUrl = value.Clone() as StringSelector;
165 /// Handler image's size in Switch.
167 /// <since_tizen> 6 </since_tizen>
168 public Size SwitchHandlerImageSize
172 return Style.Thumb?.Size ?? new Size(0, 0);
176 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) 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) 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.GetAttributes(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;