Fix passing mouse events to Grid items

Events are now only passed to the item under the mouse.
This commit is contained in:
Trevor Slocum 2021-07-05 17:51:57 -07:00
parent 964fd51438
commit a989031127
3 changed files with 19 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -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)
}
}()