[NUI] apply scaling factor to point size (#3594)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / TextLabelBindableProperty.cs
index 86ec00a..511f3cf 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright(c) 2020 Samsung Electronics Co., Ltd.
+ * Copyright(c) 2021 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
  */
 
 extern alias TizenSystemSettings;
-using TizenSystemSettings.Tizen.System;
 using System.ComponentModel;
 using Tizen.NUI.Binding;
 
@@ -36,9 +35,15 @@ namespace Tizen.NUI.BaseComponents
         public static readonly BindableProperty TranslatableTextProperty = BindableProperty.Create(nameof(TranslatableText), typeof(string), typeof(TextLabel), string.Empty, propertyChanged: (bindable, oldValue, newValue) =>
         {
             var textLabel = (TextLabel)bindable;
-            if (newValue != null)
+
+            if (newValue is Selector<string> selector)
             {
-                textLabel.translatableText = (string)newValue;
+                textLabel.TranslatableTextSelector = selector;
+            }
+            else
+            {
+                textLabel.selectorData?.TranslatableText?.Reset(textLabel);
+                textLabel.SetTranslatableText((string)newValue);
             }
         },
         defaultValueCreator: (bindable) =>
@@ -51,9 +56,15 @@ namespace Tizen.NUI.BaseComponents
         public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(TextLabel), string.Empty, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
         {
             var textLabel = (TextLabel)bindable;
-            if (newValue != null)
+
+            if (newValue is Selector<string> selector)
+            {
+                textLabel.TextSelector = selector;
+            }
+            else
             {
-                Tizen.NUI.Object.SetProperty((System.Runtime.InteropServices.HandleRef)textLabel.SwigCPtr, TextLabel.Property.TEXT, new Tizen.NUI.PropertyValue((string)newValue));
+                textLabel.selectorData?.Text?.Reset(textLabel);
+                textLabel.SetText((string)newValue);
             }
         }),
         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
@@ -68,9 +79,15 @@ namespace Tizen.NUI.BaseComponents
         public static readonly BindableProperty FontFamilyProperty = BindableProperty.Create(nameof(FontFamily), typeof(string), typeof(TextLabel), string.Empty, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
         {
             var textLabel = (TextLabel)bindable;
-            if (newValue != null)
+
+            if (newValue is Selector<string> selector)
+            {
+                textLabel.FontFamilySelector = selector;
+            }
+            else
             {
-                Tizen.NUI.Object.SetProperty((System.Runtime.InteropServices.HandleRef)textLabel.SwigCPtr, TextLabel.Property.FontFamily, new Tizen.NUI.PropertyValue((string)newValue));
+                textLabel.selectorData?.FontFamily?.Reset(textLabel);
+                textLabel.SetFontFamily((string)newValue);
             }
         }),
         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
@@ -88,6 +105,7 @@ namespace Tizen.NUI.BaseComponents
             if (newValue != null)
             {
                 Tizen.NUI.Object.SetProperty((System.Runtime.InteropServices.HandleRef)textLabel.SwigCPtr, TextLabel.Property.FontStyle, new Tizen.NUI.PropertyValue((PropertyMap)newValue));
+                textLabel.RequestLayout();
             }
         }),
         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
@@ -102,9 +120,15 @@ namespace Tizen.NUI.BaseComponents
         public static readonly BindableProperty PointSizeProperty = BindableProperty.Create(nameof(PointSize), typeof(float), typeof(TextLabel), default(float), propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
         {
             var textLabel = (TextLabel)bindable;
-            if (newValue != null)
+
+            if (newValue is Selector<float?> selector)
+            {
+                textLabel.PointSizeSelector = selector;
+            }
+            else
             {
-                Tizen.NUI.Object.SetProperty((System.Runtime.InteropServices.HandleRef)textLabel.SwigCPtr, TextLabel.Property.PointSize, new Tizen.NUI.PropertyValue((float)newValue));
+                textLabel.selectorData?.PointSize?.Reset(textLabel);
+                textLabel.SetPointSize((float?)newValue);
             }
         }),
         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
