// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System.Collections.Generic; using System.Linq; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.UserInterface; using osu.Framework.Testing; using osuTK; namespace osu.Framework.Tests.Visual { public class TestCaseSearchContainer : TestCase { public TestCaseSearchContainer() { SearchContainer search; TextBox textBox; Children = new Drawable[] { textBox = new TextBox { Size = new Vector2(300, 40), }, search = new SearchContainer { AutoSizeAxes = Axes.Both, Margin = new MarginPadding { Top = 40 }, Children = new[] { new HeaderContainer { AutoSizeAxes = Axes.Both, Children = new[] { new HeaderContainer("Subsection 1") { AutoSizeAxes = Axes.Both, Children = new Drawable[] { new SearchableText { Text = "test", }, new SearchableText { Text = "TEST", }, new SearchableText { Text = "123", }, new SearchableText { Text = "444", }, new FilterableFlowContainer { Direction = FillDirection.Horizontal, AutoSizeAxes = Axes.Both, Children = new [] { new SpriteText { Text = "multi", }, new SpriteText { Text = "piece", }, new SpriteText { Text = "container", }, } }, new SearchableText { Text = "öüäéèêáàâ", } } }, new HeaderContainer("Subsection 2") { AutoSizeAxes = Axes.Both, Children = new[] { new SearchableText { Text = "?!()[]{}" }, new SearchableText { Text = "@€$" }, }, }, }, } } } }; new Dictionary { { "test", 2 }, { "sUbSeCtIoN 1", 6 }, { "€", 1 }, { "èê", 1 }, { "321", 0 }, { "mul pi", 1}, { "header", 8 } }.ToList().ForEach(term => { AddStep("Search term: " + term.Key, () => search.SearchTerm = term.Key); AddAssert("Visible end-children: " + term.Value, () => term.Value == search.Children.SelectMany(container => container.Children.Cast()).SelectMany(container => container.Children).Count(drawable => drawable.IsPresent)); }); textBox.Current.ValueChanged += newValue => search.SearchTerm = newValue; } private class HeaderContainer : Container, IHasFilterableChildren { public IEnumerable FilterTerms => header.FilterTerms; public bool MatchingFilter { set { if (value) this.FadeIn(); else this.FadeOut(); } } public IEnumerable FilterableChildren => Children.OfType(); protected override Container Content => flowContainer; private readonly SearchableText header; private readonly FillFlowContainer flowContainer; public HeaderContainer(string headerText = "Header") { AddInternal(header = new SearchableText { Text = headerText, }); AddInternal(flowContainer = new FillFlowContainer { Margin = new MarginPadding { Top = header.TextSize, Left = 30 }, AutoSizeAxes = Axes.Both, Direction = FillDirection.Vertical, }); } } private class FilterableFlowContainer : FillFlowContainer, IFilterable { public IEnumerable FilterTerms => Children.OfType().SelectMany(d => d.FilterTerms); public bool MatchingFilter { set { if (value) Show(); else Hide(); } } } private class SearchableText : SpriteText, IFilterable { public bool MatchingFilter { set { if (value) Show(); else Hide(); } } } } }