Files
sbox-public/engine/Sandbox.Engine/Systems/UI/Controls/BasePopup.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

58 lines
1.0 KiB
C#

namespace Sandbox.UI;
/// <summary>
/// A panel that gets deleted automatically when clicked away from
/// </summary>
public abstract class BasePopup : Panel
{
static List<BasePopup> AllPopups = new();
/// <summary>
/// Stay open, even when CloseAll popups is called
/// </summary>
public bool StayOpen { get; set; }
public static void CloseAll( Panel exceptThisOne = null )
{
if ( AllPopups.Count == 0 )
return;
AllPopups.RemoveAll( x => !x.IsValid() );
BasePopup floater = null;
if ( exceptThisOne is Panel flt )
{
floater = flt.AncestorsAndSelf.OfType<BasePopup>().FirstOrDefault();
}
foreach ( var panel in AllPopups.ToArray() )
{
if ( panel == floater ) continue;
if ( panel.StayOpen && panel.Parent.IsValid() ) continue;
try
{
AllPopups.Remove( panel );
panel.Delete();
}
catch
{
// ignored
}
}
}
public BasePopup()
{
AllPopups.Add( this );
}
public override void OnDeleted()
{
base.OnDeleted();
AllPopups.Remove( this );
}
}