@@ -112,6 +136,10 @@ namespace Tizen.NUI.BaseComponents
             var textLabel = (TextLabel)bindable;
             float temp = 0.0f;
             Tizen.NUI.Object.GetProperty((System.Runtime.InteropServices.HandleRef)textLabel.SwigCPtr, TextLabel.Property.PointSize).Get(out temp);
+            if (GraphicsTypeManager.Instance.ScalingFactor != 0.0f)
+            {
+                temp = (float)temp / GraphicsTypeManager.Instance.ScalingFactor;
+            }
             return temp;
         }));
         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
@@ -122,6 +150,7 @@ namespace Tizen.NUI.BaseComponents
             if (newValue != null)
             {
                 Tizen.NUI.Object.SetProperty((System.Runtime.InteropServices.HandleRef)textLabel.SwigCPtr, TextLabel.Property.MultiLine, new Tizen.NUI.PropertyValue((bool)newValue));
+                textLabel.RequestLayout();
             }
         }),
         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
@@ -181,9 +210,15 @@ namespace Tizen.NUI.BaseComponents
         public static readonly BindableProperty TextColorProperty = BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(TextLabel), null, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
         {
             var textLabel = (TextLabel)bindable;
-            if (newValue != null)
+
+            if (newValue is Selector<Color> selector)
+            {
+                textLabel.TextColorSelector = selector;
+            }
+            else
             {
-                Tizen.NUI.Object.SetProperty((System.Runtime.InteropServices.HandleRef)textLabel.SwigCPtr, TextLabel.Property.TextColor, new Tizen.NUI.PropertyValue((Color)newValue));
+                textLabel.selectorData?.TextColor?.Reset(textLabel);
+                textLabel.SetTextColor((Color)newValue);
             }
         }),
         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
@@ -287,6 +322,7 @@ namespace Tizen.NUI.BaseComponents
             if (newValue != null)
             {
                 Tizen.NUI.Object.SetProperty((System.Runtime.InteropServices.HandleRef)textLabel.SwigCPtr, TextLabel.Property.LineSpacing, new Tizen.NUI.PropertyValue((float)newValue));
+                textLabel.RequestLayout();
             }
         }),
         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
@@ -335,9 +371,15 @@ namespace Tizen.NUI.BaseComponents
         public static readonly BindableProperty TextShadowProperty = BindableProperty.Create(nameof(TextShadow), typeof(TextShadow), typeof(TextLabel), null, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
         {
             var textLabel = (TextLabel)bindable;
-            if (newValue != null)
+
+            if (newValue is Selector<TextShadow> selector)
+            {
+                textLabel.TextShadowSelector = selector;
+            }
+            else
             {
-                Object.SetProperty((System.Runtime.InteropServices.HandleRef)textLabel.SwigCPtr, Property.SHADOW, TextShadow.ToPropertyValue((TextShadow)newValue));
+                textLabel.selectorData?.TextShadow?.Reset(textLabel);
+                textLabel.SetTextShadow((TextShadow)newValue);
             }
         }),
         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
@@ -386,9 +428,15 @@ namespace Tizen.NUI.BaseComponents
         public static readonly BindableProperty PixelSizeProperty = BindableProperty.Create(nameof(PixelSize), typeof(float), typeof(TextLabel), default(float), propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
         {
             var textLabel = (TextLabel)bindable;
-            if (newValue != null)
+
+            if (newValue is Selector<float?> selector)
+            {
+                textLabel.PixelSizeSelector = selector;
+            }
+            else
             {
-                Tizen.NUI.Object.SetProperty((System.Runtime.InteropServices.HandleRef)textLabel.SwigCPtr, TextLabel.Property.PixelSize, new Tizen.NUI.PropertyValue((float)newValue));
+                textLabel.selectorData?.PixelSize?.Reset(textLabel);
+                textLabel.SetPixelSize((float?)newValue);
             }
         }),
         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
@@ -415,6 +463,23 @@ namespace Tizen.NUI.BaseComponents
             Tizen.NUI.Object.GetProperty((System.Runtime.InteropServices.HandleRef)textLabel.SwigCPtr, TextLabel.Property.ELLIPSIS).Get(out temp);
             return temp;
         }));
