diff options
Diffstat (limited to 'lib/ui/grid.go')
-rw-r--r-- | lib/ui/grid.go | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/ui/grid.go b/lib/ui/grid.go index 7f131bd..b47c6bd 100644 --- a/lib/ui/grid.go +++ b/lib/ui/grid.go @@ -5,6 +5,8 @@ import ( "math" "sync" "sync/atomic" + + "github.com/gdamore/tcell" ) type Grid struct { @@ -141,6 +143,45 @@ func (grid *Grid) Draw(ctx *Context) { } } +func (grid *Grid) MouseEvent(localX int, localY int, event tcell.Event) { + switch event := event.(type) { + case *tcell.EventMouse: + invalid := grid.invalid + + grid.mutex.RLock() + defer grid.mutex.RUnlock() + + for _, cell := range grid.cells { + cellInvalid := cell.invalid.Load().(bool) + if !cellInvalid && !invalid { + continue + } + rows := grid.rowLayout[cell.Row : cell.Row+cell.RowSpan] + cols := grid.columnLayout[cell.Column : cell.Column+cell.ColSpan] + x := cols[0].Offset + y := rows[0].Offset + width := 0 + height := 0 + for _, col := range cols { + width += col.Size + } + for _, row := range rows { + height += row.Size + } + if x <= localX && localX < x+width && y <= localY && localY < y+height { + switch content := cell.Content.(type) { + case MouseableDrawableInteractive: + content.MouseEvent(localX-x, localY-y, event) + case Mouseable: + content.MouseEvent(localX-x, localY-y, event) + case MouseHandler: + content.MouseEvent(localX-x, localY-y, event) + } + } + } + } +} + func (grid *Grid) reflow(ctx *Context) { grid.rowLayout = nil grid.columnLayout = nil |