[NUI] Support Layout property by ViewStyle
authorJaehyun Cho <jae_hyun.cho@samsung.com>
Fri, 20 May 2022 09:13:47 +0000 (18:13 +0900)
committerdongsug-song <35130733+dongsug-song@users.noreply.github.com>
Thu, 2 Jun 2022 09:14:59 +0000 (18:14 +0900)
To separate GUI code from Control logic code, Layout property is
supported by ViewStyle.

Now, Layout property can be set in Theme cs file.

In this PR, Tizen.NUI.Components.Button's Theme supports Layout
property.

After applying this PR, all other Controls' Themes can support Layout
property.

src/Tizen.NUI.Components/Controls/Button.Internal.cs
src/Tizen.NUI.Components/Theme/DefaultThemeCommon.cs
src/Tizen.NUI/src/public/BaseComponents/Style/ViewStyle.cs
src/Tizen.NUI/src/public/BaseComponents/Style/ViewStyleBindableProperty.cs
src/Tizen.NUI/src/public/BaseComponents/View.cs
src/Tizen.NUI/src/public/Layouting/AbsoluteLayout.cs
src/Tizen.NUI/src/public/Layouting/FlexLayout.cs
src/Tizen.NUI/src/public/Layouting/GridLayout.cs
src/Tizen.NUI/src/public/Layouting/LayoutItem.cs
src/Tizen.NUI/src/public/Layouting/LinearLayout.cs
src/Tizen.NUI/src/public/Layouting/RelativeLayout.cs

index 41a6243..4a93a56 100644 (file)
@@ -338,54 +338,52 @@ namespace Tizen.NUI.Components
             Size2D cellPadding = String.IsNullOrEmpty(buttonText.Text) ? new Size2D(0, 0) : itemSpacing;
 #pragma warning restore CA2000
 
+            // If LayoutItems() is called by OnInitialize(), then layout would be null.
+            // Because layout is set by ApplyStyle() which is called after OnInitialize().
+            var layout = Layout as LinearLayout;
+            if (layout != null)
+            {
+                layout.HorizontalAlignment = itemHorizontalAlignment;
+                layout.VerticalAlignment = itemVerticalAlignment;
+                layout.CellPadding = cellPadding;
+            }
+
             if (IconRelativeOrientation == IconOrientation.Left)
             {
-                Layout = new LinearLayout()
+                if (layout != null)
                 {
-                    LinearOrientation = LinearLayout.Orientation.Horizontal,
-                    HorizontalAlignment = itemHorizontalAlignment,
-                    VerticalAlignment = itemVerticalAlignment,
-                    CellPadding = cellPadding
-                };
+                    layout.LinearOrientation = LinearLayout.Orientation.Horizontal;
+                }
 
                 Add(buttonIcon);
                 Add(buttonText);
             }
             else if (IconRelativeOrientation == IconOrientation.Right)
             {
-                Layout = new LinearLayout()
+                if (layout != null)
                 {
-                    LinearOrientation = LinearLayout.Orientation.Horizontal,
-                    HorizontalAlignment = itemHorizontalAlignment,
-                    VerticalAlignment = itemVerticalAlignment,
-                    CellPadding = cellPadding
-                };
+                    layout.LinearOrientation = LinearLayout.Orientation.Horizontal;
+                }
 
                 Add(buttonText);
                 Add(buttonIcon);
             }
             else if (IconRelativeOrientation == IconOrientation.Top)
             {
-                Layout = new LinearLayout()
+                if (layout != null)
                 {
-                    LinearOrientation = LinearLayout.Orientation.Vertical,
-                    HorizontalAlignment = itemHorizontalAlignment,
-                    VerticalAlignment = itemVerticalAlignment,
-                    CellPadding = cellPadding
-                };
+                    layout.LinearOrientation = LinearLayout.Orientation.Vertical;
+                }
 
                 Add(buttonIcon);
                 Add(buttonText);
             }
             else if (IconRelativeOrientation == IconOrientation.Bottom)
             {
-                Layout = new LinearLayout()
+                if (layout != null)
                 {
-                    LinearOrientation = LinearLayout.Orientation.Vertical,
-                    HorizontalAlignment = itemHorizontalAlignment,
-                    VerticalAlignment = itemVerticalAlignment,
-                    CellPadding = cellPadding
-                };
+                    layout.LinearOrientation = LinearLayout.Orientation.Vertical;
+                }
 
                 Add(buttonText);
                 Add(buttonIcon);
