[NUI] replace field declaration with property (#1703)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Control.cs
index baeaae2..9d777b1 100755 (executable)
@@ -19,6 +19,7 @@ using System.Collections.Generic;
 using System.ComponentModel;
 using Tizen.NUI.BaseComponents;
 using Tizen.NUI.Binding;
+using System.Windows.Input;
 
 namespace Tizen.NUI.Components
 {
@@ -30,67 +31,29 @@ namespace Tizen.NUI.Components
     [EditorBrowsable(EditorBrowsableState.Never)]
     public class Control : VisualView
     {
-        /// <summary> BackgroundImageProperty</summary>
+        /// Internal used.
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public new static readonly BindableProperty BackgroundImageProperty = BindableProperty.Create("ControlBackgroundImage", typeof(Selector<string>), typeof(Control), null, propertyChanged: (bindable, oldValue, newValue) =>
-        {
-            var control = (Control)bindable;
-            if (null != newValue)
-            {
-                control.BackgroundImageSelector.Clone((Selector<string>)newValue);
-            }
-        },
-        defaultValueCreator: (bindable) =>
-        {
-            var control = (Control)bindable;
-            return control.BackgroundImageSelector;
-        });
-        /// <summary>BackgroundBorderProperty</summary>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public new static readonly BindableProperty BackgroundImageBorderProperty = BindableProperty.Create("ControlBackgroundImageBorder", typeof(Selector<Rectangle>), typeof(Control), default(Rectangle), propertyChanged: (bindable, oldValue, newValue) =>
-        {
-            var control = (Control)bindable;
-            if (null != newValue)
-            {
-                control.backgroundImageBorderSelector.Clone((Selector<Rectangle>)newValue);
-            }
-        },
-        defaultValueCreator: (bindable) =>
-        {
-            var control = (Control)bindable;
-            return control.backgroundImageBorderSelector;
-        });
-        /// <summary> BackgroundColorProperty </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 new static readonly BindableProperty BackgroundColorProperty = BindableProperty.Create("ControlBackgroundColor", typeof(Selector<Color>), typeof(Control), null, propertyChanged: (bindable, oldValue, newValue) =>
-        {
-            var control = (Control)bindable;
-            if (null != newValue)
-            {
-                control.BackgroundColorSelector.Clone((Selector<Color>)newValue);
-            }
-        },
-        defaultValueCreator: (bindable) =>
-        {
-            var control = (Control)bindable;
-            return control.BackgroundColorSelector;
-        });
+        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 string style;
+        protected string StyleName { get; set; }
 
         private TapGestureDetector tapGestureDetector = new TapGestureDetector();
-        private bool isFocused = false;
-
-        internal ImageView backgroundImage = new ImageView();
-        internal ImageView shadowImage;
 
         /// 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.
         /// </summary>
@@ -99,6 +62,22 @@ 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);
         }
 
@@ -130,127 +109,52 @@ namespace Tizen.NUI.Components
             }
 
             ApplyStyle(viewStyle);
-            this.style = styleSheet;
+            this.StyleName = styleSheet;
 
-            Initialize(style);
+            Initialize(StyleName);
         }
 
-        private TriggerableSelector<string> _backgroundImageSelector;
-        private TriggerableSelector<string> BackgroundImageSelector
-        {
-            get
-            {
-                if (null == _backgroundImageSelector)
-                {
-                    _backgroundImageSelector = new TriggerableSelector<string>(backgroundImage, ImageView.ResourceUrlProperty);
-                }
-                return _backgroundImageSelector;
-            }
-        }
-        private TriggerableSelector<Rectangle> _backgroundImageBorderSelector;
-        private TriggerableSelector<Rectangle> backgroundImageBorderSelector
-        {
-            get
-            {
-                if (null == _backgroundImageBorderSelector)
-                {
-                    _backgroundImageBorderSelector = new TriggerableSelector<Rectangle>(backgroundImage, ImageView.BorderProperty);
-                }
-                return _backgroundImageBorderSelector;
-            }
-        }
-        private TriggerableSelector<Color> _backgroundColorSelector;
-        private TriggerableSelector<Color> BackgroundColorSelector
-        {
-            get
-            {
-                if (null == _backgroundColorSelector)
-                {
-                    _backgroundColorSelector = new TriggerableSelector<Color>(backgroundImage, View.BackgroundColorProperty);
-                }
-                return _backgroundColorSelector;
-            }
-        }
-        /// <summary>
-        /// Override view's BackgroundImage.
-        /// </summary>
-        /// 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 Selector<string> BackgroundImage
+        public ICommand Command
         {
-            get => (Selector<string>)GetValue(BackgroundImageProperty);
-            set => SetValue(BackgroundImageProperty, value);
+            get { return (ICommand)GetValue(CommandProperty); }
+            set { SetValue(CommandProperty, value); }
         }
 
