From 5b47ba1f32bb9b177c86675beb566fa9c9e3d2c8 Mon Sep 17 00:00:00 2001 From: Chris Miller Date: Mon, 4 Nov 2019 06:30:25 +0000 Subject: [PATCH] More mouse handling for primitives --- button.go | 2 +- checkbox.go | 13 +++++++++++++ demos/presentation/main.go | 2 ++ dropdown.go | 11 +++++++++++ inputfield.go | 10 ++++++++++ list.go | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 69 insertions(+), 1 deletion(-) diff --git a/button.go b/button.go index a54317b..fc46239 100644 --- a/button.go +++ b/button.go @@ -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. diff --git a/checkbox.go b/checkbox.go index 91b01bd..6e1d354 100644 --- a/checkbox.go +++ b/checkbox.go @@ -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) + } + } + }) +} diff --git a/demos/presentation/main.go b/demos/presentation/main.go index 933bb1e..ce65ef8 100644 --- a/demos/presentation/main.go +++ b/demos/presentation/main.go @@ -91,6 +91,8 @@ func main() { return event }) + app.EnableMouse() + // Start the application. if err := app.SetRoot(layout, true).Run(); err != nil { panic(err) diff --git a/dropdown.go b/dropdown.go index 455ea51..e4bbce4 100644 --- a/dropdown.go +++ b/dropdown.go @@ -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) + } + }) +} diff --git a/inputfield.go b/inputfield.go index 06f736b..7c64ecb 100644 --- a/inputfield.go +++ b/inputfield.go @@ -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) + } + }) +} diff --git a/list.go b/list.go index 251b922..5b6b22e 100644 --- a/list.go +++ b/list.go @@ -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) + } + } + }) +}