[NUI] Add RemoveAllChildren() in ScrollableBase (#2076)
authordongsug-song <35130733+dongsug-song@users.noreply.github.com>
Wed, 7 Oct 2020 07:53:24 +0000 (16:53 +0900)
committerGitHub <noreply@github.com>
Wed, 7 Oct 2020 07:53:24 +0000 (16:53 +0900)
- Add RemoveAllChildren(bool dispose) as hidden API in ScrollableBase class.
- This removes all children in ContentContainer.
- If dispose is true, removed child is also disposed.

src/Tizen.NUI.Components/Controls/ScrollableBase.cs
test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/RemoveAllChildrenTest.cs [new file with mode: 0755]

index 3e2632c..eccde39 100755 (executable)
@@ -1177,6 +1177,40 @@ namespace Tizen.NUI.Components
                 return new Position(-ContentContainer.CurrentPosition);
             }
         }
+
+        /// <summary>
+        /// Remove all children in ContentContainer.
+        /// </summary>
+        /// <param name="dispose">If true, removed child is disposed.</param>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public void RemoveAllChildren(bool dispose = false)
+        {
+            RecursiveRemoveChildren(ContentContainer, dispose);
+        }
+
+        private void RecursiveRemoveChildren(View parent, bool dispose)
+        {
+            if (parent == null)
+            {
+                return;
+            }
+            int maxChild = (int)parent.GetChildCount();
+            for (int i = maxChild - 1; i >= 0; --i)
+            {
+                View child = parent.GetChildAt((uint)i);
+                if (child == null)
+                {
+                    continue;
+                }
+                RecursiveRemoveChildren(child, dispose);
+                parent.Remove(child);
+                if (dispose)
+                {
+                    child.Dispose();
+                }
+            }
+        }
+
     }
 
 } // namespace
