2018-02-27 04:54:39 +01:00
|
|
|
package widgets
|
2018-02-27 04:41:54 +01:00
|
|
|
|
|
|
|
import (
|
2018-06-01 09:58:00 +02:00
|
|
|
"github.com/gdamore/tcell"
|
2018-02-27 04:54:39 +01:00
|
|
|
|
|
|
|
"git.sr.ht/~sircmpwn/aerc2/lib/ui"
|
2018-02-27 04:41:54 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type ExLine struct {
|
2019-04-27 18:47:59 +02:00
|
|
|
ui.Invalidatable
|
2019-05-11 19:12:44 +02:00
|
|
|
cancel func()
|
|
|
|
commit func(cmd string)
|
|
|
|
ctx *ui.Context
|
|
|
|
input *ui.TextInput
|
2018-02-27 04:41:54 +01:00
|
|
|
}
|
|
|
|
|
2018-06-12 02:04:21 +02:00
|
|
|
func NewExLine(commit func(cmd string), cancel func()) *ExLine {
|
2019-05-11 19:12:44 +02:00
|
|
|
input := ui.NewTextInput().Prompt(":")
|
|
|
|
exline := &ExLine{
|
|
|
|
cancel: cancel,
|
|
|
|
commit: commit,
|
|
|
|
input: input,
|
2018-02-28 03:02:56 +01:00
|
|
|
}
|
2019-05-11 19:12:44 +02:00
|
|
|
input.OnInvalidate(func(d ui.Drawable) {
|
|
|
|
exline.Invalidate()
|
|
|
|
})
|
|
|
|
return exline
|
2018-02-27 04:41:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ex *ExLine) Invalidate() {
|
2019-04-27 18:47:59 +02:00
|
|
|
ex.DoInvalidate(ex)
|
2018-02-27 04:41:54 +01:00
|
|
|
}
|
|
|
|
|
2018-02-27 04:54:39 +01:00
|
|
|
func (ex *ExLine) Draw(ctx *ui.Context) {
|
2019-01-14 14:07:24 +01:00
|
|
|
ex.ctx = ctx // gross
|
2019-05-11 19:12:44 +02:00
|
|
|
ex.input.Draw(ctx)
|
2018-02-27 04:41:54 +01:00
|
|
|
}
|
|
|
|
|
2019-03-17 19:02:33 +01:00
|
|
|
func (ex *ExLine) Focus(focus bool) {
|
2019-05-11 19:12:44 +02:00
|
|
|
ex.input.Focus(focus)
|
2018-02-27 04:41:54 +01:00
|
|
|
}
|
|
|
|
|
2018-06-01 09:58:00 +02:00
|
|
|
func (ex *ExLine) Event(event tcell.Event) bool {
|
|
|
|
switch event := event.(type) {
|
|
|
|
case *tcell.EventKey:
|
|
|
|
switch event.Key() {
|
|
|
|
case tcell.KeyEnter:
|
2019-03-15 06:46:14 +01:00
|
|
|
if ex.ctx != nil {
|
|
|
|
ex.ctx.HideCursor()
|
|
|
|
}
|
2019-05-11 19:12:44 +02:00
|
|
|
ex.commit(ex.input.String())
|
2018-06-01 09:58:00 +02:00
|
|
|
case tcell.KeyEsc, tcell.KeyCtrlC:
|
2019-03-15 06:46:14 +01:00
|
|
|
if ex.ctx != nil {
|
|
|
|
ex.ctx.HideCursor()
|
|
|
|
}
|
2018-02-28 03:02:56 +01:00
|
|
|
ex.cancel()
|
2019-05-11 19:12:44 +02:00
|
|
|
default:
|
|
|
|
return ex.input.Event(event)
|
2018-02-27 04:41:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|