[NUI] Fix button bugs and improve switch.
authorJiyun Yang <ji.yang@samsung.com>
Fri, 17 Feb 2023 08:51:57 +0000 (17:51 +0900)
committerJiyun Yang <ji.yang@samsung.com>
Mon, 20 Feb 2023 08:47:22 +0000 (17:47 +0900)
* Button's IconOrientation bug
In the previous code, when the IconOrientation property changed,
the LayoutItems() was called and it created a new layout.
But in case the button was inside another layout, the behaviour
described above caused unexpect look. Therefore I fixed it
by changing orientation related properties to the existing layout
instead of creating a new layout.

* Improve switch and extension relationship
The switch extension does not register any event handler to the switch components anymore.

Signed-off-by: Jiyun Yang <ji.yang@samsung.com>
src/Tizen.NUI.Components/Controls/Button.Internal.cs
src/Tizen.NUI.Components/Controls/Button.cs
src/Tizen.NUI.Components/Controls/Extension/SlidingSwitchExtension.cs
src/Tizen.NUI.Components/Controls/Extension/SwitchExtension.cs
src/Tizen.NUI.Components/Controls/Switch.cs

index ea06e8f..2b84723 100644 (file)
@@ -34,7 +34,7 @@ namespace Tizen.NUI.Components
         private EventHandler<StateChangedEventArgs> stateChangeHandler;
 
         private bool isPressed = false;
-        internal int styleApplying = 0;
+        internal bool styleApplying = false;
 
         /// <summary>
         /// Gets accessibility name.
@@ -193,7 +193,7 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         protected void UpdateState()
         {
-            if (styleApplying > 0) return;
+            if (styleApplying) return;
 
             ControlState sourceState = ControlState;
             ControlState targetState;
@@ -240,19 +240,26 @@ namespace Tizen.NUI.Components
 
             if (type == DisposeTypes.Explicit)
             {
-                Extension?.OnDispose(this);
+                if (Extension != null)
+                {
+                    Extension.OnDispose(this);
+                    Extension = null;
+                }
 
                 if (buttonIcon != null)
                 {
                     Utility.Dispose(buttonIcon);
+                    buttonIcon = null;
                 }
                 if (buttonText != null)
                 {
                     Utility.Dispose(buttonText);
+                    buttonText = null;
                 }
                 if (overlayImage != null)
                 {
                     Utility.Dispose(overlayImage);
+                    overlayImage = null;
                 }
             }
 
@@ -346,54 +353,45 @@ namespace Tizen.NUI.Components
             Size2D cellPadding = String.IsNullOrEmpty(buttonText.Text) ? new Size2D(0, 0) : itemSpacing;
 #pragma warning restore CA2000
 
+            var linearLayout = Layout as LinearLayout;
+            if (linearLayout == null) Layout = (linearLayout = new LinearLayout());
+
             if (IconRelativeOrientation == IconOrientation.Left)
             {
-                Layout = new LinearLayout()
-                {
-                    LinearOrientation = LinearLayout.Orientation.Horizontal,
-                    HorizontalAlignment = itemHorizontalAlignment,
-                    VerticalAlignment = itemVerticalAlignment,
-                    CellPadding = cellPadding
-                };
+                linearLayout.LinearOrientation = LinearLayout.Orientation.Horizontal;
+                linearLayout.HorizontalAlignment = itemHorizontalAlignment;
+                linearLayout.VerticalAlignment = itemVerticalAlignment;
+                linearLayout.CellPadding = cellPadding;
 
                 Add(buttonIcon);
                 Add(buttonText);
             }
             else if (IconRelativeOrientation == IconOrientation.Right)
             {
-                Layout = new LinearLayout()
-                {
-                    LinearOrientation = LinearLayout.Orientation.Horizontal,
-                    HorizontalAlignment = itemHorizontalAlignment,
-                    VerticalAlignment = itemVerticalAlignment,
-                    CellPadding = cellPadding
-                };
+                linearLayout.LinearOrientation = LinearLayout.Orientation.Horizontal;
+                linearLayout.HorizontalAlignment = itemHorizontalAlignment;
+                linearLayout.VerticalAlignment = itemVerticalAlignment;
+                linearLayout.CellPadding = cellPadding;
 
                 Add(buttonText);
                 Add(buttonIcon);
             }
             else if (IconRelativeOrientation == IconOrientation.Top)
             {
-                Layout = new LinearLayout()
-                {
-                    LinearOrientation = LinearLayout.Orientation.Vertical,
-                    HorizontalAlignment = itemHorizontalAlignment,
-                    VerticalAlignment = itemVerticalAlignment,
-                    CellPadding = cellPadding
-                };
+                linearLayout.LinearOrientation = LinearLayout.Orientation.Vertical;
+                linearLayout.HorizontalAlignment = itemHorizontalAlignment;
+                linearLayout.VerticalAlignment = itemVerticalAlignment;
+                linearLayout.CellPadding = cellPadding;
 
                 Add(buttonIcon);
                 Add(buttonText);
             }
             else if (IconRelativeOrientation == IconOrientation.Bottom)
             {
-                Layout = new LinearLayout()
-                {
-                    LinearOrientation = LinearLayout.Orientation.Vertical,
-                    HorizontalAlignment = itemHorizontalAlignment,
-                    VerticalAlignment = itemVerticalAlignment,
-                    CellPadding = cellPadding
-                };
+                linearLayout.LinearOrientation = LinearLayout.Orientation.Vertical;
+                linearLayout.HorizontalAlignment = itemHorizontalAlignment;
+                linearLayout.VerticalAlignment = itemVerticalAlignment;
+                linearLayout.CellPadding = cellPadding;
 
                 Add(buttonText);
                 Add(buttonIcon);
index 033867a..bda6455 100755 (executable)
@@ -1005,20 +1005,18 @@ namespace Tizen.NUI.Components
         {
             Debug.Assert(buttonIcon != null && buttonText != null);
 
-            styleApplying++;
-
             base.ApplyStyle(viewStyle);
 
-            if (viewStyle is ButtonStyle buttonStyle)
+            if (!styleApplying && viewStyle is ButtonStyle buttonStyle)
             {
-                Extension = buttonStyle.CreateExtension();
+                styleApplying = true;
 
                 if (buttonStyle.Overlay != null)
                 {
                     OverlayImage?.ApplyStyle(buttonStyle.Overlay);
                 }
 
-                if (Extension != null)
+                if ((Extension = buttonStyle.CreateExtension()) != null)
                 {
                     buttonIcon.Unparent();
                     buttonIcon = Extension.OnCreateIcon(this, buttonIcon);
@@ -1039,10 +1037,9 @@ namespace Tizen.NUI.Components
                 {
                     buttonIcon.ApplyStyle(buttonStyle.Icon);
                 }
+                styleApplying = false;
             }
 
-            styleApplying--;
-
             UpdateState();
         }
 
index a1c4483..cf877e6 100755 (executable)
@@ -82,27 +82,12 @@ namespace Tizen.NUI.Components.Extension
             }
         }
 
-        /// <inheritdoc/>
+        /// <inheritdoc />
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public override ImageView OnCreateTrack(Switch switchButton, ImageView track)
-        {
-            base.OnCreateTrack(switchButton, track);
-            track.Relayout += (s, e) => {
-                if (slidingAnimation.State == Animation.States.Playing) return;
-                switchButton.Thumb.PositionX = switchButton.IsSelected ? switchButton.Track.Size.Width - switchButton.Thumb.Size.Width : 0;
-            };
-            return track;
-        }
-
-        /// <inheritdoc/>
-        public override ImageView OnCreateThumb(Switch switchButton, ImageView thumb)
+        public override void OnTrackOrThumbResized(Switch switchButton, ImageView track, ImageView thumb)
         {
-            base.OnCreateThumb(switchButton, thumb);
-            thumb.Relayout += (s, e) => {
-                if (slidingAnimation.State == Animation.States.Playing) return;
-                thumb.PositionX = switchButton.IsSelected ? switchButton.Track.Size.Width - thumb.Size.Width : 0;
-            };
-            return thumb;
+            var destX = switchButton.IsSelected ? switchButton.Track.Size.Width - thumb.Size.Width : 0;
+            if (destX != thumb.PositionX) thumb.PositionX = destX;
         }
 
         [EditorBrowsable(EditorBrowsableState.Never)]
index 8868d8b..b9aff15 100755 (executable)
@@ -65,5 +65,11 @@ namespace Tizen.NUI.Components.Extension
         public virtual void OnSelectedChanged(Switch switchButton)
         {
         }
+
+        /// <summary> Called when the Switch's track or thumb resized. </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public virtual void OnTrackOrThumbResized(Switch switchButton, ImageView track, ImageView thumb)
+        {
+        }
     }
 }
