Simplfiied app scope key setters

This commit is contained in:
Tig
2024-08-05 08:54:05 -06:00
parent 9526b4eabd
commit e86a2fca2f
9 changed files with 90 additions and 80 deletions

View File

@@ -5,7 +5,7 @@ namespace Terminal.Gui;
public static partial class Application // Keyboard handling
{
private static Key _nextTabKey = Key.Empty; // Defined in config.json
private static Key _nextTabKey = Key.Tab; // Resources/config.json overrrides
/// <summary>Alternative key to navigate forwards through views. Ctrl+Tab is the primary key.</summary>
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
@@ -17,22 +17,13 @@ public static partial class Application // Keyboard handling
{
if (_nextTabKey != value)
{
Key oldKey = _nextTabKey;
ReplaceKey (_nextTabKey, value);
_nextTabKey = value;
if (_nextTabKey == Key.Empty)
{
KeyBindings.Remove (_nextTabKey);
}
else
{
KeyBindings.ReplaceKey (oldKey, _nextTabKey);
}
}
}
}
private static Key _prevTabKey = Key.Empty; // Defined in config.json
private static Key _prevTabKey = Key.Tab.WithShift; // Resources/config.json overrrides
/// <summary>Alternative key to navigate backwards through views. Shift+Ctrl+Tab is the primary key.</summary>
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
@@ -44,22 +35,13 @@ public static partial class Application // Keyboard handling
{
if (_prevTabKey != value)
{
Key oldKey = _prevTabKey;
ReplaceKey (_prevTabKey, value);
_prevTabKey = value;
if (_prevTabKey == Key.Empty)
{
KeyBindings.Remove (_prevTabKey);
}
else
{
KeyBindings.ReplaceKey (oldKey, _prevTabKey);
}
}
}
}
private static Key _nextTabGroupKey = Key.Empty; // Defined in config.json
private static Key _nextTabGroupKey = Key.F6; // Resources/config.json overrrides
/// <summary>Alternative key to navigate forwards through views. Ctrl+Tab is the primary key.</summary>
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
@@ -71,22 +53,13 @@ public static partial class Application // Keyboard handling
{
if (_nextTabGroupKey != value)
{
Key oldKey = _nextTabGroupKey;
ReplaceKey (_nextTabGroupKey, value);
_nextTabGroupKey = value;
if (_nextTabGroupKey == Key.Empty)
{
KeyBindings.Remove (_nextTabGroupKey);
}
else
{
KeyBindings.ReplaceKey (oldKey, _nextTabGroupKey);
}
}
}
}
private static Key _prevTabGroupKey = Key.Empty; // Defined in config.json
private static Key _prevTabGroupKey = Key.F6.WithShift; // Resources/config.json overrrides
/// <summary>Alternative key to navigate backwards through views. Shift+Ctrl+Tab is the primary key.</summary>
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
@@ -98,22 +71,13 @@ public static partial class Application // Keyboard handling
{
if (_prevTabGroupKey != value)
{
Key oldKey = _prevTabGroupKey;
ReplaceKey (_prevTabGroupKey, value);
_prevTabGroupKey = value;
if (_prevTabGroupKey == Key.Empty)
{
KeyBindings.Remove (_prevTabGroupKey);
}
else
{
KeyBindings.ReplaceKey (oldKey, _prevTabGroupKey);
}
}
}
}
private static Key _quitKey = Key.Empty; // Defined in config.json
private static Key _quitKey = Key.Esc; // Resources/config.json overrrides
/// <summary>Gets or sets the key to quit the application.</summary>
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
@@ -125,21 +89,29 @@ public static partial class Application // Keyboard handling
{
if (_quitKey != value)
{
Key oldKey = _quitKey;
ReplaceKey (_quitKey, value);
_quitKey = value;
if (_quitKey == Key.Empty)
{
KeyBindings.Remove (_quitKey);
}
else
{
KeyBindings.ReplaceKey (oldKey, _quitKey);
}
}
}
}
private static void ReplaceKey (Key oldKey, Key newKey)
{
if (KeyBindings.Bindings.Count == 0)
{
return;
}
if (newKey == Key.Empty)
{
KeyBindings.Remove (oldKey);
}
else
{
KeyBindings.ReplaceKey (oldKey, newKey);
}
}
/// <summary>
/// Event fired when the user presses a key. Fired by <see cref="OnKeyDown"/>.
/// <para>
@@ -413,6 +385,13 @@ public static partial class Application // Keyboard handling
KeyBindings.Clear ();
// Resources/config.json overrrides
NextTabKey = Key.Tab;
PrevTabKey = Key.Tab.WithShift;
NextTabGroupKey = Key.F6;
PrevTabGroupKey = Key.F6.WithShift;
QuitKey = Key.Esc;
KeyBindings.Add (QuitKey, KeyBindingScope.Application, Command.QuitToplevel);
KeyBindings.Add (Key.CursorRight, KeyBindingScope.Application, Command.NextView);

