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 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
29 [EditorBrowsable(EditorBrowsableState.Never)]
30 public class Switch : Button
32 private const int aniTime = 100; // will be defined in const file later
33 private ImageView switchBackgroundImage;
34 private ImageView switchHandlerImage;
35 private Animation handlerAni = null;
36 private SwitchAttributes switchAttributes;
39 /// Creates a new instance of a Switch.
41 /// <since_tizen> 6 </since_tizen>
42 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
43 [EditorBrowsable(EditorBrowsableState.Never)]
44 public Switch() : base()
49 /// Creates a new instance of a Switch with style.
51 /// <param name="style">Create Switch by special style defined in UX.</param>
52 /// <since_tizen> 6 </since_tizen>
53 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
54 [EditorBrowsable(EditorBrowsableState.Never)]
55 public Switch(string style) : base(style)
60 /// Creates a new instance of a Switch with attributes.
62 /// <param name="attrs">Create Switch by attributes customized by user.</param>
63 /// <since_tizen> 6 </since_tizen>
64 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
65 [EditorBrowsable(EditorBrowsableState.Never)]
66 public Switch(SwitchAttributes attrs) : base(attrs)
72 /// An event for the item selected signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
74 /// <since_tizen> 6 </since_tizen>
75 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
76 [EditorBrowsable(EditorBrowsableState.Never)]
77 public event EventHandler<SelectEventArgs> SelectedEvent;
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 switchAttributes?.SwitchBackgroundImageAttributes?.ResourceURL?.All;
95 CreateSwitchBackgroundImageAttributes();
96 if (switchAttributes.SwitchBackgroundImageAttributes.ResourceURL == null)
98 switchAttributes.SwitchBackgroundImageAttributes.ResourceURL = new StringSelector();
100 switchAttributes.SwitchBackgroundImageAttributes.ResourceURL.All = value;
107 /// Background image's resource url selector in Switch.
109 /// <since_tizen> 6 </since_tizen>
110 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
111 [EditorBrowsable(EditorBrowsableState.Never)]
112 public StringSelector SwitchBackgroundImageURLSelector
116 return switchAttributes?.SwitchBackgroundImageAttributes?.ResourceURL;
122 CreateSwitchBackgroundImageAttributes();
123 switchAttributes.SwitchBackgroundImageAttributes.ResourceURL = value.Clone() as StringSelector;
130 /// Handler image's resource url in Switch.
132 /// <since_tizen> 6 </since_tizen>
133 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
134 [EditorBrowsable(EditorBrowsableState.Never)]
135 public string SwitchHandlerImageURL
139 return switchAttributes?.SwitchHandlerImageAttributes?.ResourceURL?.All;
145 CreateSwitchHandlerImageAttributes();
146 if (switchAttributes.SwitchHandlerImageAttributes.ResourceURL == null)
148 switchAttributes.SwitchHandlerImageAttributes.ResourceURL = new StringSelector();
150 switchAttributes.SwitchHandlerImageAttributes.ResourceURL.All = value;
157 /// Handler image's resource url selector in Switch.
159 /// <since_tizen> 6 </since_tizen>
160 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
161 [EditorBrowsable(EditorBrowsableState.Never)]
162 public StringSelector SwitchHandlerImageURLSelector
166 return switchAttributes?.SwitchHandlerImageAttributes?.ResourceURL;
172 CreateSwitchHandlerImageAttributes();
173 switchAttributes.SwitchHandlerImageAttributes.ResourceURL = value.Clone() as StringSelector;
180 /// Handler image's size in Switch.
182 /// <since_tizen> 6 </since_tizen>
183 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
184 [EditorBrowsable(EditorBrowsableState.Never)]
185 public Size2D SwitchHandlerImageSize2D
189 return switchAttributes?.SwitchHandlerImageAttributes?.Size2D ?? new Size2D(0, 0);
193 CreateSwitchHandlerImageAttributes();
194 switchAttributes.SwitchHandlerImageAttributes.Size2D = value;
200 /// Dispose Switch and all children on it.
202 /// <param name="type">Dispose type.</param>
203 /// <since_tizen> 6 </since_tizen>
204 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
205 [EditorBrowsable(EditorBrowsableState.Never)]
206 protected override void Dispose(DisposeTypes type)
213 if (type == DisposeTypes.Explicit)
215 if (handlerAni != null)
217 if (handlerAni.State == Animation.States.Playing)
221 handlerAni.Dispose();
225 if (switchHandlerImage != null)
227 Utility.Dispose(switchHandlerImage);
229 if (switchBackgroundImage != null)
231 Utility.Dispose(switchBackgroundImage);
239 /// Update Switch by attributes.
241 /// <since_tizen> 6 </since_tizen>
242 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
243 [EditorBrowsable(EditorBrowsableState.Never)]
244 protected override void OnUpdate()
248 if (switchAttributes.SwitchBackgroundImageAttributes != null)
250 if (switchBackgroundImage == null)
252 switchBackgroundImage = new ImageView()
254 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
255 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
256 PositionUsesPivotPoint = true,
257 WidthResizePolicy = ResizePolicyType.FillToParent,
258 HeightResizePolicy = ResizePolicyType.FillToParent,
260 switchBackgroundImage.Name = "SwitchBackgroundImage";
261 Add(switchBackgroundImage);
263 ApplyAttributes(switchBackgroundImage, switchAttributes.SwitchBackgroundImageAttributes);
265 if (switchAttributes.SwitchHandlerImageAttributes != null)
267 if (switchHandlerImage == null)
269 switchHandlerImage = new ImageView()
271 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
272 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
273 PositionUsesPivotPoint = true,
275 switchHandlerImage.Name = "SwitchHandlerImage";
276 switchBackgroundImage.Add(switchHandlerImage);
278 ApplyAttributes(switchHandlerImage, switchAttributes.SwitchHandlerImageAttributes);
284 /// Called after a key event is received by the view that has had its focus set.
286 /// <param name="key">The key event.</param>
287 /// <returns>True if the key event should be consumed.</returns>
288 /// <since_tizen> 6 </since_tizen>
289 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
290 [EditorBrowsable(EditorBrowsableState.Never)]
291 public override bool OnKey(Key key)
293 if (IsEnabled == false)
297 bool ret = base.OnKey(key);
298 if (key.State == Key.StateType.Up)
300 if (key.KeyPressedName == "Return")
310 /// Called after a touch event is received by the owning view.<br />
311 /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
313 /// <param name="touch">The touch event.</param>
314 /// <returns>True if the event should be consumed.</returns>
315 /// <since_tizen> 6 </since_tizen>
316 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
317 [EditorBrowsable(EditorBrowsableState.Never)]
318 public override bool OnTouch(Touch touch)
320 if(IsEnabled == false)
324 PointStateType state = touch.GetState(0);
325 bool ret = base.OnTouch(touch);
328 case PointStateType.Up:
338 /// Get Switch attribues.
340 /// <since_tizen> 6 </since_tizen>
341 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
342 [EditorBrowsable(EditorBrowsableState.Never)]
343 protected override Attributes GetAttributes()
345 return new SwitchAttributes();
348 private void Initialize()
350 switchAttributes = attributes as SwitchAttributes;
351 if (switchAttributes == null)
353 throw new Exception("Switch attribute parse error.");
356 switchAttributes.IsSelectable = true;
357 CreateHandlerAnimation();
361 /// Theme change callback when theme is changed, this callback will be trigger.
363 /// <since_tizen> 6 </since_tizen>
364 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
365 [EditorBrowsable(EditorBrowsableState.Never)]
366 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
368 SwitchAttributes tempAttributes = StyleManager.Instance.GetAttributes(style) as SwitchAttributes;
369 if (tempAttributes != null)
371 attributes = switchAttributes = tempAttributes;
376 private void CreateSwitchBackgroundImageAttributes()
378 if (switchAttributes.SwitchBackgroundImageAttributes == null)
380 switchAttributes.SwitchBackgroundImageAttributes = new ImageAttributes()
382 PositionUsesPivotPoint = true,
383 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
384 PivotPoint = Tizen.NUI.PivotPoint.TopLeft,
389 private void CreateSwitchHandlerImageAttributes()
391 if (switchAttributes.SwitchHandlerImageAttributes == null)
393 switchAttributes.SwitchHandlerImageAttributes = new ImageAttributes()
395 PositionUsesPivotPoint = true,
400 private void CreateHandlerAnimation()
402 if (handlerAni == null)
404 handlerAni = new Animation(aniTime);
408 private void OnSelect()
410 if (handlerAni.State == Animation.States.Playing)
415 if (switchHandlerImage != null)
417 handlerAni.AnimateTo(switchHandlerImage, "PositionX", Size2D.Width - switchHandlerImage.Size2D.Width - switchHandlerImage.Position2D.X);
419 if (switchBackgroundImage != null)
421 switchBackgroundImage.Opacity = 0.5f; ///////need defined by UX
422 handlerAni.AnimateTo(switchBackgroundImage, "Opacity", 1);
426 if (SelectedEvent != null)
428 SelectEventArgs eventArgs = new SelectEventArgs();
429 eventArgs.IsSelected = IsSelected;
430 SelectedEvent(this, eventArgs);
435 /// SelectEventArgs is a class to record item selected arguments which will sent to user.
437 /// <since_tizen> 6 </since_tizen>
438 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
439 [EditorBrowsable(EditorBrowsableState.Never)]
440 public class SelectEventArgs : EventArgs
442 /// <summary> Select state of Switch </summary>
443 /// <since_tizen> 6 </since_tizen>
444 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
445 [EditorBrowsable(EditorBrowsableState.Never)]
446 public bool IsSelected;