rivo / tview

Terminal UI library with rich, interactive widgets — written in Golang
MIT License
11.12k stars 576 forks source link

Make Table.cellAt public #948

Closed tliron closed 7 months ago

tliron commented 9 months ago

The cellAt function is currently private.

However, it makes sense for me to make it public, to allow for custom mouse handlers to determine which cell the mouse is pointing to.

It's actually impossible to workaround this issue because the function relies on private properties in the struct.

tliron commented 9 months ago

OK, maybe there is a workaround. For reference, here's my attempt. It "works" but I have an uneasy feeling that I'm not taking into account some factors. (By the way, does GetLastPosition return negative x if the column is hidden to the left?)

table.
SetMouseCapture(func(action tview.MouseAction, event *tcell.EventMouse) (tview.MouseAction, *tcell.EventMouse) {
    if action == tview.MouseLeftDoubleClick {
        row, column := table.GetOffset()
        x, y := event.Position()
        row += y - 1 // -1 to skip the header

        // Which column are we on?
        columns := table.GetColumnCount()
        for c := 0; c < columns; c++ {
            cell := table.GetCell(row, c)
            cx, _, width := cell.GetLastPosition()
            if (x >= cx) && (x <= cx+width) {
                column = c
                break
            }
        }

        openDetails(row, column)
        self.application.ForceDraw()
        return action, nil
    }
    return action, event
})
rivo commented 7 months ago

I made Table.CellAt public.