index c693271..8700d3f 100755 (executable)
@@ -56,7 +56,14 @@ namespace Tizen.NUI.Components
                 {
                     TextColor = new Color("#FDFDFD"),
                     PixelSize = 24,
-                }
+                },
+                Layout = new LinearLayout()
+                {
+                    LinearOrientation = LinearLayout.Orientation.Horizontal,
+                    HorizontalAlignment = HorizontalAlignment.Center,
+                    VerticalAlignment = VerticalAlignment.Center,
+                    CellPadding = new Size2D(8, 8),
+                },
             });
 
             // CheckBox base style
index 73d0beb..d5af465 100755 (executable)
@@ -63,6 +63,7 @@ namespace Tizen.NUI.BaseComponents
         private Selector<Rectangle> backgroundImageBorderSelector;
         private Selector<Color> colorSelector;
         private VisualTransformPolicyType? cornerRadiusPolicy;
+        private LayoutItem layout;
 
         static ViewStyle() { }
 
@@ -521,6 +522,16 @@ namespace Tizen.NUI.BaseComponents
         public bool SolidNull { get; set; } = false;
 
         /// <summary>
+        /// Layout to position the View's children.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public LayoutItem Layout
+        {
+            get => (LayoutItem)GetValue(LayoutProperty);
+            set => SetValue(LayoutProperty, value);
+        }
+
+        /// <summary>
         /// HashSet of dirty properties. Internal use only.
         /// </summary>
         internal HashSet<BindableProperty> DirtyProperties { get; private set; }
@@ -576,7 +587,14 @@ namespace Tizen.NUI.BaseComponents
 
                 if (destinationProperty != null)
                 {
-                    InternalSetValue(destinationProperty, sourceValue);
+                    if (sourceProperty.PropertyName.Equals("Layout") && (sourceValue is LayoutItem layout))
+                    {
+                        InternalSetValue(destinationProperty, layout.Clone());
+                    }
+                    else
+                    {
+                        InternalSetValue(destinationProperty, sourceValue);
+                    }
                 }
             }
 
index 91cef66..5ba4ab6 100755 (executable)
@@ -440,5 +440,13 @@ namespace Tizen.NUI.BaseComponents
             var buttonStyle = (ViewStyle)bindable;
             return buttonStyle.isEnabled;
         });
+
+        /// <summary> Bindable property of Layout.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static readonly BindableProperty LayoutProperty = BindableProperty.Create(nameof(Layout), typeof(LayoutItem), typeof(ViewStyle), null, propertyChanged: (bindable, oldValue, newValue) =>
+        {
+            ((ViewStyle)bindable).layout = (LayoutItem)newValue;
+        }, defaultValueCreator: (bindable) => ((ViewStyle)bindable).layout);
     }
 }
index 35e2f54..ad361d5 100755 (executable)
@@ -3118,7 +3118,14 @@ namespace Tizen.NUI.BaseComponents
 
                 if (destinationProperty != null)
                 {
-                    InternalSetValue(destinationProperty, sourceValue);
+                    if (sourceProperty.PropertyName.Equals("Layout") && (sourceValue is LayoutItem layout))
+                    {
+                        InternalSetValue(destinationProperty, layout.Clone());
+                    }
+                    else
+                    {
+                        InternalSetValue(destinationProperty, sourceValue);
+                    }
                 }
             }
         }
index d6675df..afae715 100755 (executable)
@@ -14,6 +14,8 @@
  *
  */
 
+using System.ComponentModel;
+
 namespace Tizen.NUI
 {
     /// <summary>
@@ -30,6 +32,23 @@ namespace Tizen.NUI
         {
         }
 
+        /// <inheritdoc/>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public override LayoutItem Clone()
+        {
+            var layout = new AbsoluteLayout();
+
+            foreach (var prop in layout.GetType().GetProperties())
+            {
+                if (prop.GetSetMethod() != null)
+                {
+                    prop.SetValue(layout, this.GetType().GetProperty(prop.Name).GetValue(this));
+                }
+            }
+
+            return layout;
+        }
+
         /// <summary>
         /// Measure the layout and its content to determine the measured width and the measured height.<br />
         /// </summary>
index e5c19cb..479db9f 100755 (executable)
@@ -629,6 +629,23 @@ namespace Tizen.NUI
             Absolute
         }
 
