From 9b49eb3fef8215e9981ba8c78c4a0909b9fd990f Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 15 Sep 2020 13:09:44 +0200 Subject: [PATCH] Added a "clicked" handler to TableCell. Resolves #470 --- table.go | 30 ++++++++++++++++++++++++++++-- textview.go | 3 ++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/table.go b/table.go index 3ce2503..5d06466 100644 --- a/table.go +++ b/table.go @@ -42,6 +42,11 @@ type TableCell struct { // If set to true, this cell cannot be selected. 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. x, y, width int } @@ -160,6 +165,14 @@ func (c *TableCell) GetLastPosition() (x, y, width int) { 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 cell is defined via SetCell() by the TableCell type. They can be added // dynamically to the table and changed any time. @@ -1247,8 +1260,21 @@ func (t *Table) MouseHandler() func(action MouseAction, event *tcell.EventMouse, switch action { case MouseLeftClick: - if t.rowsSelectable || t.columnsSelectable { - t.Select(t.cellAt(x, y)) + selectEvent := true + 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) consumed = true diff --git a/textview.go b/textview.go index bc117fd..aadac20 100644 --- a/textview.go +++ b/textview.go @@ -273,7 +273,8 @@ func (t *TextView) SetText(text string) *TextView { // to true, any region/color tags are stripped from the text. func (t *TextView) GetText(stripTags bool) string { // Get the buffer. - buffer := t.buffer + buffer := make([]string, len(t.buffer), len(t.buffer)+1) + copy(buffer, t.buffer) if !stripTags { buffer = append(buffer, string(t.recentBytes)) }