More mouse handling for primitives

pull/363/head
Chris Miller 5 years ago
parent 2f4b6ad748
commit d7250288e2

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

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

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

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

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

@ -527,3 +527,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)
}
}
})
}

Loading…
Cancel
Save