View File

@@ -142,14 +142,12 @@ public static partial class Application
UnGrabbedMouse = null;
// Keyboard
PrevTabGroupKey = Key.Empty;
NextTabGroupKey = Key.Empty;
QuitKey = Key.Empty;
KeyDown = null;
KeyUp = null;
SizeChanging = null;
Navigation = null;
AddApplicationKeyBindings ();
Colors.Reset ();

View File

@@ -136,10 +136,9 @@ public class KeyBindings
throw new ArgumentException ("Application scoped KeyBindings must be added via Application.KeyBindings.Add");
}
if (key is null || !key.IsValid)
if (key == Key.Empty || !key.IsValid)
{
//throw new ArgumentException ("Invalid Key", nameof (commands));
return;
throw new ArgumentException (@"Invalid Key", nameof (commands));
}
if (commands.Length == 0)
@@ -150,7 +149,6 @@ public class KeyBindings
if (TryGet (key, out KeyBinding binding))
{
throw new InvalidOperationException (@$"A key binding for {key} exists ({binding}).");
//Bindings [key] = new (commands, scope, BoundView);
}
else
{
@@ -313,12 +311,17 @@ public class KeyBindings
/// <summary>Replaces a key combination already bound to a set of <see cref="Command"/>s.</summary>
/// <remarks></remarks>
/// <param name="oldKey">The key to be replaced.</param>
/// <param name="newKey">The new key to be used.</param>
/// <param name="newKey">The new key to be used. If <see cref="Key.Empty"/> no action will be taken.</param>
public void ReplaceKey (Key oldKey, Key newKey)
{
if (!TryGet (oldKey, out KeyBinding _))
{
return;
throw new InvalidOperationException ($"Key {oldKey} is not bound.");
}
if (!newKey.IsValid)
{
throw new InvalidOperationException ($"Key {newKey} is is not valid.");
}
KeyBinding value = Bindings [oldKey];

View File

@@ -681,7 +681,7 @@ public class Shortcut : View, IOrientation, IDesignable
private void UpdateKeyBinding (Key oldKey)
{
if (Key != null)
if (Key != null && Key.IsValid)
{
// Disable the command view key bindings
CommandView.KeyBindings.Remove (Key);

View File

@@ -324,11 +324,15 @@ public class TableView : View
{
if (cellActivationKey != value)
{
KeyBindings.ReplaceKey (cellActivationKey, value);
if (KeyBindings.TryGet (cellActivationKey, out _))
{
KeyBindings.ReplaceKey (cellActivationKey, value);
}
else
{
KeyBindings.Add (value, Command.Accept);
}
// of API user is mixing and matching old and new methods of keybinding then they may have lost
// the old binding (e.g. with ClearKeybindings) so KeyBindings.Replace alone will fail
KeyBindings.Add (value, Command.Accept);
cellActivationKey = value;
}
}
@@ -792,7 +796,7 @@ public class TableView : View
}
///<inheritdoc/>
protected internal override bool OnMouseEvent (MouseEvent me)
protected internal override bool OnMouseEvent (MouseEvent me)
{
if (!me.Flags.HasFlag (MouseFlags.Button1Clicked)
&& !me.Flags.HasFlag (MouseFlags.Button1DoubleClicked)