diff --git a/test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/RemoveAllChildrenTest.cs b/test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/RemoveAllChildrenTest.cs
new file mode 100755 (executable)
index 0000000..7ae671b
--- /dev/null
@@ -0,0 +1,240 @@
+
+using Tizen.NUI;
+using Tizen.NUI.BaseComponents;
+using NUnit.Framework;
+using Tizen.NUI.Components;
+
+namespace Tizen.NUI.Samples
+{
+    using tlog = Tizen.Log;
+    public class RemoveAllChildrenTest : IExample
+    {
+        const string tag = "NUITEST";
+
+        public void Activate()
+        {
+            test();
+        }
+        public void Deactivate()
+        {
+        }
+
+        private TextLabel t1, t2, t3, t4;
+        private Window window;
+        private ScrollableBase scroll;
+        private View grid;
+
+        void test()
+        {
+            window = NUIApplication.GetDefaultWindow();
+            window.KeyEvent += OnKeyEvent;
+
+            t1 = new TextLabel()
+            {
+                Size = new Size(400, 100),
+                Position = new Position(10, 10),
+                Text = "TextLable-1",
+                BackgroundColor = Color.Yellow,
+                Name = "tl-1",
+            };
+            window.Add(t1);
+
+            t2 = new TextLabel()
+            {
+                Size = new Size(400, 150),
+                Position = new Position(10, 120),
+                Text = "TextLable-2",
+                BackgroundColor = Color.Green,
+                Name = "tl-2",
+            };
+            window.Add(t2);
+
+            scroll = new ScrollableBase()
+            {
+                Size = new Size(400, 500),
+                Position = new Position(10, 280),
+                BackgroundColor = Color.Red,
+                ScrollingDirection = ScrollableBase.Direction.Vertical,
+                Layout = new LinearLayout()
+                {
+                    LinearOrientation = LinearLayout.Orientation.Vertical,
+                },
+                Padding = new Extents(20, 20, 20, 20),
+                Name = "scr-1",
+            };
+            window.Add(scroll);
+
+            grid = new View()
+            {
+                Layout = new GridLayout()
+                {
+                    GridOrientation = GridLayout.Orientation.Vertical,
+                    Columns = 3,
+                    Rows = 3,
+                },
+                WidthSpecification = 390,
+                HeightSpecification = 590,
+                BackgroundColor = new Color(global::System.Drawing.Color.FromName("SlateBlue")),
+                Padding = new Extents(5, 5, 5, 5),
+                Name = "grid-1",
+            };
+            scroll.Add(grid);
+
+            for (int i = 0; i < 9; i++)
+            {
+                var t = new TextLabel()
+                {
+                    Size = new Size(100, 180),
+                    Text = $"con-{i}",
+                    BackgroundColor = new Color(0.5f, (100 + 10 * i) / 255.0f, 0.5f, 1),
+                    Margin = new Extents(2, 2, 2, 2),
+                    Name = $"sub-tl-{i}",
+                };
+                grid.Add(t);
+            }
+
+            t3 = new TextLabel()
+            {
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                HeightSpecification = 200,
+                Text = "TextLable-3",
+                BackgroundColor = Color.Magenta,
+                Margin = new Extents(10, 10, 10, 10),
+                Name = "tl-3",
+            };
+            scroll.Add(t3);
+
+            t4 = new TextLabel()
+            {
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                HeightSpecification = 200,
+                Text = "TextLable-4",
+                BackgroundColor = new Color(global::System.Drawing.Color.FromName("HotPink")),
+                Margin = new Extents(10, 10, 10, 10),
+                Name = "tl-4",
+            };
+            scroll.Add(t4);
+        }
+
+
+        public void OnKeyEvent(object sender, Window.KeyEventArgs e)
+        {
+            if (e.Key.State == Key.StateType.Down)
+            {
+                if (e.Key.KeyPressedName == "1")
+                {
+                    ChangeScrollableBaseSizeBigger();
+                }
+                else if (e.Key.KeyPressedName == "2")
+                {
+                    ChangeScrollableBaseSizeSmaller();
+                }
+                else if (e.Key.KeyPressedName == "3")
+                {
+                    RemoveAllChildrenDisposeFalse();
+                }
+                else if (e.Key.KeyPressedName == "4")
+                {
+                    RemoveAllChildrenDisposeTrue();
+                }
+                else if (e.Key.KeyPressedName == "5")
+                {
+                    AddChildrenInScrollableBase();
+                }
+            }
+        }
+
+        [Test]
+        void ChangeScrollableBaseSizeBigger()
+        {
+            tlog.Debug(tag, "ChangeScrollableBaseSizeBigger test");
+            t2.Position += new Position(10, -100);
+            scroll.Position += new Position(0, -100);
+            scroll.HeightSpecification += 100;
+        }
+
+        [Test]
+        void ChangeScrollableBaseSizeSmaller()
+        {
+            tlog.Debug(tag, "ChangeScrollableBaseSizeSmaller test");
+            t2.Position += new Position(-10, 100);
+            scroll.Position += new Position(0, 100);
+            scroll.HeightSpecification -= 100;
+
+        }
+
+        [Test]
+        void AddChildrenInScrollableBase()
+        {
+            tlog.Debug(tag, "AddChildrenInScrollableBase test");
+
+            grid = new View()
+            {
+                Layout = new GridLayout()
+                {
+                    GridOrientation = GridLayout.Orientation.Vertical,
+                    Columns = 3,
+                    Rows = 3,
+                },
+                WidthSpecification = 390,
+                HeightSpecification = 590,
+                BackgroundColor = new Color(global::System.Drawing.Color.FromName("SlateBlue")),
+                Padding = new Extents(5, 5, 5, 5),
+                Name = "grid-1",
+            };
+            scroll.Add(grid);
+
+            for (int i = 0; i < 9; i++)
+            {
+                var t = new TextLabel()
+                {
+                    Size = new Size(100, 180),
+                    Text = $"con-{i}",
+                    BackgroundColor = new Color(0.5f, (100 + 10 * i) / 255.0f, 0.5f, 1),
+                    Margin = new Extents(2, 2, 2, 2),
+                    Name = $"sub-tl-{i}",
+                };
+                grid.Add(t);
+            }
+
+            t3 = new TextLabel()
+            {
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                HeightSpecification = 200,
+                Text = "TextLable-3",
+                BackgroundColor = Color.Magenta,
+                Margin = new Extents(10, 10, 10, 10),
+                Name = "tl-3",
+            };
+            scroll.Add(t3);
+
+            t4 = new TextLabel()
+            {
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                HeightSpecification = 200,
+                Text = "TextLable-4",
+                BackgroundColor = new Color(global::System.Drawing.Color.FromName("HotPink")),
+                Margin = new Extents(10, 10, 10, 10),
+                Name = "tl-4",
+            };
+            scroll.Add(t4);
+        }
+
+        [Test]
+        void RemoveAllChildrenDisposeTrue()
+        {
+            tlog.Debug(tag, "RemoveAllChildrenDisposeTrue test");
+
+            scroll.RemoveAllChildren(true);
+        }
+
+        [Test]
+        void RemoveAllChildrenDisposeFalse()
+        {
+            tlog.Debug(tag, "RemoveAllChildrenDisposeFalse test");
+
+            scroll.RemoveAllChildren(false);
+        }
+
+    }
+}