#nullable enable
namespace Terminal.Gui;
///
/// Provides a collection of objects bound to a combination of .
///
///
///
public class MouseBindings
{
///
/// Initializes a new instance. This constructor is used when the are not bound to a
/// . This is used for Application.MouseBindings and unit tests.
///
public MouseBindings () { }
/// Adds a to the collection.
///
///
public void Add (MouseFlags mouseFlag, MouseBinding binding)
{
if (TryGet (mouseFlag, out MouseBinding _))
{
throw new InvalidOperationException (@$"A binding for {mouseFlag} exists ({binding}).");
//Bindings [key] = binding;
}
// IMPORTANT: Add a COPY of the key. This is needed because ConfigurationManager.Apply uses DeepMemberWiseCopy
// IMPORTANT: update the memory referenced by the key, and Dictionary uses caching for performance, and thus
// IMPORTANT: Apply will update the Dictionary with the new key, but the old key will still be in the dictionary.
// IMPORTANT: See the ConfigurationManager.Illustrate_DeepMemberWiseCopy_Breaks_Dictionary test for details.
Bindings.Add (mouseFlag, binding);
}
///
/// Adds a new mouse flag combination that will trigger the commands in .
///
/// If the key is already bound to a different array of s it will be rebound
/// .
///
///
///
/// Commands are only ever applied to the current (i.e. this feature cannot be used to switch
/// focus to another view and perform multiple commands there).
///
/// The mouse flags to check.
///
/// The command to invoked on the when is received. When
/// multiple commands are provided,they will be applied in sequence. The bound event will be
/// consumed if any took effect.
///
public void Add (MouseFlags mouseFlags, params Command [] commands)
{
if (mouseFlags == MouseFlags.None)
{
throw new ArgumentException (@"Invalid MouseFlag", nameof (commands));
}
if (commands.Length == 0)
{
throw new ArgumentException (@"At least one command must be specified", nameof (commands));
}
if (TryGet (mouseFlags, out MouseBinding binding))
{
throw new InvalidOperationException (@$"A binding for {mouseFlags} exists ({binding}).");
}
Add (mouseFlags, new MouseBinding (commands));
}
// TODO: Add a dictionary comparer that ignores Scope
// TODO: This should not be public!
/// The collection of objects.
public Dictionary Bindings { get; } = new ();
///
/// Gets the that are bound.
///
///
public IEnumerable GetBoundMouseFlags ()
{
return Bindings.Keys;
}
/// Removes all objects from the collection.
public void Clear () { Bindings.Clear (); }
///
/// Removes all bindings that trigger the given command set. Views can have multiple different events bound to
/// the same command sets and this method will clear all of them.
///
///
public void Clear (params Command [] command)
{
KeyValuePair [] kvps = Bindings
.Where (kvp => kvp.Value.Commands.SequenceEqual (command))
.ToArray ();
foreach (KeyValuePair kvp in kvps)
{
Remove (kvp.Key);
}
}
/// Gets the for the specified combination of .
///
///
public MouseBinding Get (MouseFlags mouseFlags)
{
if (TryGet (mouseFlags, out MouseBinding binding))
{
return binding;
}
throw new InvalidOperationException ($"{mouseFlags} is not bound.");
}
/// Gets the array of s bound to if it exists.
/// The key to check.
///
/// The array of s if is bound. An empty array
/// if not.
///
public Command [] GetCommands (MouseFlags mouseFlags)
{
if (TryGet (mouseFlags, out MouseBinding bindings))
{
return bindings.Commands;
}
return [];
}
/// Gets the first combination of bound to the set of commands specified by .
/// The set of commands to search.
/// The first combination of bound to the set of commands specified by . if the set of caommands was not found.
public MouseFlags? GetMouseFlagsFromCommands (params Command [] commands)
{
return Bindings.FirstOrDefault (a => a.Value.Commands.SequenceEqual (commands)).Key;
}
/// Gets combination of bound to the set of commands specified by .
/// The set of commands to search.
/// The combination of bound to the set of commands specified by . An empty list if the set of caommands was not found.
public IEnumerable GetAllMouseFlagsFromCommands (params Command [] commands)
{
return Bindings.Where (a => a.Value.Commands.SequenceEqual (commands)).Select (a => a.Key);
}
/// Removes a from the collection.
///
public void Remove (MouseFlags mouseFlags)
{
if (!TryGet (mouseFlags, out MouseBinding _))
{
return;
}
Bindings.Remove (mouseFlags);
}
/// Replaces the commands already bound to a combination of .
///
///
/// If the combination of is not already bound, it will be added.
///
///
/// The combination of bound to the command to be replaced.
/// The set of commands to replace the old ones with.
public void ReplaceCommands (MouseFlags mouseFlags, params Command [] commands)
{
if (TryGet (mouseFlags, out MouseBinding binding))
{
binding.Commands = commands;
}
else
{
Add (mouseFlags, commands);
}
}
/// Replaces a combination already bound to a set of s.
///
/// The to be replaced.
/// The new to be used. If no action will be taken.
public void ReplaceKey (MouseFlags oldMouseFlags, MouseFlags newMouseFlags)
{
if (!TryGet (oldMouseFlags, out MouseBinding _))
{
throw new InvalidOperationException ($"Key {oldMouseFlags} is not bound.");
}
MouseBinding value = Bindings [oldMouseFlags];
Remove (oldMouseFlags);
Add (newMouseFlags, value);
}
/// Gets the commands bound with the specified .
///
/// The key to check.
///
/// When this method returns, contains the commands bound with the specified mouse flags, if the mouse flags are
/// found; otherwise, null. This parameter is passed uninitialized.
///
/// if the mouse flags are bound; otherwise .
public bool TryGet (MouseFlags mouseFlags, out MouseBinding binding)
{
binding = new ([]);
return Bindings.TryGetValue (mouseFlags, out binding);
}
}