+        /// This will be public opened in tizen_6.5 after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static readonly BindableProperty EllipsisPositionProperty = BindableProperty.Create(nameof(EllipsisPosition), typeof(EllipsisPosition), typeof(TextLabel), EllipsisPosition.End, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
+        {
+            var textLabel = (TextLabel)bindable;
+            if (newValue != null)
+            {
+                Tizen.NUI.Object.SetProperty((System.Runtime.InteropServices.HandleRef)textLabel.SwigCPtr, TextLabel.Property.EllipsisPosition, new Tizen.NUI.PropertyValue((int)newValue));
+            }
+        }),
+        defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
+        {
+            var textLabel = (TextLabel)bindable;
+            int temp = 0;
+            Tizen.NUI.Object.GetProperty((System.Runtime.InteropServices.HandleRef)textLabel.SwigCPtr, TextLabel.Property.EllipsisPosition).Get(out temp);
+            return (EllipsisPosition)temp;
+        }));
         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
         [EditorBrowsable(EditorBrowsableState.Never)]
         public static readonly BindableProperty AutoScrollLoopDelayProperty = BindableProperty.Create(nameof(AutoScrollLoopDelay), typeof(float), typeof(TextLabel), default(float), propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
@@ -533,6 +598,7 @@ namespace Tizen.NUI.BaseComponents
             if (newValue != null)
             {
                 Tizen.NUI.Object.SetProperty((System.Runtime.InteropServices.HandleRef)textLabel.SwigCPtr, TextLabel.Property.MinLineSize, new Tizen.NUI.PropertyValue((float)newValue));
+                textLabel.RequestLayout();
             }
         }),
         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
@@ -552,6 +618,7 @@ namespace Tizen.NUI.BaseComponents
                 using (var property = new Tizen.NUI.PropertyValue((float)newValue))
                 {
                     Tizen.NUI.Object.SetProperty((System.Runtime.InteropServices.HandleRef)textLabel.SwigCPtr, TextLabel.Property.FontSizeScale, property);
+                    textLabel.RequestLayout();
                 }
             }
         }),
@@ -563,80 +630,173 @@ namespace Tizen.NUI.BaseComponents
             return temp;
         }));
 
