From ab66504d212f629bebbc40664054e7d7e6071fc1 Mon Sep 17 00:00:00 2001 From: dongsug-song <35130733+dongsug-song@users.noreply.github.com> Date: Wed, 7 Oct 2020 16:53:24 +0900 Subject: [PATCH] [NUI] Add RemoveAllChildren() in ScrollableBase (#2076) - 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. --- .../Controls/ScrollableBase.cs | 34 +++ .../Samples/RemoveAllChildrenTest.cs | 240 +++++++++++++++++++++ 2 files changed, 274 insertions(+) create mode 100755 test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/RemoveAllChildrenTest.cs diff --git a/src/Tizen.NUI.Components/Controls/ScrollableBase.cs b/src/Tizen.NUI.Components/Controls/ScrollableBase.cs index 3e2632c..eccde39 100755 --- a/src/Tizen.NUI.Components/Controls/ScrollableBase.cs +++ b/src/Tizen.NUI.Components/Controls/ScrollableBase.cs @@ -1177,6 +1177,40 @@ namespace Tizen.NUI.Components return new Position(-ContentContainer.CurrentPosition); } } + + /// + /// Remove all children in ContentContainer. + /// + /// If true, removed child is disposed. + [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 index 0000000..7ae671b --- /dev/null +++ b/test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/RemoveAllChildrenTest.cs @@ -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); + } + + } +} -- 2.7.4