From df3e191a72dfd67dede5b029abb2a1d384bf4825 Mon Sep 17 00:00:00 2001 From: tznind Date: Thu, 31 Dec 2020 13:38:15 +0000 Subject: [PATCH] Added CellActivated event which occurs on Enter or double click --- Terminal.Gui/Views/TableView.cs | 69 ++++++++++++++++++++++++++++++ UICatalog/Scenarios/TableEditor.cs | 16 +++---- 2 files changed, 77 insertions(+), 8 deletions(-) diff --git a/Terminal.Gui/Views/TableView.cs b/Terminal.Gui/Views/TableView.cs index d631b5d92..8b185bc93 100644 --- a/Terminal.Gui/Views/TableView.cs +++ b/Terminal.Gui/Views/TableView.cs @@ -224,6 +224,16 @@ namespace Terminal.Gui { /// public event Action SelectedCellChanged; + /// + /// This event is raised when a cell is activated e.g. by double clicking or pressing + /// + public event Action CellActivated; + + /// + /// The key which when pressed should trigger event. Defaults to Enter. + /// + public Key CellActivationKey {get;set;} = Key.Enter; + /// /// Initialzies a class using layout. /// @@ -535,6 +545,12 @@ namespace Terminal.Gui { /// public override bool ProcessKey (KeyEvent keyEvent) { + if(keyEvent.Key == CellActivationKey && Table != null) { + OnCellActivated(new CellActivatedEventArgs(Table,SelectedColumn,SelectedRow)); + return true; + } + + switch (keyEvent.Key) { case Key.CursorLeft: SelectedColumn--; @@ -660,6 +676,14 @@ namespace Terminal.Gui { } } + // Double clicking a cell activates + if(me.Flags == MouseFlags.Button1DoubleClicked) { + var hit = ScreenToCell(me.OfX,me.OfY); + if(hit!= null) { + OnCellActivated(new CellActivatedEventArgs(Table,hit.Value.X,hit.Value.Y)); + } + } + return false; } @@ -813,6 +837,15 @@ namespace Terminal.Gui { { SelectedCellChanged?.Invoke(args); } + + /// + /// Invokes the event + /// + /// + protected virtual void OnCellActivated (CellActivatedEventArgs args) + { + CellActivated?.Invoke(args); + } /// /// Calculates which columns should be rendered given the in which to display and the @@ -1004,4 +1037,40 @@ namespace Terminal.Gui { NewRow = newRow; } } + + /// + /// Defines the event arguments for event + /// + public class CellActivatedEventArgs : EventArgs + { + /// + /// The current table to which the new indexes refer. May be null e.g. if selection change is the result of clearing the table from the view + /// + /// + public DataTable Table {get;} + + + /// + /// The column index of the cell that is being activated + /// + /// + public int Col {get;} + + /// + /// The row index of the cell that is being activated + /// + /// + public int Row {get;} + + /// + /// Creates a new instance of arguments describing a cell being activated in + /// + /// + public CellActivatedEventArgs(DataTable t, int col, int row) + { + Table = t; + Col = col; + Row = row; + } + } } diff --git a/UICatalog/Scenarios/TableEditor.cs b/UICatalog/Scenarios/TableEditor.cs index 863dc774b..a07212030 100644 --- a/UICatalog/Scenarios/TableEditor.cs +++ b/UICatalog/Scenarios/TableEditor.cs @@ -62,9 +62,8 @@ namespace UICatalog.Scenarios { var statusBar = new StatusBar (new StatusItem [] { //new StatusItem(Key.Enter, "~ENTER~ ApplyEdits", () => { _hexView.ApplyEdits(); }), new StatusItem(Key.F2, "~F2~ OpenExample", () => OpenExample(true)), - new StatusItem(Key.F3, "~F3~ EditCell", () => EditCurrentCell()), - new StatusItem(Key.F4, "~F4~ CloseExample", () => CloseExample()), - new StatusItem(Key.F5, "~F5~ OpenSimple", () => OpenSimple(true)), + new StatusItem(Key.F3, "~F3~ CloseExample", () => CloseExample()), + new StatusItem(Key.F4, "~F4~ OpenSimple", () => OpenSimple(true)), new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => Quit()), }); Top.Add (statusBar); @@ -83,6 +82,7 @@ namespace UICatalog.Scenarios { Win.Add(selectedCellLabel); tableView.SelectedCellChanged += (e)=>{selectedCellLabel.Text = $"{tableView.SelectedRow},{tableView.SelectedColumn}";}; + tableView.CellActivated += EditCurrentCell; } private void ClearColumnStyles () @@ -214,12 +214,12 @@ namespace UICatalog.Scenarios { tableView.Table = BuildSimpleDataTable(big ? 30 : 5, big ? 1000 : 5); } - private void EditCurrentCell () + private void EditCurrentCell (CellActivatedEventArgs e) { - if(tableView.Table == null) + if(e.Table == null) return; - var oldValue = tableView.Table.Rows[tableView.SelectedRow][tableView.SelectedColumn].ToString(); + var oldValue = e.Table.Rows[e.Row][e.Col].ToString(); bool okPressed = false; var ok = new Button ("Ok", is_default: true); @@ -231,7 +231,7 @@ namespace UICatalog.Scenarios { var lbl = new Label() { X = 0, Y = 1, - Text = tableView.Table.Columns[tableView.SelectedColumn].ColumnName + Text = e.Table.Columns[e.Col].ColumnName }; var tf = new TextField() @@ -250,7 +250,7 @@ namespace UICatalog.Scenarios { if(okPressed) { try { - tableView.Table.Rows[tableView.SelectedRow][tableView.SelectedColumn] = string.IsNullOrWhiteSpace(tf.Text.ToString()) ? DBNull.Value : (object)tf.Text; + e.Table.Rows[e.Row][e.Col] = string.IsNullOrWhiteSpace(tf.Text.ToString()) ? DBNull.Value : (object)tf.Text; } catch(Exception ex) { MessageBox.ErrorQuery(60,20,"Failed to set text", ex.Message,"Ok");