123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- #nullable enable
- using System.Text.Json.Serialization;
- namespace Terminal.Gui;
- public static partial class Application // Keyboard handling
- {
- private static Key _alternateForwardKey = Key.Empty; // Defined in config.json
- /// <summary>Alternative key to navigate forwards through views. Ctrl+Tab is the primary key.</summary>
- [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)
- {
- // TODO: The fact Top has it's own AlternateForwardKey and events is needlessly complex. Remove it.
- foreach (Toplevel top in _topLevels.ToArray ())
- {
- top.OnAlternateForwardKeyChanged (e);
- }
- }
- private static Key _alternateBackwardKey = Key.Empty; // Defined in config.json
- /// <summary>Alternative key to navigate backwards through views. Shift+Ctrl+Tab is the primary key.</summary>
- [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)
- {
- // TODO: The fact Top has it's own AlternateBackwardKey and events is needlessly complex. Remove it.
- foreach (Toplevel top in _topLevels.ToArray ())
- {
- top.OnAlternateBackwardKeyChanged (oldKey);
- }
- }
- private static Key _quitKey = Key.Empty; // Defined in config.json
- /// <summary>Gets or sets the key to quit the application.</summary>
- [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)
- {
- // TODO: The fact Top has it's own QuitKey and events is needlessly complex. Remove it.
- // Duplicate the list so if it changes during enumeration we're safe
- foreach (Toplevel top in _topLevels.ToArray ())
- {
- top.OnQuitKeyChanged (e);
- }
- }
- /// <summary>
- /// Event fired when the user presses a key. Fired by <see cref="OnKeyDown"/>.
- /// <para>
- /// Set <see cref="Key.Handled"/> to <see langword="true"/> to indicate the key was handled and to prevent
- /// additional processing.
- /// </para>
- /// </summary>
- /// <remarks>
- /// All drivers support firing the <see cref="KeyDown"/> event. Some drivers (Curses) do not support firing the
- /// <see cref="KeyDown"/> and <see cref="KeyUp"/> events.
- /// <para>Fired after <see cref="KeyDown"/> and before <see cref="KeyUp"/>.</para>
- /// </remarks>
- public static event EventHandler<Key> KeyDown;
- /// <summary>
- /// Called by the <see cref="ConsoleDriver"/> when the user presses a key. Fires the <see cref="KeyDown"/> event
- /// then calls <see cref="View.NewKeyDownEvent"/> on all top level views. Called after <see cref="OnKeyDown"/> and
- /// before <see cref="OnKeyUp"/>.
- /// </summary>
- /// <remarks>Can be used to simulate key press events.</remarks>
- /// <param name="keyEvent"></param>
- /// <returns><see langword="true"/> if the key was handled.</returns>
- 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<Key, List<View?>> binding in _keyBindings.Where (b => b.Key == keyEvent.KeyCode))
- {
- foreach (View view in binding.Value)
- {
- if (view is { }
- && view.KeyBindings.TryGet (binding.Key, KeyBindingScope.Focused | KeyBindingScope.HotKey | KeyBindingScope.Application, 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;
- }
- /// <summary>
- /// Event fired when the user releases a key. Fired by <see cref="OnKeyUp"/>.
- /// <para>
- /// Set <see cref="Key.Handled"/> to <see langword="true"/> to indicate the key was handled and to prevent
- /// additional processing.
- /// </para>
- /// </summary>
- /// <remarks>
- /// All drivers support firing the <see cref="KeyDown"/> event. Some drivers (Curses) do not support firing the
- /// <see cref="KeyDown"/> and <see cref="KeyUp"/> events.
- /// <para>Fired after <see cref="KeyDown"/>.</para>
- /// </remarks>
- public static event EventHandler<Key> KeyUp;
- /// <summary>
- /// Called by the <see cref="ConsoleDriver"/> when the user releases a key. Fires the <see cref="KeyUp"/> event
- /// then calls <see cref="View.NewKeyUpEvent"/> on all top level views. Called after <see cref="OnKeyDown"/>.
- /// </summary>
- /// <remarks>Can be used to simulate key press events.</remarks>
- /// <param name="a"></param>
- /// <returns><see langword="true"/> if the key was handled.</returns>
- 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;
- }
- /// <summary>Gets the key bindings for this view.</summary>
- public static KeyBindings KeyBindings { get; internal set; } = new ();
- /// <summary>
- /// Commands for Application.
- /// </summary>
- private static Dictionary<Command, Func<CommandContext, bool?>> CommandImplementations { get; } = new ();
- /// <summary>
- /// <para>
- /// Sets the function that will be invoked for a <see cref="Command"/>.
- /// </para>
- /// <para>
- /// If AddCommand has already been called for <paramref name="command"/> <paramref name="f"/> will
- /// replace the old one.
- /// </para>
- /// </summary>
- /// <remarks>
- /// <para>
- /// This version of AddCommand is for commands that do not require a <see cref="CommandContext"/>.
- /// </para>
- /// </remarks>
- /// <param name="command">The command.</param>
- /// <param name="f">The function.</param>
- private static void AddCommand (Command command, Func<bool?> f)
- {
- CommandImplementations [command] = ctx => f ();
- }
- ///// <summary>
- ///// The <see cref="KeyBindingScope.Application"/> key bindings.
- ///// </summary>
- //private static readonly Dictionary<Key, List<View?>> _keyBindings = new ();
- ///// <summary>
- ///// Gets the list of <see cref="KeyBindingScope.Application"/> key bindings.
- ///// </summary>
- //public static Dictionary<Key, List<View?>> GetKeyBindings () { return _keyBindings; }
- ///// <summary>
- ///// Adds an <see cref="KeyBindingScope.Application"/> scoped key binding.
- ///// </summary>
- ///// <remarks>
- ///// This is an internal method used by the <see cref="View"/> class to add Application key bindings.
- ///// </remarks>
- ///// <param name="key">The key being bound.</param>
- ///// <param name="view">The view that is bound to the key. If <see langword="null"/>, <see cref="Application.Current"/> will be used.</param>
- //internal static void AddKeyBinding (Key key, View? view)
- //{
- // if (!_keyBindings.ContainsKey (key))
- // {
- // _keyBindings [key] = [];
- // }
- // _keyBindings [key].Add (view);
- //}
- internal static void AddApplicationKeyBindings ()
- {
- // Things this view knows how to do
- AddCommand (
- Command.QuitToplevel, // TODO: IRunnable: Rename to Command.Quit to make more generic.
- () =>
- {
- if (OverlappedTop is { })
- {
- RequestStop (Current);
- }
- else
- {
- Application.RequestStop ();
- }
- return true;
- }
- );
- AddCommand (
- Command.Suspend,
- () =>
- {
- Driver?.Suspend ();
- return true;
- }
- );
- AddCommand (
- Command.NextView, // TODO: Figure out how to move this to the View that is at the root of the view hierarchy (currently Application.Top)
- () =>
- {
- Current.MoveNextView ();
- return true;
- }
- );
- AddCommand (
- Command.PreviousView,// TODO: Figure out how to move this to the View that is at the root of the view hierarchy (currently Application.Top)
- () =>
- {
- Current.MovePreviousView ();
- return true;
- }
- );
- AddCommand (
- Command.NextViewOrTop,// TODO: Figure out how to move this to the View that is at the root of the view hierarchy (currently Application.Top)
- () =>
- {
- Current.MoveNextViewOrTop ();
- return true;
- }
- );
- AddCommand (
- Command.PreviousViewOrTop,// TODO: Figure out how to move this to the View that is at the root of the view hierarchy (currently Application.Top)
- () =>
- {
- Current.MovePreviousViewOrTop ();
- return true;
- }
- );
- AddCommand (
- Command.Refresh,
- () =>
- {
- Refresh ();
- return true;
- }
- );
- KeyBindings.Add (Application.QuitKey, KeyBindingScope.Application, Command.QuitToplevel);
- KeyBindings.Add (Key.CursorRight, KeyBindingScope.Application, Command.NextView);
- KeyBindings.Add (Key.CursorDown, KeyBindingScope.Application, Command.NextView);
- KeyBindings.Add (Key.CursorLeft, KeyBindingScope.Application, Command.PreviousView);
- KeyBindings.Add (Key.CursorUp, KeyBindingScope.Application, Command.PreviousView);
- KeyBindings.Add (Key.Tab, KeyBindingScope.Application, Command.NextView);
- KeyBindings.Add (Key.Tab.WithShift, KeyBindingScope.Application, Command.PreviousView);
- KeyBindings.Add (Key.Tab.WithCtrl, KeyBindingScope.Application, Command.NextViewOrTop);
- KeyBindings.Add (Key.Tab.WithShift.WithCtrl, KeyBindingScope.Application, Command.PreviousViewOrTop);
- // TODO: Refresh Key should be configurable
- KeyBindings.Add (Key.F5, KeyBindingScope.Application, Command.Refresh);
- KeyBindings.Add (Application.AlternateForwardKey, KeyBindingScope.Application, Command.NextViewOrTop); // Needed on Unix
- KeyBindings.Add (Application.AlternateBackwardKey, KeyBindingScope.Application, Command.PreviousViewOrTop); // Needed on Unix
- if (Environment.OSVersion.Platform == PlatformID.Unix)
- {
- KeyBindings.Add (Key.Z.WithCtrl, Command.Suspend);
- }
- #if UNIX_KEY_BINDINGS
- KeyBindings.Add (Key.L.WithCtrl, Command.Refresh); // Unix
- KeyBindings.Add (Key.F.WithCtrl, Command.NextView); // Unix
- KeyBindings.Add (Key.I.WithCtrl, Command.NextView); // Unix
- KeyBindings.Add (Key.B.WithCtrl, Command.PreviousView); // Unix
- #endif
- }
- /// <summary>
- /// Gets the list of Views that have <see cref="KeyBindingScope.Application"/> key bindings.
- /// </summary>
- /// <remarks>
- /// This is an internal method used by the <see cref="View"/> class to add Application key bindings.
- /// </remarks>
- /// <returns>The list of Views that have Application-scoped key bindings.</returns>
- internal static List<KeyBinding> GetViewKeyBindings ()
- {
- // Get the list of views that do not have Application-scoped key bindings
- return KeyBindings.Bindings
- .Where (kv => kv.Value.Scope != KeyBindingScope.Application)
- .Select (kv => kv.Value)
- .Distinct ()
- .ToList ();
- }
- /// <summary>
- /// Gets the list of Views that have <see cref="KeyBindingScope.Application"/> key bindings for the specified key.
- /// </summary>
- /// <remarks>
- /// This is an internal method used by the <see cref="View"/> class to add Application key bindings.
- /// </remarks>
- /// <param name="key">The key to check.</param>
- /// <param name="views">Outputs the list of views bound to <paramref name="key"/></param>
- /// <returns><see langword="True"/> if successful.</returns>
- internal static bool TryGetKeyBindings (Key key, out List<View> views) { return _keyBindings.TryGetValue (key, out views); }
- /// <summary>
- /// Removes an <see cref="KeyBindingScope.Application"/> scoped key binding.
- /// </summary>
- /// <remarks>
- /// This is an internal method used by the <see cref="View"/> class to remove Application key bindings.
- /// </remarks>
- /// <param name="key">The key that was bound.</param>
- /// <param name="view">The view that is bound to the key.</param>
- internal static void RemoveKeyBinding (Key key, View view)
- {
- if (_keyBindings.TryGetValue (key, out List<View> views))
- {
- views.Remove (view);
- if (views.Count == 0)
- {
- _keyBindings.Remove (key);
- }
- }
- }
- /// <summary>
- /// Removes all <see cref="KeyBindingScope.Application"/> scoped key bindings for the specified view.
- /// </summary>
- /// <remarks>
- /// This is an internal method used by the <see cref="View"/> class to remove Application key bindings.
- /// </remarks>
- /// <param name="view">The view that is bound to the key.</param>
- internal static void ClearKeyBindings (View view)
- {
- foreach (Key key in _keyBindings.Keys)
- {
- _keyBindings [key].Remove (view);
- }
- }
- /// <summary>
- /// Removes all <see cref="KeyBindingScope.Application"/> scoped key bindings for the specified view.
- /// </summary>
- /// <remarks>
- /// This is an internal method used by the <see cref="View"/> class to remove Application key bindings.
- /// </remarks>
- internal static void ClearKeyBindings () { _keyBindings.Clear (); }
- }
|