using System.Text.Json.Serialization;
namespace Terminal.Gui;
partial class Application
{
private static Key _alternateForwardKey = Key.Empty; // Defined in config.json
/// Alternative key to navigate forwards through views. Ctrl+Tab is the primary key.
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
[JsonConverter (typeof (KeyJsonConverter))]
public static Key AlternateForwardKey
{
get => _alternateForwardKey;
set
{
if (_alternateForwardKey != value)
{
Key oldKey = _alternateForwardKey;
_alternateForwardKey = value;
OnAlternateForwardKeyChanged (new (oldKey, value));
}
}
}
private static void OnAlternateForwardKeyChanged (KeyChangedEventArgs e)
{
foreach (Toplevel top in _topLevels.ToArray ())
{
top.OnAlternateForwardKeyChanged (e);
}
}
private static Key _alternateBackwardKey = Key.Empty; // Defined in config.json
/// Alternative key to navigate backwards through views. Shift+Ctrl+Tab is the primary key.
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
[JsonConverter (typeof (KeyJsonConverter))]
public static Key AlternateBackwardKey
{
get => _alternateBackwardKey;
set
{
if (_alternateBackwardKey != value)
{
Key oldKey = _alternateBackwardKey;
_alternateBackwardKey = value;
OnAlternateBackwardKeyChanged (new (oldKey, value));
}
}
}
private static void OnAlternateBackwardKeyChanged (KeyChangedEventArgs oldKey)
{
foreach (Toplevel top in _topLevels.ToArray ())
{
top.OnAlternateBackwardKeyChanged (oldKey);
}
}
private static Key _quitKey = Key.Empty; // Defined in config.json
/// Gets or sets the key to quit the application.
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
[JsonConverter (typeof (KeyJsonConverter))]
public static Key QuitKey
{
get => _quitKey;
set
{
if (_quitKey != value)
{
Key oldKey = _quitKey;
_quitKey = value;
OnQuitKeyChanged (new (oldKey, value));
}
}
}
private static void OnQuitKeyChanged (KeyChangedEventArgs e)
{
// Duplicate the list so if it changes during enumeration we're safe
foreach (Toplevel top in _topLevels.ToArray ())
{
top.OnQuitKeyChanged (e);
}
}
///
/// Event fired when the user presses a key. Fired by .
///
/// Set to to indicate the key was handled and to prevent
/// additional processing.
///
///
///
/// All drivers support firing the event. Some drivers (Curses) do not support firing the
/// and events.
/// Fired after and before .
///
public static event EventHandler KeyDown;
///
/// Called by the when the user presses a key. Fires the event
/// then calls on all top level views. Called after and
/// before .
///
/// Can be used to simulate key press events.
///
/// if the key was handled.
public static bool OnKeyDown (Key keyEvent)
{
if (!_initialized)
{
return true;
}
KeyDown?.Invoke (null, keyEvent);
if (keyEvent.Handled)
{
return true;
}
foreach (Toplevel topLevel in _topLevels.ToList ())
{
if (topLevel.NewKeyDownEvent (keyEvent))
{
return true;
}
if (topLevel.Modal)
{
break;
}
}
// Invoke any global (Application-scoped) KeyBindings.
// The first view that handles the key will stop the loop.
foreach (KeyValuePair> binding in _keyBindings.Where (b => b.Key == keyEvent.KeyCode))
{
foreach (View view in binding.Value)
{
if (view is {} && view.KeyBindings.TryGet (binding.Key, (KeyBindingScope)0xFFFF, out KeyBinding kb))
{
//bool? handled = view.InvokeCommands (kb.Commands, binding.Key, kb);
bool? handled = view?.OnInvokingKeyBindings (keyEvent, kb.Scope);
if (handled != null && (bool)handled)
{
return true;
}
}
}
}
return false;
}
///
/// Event fired when the user releases a key. Fired by .
///
/// Set to to indicate the key was handled and to prevent
/// additional processing.
///
///
///
/// All drivers support firing the event. Some drivers (Curses) do not support firing the
/// and events.
/// Fired after .
///
public static event EventHandler KeyUp;
///
/// Called by the when the user releases a key. Fires the event
/// then calls on all top level views. Called after .
///
/// Can be used to simulate key press events.
///
/// if the key was handled.
public static bool OnKeyUp (Key a)
{
if (!_initialized)
{
return true;
}
KeyUp?.Invoke (null, a);
if (a.Handled)
{
return true;
}
foreach (Toplevel topLevel in _topLevels.ToList ())
{
if (topLevel.NewKeyUpEvent (a))
{
return true;
}
if (topLevel.Modal)
{
break;
}
}
return false;
}
///
/// The key bindings.
///
private static readonly Dictionary> _keyBindings = new ();
///
/// Gets the list of key bindings.
///
public static Dictionary> GetKeyBindings () { return _keyBindings; }
///
/// Adds an scoped key binding.
///
///
/// This is an internal method used by the class to add Application key bindings.
///
/// The key being bound.
/// The view that is bound to the key.
internal static void AddKeyBinding (Key key, View view)
{
if (!_keyBindings.ContainsKey (key))
{
_keyBindings [key] = [];
}
_keyBindings [key].Add (view);
}
///
/// Gets the list of Views that have key bindings.
///
///
/// This is an internal method used by the class to add Application key bindings.
///
/// The list of Views that have Application-scoped key bindings.
internal static List GetViewsWithKeyBindings () { return _keyBindings.Values.SelectMany (v => v).ToList (); }
///
/// Gets the list of Views that have key bindings for the specified key.
///
///
/// This is an internal method used by the class to add Application key bindings.
///
/// The key to check.
/// Outputs the list of views bound to
/// if successful.
internal static bool TryGetKeyBindings (Key key, out List views) { return _keyBindings.TryGetValue (key, out views); }
///
/// Removes an scoped key binding.
///
///
/// This is an internal method used by the class to remove Application key bindings.
///
/// The key that was bound.
/// The view that is bound to the key.
internal static void RemoveKeyBinding (Key key, View view)
{
if (_keyBindings.TryGetValue (key, out List views))
{
views.Remove (view);
if (views.Count == 0)
{
_keyBindings.Remove (key);
}
}
}
///
/// Removes all scoped key bindings for the specified view.
///
///
/// This is an internal method used by the class to remove Application key bindings.
///
/// The view that is bound to the key.
internal static void ClearKeyBindings (View view)
{
foreach (Key key in _keyBindings.Keys)
{
_keyBindings [key].Remove (view);
}
}
///
/// Removes all scoped key bindings for the specified view.
///
///
/// This is an internal method used by the class to remove Application key bindings.
///
internal static void ClearKeyBindings () { _keyBindings.Clear (); }
}