Facepunch / sbox-issues

178 stars 12 forks source link

[Editor] `NavigationView.SwitchPage<T>()` should be public #6460

Open badandbest opened 2 months ago

badandbest commented 2 months ago

What it is?

It feels like NavigationView.SwitchPage<T>() was meant to be public but was left internal by accident. And the list of pages are also private for some reason.

image

What should it be?

It should really be public. A way to get the list of pages could also prove useful.

badandbest commented 2 months ago

In the meantime I make do with this extension class in case anyone wants it.

using Sandbox;
using System.Collections.Generic;
using System.Linq;
namespace Editor.Extensions;

public static class NavigationViewExtensions
{
    /// <inheritdoc cref="NavigationView.pages"/>
    public static HashSet<NavigationView.Option> GetPages( this NavigationView view )
    {
        var p = EditorTypeLibrary.GetType<NavigationView>()
            .GetValue( view, "pages" );

        return p as HashSet<NavigationView.Option>;
    }

    /// <inheritdoc cref="NavigationView.SwitchPage{T}"/>
    public static void SwitchPage<T>( this NavigationView view )
    {
        var p = view.GetPages().FirstOrDefault( x => x.Page is T );
        if ( !p.IsValid() ) return;

        view.CurrentPage = p.Page;
    }

    /// <summary>
    /// Adds a page using display info from given type.
    /// </summary>
    public static NavigationView.Option AddPage<T>( this NavigationView view ) where T : Widget, new()
    {
        var di = DisplayInfo.ForType( typeof( T ) );

        var c = new T();

        return view.AddPage( di, c );
    }
}