Dismiss dropdown by click

This commit is contained in:
Chris Miller 2019-11-05 01:38:47 +00:00 committed by Trevor Slocum
parent b243f370d8
commit 5bac390ebc
1 changed files with 18 additions and 4 deletions

View File

@ -471,9 +471,21 @@ func (d *DropDown) openList(setFocus func(Primitive), app *Application) {
app.SetMouseCapture(func(event EventMouse) EventMouse {
if d.open {
// Forward the mouse event to the list.
if handler := d.list.MouseHandler(); handler != nil {
handler(event)
return EventMouse{} // handled
atX, atY := event.Position()
x, y, w, h := d.list.GetInnerRect()
if atX >= x && atY >= y && atX < x+w && atY < y+h {
// Mouse is within the list.
if handler := d.list.MouseHandler(); handler != nil {
handler(event)
return EventMouse{} // handled
}
} else {
// Mouse not within the list.
if event.Buttons() != 0 {
// If a mouse button was pressed, cancel this capture.
app.SetMouseCapture(nil)
d.closeList(nil) // Close but don't focus.
}
}
}
return event
@ -484,7 +496,9 @@ func (d *DropDown) openList(setFocus func(Primitive), app *Application) {
func (d *DropDown) closeList(setFocus func(Primitive)) {
d.open = false
setFocus(d)
if setFocus != nil {
setFocus(d)
}
}
// Focus is called by the application when the primitive receives focus.