index 9e4e69f..301f9d9 100755 (executable)
@@ -115,20 +115,29 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         public override void ApplyStyle(ViewStyle viewStyle)
         {
-            styleApplying++;
+            styleApplying = true;
 
             base.ApplyStyle(viewStyle);
 
             if (viewStyle is SwitchStyle switchStyle)
             {
-                if (Extension is SwitchExtension extension)
+                if (Extension != null) Extension.OnDispose(this);
+
+                if ((Extension = switchStyle.CreateExtension()) != null && Extension is SwitchExtension extension)
                 {
                     Icon.Unparent();
                     thumb.Unparent();
+                    TextLabel.Unparent();
                     Icon = extension.OnCreateTrack(this, Icon);
                     thumb = extension.OnCreateThumb(this, thumb);
                     Icon.Add(thumb);
                     LayoutItems();
+
+                    Icon.Relayout -= OnTrackOrThumbRelayout;
+                    Icon.Relayout += OnTrackOrThumbRelayout;
+
+                    thumb.Relayout -= OnTrackOrThumbRelayout;
+                    thumb.Relayout += OnTrackOrThumbRelayout;
                 }
 
                 if (switchStyle.Track != null)
@@ -140,12 +149,26 @@ namespace Tizen.NUI.Components
                 {
                     Thumb.ApplyStyle(switchStyle.Thumb);
                 }
+
+                if (switchStyle.Text != null)
+                {
+                    TextLabel.ThemeChangeSensitive = false;
+                    TextLabel.ApplyStyle(switchStyle.Text);
+                }
             }
-            styleApplying--;
+            styleApplying = false;
 
             UpdateState();
         }
 
+        private void OnTrackOrThumbRelayout(object sender, EventArgs args)
+        {
+            if (Extension is SwitchExtension switchExtension)
+            {
+                switchExtension.OnTrackOrThumbResized(this, Icon, thumb);
+            }
+        }
+
         /// <summary>
         /// Switch's track part.
         /// </summary>
@@ -292,7 +315,15 @@ namespace Tizen.NUI.Components
 
             if (type == DisposeTypes.Explicit)
             {
-                Utility.Dispose(thumb);
+                if (Icon != null)
+                {
+                    Icon.Relayout -= OnTrackOrThumbRelayout;
+                }
+                if (thumb != null)
+                {
+                    thumb.Relayout -= OnTrackOrThumbRelayout;
+                    Utility.Dispose(thumb);
+                }
             }
 
             base.Dispose(type);