More mouse handling for primitives

This commit is contained in:
Chris Miller 2019-11-04 06:30:25 +00:00 committed by Trevor Slocum
parent 84d2372c82
commit 5b47ba1f32
6 changed files with 69 additions and 1 deletions

View File

@ -136,7 +136,7 @@ func (b *Button) InputHandler() func(event *tcell.EventKey, setFocus func(p Prim
})
}
// InputHandler returns the handler for this primitive.
// MouseHandler returns the mouse handler for this primitive.
func (b *Button) MouseHandler() func(event EventMouse) {
return b.WrapMouseHandler(func(event EventMouse) {
// Process mouse event.

View File

@ -201,3 +201,16 @@ func (c *Checkbox) InputHandler() func(event *tcell.EventKey, setFocus func(p Pr
}
})
}
// MouseHandler returns the mouse handler for this primitive.
func (c *Checkbox) MouseHandler() func(event EventMouse) {
return c.WrapMouseHandler(func(event EventMouse) {
// Process mouse event.
if event.Buttons()&tcell.Button1 != 0 {
c.checked = !c.checked
if c.changed != nil {
c.changed(c.checked)
}
}
})
}

View File

@ -91,6 +91,8 @@ func main() {
return event
})
app.EnableMouse()
// Start the application.
if err := app.SetRoot(layout, true).Run(); err != nil {
panic(err)

View File

@ -483,3 +483,14 @@ func (d *DropDown) HasFocus() bool {
}
return d.hasFocus
}
// MouseHandler returns the mouse handler for this primitive.
func (d *DropDown) MouseHandler() func(event EventMouse) {
return d.WrapMouseHandler(func(event EventMouse) {
// Process mouse event.
if event.Buttons()&tcell.Button1 != 0 {
//d.open = !d.open // FIXME: clicks go through the dropdown!
event.SetFocus(d)
}
})
}

View File

@ -591,3 +591,13 @@ func (i *InputField) InputHandler() func(event *tcell.EventKey, setFocus func(p
}
})
}
// MouseHandler returns the mouse handler for this primitive.
func (i *InputField) MouseHandler() func(event EventMouse) {
return i.WrapMouseHandler(func(event EventMouse) {
// Process mouse event.
if event.Buttons()&tcell.Button1 != 0 {
event.SetFocus(i)
}
})
}

32
list.go
View File

@ -549,3 +549,35 @@ func (l *List) InputHandler() func(event *tcell.EventKey, setFocus func(p Primit
}
})
}
// returns -1 if not found.
func (l *List) indexAtPoint(atX, atY int) int {
_, y, _, h := l.GetInnerRect()
if atY < y || atY >= y+h {
return -1
}
n := atY - y
if l.showSecondaryText {
n /= 2
}
if n >= len(l.items) {
return -1
}
return n
}
// MouseHandler returns the mouse handler for this primitive.
func (l *List) MouseHandler() func(event EventMouse) {
return l.WrapMouseHandler(func(event EventMouse) {
// Process mouse event.
if event.Buttons()&tcell.Button1 != 0 {
atX, atY := event.Position()
index := l.indexAtPoint(atX, atY)
if index != -1 {
l.SetCurrentItem(index)
}
}
})
}