Added GetSelection() and SetSelectionChangedFunc() to Table

This commit is contained in:
Jakob Maier 2018-01-14 11:23:13 +01:00
parent 38d663c267
commit 55189331c5
1 changed files with 29 additions and 0 deletions

View File

@ -130,6 +130,11 @@ type Table struct {
// Likewise for entire columns.
selected func(row, column int)
// An optional function which gets called when the user changes the selection.
// If entire rows selected, the column value is undefined.
// Likewise for entire columns.
selectionChanged func(row, column int)
// An optional function which gets called when the user presses Escape, Tab,
// or Backtab. Also when the user presses Enter if nothing is selectable.
done func(key tcell.Key)
@ -203,6 +208,13 @@ func (t *Table) GetSelectable() (rows, columns bool) {
return t.rowsSelectable, t.columnsSelectable
}
// GetSelection returns the position of the current selection.
// If entire rows are selected, the column index is undefined.
// Likewise for entire columns.
func (t *Table) GetSelection() (row, column int) {
return t.selectedRow, t.selectedColumn
}
// Select sets the selected cell. Depending on the selection settings
// specified via SetSelectable(), this may be an entire row or column, or even
// ignored completely.
@ -230,6 +242,15 @@ func (t *Table) SetSelectedFunc(handler func(row, column int)) *Table {
return t
}
// SetSelectionChangedFunc sets a handler which is called whenever the user changes
// selected cell/row/column. The handler receives the position of the selection.
// If entire rows are selected, the column index is undefined.
// Likewise for entire columns.
func (t *Table) SetSelectionChangedFunc(handler func(row, column int)) *Table {
t.selectionChanged = handler
return t
}
// SetDoneFunc sets a handler which is called whenever the user presses the
// Escape, Tab, or Backtab key. If nothing is selected, it is also called when
// user presses the Enter key (because pressing Enter on a selection triggers
@ -637,6 +658,9 @@ func (t *Table) InputHandler() func(event *tcell.EventKey, setFocus func(p Primi
return
}
var previouslySelectedRow = t.selectedRow
var previouslySelectedColumn = t.selectedColumn
// Movement functions.
var (
getCell = func(row, column int) *TableCell {
@ -819,5 +843,10 @@ func (t *Table) InputHandler() func(event *tcell.EventKey, setFocus func(p Primi
t.selected(t.selectedRow, t.selectedColumn)
}
}
if t.selectionChanged != nil &&
(previouslySelectedRow != t.selectedRow || previouslySelectedColumn != t.selectedColumn) {
t.selectionChanged(t.selectedRow, t.selectedColumn)
}
}
}