-        #region Selectors
-        internal static readonly BindableProperty TranslatableTextSelectorProperty = BindableProperty.Create("TranslatableTextSelector", typeof(Selector<string>), typeof(TextLabel), null, propertyChanged: (bindable, oldValue, newValue) =>
+        internal Selector<string> TranslatableTextSelector
         {
-            var textLabel = (TextLabel)bindable;
-            textLabel.SelectorData.TranslatableText.Update(textLabel, (Selector<string>)newValue, true);
-        },
-        defaultValueCreator: (bindable) =>
-        {
-            var textLabel = (TextLabel)bindable;
-            return textLabel.SelectorData.TranslatableText.Get(textLabel);
-        });
-        internal static readonly BindableProperty TextSelectorProperty = BindableProperty.Create("TextSelector", typeof(Selector<string>), typeof(TextLabel), null, propertyChanged: (bindable, oldValue, newValue) =>
+            get => GetSelector<string>(selectorData?.TranslatableText, TextLabel.TranslatableTextProperty);
+            set
+            {
+                selectorData?.TranslatableText?.Reset(this);
+                if (value == null) return;
+
+                if (value.HasAll()) SetTranslatableText(value.All);
+                else EnsureSelectorData().TranslatableText = new TriggerableSelector<string>(this, value, SetTranslatableText, true);
+            }
+        }
+
+        internal Selector<string> TextSelector
         {
-            var textLabel = (TextLabel)bindable;
-            textLabel.SelectorData.Text.Update(textLabel, (Selector<string>)newValue, true);
-        },
-        defaultValueCreator: (bindable) =>
+            get => GetSelector<string>(selectorData?.Text, TextLabel.TextProperty);
+            set
+            {
+                selectorData?.Text?.Reset(this);
+                if (value == null) return;
+
+                if (value.HasAll()) SetText(value.All);
+                else EnsureSelectorData().Text = new TriggerableSelector<string>(this, value, SetText, true);
+            }
+        }
+
+        internal Selector<string> FontFamilySelector
         {
-            var textLabel = (TextLabel)bindable;
-            return textLabel.SelectorData.Text.Get(textLabel);
-        });
-        internal static readonly BindableProperty FontFamilySelectorProperty = BindableProperty.Create("FontFamilySelector", typeof(Selector<string>), typeof(TextLabel), null, propertyChanged: (bindable, oldValue, newValue) =>
+            get => GetSelector<string>(selectorData?.FontFamily, TextLabel.FontFamilyProperty);
+            set
+            {
+                selectorData?.FontFamily?.Reset(this);
+                if (value == null) return;
+
+                if (value.HasAll()) SetFontFamily(value.All);
+                else EnsureSelectorData().FontFamily = new TriggerableSelector<string>(this, value, SetFontFamily, true);
+            }
+        }
+
+        internal Selector<float?> PointSizeSelector
         {
-            var textLabel = (TextLabel)bindable;
-            textLabel.SelectorData.FontFamily.Update(textLabel, (Selector<string>)newValue, true);
-        },
-        defaultValueCreator: (bindable) =>
+            get => GetSelector<float?>(selectorData?.PointSize, TextLabel.PointSizeProperty);
+            set
+            {
+                selectorData?.PointSize?.Reset(this);
+                if (value == null) return;
+
+                if (value.HasAll()) SetPointSize(value.All);
+                else EnsureSelectorData().PointSize = new TriggerableSelector<float?>(this, value, SetPointSize, true);
+            }
+        }
+
+        internal Selector<Color> TextColorSelector
         {
-            var textLabel = (TextLabel)bindable;
-            return textLabel.SelectorData.FontFamily.Get(textLabel);
-        });
-        internal static readonly BindableProperty PointSizeSelectorProperty = BindableProperty.Create("PointSizeSelector", typeof(Selector<float?>), typeof(TextLabel), null, propertyChanged: (bindable, oldValue, newValue) =>
+            get
+            {
+                var selector =  selectorData?.TextColor?.Get();
+                if (selector != null)
+                {
+                    return selector;
+                }
+
+                Color color = new Color();
+                if (!GetProperty(TextLabel.Property.TextColor).Get(color))
+                {
+                    return null;
+                }
+                return new Selector<Color>(color);
+            }
+            set
+            {
+                selectorData?.TextColor?.Reset(this);
+                if (value == null) return;
+
+                if (value.HasAll()) SetTextColor(value.All);
+                else EnsureSelectorData().TextColor = new TriggerableSelector<Color>(this, value, SetTextColor, true);
+            }
+        }
+
+        internal Selector<float?> PixelSizeSelector
         {
-            var textLabel = (TextLabel)bindable;
-            textLabel.SelectorData.PointSize.Update(textLabel, (Selector<float?>)newValue, true);
-        },
-        defaultValueCreator: (bindable) =>
+            get => GetSelector<float?>(selectorData?.PixelSize, TextLabel.PixelSizeProperty);
+            set
+            {
+                selectorData?.PixelSize?.Reset(this);
+                if (value == null) return;
+
+                if (value.HasAll()) SetPixelSize(value.All);
+                else EnsureSelectorData().PixelSize = new TriggerableSelector<float?>(this, value, SetPixelSize, true);
+            }
+        }
+
+        internal Selector<TextShadow> TextShadowSelector
         {
-            var textLabel = (TextLabel)bindable;
-            return textLabel.SelectorData.PointSize.Get(textLabel);
-        });
-        internal static readonly BindableProperty TextColorSelectorProperty = BindableProperty.Create("TextColorSelector", typeof(Selector<Color>), typeof(TextLabel), null, propertyChanged: (bindable, oldValue, newValue) =>
+            get => GetSelector<TextShadow>(selectorData?.TextShadow, TextLabel.TextShadowProperty);
+            set
+            {
+                selectorData?.TextShadow?.Reset(this);
+                if (value == null) return;
+
+                if (value.HasAll()) SetTextShadow(value.All);
+                else EnsureSelectorData().TextShadow = new TriggerableSelector<TextShadow>(this, value, SetTextShadow);
+            }
+        }
+
+        private void SetTranslatableText(string value)
         {
-            var textLabel = (TextLabel)bindable;
-            textLabel.SelectorData.TextColor.Update(textLabel, (Selector<Color>)newValue, true);
-        },
-        defaultValueCreator: (bindable) =>
+            if (value != null)
+            {
+                translatableText = value;
+            }
+        }
+
+        private void SetText(string value)
         {
-            var textLabel = (TextLabel)bindable;
-            return textLabel.SelectorData.TextColor.Get(textLabel);
-        });
+            if (value != null)
+            {
+                Tizen.NUI.Object.SetProperty((System.Runtime.InteropServices.HandleRef)SwigCPtr, TextLabel.Property.TEXT, new Tizen.NUI.PropertyValue(value));
+                RequestLayout();
+            }
+        }
 
