Editor/MessagePromptDialog.cs

An editor UI dialog that prompts the user for a short text message. It builds a modal window with a label, a LineEdit, and Post/Cancel buttons, then invokes a provided Action<string> callback with the entered text when submitted.

File Access
using System;
using Sandbox;

namespace Editor.SuperShot;

public sealed class MessagePromptDialog : Dialog
{
	public MessagePromptDialog( string title, string prompt, Action<string> onPost, string okayText = "Post" ) : base( null )
	{
		Window.SetModal( true, true );
		Window.Title = title;
		Window.SetWindowIcon( "chat" );
		Window.Size = new Vector2( 480, 170 );

		var label = new Label( this );
		label.Text = prompt;
		label.WordWrap = true;

		var edit = new LineEdit( this );
		edit.ReturnPressed += () => Submit( edit, onPost );

		var post = new Button( okayText, this );
		post.SetProperty( "type", "primary" );
		post.Clicked = () => Submit( edit, onPost );

		var cancel = new Button( "Cancel", this );
		cancel.Clicked = Close;

		Layout = Layout.Column();

		var content = Layout.AddColumn();
		content.Margin = 16;
		content.Add( label );
		content.AddSpacingCell( 12 );
		content.Add( edit );

		var footer = Layout.AddRow();
		footer.Margin = 16;
		footer.Spacing = 8;
		footer.AddStretchCell();
		footer.Add( post );
		footer.Add( cancel );

		Show();
		edit.Focus();
	}

	void Submit( LineEdit edit, Action<string> onPost )
	{
		onPost?.Invoke( edit.Text ?? "" );
		Close();
	}
}