From a989031127f294ee1396c4472ddf98e08dc78db8 Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Mon, 5 Jul 2021 17:51:57 -0700 Subject: [PATCH] Fix passing mouse events to Grid items Events are now only passed to the item under the mouse. --- CHANGELOG | 1 + grid.go | 9 ++++++++- textview.go | 11 ++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 5022795..a7c6674 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -7,6 +7,7 @@ v1.5.6 (WIP) - Add DropDown.SetAlwaysDrawDropDownSymbol (DropDown symbols are now shown only when focused by default) - Add DropDown.SetDropDownOpenSymbolRune - Fix TextView always visible scroll bar not appearing when empty +- Fix passing mouse events to Grid items (events are now only passed to the item under the mouse) - Draw additional accents when rendering a list divider - Update Application.Draw and Application.QueueUpdateDraw to accept one or more primitives to draw instead of the whole screen diff --git a/grid.go b/grid.go index 7c20823..6c40586 100644 --- a/grid.go +++ b/grid.go @@ -702,8 +702,15 @@ func (g *Grid) MouseHandler() func(action MouseAction, event *tcell.EventMouse, return false, nil } - // Pass mouse events along to the first child item that takes it. + // Pass mouse events along to the first child item under the mouse that consumes it. + x, y := event.Position() for _, item := range g.items { + rectX, rectY, width, height := item.Item.GetRect() + inRect := x >= rectX && x < rectX+width && y >= rectY && y < rectY+height + if !inRect { + continue + } + consumed, capture = item.Item.MouseHandler()(action, event, setFocus) if consumed { return diff --git a/textview.go b/textview.go index b95bf62..1fac443 100644 --- a/textview.go +++ b/textview.go @@ -1060,9 +1060,18 @@ func (t *TextView) Draw(screen tcell.Screen) { if !showVerticalScrollBar { return } + + items := len(t.index) cursor := int(float64(len(t.index)) * (float64(t.lineOffset) / float64(len(t.index)-height))) + + // Render cursor at the bottom when tracking end + if t.trackEnd && items <= height { + items = height + 1 + cursor = height + } + for printed := 0; printed < height; printed++ { - RenderScrollBar(screen, t.scrollBarVisibility, x+width, y+printed, height, len(t.index), cursor, printed, t.hasFocus, t.scrollBarColor) + RenderScrollBar(screen, t.scrollBarVisibility, x+width, y+printed, height, items, cursor, printed, t.hasFocus, t.scrollBarColor) } }()