2 * Copyright(c) 2020 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.
19 using System.ComponentModel;
20 using System.Reflection;
21 using System.Runtime.InteropServices;
22 using Tizen.NUI.Binding;
24 namespace Tizen.NUI.BaseComponents
27 /// View is the base class for all views.
29 /// <since_tizen> 3 </since_tizen>
30 public partial class View
33 /// Perform an action on a visual registered to this view. <br />
34 /// Visuals will have actions. This API is used to perform one of these actions with the given attributes.
36 /// <param name="propertyIndexOfVisual">The Property index of the visual.</param>
37 /// <param name="propertyIndexOfActionId">The action to perform. See Visual to find the supported actions.</param>
38 /// <param name="attributes">Optional attributes for the action.</param>
39 /// <since_tizen> 5 </since_tizen>
40 public void DoAction(int propertyIndexOfVisual, int propertyIndexOfActionId, PropertyValue attributes)
42 Interop.View.DoAction(SwigCPtr, propertyIndexOfVisual, propertyIndexOfActionId, PropertyValue.getCPtr(attributes));
43 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
47 /// Creates an animation to animate the background color visual. If there is no
48 /// background visual, creates one with transparent black as it's mixColor.
50 /// <since_tizen> 3 </since_tizen>
51 public Animation AnimateBackgroundColor(object destinationValue,
54 AlphaFunction.BuiltinFunctions? alphaFunction = null,
55 object initialValue = null)
57 Tizen.NUI.PropertyMap background = Background;
59 if (background.Empty())
61 // If there is no background yet, ensure there is a transparent
63 BackgroundColor = new Color(0.0f, 0.0f, 0.0f, 0.0f);
64 background = Background;
66 return AnimateColor("background", destinationValue, startTime, endTime, alphaFunction, initialValue);
70 /// Creates an animation to animate the mixColor of the named visual.
72 /// <since_tizen> 3 </since_tizen>
73 public Animation AnimateColor(string targetVisual, object destinationColor, int startTime, int endTime, AlphaFunction.BuiltinFunctions? alphaFunction = null, object initialColor = null)
75 Animation animation = null;
76 using (PropertyMap animator = new PropertyMap())
77 using (PropertyMap timePeriod = new PropertyMap())
78 using (PropertyValue pvDuration = new PropertyValue((endTime - startTime) / 1000.0f))
79 using (PropertyValue pvDelay = new PropertyValue(startTime / 1000.0f))
80 using (PropertyMap transition = new PropertyMap())
81 using (PropertyValue pvTarget = new PropertyValue(targetVisual))
82 using (PropertyValue pvProperty = new PropertyValue("mixColor"))
83 using (PropertyValue destValue = PropertyValue.CreateFromObject(destinationColor))
85 if (alphaFunction != null)
87 using (PropertyValue pvAlpha = new PropertyValue(AlphaFunction.BuiltinToPropertyKey(alphaFunction)))
89 animator.Add("alphaFunction", pvAlpha);
93 timePeriod.Add("duration", pvDuration);
94 timePeriod.Add("delay", pvDelay);
95 using (PropertyValue pvTimePeriod = new PropertyValue(timePeriod))
97 animator.Add("timePeriod", pvTimePeriod);
99 using (PropertyValue pvAnimator = new PropertyValue(animator))
101 transition.Add("animator", pvAnimator);
103 transition.Add("target", pvTarget);
104 transition.Add("property", pvProperty);
106 if (initialColor != null)
108 using (PropertyValue initValue = PropertyValue.CreateFromObject(initialColor))
110 transition.Add("initialValue", initValue);
114 transition.Add("targetValue", destValue);
115 using (TransitionData transitionData = new TransitionData(transition))
117 animation = new Animation(Interop.View.CreateTransition(SwigCPtr, TransitionData.getCPtr(transitionData)), true);
119 if (NDalicPINVOKE.SWIGPendingException.Pending)
120 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
125 // From Container Base class
127 /// Adds a child view to this view.
129 /// <seealso cref="Container.Add" />
130 /// <since_tizen> 4 </since_tizen>
131 public override void Add(View child)
133 bool hasLayout = (layout != null);
137 Tizen.Log.Fatal("NUI", "Child is null");
141 Container oldParent = child.GetParent();
142 if (oldParent != this)
144 // If child already has a parent then re-parent child
145 if (oldParent != null)
147 if (child.Layout != null)
149 child.Layout.SetReplaceFlag();
151 oldParent.Remove(child);
153 child.InternalParent = this;
155 Interop.Actor.Add(SwigCPtr, View.getCPtr(child));
157 if (NDalicPINVOKE.SWIGPendingException.Pending)
158 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
161 if (ChildAdded != null)
163 ChildAddedEventArgs e = new ChildAddedEventArgs
169 BindableObject.SetInheritedBindingContext(child, this?.BindingContext);
174 /// Removes a child view from this View. If the view was not a child of this view, this is a no-op.
176 /// <seealso cref="Container.Remove" />
177 /// <since_tizen> 4 </since_tizen>
178 /// <exception cref="InvalidOperationException">Thrown when deleting a view that is not a child of this view</exception>
179 public override void Remove(View child)
181 if (child == null || child.GetParent() == null) // Early out if child null.
184 if (child.GetParent() != this)
186 //throw new System.InvalidOperationException("You have deleted a view that is not a child of this view.");
187 Tizen.Log.Error("NUI", "You have deleted a view that is not a child of this view.");
190 bool hasLayout = (layout != null);
192 // If View has a layout then do a deferred child removal
193 // Actual child removal is performed by the layouting system so
194 // transitions can be completed.
197 (layout as LayoutGroup)?.RemoveChildFromLayoutGroup(child);
204 /// Retrieves a child view by index.
206 /// <seealso cref="Container.GetChildAt" />
207 /// <since_tizen> 4 </since_tizen>
208 public override View GetChildAt(uint index)
210 if (index < Children.Count)
212 return Children[Convert.ToInt32(index)];
221 /// Retrieves the number of children held by the view.
223 /// <seealso cref="Container.GetChildCount" />
224 /// <since_tizen> 4 </since_tizen>
225 [Obsolete("Deprecated in API9, will be removed in API11. Please use ChildCount property instead!")]
226 public override uint GetChildCount()
228 return Convert.ToUInt32(Children.Count);
232 /// Gets the views parent.
234 /// <seealso cref="Container.GetParent()" />
235 /// <since_tizen> 4 </since_tizen>
236 public override Container GetParent()
238 return this.InternalParent as Container;
242 /// Queries whether the view has a focus.
244 /// <returns>True if this view has a focus.</returns>
245 /// <since_tizen> 3 </since_tizen>
246 public bool HasFocus()
249 if (SwigCPtr.Handle != global::System.IntPtr.Zero)
251 ret = Interop.View.HasKeyInputFocus(SwigCPtr);
252 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
256 Tizen.Log.Error("NUI", "swigCPtr of view is aleady disposed.");
262 /// Sets the name of the style to be applied to the view.
264 /// <param name="styleName">A string matching a style described in a stylesheet.</param>
265 /// <since_tizen> 3 </since_tizen>
266 [Obsolete("Deprecated in API9, will be removed in API11. Please use StyleName property instead!")]
267 public void SetStyleName(string styleName)
269 Interop.View.SetStyleName(SwigCPtr, styleName);
270 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
274 /// Retrieves the name of the style to be applied to the view (if any).
276 /// <returns>A string matching a style, or an empty string.</returns>
277 /// <since_tizen> 3 </since_tizen>
278 [Obsolete("Deprecated in API9, will be removed in API11. Please use StyleName property instead!")]
279 public string GetStyleName()
281 string ret = Interop.View.GetStyleName(SwigCPtr);
282 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
287 /// Clears the background.
289 /// <since_tizen> 3 </since_tizen>
290 public void ClearBackground()
292 Interop.View.ClearBackground(SwigCPtr);
293 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
300 /// This is an asynchronous method.
302 /// <since_tizen> 3 </since_tizen>
312 /// This is an asynchronous method.
313 /// If the view is hidden, then the view and its children will not be rendered.
314 /// This is regardless of the individual visibility of the children, i.e., the view will only be rendered if all of its parents are shown.
316 /// <since_tizen> 3 </since_tizen>
323 /// Raises the view above all other views.
326 /// Sibling order of views within the parent will be updated automatically.
327 /// Once a raise or lower API is used, that view will then have an exclusive sibling order independent of insertion.
329 /// <since_tizen> 3 </since_tizen>
330 public void RaiseToTop()
332 var parentChildren = GetParent()?.Children;
334 if (parentChildren != null)
336 parentChildren.Remove(this);
337 parentChildren.Add(this);
339 LayoutGroup layout = Layout as LayoutGroup;
340 layout?.ChangeLayoutSiblingOrder(parentChildren.Count - 1);
342 Interop.NDalic.RaiseToTop(SwigCPtr);
343 if (NDalicPINVOKE.SWIGPendingException.Pending)
344 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
350 /// Lowers the view to the bottom of all views.
353 /// The sibling order of views within the parent will be updated automatically.
354 /// Once a raise or lower API is used that view will then have an exclusive sibling order independent of insertion.
356 /// <since_tizen> 3 </since_tizen>
357 public void LowerToBottom()
359 var parentChildren = GetParent()?.Children;
361 if (parentChildren != null)
363 parentChildren.Remove(this);
364 parentChildren.Insert(0, this);
366 LayoutGroup layout = Layout as LayoutGroup;
367 layout?.ChangeLayoutSiblingOrder(0);
369 Interop.NDalic.LowerToBottom(SwigCPtr);
370 if (NDalicPINVOKE.SWIGPendingException.Pending)
371 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
376 /// Queries if all resources required by a view are loaded and ready.
378 /// <remarks>Most resources are only loaded when the control is placed on the stage.
380 /// <since_tizen> 3 </since_tizen>
381 public bool IsResourceReady()
383 bool ret = Interop.View.IsResourceReady(SwigCPtr);
384 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
389 /// Gets the parent layer of this view.If a view has no parent, this method does not do anything.
391 /// <pre>The view has been initialized. </pre>
392 /// <returns>The parent layer of view </returns>
393 /// <since_tizen> 5 </since_tizen>
394 public Layer GetLayer()
396 //to fix memory leak issue, match the handle count with native side.
397 IntPtr cPtr = Interop.Actor.GetLayer(SwigCPtr);
398 Layer ret = this.GetInstanceSafely<Layer>(cPtr);
399 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
404 /// Removes a view from its parent view or layer. If a view has no parent, this method does nothing.
406 /// <pre>The (child) view has been initialized. </pre>
407 /// <since_tizen> 4 </since_tizen>
408 public void Unparent()
410 GetParent()?.Remove(this);
414 /// Search through this view's hierarchy for a view with the given name.
415 /// The view itself is also considered in the search.
417 /// <pre>The view has been initialized.</pre>
418 /// <param name="viewName">The name of the view to find.</param>
419 /// <returns>A handle to the view if found, or an empty handle if not.</returns>
420 /// <since_tizen> 3 </since_tizen>
421 public View FindChildByName(string viewName)
423 //to fix memory leak issue, match the handle count with native side.
424 IntPtr cPtr = Interop.Actor.FindChildByName(SwigCPtr, viewName);
425 View ret = this.GetInstanceSafely<View>(cPtr);
426 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
431 /// Converts screen coordinates into the view's coordinate system using the default camera.
433 /// <pre>The view has been initialized.</pre>
434 /// <remarks>The view coordinates are relative to the top-left(0.0, 0.0, 0.5).</remarks>
435 /// <param name="localX">On return, the X-coordinate relative to the view.</param>
436 /// <param name="localY">On return, the Y-coordinate relative to the view.</param>
437 /// <param name="screenX">The screen X-coordinate.</param>
438 /// <param name="screenY">The screen Y-coordinate.</param>
439 /// <returns>True if the conversion succeeded.</returns>
440 /// <since_tizen> 3 </since_tizen>
441 public bool ScreenToLocal(out float localX, out float localY, float screenX, float screenY)
443 bool ret = Interop.Actor.ScreenToLocal(SwigCPtr, out localX, out localY, screenX, screenY);
444 if (NDalicPINVOKE.SWIGPendingException.Pending)
445 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
450 /// Sets the relative to parent size factor of the view.<br />
451 /// This factor is only used when ResizePolicy is set to either:
452 /// ResizePolicy::SIZE_RELATIVE_TO_PARENT or ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT.<br />
453 /// This view's size is set to the view's size multiplied by or added to this factor, depending on ResizePolicy.<br />
455 /// <pre>The view has been initialized.</pre>
456 /// <param name="factor">A Vector3 representing the relative factor to be applied to each axis.</param>
457 /// <since_tizen> 3 </since_tizen>
458 public void SetSizeModeFactor(Vector3 factor)
460 Interop.Actor.SetSizeModeFactor(SwigCPtr, Vector3.getCPtr(factor));
461 if (NDalicPINVOKE.SWIGPendingException.Pending)
462 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
465 /// Calculates the height of the view given a width.<br />
466 /// The natural size is used for default calculation.<br />
467 /// Size 0 is treated as aspect ratio 1:1.<br />
469 /// <param name="width">The width to use.</param>
470 /// <returns>The height based on the width.</returns>
471 /// <since_tizen> 3 </since_tizen>
472 [Obsolete("Deprecated in API9, will be removed in API11. Please use HeightForWidth property instead!")]
473 public float GetHeightForWidth(float width)
475 float ret = Interop.Actor.GetHeightForWidth(SwigCPtr, width);
476 if (NDalicPINVOKE.SWIGPendingException.Pending)
477 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
482 /// Calculates the width of the view given a height.<br />
483 /// The natural size is used for default calculation.<br />
484 /// Size 0 is treated as aspect ratio 1:1.<br />
486 /// <param name="height">The height to use.</param>
487 /// <returns>The width based on the height.</returns>
488 /// <since_tizen> 3 </since_tizen>
489 [Obsolete("Deprecated in API9, will be removed in API11. Please use WidthForHeight property instead!")]
490 public float GetWidthForHeight(float height)
492 float ret = Interop.Actor.GetWidthForHeight(SwigCPtr, height);
493 if (NDalicPINVOKE.SWIGPendingException.Pending)
494 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
499 /// Return the amount of size allocated for relayout.
501 /// <param name="dimension">The dimension to retrieve.</param>
502 /// <returns>Return the size.</returns>
503 /// <since_tizen> 3 </since_tizen>
504 public float GetRelayoutSize(DimensionType dimension)
506 float ret = Interop.Actor.GetRelayoutSize(SwigCPtr, (int)dimension);
507 if (NDalicPINVOKE.SWIGPendingException.Pending)
508 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
513 /// Set the padding for the view.
515 /// <param name="padding">Padding for the view.</param>
516 /// <since_tizen> 3 </since_tizen>
517 // [Obsolete("Deprecated in API9, will be removed in API11. Please use Padding property instead!")]
518 public void SetPadding(PaddingType padding)
520 Interop.Actor.SetPadding(SwigCPtr, PaddingType.getCPtr(padding));
521 if (NDalicPINVOKE.SWIGPendingException.Pending)
522 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
526 /// Return the value of padding for the view.
528 /// <param name="paddingOut">the value of padding for the view</param>
529 /// <since_tizen> 3 </since_tizen>
530 [Obsolete("Deprecated in API9, will be removed in API11. Please use Padding property instead!")]
531 public void GetPadding(PaddingType paddingOut)
533 Interop.Actor.GetPadding(SwigCPtr, PaddingType.getCPtr(paddingOut));
534 if (NDalicPINVOKE.SWIGPendingException.Pending)
535 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
538 /// <since_tizen> 3 </since_tizen>
539 public uint AddRenderer(Renderer renderer)
541 uint ret = Interop.Actor.AddRenderer(SwigCPtr, Renderer.getCPtr(renderer));
542 if (NDalicPINVOKE.SWIGPendingException.Pending)
543 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
547 /// <since_tizen> 3 </since_tizen>
548 public Renderer GetRendererAt(uint index)
550 //to fix memory leak issue, match the handle count with native side.
551 IntPtr cPtr = Interop.Actor.GetRendererAt(SwigCPtr, index);
552 HandleRef CPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
553 Renderer ret = Registry.GetManagedBaseHandleFromNativePtr(CPtr.Handle) as Renderer;
554 if (cPtr != null && ret == null)
556 ret = new Renderer(cPtr, false);
557 if (NDalicPINVOKE.SWIGPendingException.Pending)
558 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
561 Interop.BaseHandle.DeleteBaseHandle(CPtr);
562 CPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
564 if (NDalicPINVOKE.SWIGPendingException.Pending)
565 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
569 /// <since_tizen> 3 </since_tizen>
570 public void RemoveRenderer(Renderer renderer)
572 Interop.Actor.RemoveRenderer(SwigCPtr, Renderer.getCPtr(renderer));
573 if (NDalicPINVOKE.SWIGPendingException.Pending)
574 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
577 /// <since_tizen> 3 </since_tizen>
578 public void RemoveRenderer(uint index)
580 Interop.Actor.RemoveRenderer(SwigCPtr, index);
581 if (NDalicPINVOKE.SWIGPendingException.Pending)
582 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
585 /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
586 [EditorBrowsable(EditorBrowsableState.Never)]
587 public void RotateBy(Degree angle, Vector3 axis)
589 Interop.ActorInternal.RotateByDegree(SwigCPtr, Degree.getCPtr(angle), Vector3.getCPtr(axis));
590 if (NDalicPINVOKE.SWIGPendingException.Pending)
591 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
594 /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
595 [EditorBrowsable(EditorBrowsableState.Never)]
596 public void RotateBy(Radian angle, Vector3 axis)
598 Interop.ActorInternal.RotateByRadian(SwigCPtr, Radian.getCPtr(angle), Vector3.getCPtr(axis));
599 if (NDalicPINVOKE.SWIGPendingException.Pending)
600 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
603 /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
604 [EditorBrowsable(EditorBrowsableState.Never)]
605 public void RotateBy(Rotation relativeRotation)
607 Interop.ActorInternal.RotateByQuaternion(SwigCPtr, Rotation.getCPtr(relativeRotation));
608 if (NDalicPINVOKE.SWIGPendingException.Pending)
609 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
612 /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
613 [EditorBrowsable(EditorBrowsableState.Never)]
614 public void ScaleBy(Vector3 relativeScale)
616 Interop.ActorInternal.ScaleBy(SwigCPtr, Vector3.getCPtr(relativeScale));
617 if (NDalicPINVOKE.SWIGPendingException.Pending)
618 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
621 /// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
622 [EditorBrowsable(EditorBrowsableState.Never)]
623 public void SetColorMode(ColorMode colorMode)
625 Interop.ActorInternal.SetColorMode(SwigCPtr, (int)colorMode);
626 if (NDalicPINVOKE.SWIGPendingException.Pending)
627 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
632 /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
633 [EditorBrowsable(EditorBrowsableState.Never)]
634 public Transition GetTransition(string transitionName)
636 Transition trans = null;
637 transDictionary.TryGetValue(transitionName, out trans);
641 /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
642 [EditorBrowsable(EditorBrowsableState.Never)]
643 public void ObjectDump()
645 if (0 == Children.Count)
647 Type type = this.GetType();
648 PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
649 foreach (var property in properties)
651 if (null != property && property.CanRead)
653 Tizen.Log.Fatal("NUI", $"{type.Name} {property.Name} ({property.PropertyType.Name}): {property.GetValueString(this, property.PropertyType)}");
659 foreach (View view in Children)
666 /// Search through this View's hierarchy for a View with the given unique ID.
667 /// The View itself is also considered in the search.
669 /// <param name="id">The ID of the View to find</param>
670 /// <returns>A View if found or a null if not</returns>
671 [EditorBrowsable(EditorBrowsableState.Never)]
672 public View FindChildByID(uint id)
674 IntPtr cPtr = Interop.Actor.FindChildById(SwigCPtr, id);
675 View ret = this.GetInstanceSafely<View>(cPtr);
676 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();