Browse Source

Refactor EventMouse

merge-requests/1/merge
Chris Miller 2 years ago
committed by Trevor Slocum
parent
commit
a52eae5a0d
  1. 8
      application.go
  2. 14
      box.go
  3. 6
      button.go
  4. 6
      checkbox.go
  5. 20
      dropdown.go
  6. 51
      events.go
  7. 6
      inputfield.go
  8. 6
      list.go
  9. 4
      primitive.go

8
application.go

@ -83,7 +83,7 @@ type Application struct {
// An optional capture function which receives a mouse event and returns the
// event to be forwarded to the default mouse handler (nil if nothing should
// be forwarded).
mouseCapture func(event EventMouse) EventMouse
mouseCapture func(event *EventMouse) *EventMouse
lastMouseX, lastMouseY int
lastMouseBtn tcell.ButtonMask
@ -163,7 +163,7 @@ func (a *Application) SetMouseCapture(capture func(event EventMouse) EventMouse)
// GetMouseCapture returns the function installed with SetMouseCapture() or nil
// if no such function has been installed.
func (a *Application) GetMouseCapture() func(event EventMouse) EventMouse {
func (a *Application) GetMouseCapture() func(event *EventMouse) *EventMouse {
return a.mouseCapture
}
@ -401,12 +401,12 @@ EventLoop:
a.lastMouseBtn = btn
}
event2 := EventMouse{event, ptarget, a, act}
event2 := NewEventMouse(event, ptarget, a, act)
// Intercept event.
if mouseCapture != nil {
event2 = mouseCapture(event2)
if event2.IsZero() {
if event2 == nil {
a.draw()
continue // Don't forward event.
}

14
box.go

@ -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 EventMouse) EventMouse
mouseCapture func(event *EventMouse) *EventMouse
}
// NewBox returns a Box without a border.
@ -202,19 +202,19 @@ 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(EventMouse)) func(EventMouse) {
return func(event EventMouse) {
func (b *Box) WrapMouseHandler(mouseHandler func(*EventMouse)) func(*EventMouse) {
return func(event *EventMouse) {
if b.mouseCapture != nil {
event = b.mouseCapture(event)
}
if !event.IsZero() && mouseHandler != nil {
if event != nil && mouseHandler != nil {
mouseHandler(event)
}
}
}
// MouseHandler returns nil.
func (b *Box) MouseHandler() func(event EventMouse) {
func (b *Box) MouseHandler() func(event *EventMouse) {
return b.WrapMouseHandler(nil)
}
@ -225,14 +225,14 @@ func (b *Box) MouseHandler() func(event EventMouse) {
// be called.
//
// Providing a nil handler will remove a previously existing handler.
func (b *Box) SetMouseCapture(capture func(EventMouse) EventMouse) *Box {
func (b *Box) SetMouseCapture(capture func(*EventMouse) *EventMouse) *Box {
b.mouseCapture = capture
return b
}
// GetMouseCapture returns the function installed with SetMouseCapture() or nil
// if no such function has been installed.
func (b *Box) GetMouseCapture() func(EventMouse) EventMouse {
func (b *Box) GetMouseCapture() func(*EventMouse) *EventMouse {
return b.mouseCapture
}

6
button.go

@ -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(event EventMouse) {
return b.WrapMouseHandler(func(event EventMouse) {
func (b *Button) MouseHandler() func(event *EventMouse) {
return b.WrapMouseHandler(func(event *EventMouse) {
// Process mouse event.
if event.Action&MouseClick != 0 {
if event.Action()&MouseClick != 0 {
if b.selected != nil {
b.selected()
}

6
checkbox.go

@ -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(event EventMouse) {
return c.WrapMouseHandler(func(event EventMouse) {
func (c *Checkbox) MouseHandler() func(event *EventMouse) {
return c.WrapMouseHandler(func(event *EventMouse) {
// Process mouse event.
if event.Action&MouseClick != 0 {
if event.Action()&MouseClick != 0 {
c.checked = !c.checked
if c.changed != nil {
c.changed(c.checked)

20
dropdown.go

@ -468,7 +468,7 @@ func (d *DropDown) openList(setFocus func(Primitive), app *Application) {
return event
})
if app != nil {
app.SetMouseCapture(func(event EventMouse) EventMouse {
app.SetMouseCapture(func(event *EventMouse) *EventMouse {
if d.open {
// Forward the mouse event to the list.
atX, atY := event.Position()
@ -476,17 +476,19 @@ func (d *DropDown) openList(setFocus func(Primitive), app *Application) {
if atX >= x && atY >= y && atX < x+w && atY < y+h {
// Mouse is within the list.
if handler := d.list.MouseHandler(); handler != nil {
if event.Action&MouseUp != 0 {
if event.Action()&MouseUp != 0 {
// Treat mouse up as click here.
// This allows you to expand and select in one go.
event.Action |= MouseClick
event = NewEventMouse(event.EventMouse,
event.Target(), event.Application(),
event.Action()|MouseClick)
}
handler(event)
return EventMouse{} // handled
return nil // handled
}
} else {
// Mouse not within the list.
if event.Action&MouseDown != 0 {
if event.Action()&MouseDown != 0 {
// If a mouse button was pressed, cancel this capture.
app.SetMouseCapture(nil)
d.closeList(event.SetFocus)
@ -523,16 +525,16 @@ func (d *DropDown) HasFocus() bool {
}
// MouseHandler returns the mouse handler for this primitive.
func (d *DropDown) MouseHandler() func(event EventMouse) {
return d.WrapMouseHandler(func(event EventMouse) {
func (d *DropDown) MouseHandler() func(event *EventMouse) {
return d.WrapMouseHandler(func(event *EventMouse) {
// Process mouse event.
if event.Action&MouseDown != 0 && event.Buttons()&tcell.Button1 != 0 {
if event.Action()&MouseDown != 0 && event.Buttons()&tcell.Button1 != 0 {
//d.open = !d.open
//event.SetFocus(d)
if d.open {
d.closeList(event.SetFocus)
} else {
d.openList(event.SetFocus, event.Application)
d.openList(event.SetFocus, event.Application())
}
}
})

51
events.go

@ -0,0 +1,51 @@
package cview
import "github.com/gdamore/tcell"
// EventKey is the key input event info.
// This exists for some consistency with EventMouse,
// even though it's just an alias to tcell.EventKey for backwards compatibility.
type EventKey = tcell.EventKey
// MouseAction are bit flags indicating what the mouse is logically doing.
type MouseAction int
const (
MouseDown MouseAction = 1 << iota
MouseUp
MouseClick // Button1 only.
MouseMove // The mouse position changed.
)
// EventMouse is the mouse event info.
type EventMouse struct {
*tcell.EventMouse
target Primitive
app *Application
action MouseAction
}
// Target gets the target Primitive of the mouse event.
func (e *EventMouse) Target() Primitive {
return e.target
}
// Application gets the event originating *Application.
func (e *EventMouse) Application() *Application {
return e.app
}
// MouseAction gets the mouse action of this event.
func (e *EventMouse) Action() MouseAction {
return e.action
}
// SetFocus will set focus to the primitive.
func (e *EventMouse) SetFocus(p Primitive) {
e.app.SetFocus(p)
}
// NewEventMouse creates a new mouse event.
func NewEventMouse(base *tcell.EventMouse, target Primitive, app *Application, action MouseAction) *EventMouse {
return &EventMouse{base, target, app, action}
}

6
inputfield.go

@ -593,10 +593,10 @@ 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) {
func (i *InputField) MouseHandler() func(event *EventMouse) {
return i.WrapMouseHandler(func(event *EventMouse) {
// Process mouse event.
if event.Action&MouseDown != 0 {
if event.Action()&MouseDown != 0 {
event.SetFocus(i)
}
})

6
list.go

@ -569,10 +569,10 @@ func (l *List) indexAtPoint(atX, atY int) int {
}
// MouseHandler returns the mouse handler for this primitive.
func (l *List) MouseHandler() func(event EventMouse) {
return l.WrapMouseHandler(func(event EventMouse) {
func (l *List) MouseHandler() func(event *EventMouse) {
return l.WrapMouseHandler(func(event *EventMouse) {
// Process mouse event.
if event.Action&MouseClick != 0 {
if event.Action()&MouseClick != 0 {
atX, atY := event.Position()
index := l.indexAtPoint(atX, atY)
if index != -1 {

4
primitive.go

@ -50,10 +50,10 @@ type Primitive interface {
// MouseHandler returns a handler which receives mouse events.
// It is called by the Application class.
//
// A zero value of EventMouse{} may also be returned to stop propagation.
// A value of nil may also be returned to stop propagation.
//
// 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(event EventMouse)
MouseHandler() func(event *EventMouse)
}

Loading…
Cancel
Save