// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using osu.Framework.Bindables; namespace osu.Framework.Graphics.Containers { /// /// A container which adds a basic visibility state. /// public abstract class VisibilityContainer : Container { /// /// The current visibility state. /// public readonly Bindable State = new Bindable(); private bool didInitialHide; /// /// Whether we should be in a hidden state when first displayed. /// Override this and set to true to *always* perform a animation even when the state is non-hidden at /// first display. /// protected virtual bool StartHidden => State.Value == Visibility.Hidden; protected override void LoadAsyncComplete() { base.LoadAsyncComplete(); if (StartHidden) { // do this without triggering the StateChanged event, since hidden is a default. PopOut(); FinishTransforms(true); didInitialHide = true; } } protected override void LoadComplete() { State.BindValueChanged(UpdateState, State.Value == Visibility.Visible || !didInitialHide); base.LoadComplete(); } /// /// Show this container by setting its visibility to . /// public override void Show() => State.Value = Visibility.Visible; /// /// Hide this container by setting its visibility to . /// public override void Hide() => State.Value = Visibility.Hidden; /// /// Toggle this container's visibility. /// public void ToggleVisibility() => State.Value = State.Value == Visibility.Visible ? Visibility.Hidden : Visibility.Visible; public override bool PropagateNonPositionalInputSubTree => base.PropagateNonPositionalInputSubTree && State.Value == Visibility.Visible; public override bool PropagatePositionalInputSubTree => base.PropagatePositionalInputSubTree && State.Value == Visibility.Visible; /// /// Implement any transition to be played when becomes . /// protected abstract void PopIn(); /// /// Implement any transition to be played when becomes . /// Will be invoked once on if is set. /// protected abstract void PopOut(); /// /// Called whenever is changed. /// Used to update this container's elements according to the new visibility state. /// /// The provided by protected virtual void UpdateState(ValueChangedEvent state) { switch (state.NewValue) { case Visibility.Hidden: PopOut(); break; case Visibility.Visible: PopIn(); break; } } } public enum Visibility { Hidden, Visible } }