+        /// <inheritdoc/>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public override LayoutItem Clone()
+        {
+            var layout = new FlexLayout();
+
+            foreach (var prop in layout.GetType().GetProperties())
+            {
+                if (prop.GetSetMethod() != null)
+                {
+                    prop.SetValue(layout, this.GetType().GetProperty(prop.Name).GetValue(this));
+                }
+            }
+
+            return layout;
+        }
+
         private void measureChild(global::System.IntPtr childPtr, float width, int measureModeWidth, float height, int measureModeHeight, out MeasuredSize measureSize)
         {
             // We need to measure child layout
index 87a2290..6c328d6 100755 (executable)
@@ -544,6 +544,23 @@ namespace Tizen.NUI
             /// <since_tizen> 8 </since_tizen>
             End = 2,
         }
+
+        /// <inheritdoc/>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public override LayoutItem Clone()
+        {
+            var layout = new GridLayout();
+
+            foreach (var prop in layout.GetType().GetProperties())
+            {
+                if (prop.GetSetMethod() != null)
+                {
+                    prop.SetValue(layout, this.GetType().GetProperty(prop.Name).GetValue(this));
+                }
+            }
+
+            return layout;
+        }
     }
 
     // Extension Method of GridLayout.Alignment.
index 7f9e69a..0111bbb 100755 (executable)
@@ -417,7 +417,7 @@ namespace Tizen.NUI
         {
             get
             {
-                return Owner.SuggestedMinimumWidth;
+                return (Owner != null) ? Owner.SuggestedMinimumWidth : new LayoutLength(0);
             }
         }
 
@@ -430,7 +430,7 @@ namespace Tizen.NUI
         {
             get
             {
-                return Owner.SuggestedMinimumHeight;
+                return (Owner != null) ? Owner.SuggestedMinimumHeight : new LayoutLength(0);
             }
         }
 
@@ -663,5 +663,24 @@ namespace Tizen.NUI
                 }
             }
         }
+
+        /// <summary>
+        /// Creates a cloned LayoutItem.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public virtual LayoutItem Clone()
+        {
+            var layout = new LayoutItem();
+
+            foreach (var prop in layout.GetType().GetProperties())
+            {
+                if (prop.GetSetMethod() != null)
+                {
+                    prop.SetValue(layout, this.GetType().GetProperty(prop.Name).GetValue(this));
+                }
+            }
+
+            return layout;
+        }
     }
 }
index 1f01206..f6a1d23 100755 (executable)
@@ -209,6 +209,23 @@ namespace Tizen.NUI
         {
         }
 
+        /// <inheritdoc/>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public override LayoutItem Clone()
+        {
+            var layout = new LinearLayout();
+
+            foreach (var prop in layout.GetType().GetProperties())
+            {
+                if (prop.GetSetMethod() != null)
+                {
+                    prop.SetValue(layout, this.GetType().GetProperty(prop.Name).GetValue(this));
+                }
+            }
+
+            return layout;
+        }
+
         /// <summary>
         /// Measure the layout and its content to determine the measured width and the measured height.
         /// </summary>
index 84fcb78..28119d0 100755 (executable)
@@ -337,6 +337,23 @@ namespace Tizen.NUI
         public static void SetFillVertical(View view, bool value) => SetAttachedValue(view, FillVerticalProperty, value);
 
         /// <inheritdoc/>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public override LayoutItem Clone()
+        {
+            var layout = new RelativeLayout();
+
+            foreach (var prop in layout.GetType().GetProperties())
+            {
+                if (prop.GetSetMethod() != null)
+                {
+                    prop.SetValue(layout, this.GetType().GetProperty(prop.Name).GetValue(this));
+                }
+            }
+
+            return layout;
+        }
+
+        /// <inheritdoc/>
         /// <since_tizen> 9 </since_tizen>
         protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
         {