Skip to content

Commit

Permalink
Merge pull request gui-cs#3856 from MaciekWin3/text-inputs-keybinding…
Browse files Browse the repository at this point in the history
…s-update

Fixes gui-cs#3855 - Text inputs keybindings update
  • Loading branch information
tig authored Nov 26, 2024
2 parents b856e9a + 292e1a1 commit 68daff5
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Showcase.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

- **[Muse](https://github.com/MaciekWin3/Muse)** - Muse is terminal music player built with Terminal.Gui and NAudio on .NET platform.
![Muse](https://github.com/user-attachments/assets/94aeb559-a889-4b52-bb0d-453b3e19b290)
z

- **[Whale](https://github.com/MaciekWin3/Whale)** - Lightweight terminal user interface application that helps software engineers manage Docker containers.
![Whale](https://github.com/user-attachments/assets/7ef6e348-c36b-4aee-a63c-4e5c60c3aad2)

Expand Down
64 changes: 32 additions & 32 deletions Terminal.Gui/Views/DateField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public override void DeleteCharRight ()
}

/// <inheritdoc/>
protected override bool OnMouseEvent (MouseEventArgs ev)
protected override bool OnMouseEvent (MouseEventArgs ev)
{
if (base.OnMouseEvent (ev) || ev.Handled)
{
Expand Down Expand Up @@ -410,7 +410,7 @@ private void SetInitialProperties (DateTime date)
KeyBindings.ReplaceCommands (Key.Backspace, Command.DeleteCharLeft);

KeyBindings.ReplaceCommands (Key.Home, Command.LeftStart);
KeyBindings.ReplaceCommands (Key.A.WithCtrl, Command.LeftStart);
KeyBindings.ReplaceCommands (Key.Home.WithCtrl, Command.LeftStart);

KeyBindings.ReplaceCommands (Key.CursorLeft, Command.Left);
KeyBindings.ReplaceCommands (Key.B.WithCtrl, Command.Left);
Expand Down Expand Up @@ -551,35 +551,35 @@ private bool SetText (string text)
private static string StandardizeDateFormat (string format)
{
return format switch
{
"MM/dd/yyyy" => "MM/dd/yyyy",
"yyyy-MM-dd" => "yyyy-MM-dd",
"yyyy/MM/dd" => "yyyy/MM/dd",
"dd/MM/yyyy" => "dd/MM/yyyy",
"d?/M?/yyyy" => "dd/MM/yyyy",
"dd.MM.yyyy" => "dd.MM.yyyy",
"dd-MM-yyyy" => "dd-MM-yyyy",
"dd/MM yyyy" => "dd/MM/yyyy",
"d. M. yyyy" => "dd.MM.yyyy",
"yyyy.MM.dd" => "yyyy.MM.dd",
"g yyyy/M/d" => "yyyy/MM/dd",
"d/M/yyyy" => "dd/MM/yyyy",
"d?/M?/yyyy g" => "dd/MM/yyyy",
"d-M-yyyy" => "dd-MM-yyyy",
"d.MM.yyyy" => "dd.MM.yyyy",
"d.MM.yyyy '?'." => "dd.MM.yyyy",
"M/d/yyyy" => "MM/dd/yyyy",
"d. M. yyyy." => "dd.MM.yyyy",
"d.M.yyyy." => "dd.MM.yyyy",
"g yyyy-MM-dd" => "yyyy-MM-dd",
"d.M.yyyy" => "dd.MM.yyyy",
"d/MM/yyyy" => "dd/MM/yyyy",
"yyyy/M/d" => "yyyy/MM/dd",
"dd. MM. yyyy." => "dd.MM.yyyy",
"yyyy. MM. dd." => "yyyy.MM.dd",
"yyyy. M. d." => "yyyy.MM.dd",
"d. MM. yyyy" => "dd.MM.yyyy",
_ => "dd/MM/yyyy"
};
{
"MM/dd/yyyy" => "MM/dd/yyyy",
"yyyy-MM-dd" => "yyyy-MM-dd",
"yyyy/MM/dd" => "yyyy/MM/dd",
"dd/MM/yyyy" => "dd/MM/yyyy",
"d?/M?/yyyy" => "dd/MM/yyyy",
"dd.MM.yyyy" => "dd.MM.yyyy",
"dd-MM-yyyy" => "dd-MM-yyyy",
"dd/MM yyyy" => "dd/MM/yyyy",
"d. M. yyyy" => "dd.MM.yyyy",
"yyyy.MM.dd" => "yyyy.MM.dd",
"g yyyy/M/d" => "yyyy/MM/dd",
"d/M/yyyy" => "dd/MM/yyyy",
"d?/M?/yyyy g" => "dd/MM/yyyy",
"d-M-yyyy" => "dd-MM-yyyy",
"d.MM.yyyy" => "dd.MM.yyyy",
"d.MM.yyyy '?'." => "dd.MM.yyyy",
"M/d/yyyy" => "MM/dd/yyyy",
"d. M. yyyy." => "dd.MM.yyyy",
"d.M.yyyy." => "dd.MM.yyyy",
"g yyyy-MM-dd" => "yyyy-MM-dd",
"d.M.yyyy" => "dd.MM.yyyy",
"d/MM/yyyy" => "dd/MM/yyyy",
"yyyy/M/d" => "yyyy/MM/dd",
"dd. MM. yyyy." => "dd.MM.yyyy",
"yyyy. MM. dd." => "yyyy.MM.dd",
"yyyy. M. d." => "yyyy.MM.dd",
"d. MM. yyyy" => "dd.MM.yyyy",
_ => "dd/MM/yyyy"
};
}
}
5 changes: 2 additions & 3 deletions Terminal.Gui/Views/TextField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ public TextField ()

KeyBindings.Add (Key.Home, Command.LeftStart);
KeyBindings.Add (Key.Home.WithCtrl, Command.LeftStart);
KeyBindings.Add (Key.A.WithCtrl, Command.LeftStart);

KeyBindings.Add (Key.CursorLeft.WithShift, Command.LeftExtend);
KeyBindings.Add (Key.CursorUp.WithShift, Command.LeftExtend);
Expand Down Expand Up @@ -393,14 +392,14 @@ public TextField ()
KeyBindings.Add (Key.C.WithCtrl, Command.Copy);
KeyBindings.Add (Key.X.WithCtrl, Command.Cut);
KeyBindings.Add (Key.V.WithCtrl, Command.Paste);
KeyBindings.Add (Key.T.WithCtrl, Command.SelectAll);
KeyBindings.Add (Key.A.WithCtrl, Command.SelectAll);

KeyBindings.Add (Key.R.WithCtrl, Command.DeleteAll);
KeyBindings.Add (Key.D.WithCtrl.WithShift, Command.DeleteAll);

_currentCulture = Thread.CurrentThread.CurrentUICulture;

ContextMenu = new() { Host = this };
ContextMenu = new () { Host = this };
ContextMenu.KeyChanged += ContextMenu_KeyChanged;

KeyBindings.Add (ContextMenu.Key, KeyBindingScope.HotKey, Command.Context);
Expand Down
3 changes: 1 addition & 2 deletions Terminal.Gui/Views/TextView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2347,7 +2347,6 @@ public TextView ()
KeyBindings.Add (Key.Backspace, Command.DeleteCharLeft);

KeyBindings.Add (Key.Home, Command.LeftStart);
KeyBindings.Add (Key.A.WithCtrl, Command.LeftStart);

KeyBindings.Add (Key.Home.WithShift, Command.LeftStartExtend);

Expand Down Expand Up @@ -2387,7 +2386,7 @@ public TextView ()
KeyBindings.Add (Key.End.WithCtrl.WithShift, Command.EndExtend);
KeyBindings.Add (Key.Home.WithCtrl, Command.Start);
KeyBindings.Add (Key.Home.WithCtrl.WithShift, Command.StartExtend);
KeyBindings.Add (Key.T.WithCtrl, Command.SelectAll);
KeyBindings.Add (Key.A.WithCtrl, Command.SelectAll);
KeyBindings.Add (Key.InsertChar, Command.ToggleOverwrite);
KeyBindings.Add (Key.Tab, Command.Tab);
KeyBindings.Add (Key.Tab.WithShift, Command.BackTab);
Expand Down
2 changes: 0 additions & 2 deletions UnitTests/Views/DateFieldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ public void KeyBindings_Command ()
Assert.Equal (1, df.CursorPosition);
Assert.True (df.NewKeyDownEvent (Key.End));
Assert.Equal (10, df.CursorPosition);
Assert.True (df.NewKeyDownEvent (Key.A.WithCtrl));
Assert.Equal (1, df.CursorPosition);
Assert.True (df.NewKeyDownEvent (Key.E.WithCtrl));
Assert.Equal (10, df.CursorPosition);
Assert.True (df.NewKeyDownEvent (Key.CursorLeft));
Expand Down
10 changes: 5 additions & 5 deletions UnitTests/Views/TextFieldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ public void KeyBindings_Command ()
Assert.Null (tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.A.WithCtrl));
Assert.Equal ("is is a test.", tf.Text);
Assert.Equal (0, tf.CursorPosition);
Assert.Equal (tf.Text.Length, tf.CursorPosition);
tf.CursorPosition = 5;
tf.SelectedStart = -1;
Assert.Null (tf.SelectedText);
Expand Down Expand Up @@ -804,7 +804,7 @@ public void KeyBindings_Command ()
tf.CursorPosition = tf.Text.Length;
Assert.True (tf.NewKeyDownEvent (Key.Backspace.WithCtrl));
Assert.Equal ("to jump between text fields", tf.Text);
Assert.True (tf.NewKeyDownEvent (Key.T.WithCtrl));
Assert.True (tf.NewKeyDownEvent (Key.A.WithCtrl));
Assert.Equal ("to jump between text fields", tf.SelectedText);
Assert.True (tf.NewKeyDownEvent (Key.D.WithCtrl.WithShift));
Assert.Equal ("", tf.Text);
Expand Down Expand Up @@ -2109,10 +2109,10 @@ public void Right_CursorAtEnd_WithSelection_ShouldClearSelection ()
tf.CursorPosition = 5;

// When there is selected text and the cursor is at the end of the text field
Assert.Equal ("Hello",tf.SelectedText);
Assert.Equal ("Hello", tf.SelectedText);

// Pressing right should not move focus, instead it should clear selection
Assert.True(tf.NewKeyDownEvent (Key.CursorRight));
Assert.True (tf.NewKeyDownEvent (Key.CursorRight));
Assert.Null (tf.SelectedText);

// Now that the selection is cleared another right keypress should move focus
Expand All @@ -2139,7 +2139,7 @@ public void Left_CursorAtStart_WithSelection_ShouldClearSelection ()
Assert.Null (tf.SelectedText);

// When clearing selected text with left the cursor should be at the start of the selection
Assert.Equal (0,tf.CursorPosition);
Assert.Equal (0, tf.CursorPosition);

// Now that the selection is cleared another left keypress should move focus
Assert.False (tf.NewKeyDownEvent (Key.CursorLeft));
Expand Down
11 changes: 5 additions & 6 deletions UnitTests/Views/TextViewTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.ComponentModel;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -5137,7 +5136,7 @@ public void KeyBindings_Command ()
Assert.Equal (0, tv.SelectedLength);
Assert.Equal ("", tv.SelectedText);
Assert.False (tv.IsSelecting);
Assert.True (tv.NewKeyDownEvent (Key.A.WithCtrl));
Assert.True (tv.NewKeyDownEvent (Key.Home));
Assert.Equal (Point.Empty, tv.CursorPosition);
Assert.Equal (0, tv.SelectedLength);
Assert.Equal ("", tv.SelectedText);
Expand Down Expand Up @@ -5474,7 +5473,7 @@ public void KeyBindings_Command ()
Assert.Equal (Environment.NewLine.Length, tv.SelectedLength);
Assert.Equal ($"{Environment.NewLine}", tv.SelectedText);
Assert.True (tv.IsSelecting);
Assert.True (tv.NewKeyDownEvent (Key.T.WithCtrl));
Assert.True (tv.NewKeyDownEvent (Key.A.WithCtrl));

Assert.Equal (
$"{Environment.NewLine}This is the second line.{Environment.NewLine}This is the third ",
Expand Down Expand Up @@ -8333,7 +8332,7 @@ public void WordWrap_ReadOnly_CursorPosition_SelectedText_Copy ()

var top = new Toplevel ();
top.Add (tv);
top.Layout();
top.Layout ();
tv.Draw ();

TestHelpers.AssertDriverContentsWithFrameAre (
Expand Down Expand Up @@ -8810,7 +8809,7 @@ public void Right_CursorAtEnd_WithSelection_ShouldClearSelection ()
tv.SetFocus ();

tv.NewKeyDownEvent (Key.End.WithShift);
Assert.Equal (5,tv.CursorPosition.X);
Assert.Equal (5, tv.CursorPosition.X);

// When there is selected text and the cursor is at the end of the text field
Assert.Equal ("Hello", tv.SelectedText);
Expand All @@ -8834,7 +8833,7 @@ public void Left_CursorAtStart_WithSelection_ShouldClearSelection ()
tv.NewKeyDownEvent (Key.CursorRight);
tv.NewKeyDownEvent (Key.CursorRight);

Assert.Equal (2,tv.CursorPosition.X);
Assert.Equal (2, tv.CursorPosition.X);

Assert.True (tv.NewKeyDownEvent (Key.CursorLeft.WithShift));
Assert.True (tv.NewKeyDownEvent (Key.CursorLeft.WithShift));
Expand Down
3 changes: 2 additions & 1 deletion UnitTests/Views/TimeFieldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ public void KeyBindings_Command ()
Assert.True (tf.NewKeyDownEvent (Key.End));
Assert.Equal (8, tf.CursorPosition);
Assert.True (tf.NewKeyDownEvent (Key.A.WithCtrl));
Assert.Equal (1, tf.CursorPosition);
Assert.Equal (9, tf.CursorPosition);
Assert.Equal (tf.SelectedLength, tf.Text.Length);
Assert.True (tf.NewKeyDownEvent (Key.E.WithCtrl));
Assert.Equal (8, tf.CursorPosition);
Assert.True (tf.NewKeyDownEvent (Key.CursorLeft));
Expand Down

0 comments on commit 68daff5

Please sign in to comment.