Added CellActivated event which occurs on Enter or double click

This commit is contained in:
tznind
2020-12-31 13:38:15 +00:00
parent 31bdec4535
commit df3e191a72
2 changed files with 77 additions and 8 deletions

View File

@@ -224,6 +224,16 @@ namespace Terminal.Gui {
/// </summary>
public event Action<SelectedCellChangedEventArgs> SelectedCellChanged;
/// <summary>
/// This event is raised when a cell is activated e.g. by double clicking or pressing <see cref="CellActivationKey"/>
/// </summary>
public event Action<CellActivatedEventArgs> CellActivated;
/// <summary>
/// The key which when pressed should trigger <see cref="CellActivated"/> event. Defaults to Enter.
/// </summary>
public Key CellActivationKey {get;set;} = Key.Enter;
/// <summary>
/// Initialzies a <see cref="TableView"/> class using <see cref="LayoutStyle.Computed"/> layout.
/// </summary>
@@ -535,6 +545,12 @@ namespace Terminal.Gui {
/// <inheritdoc/>
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);
}
/// <summary>
/// Invokes the <see cref="CellActivated"/> event
/// </summary>
/// <param name="args"></param>
protected virtual void OnCellActivated (CellActivatedEventArgs args)
{
CellActivated?.Invoke(args);
}
/// <summary>
/// Calculates which columns should be rendered given the <paramref name="bounds"/> in which to display and the <see cref="ColumnOffset"/>
@@ -1004,4 +1037,40 @@ namespace Terminal.Gui {
NewRow = newRow;
}
}
/// <summary>
/// Defines the event arguments for <see cref="TableView.CellActivated"/> event
/// </summary>
public class CellActivatedEventArgs : EventArgs
{
/// <summary>
/// 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
/// </summary>
/// <value></value>
public DataTable Table {get;}
/// <summary>
/// The column index of the <see cref="Table"/> cell that is being activated
/// </summary>
/// <value></value>
public int Col {get;}
/// <summary>
/// The row index of the <see cref="Table"/> cell that is being activated
/// </summary>
/// <value></value>
public int Row {get;}
/// <summary>
/// Creates a new instance of arguments describing a cell being activated in <see cref="TableView"/>
/// </summary>
/// <param name="t"></param>
public CellActivatedEventArgs(DataTable t, int col, int row)
{
Table = t;
Col = col;
Row = row;
}
}
}

View File

@@ -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");