[NUI] replace field declaration with property (#1703)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Control.cs
index 1c34649..9d777b1 100755 (executable)
@@ -18,6 +18,8 @@ using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using Tizen.NUI.BaseComponents;
+using Tizen.NUI.Binding;
+using System.Windows.Input;
 
 namespace Tizen.NUI.Components
 {
@@ -27,25 +29,30 @@ namespace Tizen.NUI.Components
     /// <since_tizen> 6 </since_tizen>
     /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
     [EditorBrowsable(EditorBrowsableState.Never)]
-    public abstract class Control : VisualView
+    public class Control : VisualView
     {
-        /// <summary>
-        /// Control style.
-        /// </summary>
-        /// <since_tizen> 6 </since_tizen>
-        /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
+        /// Internal used.
         [EditorBrowsable(EditorBrowsableState.Never)]
-        protected string style;
-        /// <summary>
-        /// Control attributes.
-        /// </summary>
+        public static readonly BindableProperty CommandProperty = BindableProperty.Create("Command", typeof(ICommand), typeof(Control), null, propertyChanged: (bo, o, n) => ((Control)bo).OnCommandChanged());
+
+        /// Internal used.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create("CommandParameter", typeof(object), typeof(Button), null,
+            propertyChanged: (bindable, oldvalue, newvalue) => ((Button)bindable).CommandCanExecuteChanged(bindable, EventArgs.Empty));
+
+        /// <summary> Control style. </summary>
         /// <since_tizen> 6 </since_tizen>
         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
         [EditorBrowsable(EditorBrowsableState.Never)]
-        protected Attributes attributes;
+        protected string StyleName { get; set; }
 
         private TapGestureDetector tapGestureDetector = new TapGestureDetector();
-        private bool isFocused = false;
+
+        /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public ControlStyle Style => ViewStyle as ControlStyle;
+
+        static Control() { }
 
         /// <summary>
         /// Construct an empty Control.
@@ -55,292 +62,98 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         public Control() : base()
         {
+            var cur_type = this.GetType();
+            ViewStyle viewStyle = null;
+
+            do
+            {
+                if (cur_type.Equals(typeof(Tizen.NUI.Components.Control))) break;
+                viewStyle = StyleManager.Instance.GetComponentStyle(cur_type);
+                cur_type = cur_type.BaseType;
+            }
+            while (viewStyle == null);
+
+            if (viewStyle != null)
+            {
+                ApplyStyle(viewStyle);
+            }
+
             Initialize(null);
         }
 
         /// <summary>
-        /// Construct with attributes
+        /// Construct with style.
         /// </summary>
-        /// <param name="attributes">Create attributes customized by user</param>
+        /// <param name="style">Create control with style.</param>
         /// <since_tizen> 6 </since_tizen>
         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public Control(Attributes attributes) : base()
+        public Control(ControlStyle style) : base(style)
         {
             Initialize(null);
-            this.attributes = attributes.Clone();
         }
 
         /// <summary>
-        /// Construct with style
+        /// Construct with styleSheet
         /// </summary>
-        /// <param name="style">Style to be applied</param>
+        /// <param name="styleSheet">StyleSheet to be applied</param>
         /// <since_tizen> 6 </since_tizen>
         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public Control(string style) : base()
+        public Control(string styleSheet) : base()
         {
-            Initialize(style);
+            ViewStyle viewStyle = StyleManager.Instance.GetViewStyle(styleSheet);
+            if (viewStyle == null)
+            {
+                throw new InvalidOperationException($"There is no style {styleSheet}");
+            }
+
+            ApplyStyle(viewStyle);
+            this.StyleName = styleSheet;
+
+            Initialize(StyleName);
         }
 
-        /// <summary>
-        /// Get/Set the control state.
-        /// </summary>
-        /// <since_tizen> 6 </since_tizen>
-        /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
+        /// Internal used.
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public new ControlStates State
-        {
-            get;
-            set;
-        }
-        /// <summary>
-        /// Whether focusable when touch
-        /// </summary>
-        /// <since_tizen> 6 </since_tizen>
-        internal bool StateFocusableOnTouchMode
+        public ICommand Command
         {
-            get;
-            set;
+            get { return (ICommand)GetValue(CommandProperty); }
+            set { SetValue(CommandProperty, value); }
         }
 
-        internal bool IsFocused
+        /// Internal used.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public object CommandParameter
         {
-            get
-            {
-                return isFocused || HasFocus();
-            }
+            get { return GetValue(CommandParameterProperty); }
+            set { SetValue(CommandParameterProperty, value); }
         }
+
         /// <summary>
-        /// Apply attributes for View, Image or TextLabel.
+        /// Whether focusable when touch
         /// </summary>
-        /// <param name="view">View which will be applied attrs</param>
-        /// <param name="attrs">Attributes for View, Image or TextLabel</param>
         /// <since_tizen> 6 </since_tizen>
-        /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        protected  void ApplyAttributes(View view, ViewAttributes attrs)
-        {
-            if (view == null || attrs == null)
-            {
-                return;
-            }
+        internal bool StateFocusableOnTouchMode { get; set; }
 
-            if (attrs.Position2D != null)
-            {
-                view.Position2D = attrs.Position2D;
-            }
-            if (attrs.Size2D != null)
-            {
-                view.Size2D = attrs.Size2D;
-            }
-            if (attrs.MinimumSize != null)
-            {
-                view.MinimumSize = attrs.MinimumSize;
-            }
-            if (attrs.BackgroundColor?.GetValue(State) != null)
-            {
-                view.BackgroundColor = attrs.BackgroundColor.GetValue(State);
-            }
-            if (attrs.PositionUsesPivotPoint != null)
-            {
-                view.PositionUsesPivotPoint = attrs.PositionUsesPivotPoint.Value;
-            }
-            if (attrs.ParentOrigin != null)
-            {
-                view.ParentOrigin = attrs.ParentOrigin;
-            }
-            if (attrs.PivotPoint != null)
-            {
-                view.PivotPoint = attrs.PivotPoint;
-            }
-            if (attrs.WidthResizePolicy!= null)
-            {
-                view.WidthResizePolicy = attrs.WidthResizePolicy.Value;
-            }
-            if (attrs.HeightResizePolicy != null)
-            {
-                view.HeightResizePolicy = attrs.HeightResizePolicy.Value;
-            }
-            if (attrs.SizeModeFactor != null)
-            {
-                view.SizeModeFactor = attrs.SizeModeFactor;
-            }
-            if (attrs.Opacity?.GetValue(State) != null)
-            {
-                view.Opacity = attrs.Opacity.GetValue(State).Value;
-            }
-
-            ImageView image = view as ImageView;
-            ImageAttributes imageAttrs = attrs as ImageAttributes;
-            if (image != null && imageAttrs != null)
-            {
-                if (imageAttrs.ResourceURL?.GetValue(State) != null)
-                {
-                    image.ResourceUrl = imageAttrs.ResourceURL.GetValue(State);
-                }
-                if (imageAttrs.Border?.GetValue(State) != null)
-                {
-                    image.Border = imageAttrs.Border.GetValue(State);
-                }
-      
-            }
+        internal bool IsFocused { get; set; } = false;
 
-            TextLabel text = view as TextLabel;
-            TextAttributes textAttrs = attrs as TextAttributes;
-            if (text != null && textAttrs != null)
-            {
-                if (textAttrs.Text?.GetValue(State) != null )
-                {
-                    text.Text = textAttrs.Text.GetValue(State);
-                }
-                if (textAttrs.TranslatableText?.GetValue(State) != null)
-                {
-                    text.TranslatableText = textAttrs.TranslatableText.GetValue(State);
-                }
-                if (textAttrs.MultiLine != null)
-                {
-                    text.MultiLine = textAttrs.MultiLine.Value;
-                }
-                if (textAttrs.HorizontalAlignment != null)
-                {
-                    text.HorizontalAlignment = textAttrs.HorizontalAlignment.Value;
-                }
-                if (textAttrs.VerticalAlignment != null)
-                {
-                    text.VerticalAlignment = textAttrs.VerticalAlignment.Value;
-                }
-                if (textAttrs.EnableMarkup != null)
-                {
-                    text.EnableMarkup = textAttrs.EnableMarkup.Value;
-                }
-                if (textAttrs.AutoScrollLoopCount != null)
-                {
-                    text.AutoScrollLoopCount = textAttrs.AutoScrollLoopCount.Value;
-                }
-                if (textAttrs.AutoScrollSpeed != null)
-                {
-                    text.AutoScrollSpeed = textAttrs.AutoScrollSpeed.Value;
-                }
-                if (textAttrs.AutoScrollGap != null)
-                {
-                    text.AutoScrollGap = textAttrs.AutoScrollGap.Value;
-                }
-                if (textAttrs.AutoScrollLoopDelay != null)
-                {
-                    text.AutoScrollLoopDelay = textAttrs.AutoScrollLoopDelay.Value;
-                }
-                if (textAttrs.AutoScrollStopMode != null)
-                {
-                    text.AutoScrollStopMode = textAttrs.AutoScrollStopMode.Value;
-                }
-                if (textAttrs.LineSpacing != null)
-                {
-                    text.LineSpacing = textAttrs.LineSpacing.Value;
-                }
-                if (textAttrs.TextColor?.GetValue(State) != null)
-                {
-                    text.TextColor = textAttrs.TextColor.GetValue(State);
-                }
-                if (textAttrs.FontFamily != null)
-                {
-                    text.FontFamily = textAttrs.FontFamily;
-                }
-                if (textAttrs.PointSize?.GetValue(State) != null)
-                {
-                    text.PointSize = textAttrs.PointSize.GetValue(State).Value;
-                }
-
-                int thickness = 0;
-
-                if (textAttrs.OutstrokeThickness?.GetValue(State) != null)
-                {
-                    thickness = textAttrs.OutstrokeThickness.GetValue(State).Value;
-                }
-                if (textAttrs.OutstrokeColor?.GetValue(State) != null)
-                {
-                    Color outstrokeColor = textAttrs.OutstrokeColor.GetValue(State);
-                    PropertyMap outlineMap = new PropertyMap();
-                    outlineMap.Add("color", new PropertyValue(new Color(outstrokeColor.R, outstrokeColor.G, outstrokeColor.B, outstrokeColor.A)));
-                    outlineMap.Add("width", new PropertyValue(thickness));
-                    text.Outline = outlineMap;
-                }
-                else
-                {
-                    text.Outline = new PropertyMap();
-                }
-            }
+        internal void CommandCanExecuteChanged(object sender, EventArgs eventArgs)
+        {
+            ICommand cmd = Command;
+            if (cmd != null)
+                cmd.CanExecute(CommandParameter);
+        }
 
-            TextField textField = view as TextField;
-            TextFieldAttributes textFieldAttrs = attrs as TextFieldAttributes;
-            if (textField != null && textFieldAttrs != null)
+        internal void OnCommandChanged()
+        {
+            if (Command != null)
             {
-                if (textFieldAttrs.Text?.GetValue(State) != null)
-                {
-                    textField.Text = textFieldAttrs.Text.GetValue(State);
-                }
-                if (textFieldAttrs.PlaceholderText?.GetValue(State) != null)
-                {
-                    textField.PlaceholderText = textFieldAttrs.PlaceholderText.GetValue(State);
-                }
-                if (textFieldAttrs.TranslatablePlaceholderText?.GetValue(State) != null)
-                {
-                    textField.TranslatablePlaceholderText = textFieldAttrs.TranslatablePlaceholderText.GetValue(State);
-                }
-                if (textFieldAttrs.HorizontalAlignment != null)
-                {
-                    textField.HorizontalAlignment = textFieldAttrs.HorizontalAlignment.Value;
-                }
-                if (textFieldAttrs.VerticalAlignment != null)
-                {
-                    textField.VerticalAlignment = textFieldAttrs.VerticalAlignment.Value;
-                }
-                if (textFieldAttrs.EnableMarkup != null)
-                {
-                    textField.EnableMarkup = textFieldAttrs.EnableMarkup.Value;
-                }
-                if (textFieldAttrs.TextColor?.GetValue(State) != null)
-                {
-                    textField.TextColor = textFieldAttrs.TextColor.GetValue(State);
-                }
-                if (textFieldAttrs.PlaceholderTextColor?.GetValue(State) != null)
-                {
-                    textField.PlaceholderTextColor = textFieldAttrs.PlaceholderTextColor.GetValue(State);
-                }
-                if (textFieldAttrs.PrimaryCursorColor?.GetValue(State) != null)
-                {
-                    textField.PrimaryCursorColor = textFieldAttrs.PrimaryCursorColor.GetValue(State);
-                }
-                if (textFieldAttrs.SecondaryCursorColor?.GetValue(State) != null)
-                {
-                    textField.SecondaryCursorColor = textFieldAttrs.SecondaryCursorColor.GetValue(State);
-                }
-                if (textFieldAttrs.FontFamily != null)
-                {
-                    textField.FontFamily = textFieldAttrs.FontFamily;
-                }
-                if (textFieldAttrs.PointSize?.GetValue(State) != null)
-                {
-                    textField.PointSize = textFieldAttrs.PointSize.GetValue(State).Value;
-                }
-                if (textFieldAttrs.EnableCursorBlink != null)
-                {
-                    textField.EnableCursorBlink = textFieldAttrs.EnableCursorBlink.Value;
-                }
-                if (textFieldAttrs.EnableSelection != null)
-                {
-                    textField.EnableSelection = textFieldAttrs.EnableSelection.Value;
-                }
-                if (textFieldAttrs.CursorWidth != null)
-                {
-                    textField.CursorWidth = textFieldAttrs.CursorWidth.Value;
-                }
-                if (textFieldAttrs.EnableEllipsis != null)
-                {
-                    textField.Ellipsis = textFieldAttrs.EnableEllipsis.Value;
-                }
+                Command.CanExecuteChanged += CommandCanExecuteChanged;
+                CommandCanExecuteChanged(this, EventArgs.Empty);
             }
         }
+
         /// <summary>
         /// Dispose Control and all children on it.
         /// </summary>
@@ -361,15 +174,10 @@ namespace Tizen.NUI.Components
                 tapGestureDetector.Detected -= OnTapGestureDetected;
                 tapGestureDetector.Detach(this);
             }
+
             base.Dispose(type);
         }
-        /// <summary>
-        /// Get attribues, it is abstract function and must be override.
-        /// </summary>
-        /// <since_tizen> 6 </since_tizen>
-        /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        protected abstract Attributes GetAttributes();
+
         /// <summary>
         /// Called after a key event is received by the view that has had its focus set.
         /// </summary>
@@ -396,6 +204,7 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         public override void OnRelayout(Vector2 size, RelayoutContainer container)
         {
+            base.OnRelayout(size, container);
             OnUpdate();
         }
 
@@ -407,7 +216,7 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         public override void OnFocusGained()
         {
-            isFocused = true;
+            IsFocused = true;
         }
 
         /// <summary>
@@ -418,7 +227,7 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         public override void OnFocusLost()
         {
-            isFocused = false;
+            IsFocused = false;
         }
 
         /// <summary>
@@ -429,9 +238,8 @@ namespace Tizen.NUI.Components
         /// <since_tizen> 6 </since_tizen>
         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
         [EditorBrowsable(EditorBrowsableState.Never)]
-        protected virtual void OnTapGestureDetected(object source, TapGestureDetector.DetectedEventArgs e)
-        {
-        }
+        protected virtual void OnTapGestureDetected(object source, TapGestureDetector.DetectedEventArgs e) { }
+
         /// <summary>
         /// Called after a touch event is received by the owning view.<br />
         /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
@@ -443,11 +251,28 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         public override bool OnTouch(Touch touch)
         {
+            // Handle Normal and Pressed states
+            PointStateType state = touch.GetState(0);
+            switch(state)
+            {
+                case PointStateType.Down:
+                    ControlState = ControlStates.Pressed;
+                    break;
+                case PointStateType.Interrupted:
+                case PointStateType.Up:
+                    if (ControlState == ControlStates.Pressed)
+                    {
+                        ControlState = ControlStates.Normal;
+                    }
+                    break;
+                default:
+                    break;
+            }
             return false;
         }
 
         /// <summary>
-        /// Update by attributes.
+        /// Update by style.
         /// </summary>
         /// <since_tizen> 6 </since_tizen>
         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
@@ -455,6 +280,7 @@ namespace Tizen.NUI.Components
         protected virtual void OnUpdate()
         {
         }
+
         /// <summary>
         /// Theme change callback when theme is changed, this callback will be trigger.
         /// </summary>
@@ -463,14 +289,24 @@ namespace Tizen.NUI.Components
         /// <since_tizen> 6 </since_tizen>
         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
         [EditorBrowsable(EditorBrowsableState.Never)]
-        protected virtual void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
+        protected virtual void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e) { }
+
+        /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        protected virtual void RegisterDetectionOfSubstyleChanges() { }
+
+        /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        protected override ViewStyle CreateViewStyle()
         {
+            return new ControlStyle();
         }
 
         private void Initialize(string style)
         {
-            attributes = (style == null) ? GetAttributes() : GetAttributes(style);
-            State = ControlStates.Normal;
+            ControlState = ControlStates.Normal;
+
+            RegisterDetectionOfSubstyleChanges();
 
             LeaveRequired = true;
 
@@ -481,17 +317,5 @@ namespace Tizen.NUI.Components
 
             StyleManager.Instance.ThemeChangedEvent += OnThemeChangedEvent;
         }
-
-        private Attributes GetAttributes(string style)
-        {
-            Attributes attributes = StyleManager.Instance.GetAttributes(style);
-            if(attributes == null)
-            {
-                throw new InvalidOperationException($"There is no style {style}");
-            }
-            this.style = style;
-            return attributes;
-        }
-
     }
 }