-        /// <summary>
-        /// Override view's BackgroundImageBorder.
-        /// </summary>
-        /// 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 new Selector<Rectangle> BackgroundImageBorder
-        {
-            get => (Selector<Rectangle>)GetValue(BackgroundImageBorderProperty);
-            set => SetValue(BackgroundImageBorderProperty, value);
-        }
-        /// <summary>
-        /// Override view's BackgroundBorder.
-        /// </summary>
-        /// 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 Selector<Color> BackgroundColor
+        public object CommandParameter
         {
-            get => (Selector<Color>)GetValue(BackgroundColorProperty);
-            set => SetValue(BackgroundColorProperty, value);
+            get { return GetValue(CommandParameterProperty); }
+            set { SetValue(CommandParameterProperty, value); }
         }
 
         /// <summary>
-        /// Shadow image.
+        /// Whether focusable when touch
         /// </summary>
-        /// 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 Selector<string> ShadowImage
+        /// <since_tizen> 6 </since_tizen>
+        internal bool StateFocusableOnTouchMode { get; set; }
+
+        internal bool IsFocused { get; set; } = false;
+
+        internal void CommandCanExecuteChanged(object sender, EventArgs eventArgs)
         {
-            get
-            {
-                return Style.Shadow.ResourceUrl;
-            }
-            set
-            {
-                Style.Shadow.ResourceUrl = value;
-            }
+            ICommand cmd = Command;
+            if (cmd != null)
+                cmd.CanExecute(CommandParameter);
         }
 
-        /// <summary>
-        /// Shadow image border.
-        /// </summary>
-        /// 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 Selector<Rectangle> ShadowImageBorder
+        internal void OnCommandChanged()
         {
-            get
+            if (Command != null)
             {
-                return Style.Shadow.Border;
+                Command.CanExecuteChanged += CommandCanExecuteChanged;
+                CommandCanExecuteChanged(this, EventArgs.Empty);
             }
-            set
-            {
-                Style.Shadow.Border = value;
-            }
-        }
-
-        internal void ApplyAttributes(View view, ViewStyle viewStyle)
-        {
-            view.CopyFrom(viewStyle);
         }
 
         /// <summary>
-        /// Whether focusable when touch
-        /// </summary>
-        /// <since_tizen> 6 </since_tizen>
-        internal bool StateFocusableOnTouchMode { get; set; }
-
-        internal bool IsFocused => (isFocused || HasFocus());
-
-        /// <summary>
         /// Dispose Control and all children on it.
         /// </summary>
         /// <param name="type">Dispose type.</param>
@@ -271,15 +175,6 @@ namespace Tizen.NUI.Components
                 tapGestureDetector.Detach(this);
             }
 
-            if (backgroundImage != null)
-            {
-                Utility.Dispose(backgroundImage);
-            }
-            if (shadowImage != null)
-            {
-                Utility.Dispose(shadowImage);
-            }
-
             base.Dispose(type);
         }
 
@@ -309,6 +204,7 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         public override void OnRelayout(Vector2 size, RelayoutContainer container)
         {
+            base.OnRelayout(size, container);
             OnUpdate();
         }
 
@@ -320,7 +216,7 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         public override void OnFocusGained()
         {
-            isFocused = true;
+            IsFocused = true;
         }
 
         /// <summary>
@@ -331,38 +227,7 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         public override void OnFocusLost()
         {
-            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 override void ApplyStyle(ViewStyle viewStyle)
-        {
-            base.ApplyStyle(viewStyle);
-
-            ControlStyle controlStyle = viewStyle as ControlStyle;
-
-            if (null != controlStyle?.Shadow)
-            {
-                if (null == shadowImage)
-                {
-                    shadowImage = new ImageView()
-                    {
-                        WidthResizePolicy = ResizePolicyType.FillToParent,
-                        HeightResizePolicy = ResizePolicyType.FillToParent,
-                    };
-                    this.Add(shadowImage);
-                    shadowImage.LowerToBottom();
-                }
-
-                shadowImage.ApplyStyle(controlStyle.Shadow);
-            }
-            if (null != controlStyle.BackgroundImage)
-            {
-                backgroundImage.WidthResizePolicy = ResizePolicyType.FillToParent;
-                backgroundImage.HeightResizePolicy = ResizePolicyType.FillToParent;
-                this.Add(backgroundImage);
-            }
+            IsFocused = false;
         }
 
         /// <summary>
@@ -386,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.
@@ -415,7 +297,7 @@ namespace Tizen.NUI.Components
 
         /// 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 GetViewStyle()
+        protected override ViewStyle CreateViewStyle()
         {
             return new ControlStyle();
         }