[NUI] replace field declaration with property (#1703)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Control.cs
index 0f361d5..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
 {
@@ -29,24 +31,29 @@ namespace Tizen.NUI.Components
     [EditorBrowsable(EditorBrowsableState.Never)]
     public class Control : VisualView
     {
-        /// <summary>
-        /// Control style.
-        /// </summary>
+        /// Internal used.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        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 string style;
+        protected string StyleName { get; set; }
 
         private TapGestureDetector tapGestureDetector = new TapGestureDetector();
-        private bool isFocused = false;
-
-        internal ImageView backgroundImage;
-        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>
@@ -55,101 +62,95 @@ 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(ControlStyle controlStyle) : base(controlStyle)
+        public Control(ControlStyle style) : base(style)
         {
             Initialize(null);
         }
 
         /// <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);
-        }
-
-        public new Selector<string> BackgroundImage
-        {
-            get
+            ViewStyle viewStyle = StyleManager.Instance.GetViewStyle(styleSheet);
+            if (viewStyle == null)
             {
-                return Style.Background.ResourceUrl;
+                throw new InvalidOperationException($"There is no style {styleSheet}");
             }
-            set
-            {
-                Style.Background.ResourceUrl = value;
-            }
-        }
 
-        public Selector<Rectangle> BackgroundBorder
-        {
-            get
-            {
-                return Style.Background.Border;
-            }
-            set
-            {
-                Style.Background.Border = value;
-            }
-        }
+            ApplyStyle(viewStyle);
+            this.StyleName = styleSheet;
 
-        public Selector<string> ShadowImage
-        {
-            get
-            {
-                return Style.Shadow.ResourceUrl;
-            }
-            set
-            {
-                Style.Shadow.ResourceUrl = value;
-            }
+            Initialize(StyleName);
         }
 
-        public Selector<Rectangle> ShadowImageBorder
+        /// Internal used.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public ICommand Command
         {
-            get
-            {
-                return Style.Shadow.Border;
-            }
-            set
-            {
-                Style.Shadow.Border = value;
-            }
+            get { return (ICommand)GetValue(CommandProperty); }
+            set { SetValue(CommandProperty, value); }
         }
 
-        internal void ApplyAttributes(View view, ViewStyle viewStyle)
+        /// Internal used.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public object CommandParameter
         {
-            view.CopyFrom(viewStyle);
+            get { return GetValue(CommandParameterProperty); }
+            set { SetValue(CommandParameterProperty, value); }
         }
 
         /// <summary>
         /// Whether focusable when touch
         /// </summary>
         /// <since_tizen> 6 </since_tizen>
-        internal bool StateFocusableOnTouchMode
+        internal bool StateFocusableOnTouchMode { get; set; }
+
+        internal bool IsFocused { get; set; } = false;
+
+        internal void CommandCanExecuteChanged(object sender, EventArgs eventArgs)
         {
-            get;
-            set;
+            ICommand cmd = Command;
+            if (cmd != null)
+                cmd.CanExecute(CommandParameter);
         }
 
-        internal bool IsFocused
+        internal void OnCommandChanged()
         {
-            get
+            if (Command != null)
             {
-                return isFocused || HasFocus();
+                Command.CanExecuteChanged += CommandCanExecuteChanged;
+                CommandCanExecuteChanged(this, EventArgs.Empty);
             }
         }
 
@@ -174,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);
         }
 
@@ -212,6 +204,7 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         public override void OnRelayout(Vector2 size, RelayoutContainer container)
         {
+            base.OnRelayout(size, container);
             OnUpdate();
         }
 
@@ -223,7 +216,7 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         public override void OnFocusGained()
         {
-            isFocused = true;
+            IsFocused = true;
         }
 
         /// <summary>
@@ -234,37 +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();
-                    this.Add(shadowImage);
-                }
-
-                shadowImage.ApplyStyle(controlStyle.Shadow);
-            }
-
-            if (null != controlStyle?.Background)
-            {
-                if (null == backgroundImage)
-                {
-                    backgroundImage = new ImageView();
-                    this.Add(backgroundImage);
-                }
-                backgroundImage.ApplyStyle(controlStyle.Background);
-            }
+            IsFocused = false;
         }
 
         /// <summary>
@@ -275,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 />
@@ -289,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.
@@ -301,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>
@@ -309,20 +289,15 @@ 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()
-        {
-
-        }
+        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 GetViewStyle()
+        protected override ViewStyle CreateViewStyle()
         {
             return new ControlStyle();
         }
@@ -342,16 +317,5 @@ namespace Tizen.NUI.Components
 
             StyleManager.Instance.ThemeChangedEvent += OnThemeChangedEvent;
         }
-
-        private ViewStyle GetAttributes(string style)
-        {
-            ViewStyle attributes = StyleManager.Instance.GetAttributes(style);
-            if(attributes == null)
-            {
-                throw new InvalidOperationException($"There is no style {style}");
-            }
-            this.style = style;
-            return attributes;
-        }
     }
 }