Editor/Prism/Ui/InlineEditors/BoolInlineEditor.cs

An inline boolean editor component for the editor UI. It renders a small switch, tracks a boolean state derived from a typed Value, measures its UI size, paints the switch with theme colors, and toggles the value on left-click inside the pill area.

Native Interop
using Editor.Prism.Core;
using Editor.Prism.Serialization;
using Editor.Prism.Ui.Adapters;

namespace Editor.Prism.Ui.InlineEditors;

/// <summary>
/// A boolean, drawn as a small switch. A switch reads as a state at any zoom level; a checkbox does
/// not, and a text field reading "false" is worse than either.
/// </summary>
public sealed class BoolInlineEditor : PrismInlineEditor
{
	/// <summary>Bind a boolean editor to a port.</summary>
	public BoolInlineEditor( Plug plug, PrismPlugIn port ) : base( plug, port ) { }

	/// <inheritdoc/>
	protected override float PillHeight => 14f;

	/// <summary>The current state.</summary>
	public bool Current => Value switch
	{
		bool b => b,
		float f => f != 0f,
		int i => i != 0,
		_ => false
	};

	/// <inheritdoc/>
	protected override float MeasureWidth() => 26f;

	/// <inheritdoc/>
	protected override void OnPaintPill( Rect pill )
	{
		var hovered = Paint.HasMouseOver;
		var on = Current;

		PrismPaint.Switch( pill, on,
			PrismTheme.Elevated.Lighten( hovered ? 0.2f : 0f ),
			on ? PrismTheme.TextOnAccent : PrismTheme.TextSecondary,
			TypeColor.WithAlpha( hovered ? 1f : 0.85f ) );
	}

	/// <inheritdoc/>
	protected override void OnMousePressed( GraphicsMouseEvent e )
	{
		if ( !ShouldDraw || !e.LeftMouseButton ) return;
		if ( !PillRect.Grow( 4f ).IsInside( e.LocalPosition ) ) return;

		Commit( Shape( !Current ) );

		e.Accepted = true;
	}
}