From acaa7aa7b6f6c6fd1a1fd9dea51d2df179b456c8 Mon Sep 17 00:00:00 2001 From: Thomas Nind <31306100+tznind@users.noreply.github.com> Date: Fri, 18 Feb 2022 15:17:12 +0000 Subject: [PATCH] Fixes TableView not activating after keybinding refactor (#1593) * Fixes TableView not activating after keybinding refactor * Fix reading old value not new one --- Terminal.Gui/Views/TableView.cs | 6 +++++- UnitTests/TableViewTests.cs | 38 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Terminal.Gui/Views/TableView.cs b/Terminal.Gui/Views/TableView.cs index 8d51ddcf5..bf8e7ae68 100644 --- a/Terminal.Gui/Views/TableView.cs +++ b/Terminal.Gui/Views/TableView.cs @@ -177,6 +177,10 @@ namespace Terminal.Gui { set { if (cellActivationKey != value) { ReplaceKeyBinding (cellActivationKey, value); + + // 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 ReplaceKeyBinding alone will fail + AddKeyBinding (value, Command.Accept); cellActivationKey = value; } } @@ -222,7 +226,7 @@ namespace Terminal.Gui { AddCommand (Command.BottomEndExtend, () => { ChangeSelectionToEndOfTable (true); return true; }); AddCommand (Command.SelectAll, () => { SelectAll(); return true; }); - AddCommand (Command.Accept, () => { new CellActivatedEventArgs (Table, SelectedColumn, SelectedRow); return true; }); + AddCommand (Command.Accept, () => { OnCellActivated(new CellActivatedEventArgs (Table, SelectedColumn, SelectedRow)); return true; }); // Default keybindings for this view AddKeyBinding (Key.CursorLeft, Command.Left); diff --git a/UnitTests/TableViewTests.cs b/UnitTests/TableViewTests.cs index 31d262e53..d27b4ab0e 100644 --- a/UnitTests/TableViewTests.cs +++ b/UnitTests/TableViewTests.cs @@ -492,6 +492,44 @@ namespace Terminal.Gui.Views { Application.Shutdown (); } + [Fact] + [AutoInitShutdown] + public void TableView_Activate() + { + string activatedValue = null; + var tv = new TableView (BuildTable(1,1)); + tv.CellActivated += (c) => activatedValue = c.Table.Rows[c.Row][c.Col].ToString(); + + Application.Top.Add (tv); + Application.Begin (Application.Top); + + // pressing enter should activate the first cell (selected cell) + tv.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())); + Assert.Equal ("R0C0",activatedValue); + + // reset the test + activatedValue = null; + + // clear keybindings and ensure that Enter does not trigger the event anymore + tv.ClearKeybindings (); + tv.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())); + Assert.Null(activatedValue); + + // New method for changing the activation key + tv.AddKeyBinding (Key.z, Command.Accept); + tv.ProcessKey (new KeyEvent (Key.z, new KeyModifiers ())); + Assert.Equal ("R0C0", activatedValue); + + // reset the test + activatedValue = null; + tv.ClearKeybindings (); + + // Old method for changing the activation key + tv.CellActivationKey = Key.z; + tv.ProcessKey (new KeyEvent (Key.z, new KeyModifiers ())); + Assert.Equal ("R0C0", activatedValue); + } + [Fact] public void TableView_ColorsTest_ColorGetter () {