Change mouse handler func

pull/363/head
Chris Miller 4 years ago
parent 080a8e624e
commit 8abbf770ec

@ -71,8 +71,11 @@ type Application struct {
// be forwarded).
mouseCapture func(event *tcell.EventMouse, action MouseAction) (*tcell.EventMouse, MouseAction)
lastMouseX, lastMouseY int16 // track last mouse pos
mouseDownX, mouseDownY int16 // track last mouse down pos
// An optional mouse capture Primitive returned from the MouseHandler.
mouseHandlerCapture Primitive
lastMouseX, lastMouseY int // track last mouse pos
mouseDownX, mouseDownY int // track last mouse down pos
lastMouseAct MouseAction
lastMouseBtn tcell.ButtonMask
}
@ -251,6 +254,7 @@ EventLoop:
p := a.focus
inputCapture := a.inputCapture
mouseCapture := a.mouseCapture
mouseHandlerCapture := a.mouseHandlerCapture
screen := a.screen
root := a.root
a.RUnlock()
@ -294,18 +298,18 @@ EventLoop:
var action MouseAction
if atX != int(a.lastMouseX) || atY != int(a.lastMouseY) {
action |= MouseMove
a.lastMouseX = int16(atX)
a.lastMouseY = int16(atY)
a.lastMouseX = atX
a.lastMouseY = atY
}
action |= getMouseButtonAction(a.lastMouseBtn, btn)
action = action.getMouseButtonAction(a.lastMouseBtn, btn)
if atX == int(a.mouseDownX) && atY == int(a.mouseDownY) {
action |= getMouseClickAction(a.lastMouseAct, action)
action = action.getMouseClickAction(a.lastMouseAct)
}
a.lastMouseAct = action
a.lastMouseBtn = btn
if action&(MouseLeftDown|MouseMiddleDown|MouseRightDown) != 0 {
a.mouseDownX = int16(atX)
a.mouseDownY = int16(atY)
a.mouseDownX = atX
a.mouseDownY = atY
}
// Intercept event.
@ -317,13 +321,24 @@ EventLoop:
}
}
if handler := root.MouseHandler(); handler != nil {
//consumed, capture :=
handler(event, action, func(p Primitive) {
var newHandlerCapture Primitive = nil // Clear it by default.
if mouseHandlerCapture != nil { // Check if already captured.
if handler := mouseHandlerCapture.MouseHandler(); handler != nil {
_, newHandlerCapture = handler(action, event, func(p Primitive) {
a.SetFocus(p)
})
a.draw()
}
} else if handler := root.MouseHandler(); handler != nil {
_, newHandlerCapture = handler(action, event, func(p Primitive) {
a.SetFocus(p)
})
a.draw()
}
a.Lock()
a.mouseHandlerCapture = newHandlerCapture
a.Unlock()
}
// If we have updates, now is the time to execute them.

@ -62,7 +62,7 @@ type Box struct {
// An optional capture function which receives a mouse event and returns the
// event to be forwarded to the primitive's default mouse event handler (nil if
// nothing should be forwarded).
mouseCapture func(event *tcell.EventMouse, action MouseAction) (*tcell.EventMouse, MouseAction)
mouseCapture func(action MouseAction, event *tcell.EventMouse) (MouseAction, *tcell.EventMouse)
}
// NewBox returns a Box without a border.
@ -202,20 +202,20 @@ func (b *Box) GetInputCapture() func(event *tcell.EventKey) *tcell.EventKey {
// on to the provided (default) event handler.
//
// This is only meant to be used by subclassing primitives.
func (b *Box) WrapMouseHandler(mouseHandler func(*tcell.EventMouse, MouseAction, func(p Primitive)) (bool, bool)) func(*tcell.EventMouse, MouseAction, func(p Primitive)) (bool, bool) {
return func(event *tcell.EventMouse, action MouseAction, setFocus func(p Primitive)) (consumed, capture bool) {
func (b *Box) WrapMouseHandler(mouseHandler func(MouseAction, *tcell.EventMouse, func(p Primitive)) (bool, Primitive)) func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
return func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
if b.mouseCapture != nil {
event, action = b.mouseCapture(event, action)
action, event = b.mouseCapture(action, event)
}
if event != nil && mouseHandler != nil {
consumed, capture = mouseHandler(event, action, setFocus)
consumed, capture = mouseHandler(action, event, setFocus)
}
return
}
}
// MouseHandler returns nil.
func (b *Box) MouseHandler() func(*tcell.EventMouse, MouseAction, func(p Primitive)) (bool, bool) {
func (b *Box) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
return b.WrapMouseHandler(nil)
}
@ -226,7 +226,7 @@ func (b *Box) MouseHandler() func(*tcell.EventMouse, MouseAction, func(p Primiti
// be called.
//
// Providing a nil handler will remove a previously existing handler.
func (b *Box) SetMouseCapture(capture func(event *tcell.EventMouse, action MouseAction) (*tcell.EventMouse, MouseAction)) *Box {
func (b *Box) SetMouseCapture(capture func(action MouseAction, event *tcell.EventMouse) (MouseAction, *tcell.EventMouse)) *Box {
b.mouseCapture = capture
return b
}
@ -239,7 +239,7 @@ func (b *Box) InRect(atX, atY int) bool {
// GetMouseCapture returns the function installed with SetMouseCapture() or nil
// if no such function has been installed.
func (b *Box) GetMouseCapture() func(event *tcell.EventMouse, action MouseAction) (*tcell.EventMouse, MouseAction) {
func (b *Box) GetMouseCapture() func(action MouseAction, event *tcell.EventMouse) (MouseAction, *tcell.EventMouse) {
return b.mouseCapture
}

@ -137,10 +137,10 @@ func (b *Button) InputHandler() func(event *tcell.EventKey, setFocus func(p Prim
}
// MouseHandler returns the mouse handler for this primitive.
func (b *Button) MouseHandler() func(*tcell.EventMouse, MouseAction, func(p Primitive)) (bool, bool) {
return b.WrapMouseHandler(func(event *tcell.EventMouse, action MouseAction, setFocus func(p Primitive)) (consumed, capture bool) {
func (b *Button) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
return b.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
if !b.InRect(event.Position()) {
return false, false
return false, nil
}
// Process mouse event.
if action&MouseLeftClick != 0 {
@ -148,6 +148,6 @@ func (b *Button) MouseHandler() func(*tcell.EventMouse, MouseAction, func(p Prim
b.selected()
}
}
return true, false
return true, nil
})
}

@ -203,10 +203,10 @@ 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(*tcell.EventMouse, MouseAction, func(p Primitive)) (bool, bool) {
return c.WrapMouseHandler(func(event *tcell.EventMouse, action MouseAction, setFocus func(p Primitive)) (consumed, capture bool) {
func (c *Checkbox) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
return c.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
if !c.InRect(event.Position()) {
return false, false
return false, nil
}
// Process mouse event.
if action&MouseLeftClick != 0 {
@ -215,6 +215,6 @@ func (c *Checkbox) MouseHandler() func(*tcell.EventMouse, MouseAction, func(p Pr
c.changed(c.checked)
}
}
return true, false
return true, nil
})
}

@ -493,7 +493,7 @@ func (d *DropDown) HasFocus() bool {
return d.hasFocus
}
func (d *DropDown) listClick(event *tcell.EventMouse, action MouseAction, setFocus func(p Primitive)) (consumed, capture bool) {
func (d *DropDown) listClick(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
atX, atY := event.Position()
x, y, w, h := d.list.GetRect()
if atX >= x && atY >= y && atX < x+w && atY < y+h {
@ -501,23 +501,23 @@ func (d *DropDown) listClick(event *tcell.EventMouse, action MouseAction, setFoc
if handler := d.list.MouseHandler(); handler != nil {
// Treat mouse up as click here.
// This allows you to expand and select in one go.
return handler(event, MouseLeftUp|MouseLeftClick, setFocus)
handler(MouseLeftUp|MouseLeftClick, event, setFocus)
}
return true, false
return true, d
}
return false, false
return false, nil
}
// MouseHandler returns the mouse handler for this primitive.
func (d *DropDown) MouseHandler() func(*tcell.EventMouse, MouseAction, func(p Primitive)) (bool, bool) {
return d.WrapMouseHandler(func(event *tcell.EventMouse, action MouseAction, setFocus func(p Primitive)) (consumed, capture bool) {
func (d *DropDown) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
return d.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
inRect := d.InRect(event.Position())
if !d.open && !inRect {
return false, false
return false, nil
}
// Process mouse event.
if d.open && action&(MouseLeftDown|MouseLeftUp) != 0 { // Close it:
consumed, capture = d.listClick(event, action, setFocus)
consumed, capture = d.listClick(action, event, setFocus)
if consumed {
d.closeList(setFocus)
return consumed, capture
@ -527,8 +527,14 @@ func (d *DropDown) MouseHandler() func(*tcell.EventMouse, MouseAction, func(p Pr
}
} else if !d.open && inRect && action&MouseLeftDown != 0 { // Open it:
d.openList(setFocus)
return true, true // capture
return true, d // capture
} else if d.open {
// Non-click while list is open.
if handler := d.list.MouseHandler(); handler != nil {
handler(action, event, setFocus)
}
return true, d // capture
}
return true, false
return true, nil
})
}

@ -197,18 +197,18 @@ func (f *Flex) HasFocus() bool {
}
// MouseHandler returns the mouse handler for this primitive.
func (f *Flex) MouseHandler() func(*tcell.EventMouse, MouseAction, func(p Primitive)) (bool, bool) {
return f.WrapMouseHandler(func(event *tcell.EventMouse, action MouseAction, setFocus func(p Primitive)) (consumed, capture bool) {
func (f *Flex) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
return f.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
if !f.InRect(event.Position()) {
return false, false
return false, nil
}
// Process mouse event.
for _, item := range f.items {
consumed, capture = item.Item.MouseHandler()(event, action, setFocus)
consumed, capture = item.Item.MouseHandler()(action, event, setFocus)
if consumed {
return consumed, capture
}
}
return true, false
return true, nil
})
}

@ -602,24 +602,24 @@ func (f *Form) focusIndex() int {
}
// MouseHandler returns the mouse handler for this primitive.
func (f *Form) MouseHandler() func(*tcell.EventMouse, MouseAction, func(p Primitive)) (bool, bool) {
return f.WrapMouseHandler(func(event *tcell.EventMouse, action MouseAction, setFocus func(p Primitive)) (consumed, capture bool) {
func (f *Form) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
return f.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
if !f.InRect(event.Position()) {
return false, false
return false, nil
}
// Process mouse event.
for _, item := range f.items {
consumed, capture = item.MouseHandler()(event, action, setFocus)
consumed, capture = item.MouseHandler()(action, event, setFocus)
if consumed {
return consumed, capture
}
}
for _, button := range f.buttons {
consumed, capture = button.MouseHandler()(event, action, setFocus)
consumed, capture = button.MouseHandler()(action, event, setFocus)
if consumed {
return consumed, capture
}
}
return true, false
return true, nil
})
}

@ -157,16 +157,16 @@ func (f *Frame) HasFocus() bool {
}
// MouseHandler returns the mouse handler for this primitive.
func (f *Frame) MouseHandler() func(*tcell.EventMouse, MouseAction, func(p Primitive)) (bool, bool) {
return f.WrapMouseHandler(func(event *tcell.EventMouse, action MouseAction, setFocus func(p Primitive)) (consumed, capture bool) {
func (f *Frame) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
return f.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
if !f.InRect(event.Position()) {
return false, false
return false, nil
}
// Process mouse event.
consumed, capture = f.primitive.MouseHandler()(event, action, setFocus)
consumed, capture = f.primitive.MouseHandler()(action, event, setFocus)
if consumed {
return consumed, capture
}
return true, false
return true, nil
})
}

@ -662,18 +662,18 @@ func (g *Grid) Draw(screen tcell.Screen) {
}
// MouseHandler returns the mouse handler for this primitive.
func (g *Grid) MouseHandler() func(*tcell.EventMouse, MouseAction, func(p Primitive)) (bool, bool) {
return g.WrapMouseHandler(func(event *tcell.EventMouse, action MouseAction, setFocus func(p Primitive)) (consumed, capture bool) {
func (g *Grid) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
return g.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
if !g.InRect(event.Position()) {
return false, false
return false, nil
}
// Process mouse event.
for _, item := range g.items {
consumed, capture = item.Item.MouseHandler()(event, action, setFocus)
consumed, capture = item.Item.MouseHandler()(action, event, setFocus)
if consumed {
return consumed, capture
}
}
return true, false
return true, nil
})
}

@ -593,15 +593,15 @@ func (i *InputField) InputHandler() func(event *tcell.EventKey, setFocus func(p
}
// MouseHandler returns the mouse handler for this primitive.
func (i *InputField) MouseHandler() func(*tcell.EventMouse, MouseAction, func(p Primitive)) (bool, bool) {
return i.WrapMouseHandler(func(event *tcell.EventMouse, action MouseAction, setFocus func(p Primitive)) (consumed, capture bool) {
func (i *InputField) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
return i.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
if !i.InRect(event.Position()) {
return false, false
return false, nil
}
// Process mouse event.
if action&MouseLeftDown != 0 {
setFocus(i)
}
return true, false
return true, nil
})
}

@ -547,10 +547,10 @@ func (l *List) indexAtPoint(atX, atY int) int {
}
// MouseHandler returns the mouse handler for this primitive.
func (l *List) MouseHandler() func(*tcell.EventMouse, MouseAction, func(p Primitive)) (bool, bool) {
return l.WrapMouseHandler(func(event *tcell.EventMouse, action MouseAction, setFocus func(p Primitive)) (consumed, capture bool) {
func (l *List) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
return l.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
if !l.InRect(event.Position()) {
return false, false
return false, nil
}
// Process mouse event.
if action&MouseLeftClick != 0 {
@ -570,6 +570,6 @@ func (l *List) MouseHandler() func(*tcell.EventMouse, MouseAction, func(p Primit
l.currentItem = index
}
}
return true, false
return true, nil
})
}

@ -171,16 +171,16 @@ func (m *Modal) Draw(screen tcell.Screen) {
}
// MouseHandler returns the mouse handler for this primitive.
func (m *Modal) MouseHandler() func(*tcell.EventMouse, MouseAction, func(p Primitive)) (bool, bool) {
return m.WrapMouseHandler(func(event *tcell.EventMouse, action MouseAction, setFocus func(p Primitive)) (consumed, capture bool) {
func (m *Modal) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
return m.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
if !m.InRect(event.Position()) {
return false, false
return false, nil
}
// Process mouse event.
consumed, capture = m.frame.MouseHandler()(event, action, setFocus)
consumed, capture = m.frame.MouseHandler()(action, event, setFocus)
if consumed {
return consumed, capture
}
return true, false
return true, nil
})
}

@ -28,9 +28,8 @@ const (
)
// Does not set MouseMove or *Click actions.
func getMouseButtonAction(lastBtn, btn tcell.ButtonMask) MouseAction {
func (action MouseAction) getMouseButtonAction(lastBtn, btn tcell.ButtonMask) MouseAction {
btnDiff := btn ^ lastBtn
var action MouseAction
if btnDiff&tcell.Button1 != 0 {
if btn&tcell.Button1 != 0 {
@ -75,7 +74,7 @@ func getMouseButtonAction(lastBtn, btn tcell.ButtonMask) MouseAction {
// Do not call if the mouse moved.
// Sets the *Click, including *DoubleClick.
// This should be called last, after setting all the other flags.
func getMouseClickAction(lastAct, action MouseAction) MouseAction {
func (action MouseAction) getMouseClickAction(lastAct MouseAction) MouseAction {
if action&MouseMove == 0 {
if action&MouseLeftUp != 0 {
if lastAct&(MouseLeftClick|MouseLeftDoubleClick) == 0 {

@ -280,20 +280,20 @@ func (p *Pages) Draw(screen tcell.Screen) {
}
// MouseHandler returns the mouse handler for this primitive.
func (p *Pages) MouseHandler() func(*tcell.EventMouse, MouseAction, func(p Primitive)) (bool, bool) {
return p.WrapMouseHandler(func(event *tcell.EventMouse, action MouseAction, setFocus func(p Primitive)) (consumed, capture bool) {
func (p *Pages) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
return p.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
if !p.InRect(event.Position()) {
return false, false
return false, nil
}
// Process mouse event.
for _, page := range p.pages {
if page.Visible {
consumed, capture = page.Item.MouseHandler()(event, action, setFocus)
consumed, capture = page.Item.MouseHandler()(action, event, setFocus)
if consumed {
return consumed, capture
}
}
}
return true, false
return true, nil
})
}

@ -52,5 +52,5 @@ type Primitive interface {
// The Box class provides functionality to intercept mouse events. If you
// subclass from Box, it is recommended that you wrap your handler using
// Box.WrapMouseHandler() so you inherit that functionality.
MouseHandler() func(*tcell.EventMouse, MouseAction, func(p Primitive)) (bool, bool)
MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive)
}

Loading…
Cancel
Save