[NUI] Fix Switch thumb positioning
authorJiyun Yang <ji.yang@samsung.com>
Wed, 15 Feb 2023 02:36:52 +0000 (11:36 +0900)
committerJiyun Yang <ji.yang@samsung.com>
Wed, 15 Feb 2023 07:45:17 +0000 (16:45 +0900)
When the size of thumb or track changes, the it should update the position of the thumb properly.

And to prevent unnecessary switch state updating while applying style, upgrade styleApplying flag to int type.

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/Switch.cs

index b4815a2..071f7f2 100644 (file)
@@ -34,7 +34,7 @@ namespace Tizen.NUI.Components
         private EventHandler<StateChangedEventArgs> stateChangeHandler;
 
         private bool isPressed = false;
-        private bool styleApplying = false;
+        internal int styleApplying = 0;
 
         /// <summary>
         /// Gets accessibility name.
@@ -193,7 +193,7 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         protected void UpdateState()
         {
-            if (styleApplying) return;
+            if (styleApplying > 0) return;
 
             ControlState sourceState = ControlState;
             ControlState targetState;
index cc4bd78..033867a 100755 (executable)
@@ -1005,7 +1005,7 @@ namespace Tizen.NUI.Components
         {
             Debug.Assert(buttonIcon != null && buttonText != null);
 
-            styleApplying = true;
+            styleApplying++;
 
             base.ApplyStyle(viewStyle);
 
@@ -1041,7 +1041,8 @@ namespace Tizen.NUI.Components
                 }
             }
 
-            styleApplying = false;
+            styleApplying--;
+
             UpdateState();
         }
 
index b52ba0e..b431584 100755 (executable)
@@ -82,6 +82,26 @@ namespace Tizen.NUI.Components.Extension
             }
         }
 
+        /// <inheritdoc/>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public override ImageView OnCreateTrack(Switch switchButton, ImageView track)
+        {
+            base.OnCreateTrack(switchButton, track);
+            track.Relayout += (s, e) => {
+                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)
+        {
+            base.OnCreateThumb(switchButton, thumb);
+            thumb.Relayout += (s, e) => {
+                thumb.PositionX = switchButton.IsSelected ? switchButton.Track.Size.Width - thumb.Size.Width : 0;
+            };
+            return thumb;
+        }
 
         [EditorBrowsable(EditorBrowsableState.Never)]
         protected virtual void Dispose(bool disposing)
index 1c1c885..9e4e69f 100755 (executable)
@@ -115,6 +115,10 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         public override void ApplyStyle(ViewStyle viewStyle)
         {
+            styleApplying++;
+
+            base.ApplyStyle(viewStyle);
+
             if (viewStyle is SwitchStyle switchStyle)
             {
                 if (Extension is SwitchExtension extension)
@@ -137,8 +141,9 @@ namespace Tizen.NUI.Components
                     Thumb.ApplyStyle(switchStyle.Thumb);
                 }
             }
+            styleApplying--;
 
-            base.ApplyStyle(viewStyle);
+            UpdateState();
         }
 
         /// <summary>