Added a "clicked" handler to TableCell. Resolves #470

This commit is contained in:
Oliver 2020-09-15 13:09:44 +02:00
parent 53d50e499b
commit 9b49eb3fef
2 changed files with 30 additions and 3 deletions

View File

@ -42,6 +42,11 @@ type TableCell struct {
// If set to true, this cell cannot be selected. // If set to true, this cell cannot be selected.
NotSelectable bool NotSelectable bool
// An optional handler for mouse clicks. This also fires if the cell is not
// selectable. If true is returned, no additional "selected" event is fired
// on selectable cells.
Clicked func() bool
// The position and width of the cell the last time table was drawn. // The position and width of the cell the last time table was drawn.
x, y, width int x, y, width int
} }
@ -160,6 +165,14 @@ func (c *TableCell) GetLastPosition() (x, y, width int) {
return c.x, c.y, c.width return c.x, c.y, c.width
} }
// SetClickedFunc sets a handler which fires when this cell is clicked. This is
// independent of whether the cell is selectable or not. But for selectable
// cells, if the function returns "true", the "selected" event is not fired.
func (c *TableCell) SetClickedFunc(clicked func() bool) *TableCell {
c.Clicked = clicked
return c
}
// Table visualizes two-dimensional data consisting of rows and columns. Each // Table visualizes two-dimensional data consisting of rows and columns. Each
// Table cell is defined via SetCell() by the TableCell type. They can be added // Table cell is defined via SetCell() by the TableCell type. They can be added
// dynamically to the table and changed any time. // dynamically to the table and changed any time.
@ -1247,8 +1260,21 @@ func (t *Table) MouseHandler() func(action MouseAction, event *tcell.EventMouse,
switch action { switch action {
case MouseLeftClick: case MouseLeftClick:
if t.rowsSelectable || t.columnsSelectable { selectEvent := true
t.Select(t.cellAt(x, y)) row, column := t.cellAt(x, y)
if row >= 0 && row < len(t.cells) && column >= 0 {
cells := t.cells[row]
if column < len(cells) {
cell := cells[column]
if cell != nil && cell.Clicked != nil {
if noSelect := cell.Clicked(); noSelect {
selectEvent = false
}
}
}
}
if selectEvent && (t.rowsSelectable || t.columnsSelectable) {
t.Select(row, column)
} }
setFocus(t) setFocus(t)
consumed = true consumed = true

View File

@ -273,7 +273,8 @@ func (t *TextView) SetText(text string) *TextView {
// to true, any region/color tags are stripped from the text. // to true, any region/color tags are stripped from the text.
func (t *TextView) GetText(stripTags bool) string { func (t *TextView) GetText(stripTags bool) string {
// Get the buffer. // Get the buffer.
buffer := t.buffer buffer := make([]string, len(t.buffer), len(t.buffer)+1)
copy(buffer, t.buffer)
if !stripTags { if !stripTags {
buffer = append(buffer, string(t.recentBytes)) buffer = append(buffer, string(t.recentBytes))
} }