MenuStyle.cs

Static helper that injects a stylesheet into Popup panels (drop down menus) opened from a given root panel, so they match the mod's Dark and Light theme instead of the engine default. Marks the current button option with a `current` class and re-syncs the theme class each call.

using Sandbox.UI;

namespace Bimp;

/// <summary>
/// A drop down's menu is a popup in the root panel, where a component's stylesheet doesn't reach - it came up in the
/// engine's default look next to our remote. Menus opened from our panel get their own sheet, matching Sandbox's
/// ContextMenu / dropdown design in both Dark and Light themes, with hover, the pad's selection ("pad-selected",
/// see <see cref="PadNavigation"/>), and the current choice marked.
/// </summary>
public static class MenuStyle
{
	const string Styles = """
		.popup-panel.bimp-menu
		{
			background-color: #2a3032;
			border: 1px solid #384042;
			border-radius: 8px;
			box-shadow: none;
			filter: drop-shadow( 0 5px 10px rgba( 0, 0, 0, 0.38 ) );
			color: #dbe1df;
			font-family: "Inter";
			font-size: 13px;
			font-weight: 500;
			padding: 5px;
			margin-top: 4px;
			min-width: 180px;
		}
		.popup-panel.bimp-menu button
		{
			height: 30px;
			padding: 0 8px;
			border: 1px solid transparent;
			border-radius: 5px;
			font-family: "Inter";
			font-size: 13px;
			font-weight: 500;
			color: #dbe1df;
			background-color: transparent;
			white-space: nowrap;
			transition: none;
		}
		.popup-panel.bimp-menu button:hover
		{
			background-color: rgba( 145, 180, 255, 0.14 );
			color: #f1f5f2;
			sound-in: ui.hover;
		}
		.popup-panel.bimp-menu button:active
		{
			sound-in: ui.select;
		}
		.popup-panel.bimp-menu button.current
		{
			background-color: #2d5da1;
			color: #a1e2ff;
			font-weight: 600;
		}
		.popup-panel.bimp-menu button.current:hover
		{
			border-color: #91b4ff;
		}
		.popup-panel.bimp-menu button.pad-selected
		{
			background-color: rgba( 145, 180, 255, 0.14 );
			box-shadow: 0 0 0 2px #6891e0;
			color: #f1f5f2;
		}

		.popup-panel.bimp-menu.bimp-light
		{
			background-color: #f8f8f8;
			border: 1px solid rgba( 0, 0, 0, 0.12 );
			color: #555555;
		}
		.popup-panel.bimp-menu.bimp-light button
		{
			color: #555555;
		}
		.popup-panel.bimp-menu.bimp-light button:hover
		{
			background-color: rgba( 62, 131, 235, 0.14 );
			color: #444444;
		}
		.popup-panel.bimp-menu.bimp-light button.current
		{
			background-color: #2967ba;
			color: white;
			font-weight: 600;
		}
		.popup-panel.bimp-menu.bimp-light button.current:hover
		{
			border-color: rgba( 62, 131, 235, 0.68 );
		}
		.popup-panel.bimp-menu.bimp-light button.pad-selected
		{
			background-color: rgba( 62, 131, 235, 0.14 );
			box-shadow: 0 0 0 2px #3e83eb;
			color: #444444;
		}
		""";

	/// <summary> Call every frame: styles any menu opened from <paramref name="owner"/> and keeps its theme in sync. </summary>
	public static void Apply( Panel owner, bool isLight = false )
	{
		if ( owner is null ) return;
		var top = owner;
		while ( top.Parent is not null ) top = top.Parent;

		foreach ( var menu in top.Descendants.OfType<Popup>() )
		{
			if ( !IsInside( menu.PopupSource, owner ) ) continue;

			if ( !menu.HasClass( "bimp-menu" ) )
			{
				menu.AddClass( "bimp-menu" );
				menu.StyleSheet.AddInline( Styles, "bimp-menu" );

				// the engine's own "active" mark for the current choice compares the values by reference and misses; ours
				// goes by the text the drop down shows
				var current = (menu.PopupSource as Button)?.Text;
				foreach ( var option in menu.Children )
					option.SetClass( "current", option is Button b && b.Text == current );
			}

			menu.SetClass( "bimp-light", isLight );
		}
	}

	static bool IsInside( Panel panel, Panel owner )
	{
		for ( var p = panel; p is not null; p = p.Parent )
			if ( p == owner ) return true;
		return false;
	}
}