-        internal static readonly BindableProperty TextShadowSelectorProperty = BindableProperty.Create("TextShadowSelector", typeof(Selector<TextShadow>), typeof(TextLabel), null, propertyChanged: (bindable, oldValue, newValue) =>
+        private void SetFontFamily(string value)
         {
-            var textLabel = (TextLabel)bindable;
-            textLabel.SelectorData.TextShadow.Update(textLabel, (Selector<TextShadow>)newValue, true);
-        },
-        defaultValueCreator: (bindable) =>
+            if (value != null)
+            {
+                Tizen.NUI.Object.SetProperty((System.Runtime.InteropServices.HandleRef)SwigCPtr, TextLabel.Property.FontFamily, new Tizen.NUI.PropertyValue(value));
+                RequestLayout();
+            }
+        }
+
+        private void SetTextColor(Color value)
         {
-            var textLabel = (TextLabel)bindable;
-            return textLabel.SelectorData.TextShadow.Get(textLabel);
-        });
+            if (value != null)
+            {
+                Tizen.NUI.Object.SetProperty((System.Runtime.InteropServices.HandleRef)SwigCPtr, TextLabel.Property.TextColor, new Tizen.NUI.PropertyValue(value));
+            }
+        }
 
-        internal static readonly BindableProperty PixelSizeSelectorProperty = BindableProperty.Create("PixelSizeSelector", typeof(Selector<float?>), typeof(TextLabel), null, propertyChanged: (bindable, oldValue, newValue) =>
+        private void SetPointSize(float? value)
         {
-            var textLabel = (TextLabel)bindable;
-            ((TextLabel)bindable).SelectorData.PixelSize.Update(textLabel, (Selector<float?>)newValue, true);
-        },
-        defaultValueCreator: (bindable) =>
+            if (value != null)
+            {
+                if (GraphicsTypeManager.Instance.ScalingFactor != 0.0f)
+                {
+                    value = value * GraphicsTypeManager.Instance.ScalingFactor;
+                }
+                Tizen.NUI.Object.SetProperty((System.Runtime.InteropServices.HandleRef)SwigCPtr, TextLabel.Property.PointSize, new Tizen.NUI.PropertyValue((float)value));
+                RequestLayout();
+            }
+        }
+
+        private void SetPixelSize(float? value)
         {
-            var textLabel = (TextLabel)bindable;
-            return textLabel.SelectorData.PixelSize.Get(textLabel);
-        });
-        #endregion
+            if (value != null)
+            {
+                Tizen.NUI.Object.SetProperty((System.Runtime.InteropServices.HandleRef)SwigCPtr, TextLabel.Property.PixelSize, new Tizen.NUI.PropertyValue((float)value));
+                RequestLayout();
+            }
+        }
 
+        private void SetTextShadow(TextShadow value)
+        {
+            if (value != null)
+            {
+                Object.SetProperty((System.Runtime.InteropServices.HandleRef)SwigCPtr, Property.SHADOW, TextShadow.ToPropertyValue(value));
+            }
+        }
     }
 }