#nullable enable
using System.ComponentModel;
namespace Terminal.Gui;
public partial class View // Command APIs
{
#region Default Implementation
///
/// Helper to configure all things Command related for a View. Called from the View constructor.
///
private void SetupCommands ()
{
// Enter - Raise Accepted
AddCommand (Command.Accept, RaiseAccepting);
// HotKey - SetFocus and raise HandlingHotKey
AddCommand (Command.HotKey,
() =>
{
if (RaiseHandlingHotKey () is true)
{
return true;
}
SetFocus ();
return true;
});
// Space or single-click - Raise Selecting
AddCommand (Command.Select, (ctx) =>
{
if (RaiseSelecting (ctx) is true)
{
return true;
}
if (CanFocus)
{
SetFocus ();
return true;
}
return false;
});
}
///
/// Called when the user is accepting the state of the View and the has been invoked. Calls which can be cancelled; if not cancelled raises .
/// event. The default handler calls this method.
///
///
///
/// The event should raised after the state of the View has changed (after is raised).
///
///
/// If the Accepting event is not handled, will be invoked on the SuperView, enabling default Accept behavior.
///
///
/// If a peer-View raises the Accepting event and the event is not cancelled, the will be invoked on the
/// first Button in the SuperView that has set to .
///
///
///
/// if no event was raised; input proessing should continue.
/// if the event was raised and was not handled (or cancelled); input proessing should continue.
/// if the event was raised and handled (or cancelled); input proessing should stop.
///
protected bool? RaiseAccepting (CommandContext ctx)
{
CommandEventArgs args = new () { Context = ctx };
// Best practice is to invoke the virtual method first.
// This allows derived classes to handle the event and potentially cancel it.
args.Cancel = OnAccepting (args) || args.Cancel;
if (!args.Cancel)
{
// If the event is not canceled by the virtual method, raise the event to notify any external subscribers.
Accepting?.Invoke (this, args);
}
// Accept is a special case where if the event is not canceled, the event is
// - Invoked on any peer-View with IsDefault == true
// - bubbled up the SuperView hierarchy.
if (!args.Cancel)
{
// If there's an IsDefault peer view in Subviews, try it
var isDefaultView = SuperView?.Subviews.FirstOrDefault (v => v is Button { IsDefault: true });
if (isDefaultView != this && isDefaultView is Button { IsDefault: true } button)
{
bool? handled = isDefaultView.InvokeCommand (Command.Accept, ctx: new (Command.Accept, null, null, this));
if (handled == true)
{
return true;
}
}
return SuperView?.InvokeCommand (Command.Accept, ctx: new (Command.Accept, null, null, this)) == true;
}
return Accepting is null ? null : args.Cancel;
}
///
/// Called when the user is accepting the state of the View and the has been invoked. Set CommandEventArgs.Cancel to
/// and return to stop processing.
///
///
///
/// See for more information.
///
///
///
/// to stop processing.
protected virtual bool OnAccepting (CommandEventArgs args) { return false; }
///
/// Cancelable event raised when the user is accepting the state of the View and the has been invoked. Set
/// CommandEventArgs.Cancel to cancel the event.
///
///
///
/// See for more information.
///
///
public event EventHandler? Accepting;
///
/// Called when the user has performed an action (e.g. ) causing the View to change state. Calls which can be cancelled; if not cancelled raises .
/// event. The default handler calls this method.
///
///
/// The event should raised after the state of the View has been changed and before see .
///
///
/// if no event was raised; input proessing should continue.
/// if the event was raised and was not handled (or cancelled); input proessing should continue.
/// if the event was raised and handled (or cancelled); input proessing should stop.
///
protected bool? RaiseSelecting (CommandContext ctx)
{
CommandEventArgs args = new () { Context = ctx };
// Best practice is to invoke the virtual method first.
// This allows derived classes to handle the event and potentially cancel it.
if (OnSelecting (args) || args.Cancel)
{
return true;
}
// If the event is not canceled by the virtual method, raise the event to notify any external subscribers.
Selecting?.Invoke (this, args);
return Selecting is null ? null : args.Cancel;
}
///
/// Called when the user has performed an action (e.g. ) causing the View to change state.
/// Set CommandEventArgs.Cancel to
/// and return to cancel the state change. The default implementation does nothing.
///
/// The event arguments.
/// to stop processing.
protected virtual bool OnSelecting (CommandEventArgs args) { return false; }
///
/// Cancelable event raised when the user has performed an action (e.g. ) causing the View to change state.
/// CommandEventArgs.Cancel to to cancel the state change.
///
public event EventHandler? Selecting;
///
/// Called when the View is handling the user pressing the View's s. Calls which can be cancelled; if not cancelled raises .
/// event. The default handler calls this method.
///
///
/// if no event was raised; input proessing should continue.
/// if the event was raised and was not handled (or cancelled); input proessing should continue.
/// if the event was raised and handled (or cancelled); input proessing should stop.
///
protected bool? RaiseHandlingHotKey ()
{
CommandEventArgs args = new ();
// Best practice is to invoke the virtual method first.
// This allows derived classes to handle the event and potentially cancel it.
if (OnHandlingHotKey (args) || args.Cancel)
{
return true;
}
// If the event is not canceled by the virtual method, raise the event to notify any external subscribers.
HandlingHotKey?.Invoke (this, args);
return HandlingHotKey is null ? null : args.Cancel;
}
///
/// Called when the View is handling the user pressing the View's . Set CommandEventArgs.Cancel to
/// to stop processing.
///
///
/// to stop processing.
protected virtual bool OnHandlingHotKey (CommandEventArgs args) { return false; }
///
/// Cancelable event raised when the View is handling the user pressing the View's . Set
/// CommandEventArgs.Cancel to cancel the event.
///
public event EventHandler? HandlingHotKey;
#endregion Default Implementation
///
/// Function signature commands.
///
/// Provides information about the circumstances of invoking the command (e.g. )
///
/// if no command was found; input proessing should continue.
/// if the command was invoked and was not handled (or cancelled); input proessing should continue.
/// if the command was invoked the command was handled (or cancelled); input proessing should stop.
///
public delegate bool? CommandImplementation (CommandContext ctx);
///
///
/// Sets the function that will be invoked for a . Views should call
/// AddCommand for each command they support.
///
///
/// If AddCommand has already been called for will
/// replace the old one.
///
///
///
///
/// This version of AddCommand is for commands that require .
///
///
/// The command.
/// The delegate.
protected void AddCommand (Command command, CommandImplementation impl) { CommandImplementations [command] = impl; }
///
///
/// Sets the function that will be invoked for a . Views should call
/// AddCommand for each command they support.
///
///
/// If AddCommand has already been called for will
/// replace the old one.
///
///
///
///
/// This version of AddCommand is for commands that do not require a .
/// If the command requires context, use
///
///
///
/// The command.
/// The delegate.
protected void AddCommand (Command command, Func impl) { CommandImplementations [command] = ctx => impl (); }
/// Returns all commands that are supported by this .
///
public IEnumerable GetSupportedCommands () { return CommandImplementations.Keys; }
///
/// Invokes the specified commands.
///
/// The set of commands to invoke.
/// The key that caused the command to be invoked, if any. This will be passed as context with the command.
/// The key binding that was bound to the key and caused the invocation, if any. This will be passed as context with the command.
///
/// if no command was found; input proessing should continue.
/// if at least one command was invoked and was not handled (or cancelled); input proessing should continue.
/// if at least one command was invoked the command was handled (or cancelled); input proessing should stop.
///
public bool? InvokeCommands (Command [] commands, Key? key = null, KeyBinding? keyBinding = null)
{
bool? toReturn = null;
foreach (Command command in commands)
{
if (!CommandImplementations.ContainsKey (command))
{
throw new NotSupportedException (
@$"A KeyBinding was set up for the command {command} ({key}) but that command is not supported by this View ({GetType ().Name})"
);
}
// each command has its own return value
bool? thisReturn = InvokeCommand (command, key, keyBinding);
// if we haven't got anything yet, the current command result should be used
toReturn ??= thisReturn;
// if ever see a true then that's what we will return
if (thisReturn ?? false)
{
toReturn = true;
}
}
return toReturn;
}
/// Invokes the specified command.
/// The command to invoke.
/// The key that caused the command to be invoked, if any. This will be passed as context with the command.
/// The key binding that was bound to the key and caused the invocation, if any. This will be passed as context with the command.
///
/// if no command was found; input proessing should continue.
/// if the command was invoked and was not handled (or cancelled); input proessing should continue.
/// if the command was invoked the command was handled (or cancelled); input proessing should stop.
///
public bool? InvokeCommand (Command command, Key? key = null, KeyBinding? keyBinding = null)
{
if (CommandImplementations.TryGetValue (command, out CommandImplementation? implementation))
{
var context = new CommandContext (command, key, keyBinding); // Create the context here
return implementation (context);
}
return null;
}
///
/// Invokes the specified command.
///
/// The command to invoke.
/// Context to pass with the invocation.
///
/// if no command was found; input proessing should continue.
/// if the command was invoked and was not handled (or cancelled); input proessing should continue.
/// if the command was invoked the command was handled (or cancelled); input proessing should stop.
///
public bool? InvokeCommand (Command command, CommandContext ctx)
{
if (CommandImplementations.TryGetValue (command, out CommandImplementation? implementation))
{
return implementation (ctx);
}
return null;
}
}