|
|
@ -394,9 +394,32 @@ func (d *DropDown) Draw(screen tcell.Screen) {
|
|
|
|
// InputHandler returns the handler for this primitive.
|
|
|
|
// InputHandler returns the handler for this primitive.
|
|
|
|
func (d *DropDown) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
|
|
|
func (d *DropDown) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
|
|
|
return d.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
|
|
|
return d.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
|
|
|
// A helper function which selects an item in the drop-down list based on
|
|
|
|
// Process key event.
|
|
|
|
// the current prefix.
|
|
|
|
switch key := event.Key(); key {
|
|
|
|
evalPrefix := func() {
|
|
|
|
case tcell.KeyEnter, tcell.KeyRune, tcell.KeyDown:
|
|
|
|
|
|
|
|
d.prefix = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If the first key was a letter already, it becomes part of the prefix.
|
|
|
|
|
|
|
|
if r := event.Rune(); key == tcell.KeyRune && r != ' ' {
|
|
|
|
|
|
|
|
d.prefix += string(r)
|
|
|
|
|
|
|
|
d.evalPrefix()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
d.openList(setFocus, nil)
|
|
|
|
|
|
|
|
case tcell.KeyEscape, tcell.KeyTab, tcell.KeyBacktab:
|
|
|
|
|
|
|
|
if d.done != nil {
|
|
|
|
|
|
|
|
d.done(key)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.finished != nil {
|
|
|
|
|
|
|
|
d.finished(key)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// A helper function which selects an item in the drop-down list based on
|
|
|
|
|
|
|
|
// the current prefix.
|
|
|
|
|
|
|
|
func (d *DropDown) evalPrefix() {
|
|
|
|
if len(d.prefix) > 0 {
|
|
|
|
if len(d.prefix) > 0 {
|
|
|
|
for index, option := range d.options {
|
|
|
|
for index, option := range d.options {
|
|
|
|
if strings.HasPrefix(strings.ToLower(option.Text), d.prefix) {
|
|
|
|
if strings.HasPrefix(strings.ToLower(option.Text), d.prefix) {
|
|
|
@ -408,27 +431,16 @@ func (d *DropDown) InputHandler() func(event *tcell.EventKey, setFocus func(p Pr
|
|
|
|
r := []rune(d.prefix)
|
|
|
|
r := []rune(d.prefix)
|
|
|
|
d.prefix = string(r[:len(r)-1])
|
|
|
|
d.prefix = string(r[:len(r)-1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Process key event.
|
|
|
|
|
|
|
|
switch key := event.Key(); key {
|
|
|
|
|
|
|
|
case tcell.KeyEnter, tcell.KeyRune, tcell.KeyDown:
|
|
|
|
|
|
|
|
d.prefix = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If the first key was a letter already, it becomes part of the prefix.
|
|
|
|
|
|
|
|
if r := event.Rune(); key == tcell.KeyRune && r != ' ' {
|
|
|
|
|
|
|
|
d.prefix += string(r)
|
|
|
|
|
|
|
|
evalPrefix()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Hand control over to the list.
|
|
|
|
// Hand control over to the list.
|
|
|
|
|
|
|
|
func (d *DropDown) openList(setFocus func(Primitive), app *Application) {
|
|
|
|
d.open = true
|
|
|
|
d.open = true
|
|
|
|
optionBefore := d.currentOption
|
|
|
|
optionBefore := d.currentOption
|
|
|
|
d.list.SetSelectedFunc(func(index int, mainText, secondaryText string, shortcut rune) {
|
|
|
|
d.list.SetSelectedFunc(func(index int, mainText, secondaryText string, shortcut rune) {
|
|
|
|
// An option was selected. Close the list again.
|
|
|
|
// An option was selected. Close the list again.
|
|
|
|
d.open = false
|
|
|
|
|
|
|
|
setFocus(d)
|
|
|
|
|
|
|
|
d.currentOption = index
|
|
|
|
d.currentOption = index
|
|
|
|
|
|
|
|
d.closeList(setFocus)
|
|
|
|
|
|
|
|
|
|
|
|
// Trigger "selected" event.
|
|
|
|
// Trigger "selected" event.
|
|
|
|
if d.selected != nil {
|
|
|
|
if d.selected != nil {
|
|
|
@ -440,32 +452,39 @@ func (d *DropDown) InputHandler() func(event *tcell.EventKey, setFocus func(p Pr
|
|
|
|
}).SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
}).SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
if event.Key() == tcell.KeyRune {
|
|
|
|
if event.Key() == tcell.KeyRune {
|
|
|
|
d.prefix += string(event.Rune())
|
|
|
|
d.prefix += string(event.Rune())
|
|
|
|
evalPrefix()
|
|
|
|
d.evalPrefix()
|
|
|
|
} else if event.Key() == tcell.KeyBackspace || event.Key() == tcell.KeyBackspace2 {
|
|
|
|
} else if event.Key() == tcell.KeyBackspace || event.Key() == tcell.KeyBackspace2 {
|
|
|
|
if len(d.prefix) > 0 {
|
|
|
|
if len(d.prefix) > 0 {
|
|
|
|
r := []rune(d.prefix)
|
|
|
|
r := []rune(d.prefix)
|
|
|
|
d.prefix = string(r[:len(r)-1])
|
|
|
|
d.prefix = string(r[:len(r)-1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
evalPrefix()
|
|
|
|
d.evalPrefix()
|
|
|
|
} else if event.Key() == tcell.KeyEscape {
|
|
|
|
} else if event.Key() == tcell.KeyEscape {
|
|
|
|
d.open = false
|
|
|
|
|
|
|
|
d.currentOption = optionBefore
|
|
|
|
d.currentOption = optionBefore
|
|
|
|
setFocus(d)
|
|
|
|
d.closeList(setFocus)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
d.prefix = ""
|
|
|
|
d.prefix = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return event
|
|
|
|
return event
|
|
|
|
})
|
|
|
|
})
|
|
|
|
setFocus(d.list)
|
|
|
|
if app != nil {
|
|
|
|
case tcell.KeyEscape, tcell.KeyTab, tcell.KeyBacktab:
|
|
|
|
app.SetMouseCapture(func(event EventMouse) EventMouse {
|
|
|
|
if d.done != nil {
|
|
|
|
if d.open {
|
|
|
|
d.done(key)
|
|
|
|
// Forward the mouse event to the list.
|
|
|
|
}
|
|
|
|
if handler := d.list.MouseHandler(); handler != nil {
|
|
|
|
if d.finished != nil {
|
|
|
|
handler(event)
|
|
|
|
d.finished(key)
|
|
|
|
return EventMouse{} // handled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return event
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
setFocus(d.list)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (d *DropDown) closeList(setFocus func(Primitive)) {
|
|
|
|
|
|
|
|
d.open = false
|
|
|
|
|
|
|
|
setFocus(d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Focus is called by the application when the primitive receives focus.
|
|
|
|
// Focus is called by the application when the primitive receives focus.
|
|
|
@ -489,8 +508,13 @@ func (d *DropDown) MouseHandler() func(event EventMouse) {
|
|
|
|
return d.WrapMouseHandler(func(event EventMouse) {
|
|
|
|
return d.WrapMouseHandler(func(event EventMouse) {
|
|
|
|
// Process mouse event.
|
|
|
|
// Process mouse event.
|
|
|
|
if event.Buttons()&tcell.Button1 != 0 {
|
|
|
|
if event.Buttons()&tcell.Button1 != 0 {
|
|
|
|
//d.open = !d.open // FIXME: clicks go through the dropdown!
|
|
|
|
//d.open = !d.open
|
|
|
|
event.SetFocus(d)
|
|
|
|
//event.SetFocus(d)
|
|
|
|
|
|
|
|
if d.open {
|
|
|
|
|
|
|
|
d.closeList(event.SetFocus)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
d.openList(event.SetFocus, event.Application)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|