2019-11-15 21:28:34 +01:00
|
|
|
|
package widgets
|
|
|
|
|
|
|
|
|
|
import (
|
2022-05-07 05:29:43 +02:00
|
|
|
|
"fmt"
|
|
|
|
|
|
2020-11-30 23:07:03 +01:00
|
|
|
|
"github.com/gdamore/tcell/v2"
|
2022-05-07 05:29:43 +02:00
|
|
|
|
"github.com/mattn/go-runewidth"
|
2019-11-15 21:28:34 +01:00
|
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
|
"git.sr.ht/~rjarry/aerc/config"
|
|
|
|
|
"git.sr.ht/~rjarry/aerc/lib/ui"
|
2019-11-15 21:28:34 +01:00
|
|
|
|
)
|
|
|
|
|
|
2020-07-27 10:03:55 +02:00
|
|
|
|
type Selector struct {
|
2019-11-15 21:28:34 +01:00
|
|
|
|
ui.Invalidatable
|
2020-07-27 10:03:55 +02:00
|
|
|
|
chooser bool
|
|
|
|
|
focused bool
|
|
|
|
|
focus int
|
|
|
|
|
options []string
|
2022-07-03 17:11:12 +02:00
|
|
|
|
uiConfig *config.UIConfig
|
2019-11-15 21:28:34 +01:00
|
|
|
|
|
|
|
|
|
onChoose func(option string)
|
|
|
|
|
onSelect func(option string)
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-03 17:11:12 +02:00
|
|
|
|
func NewSelector(options []string, focus int, uiConfig *config.UIConfig) *Selector {
|
2020-07-27 10:03:55 +02:00
|
|
|
|
return &Selector{
|
|
|
|
|
focus: focus,
|
|
|
|
|
options: options,
|
|
|
|
|
uiConfig: uiConfig,
|
2019-11-15 21:28:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 10:03:55 +02:00
|
|
|
|
func (sel *Selector) Chooser(chooser bool) *Selector {
|
2019-11-15 21:28:34 +01:00
|
|
|
|
sel.chooser = chooser
|
|
|
|
|
return sel
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 10:03:55 +02:00
|
|
|
|
func (sel *Selector) Invalidate() {
|
2019-11-15 21:28:34 +01:00
|
|
|
|
sel.DoInvalidate(sel)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 10:03:55 +02:00
|
|
|
|
func (sel *Selector) Draw(ctx *ui.Context) {
|
2022-05-07 05:29:43 +02:00
|
|
|
|
defaultSelectorStyle := sel.uiConfig.GetStyle(config.STYLE_SELECTOR_DEFAULT)
|
|
|
|
|
w, h := ctx.Width(), ctx.Height()
|
|
|
|
|
ctx.Fill(0, 0, w, h, ' ', defaultSelectorStyle)
|
|
|
|
|
|
|
|
|
|
if w < 5 || h < 1 {
|
|
|
|
|
// if width and height are that small, don't even try to draw
|
|
|
|
|
// something
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
y := 1
|
|
|
|
|
if h == 1 {
|
|
|
|
|
y = 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
format := "[%s]"
|
|
|
|
|
|
|
|
|
|
calculateWidth := func(space int) int {
|
|
|
|
|
neededWidth := 2
|
|
|
|
|
for i, option := range sel.options {
|
|
|
|
|
neededWidth += runewidth.StringWidth(fmt.Sprintf(format, option))
|
|
|
|
|
if i < len(sel.options)-1 {
|
|
|
|
|
neededWidth += space
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return neededWidth - space
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
space := 5
|
|
|
|
|
for ; space > 0; space-- {
|
|
|
|
|
if w > calculateWidth(space) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 21:28:34 +01:00
|
|
|
|
x := 2
|
|
|
|
|
for i, option := range sel.options {
|
2022-05-07 05:29:43 +02:00
|
|
|
|
style := defaultSelectorStyle
|
2019-11-15 21:28:34 +01:00
|
|
|
|
if sel.focus == i {
|
|
|
|
|
if sel.focused {
|
2020-07-27 10:03:55 +02:00
|
|
|
|
style = sel.uiConfig.GetStyle(config.STYLE_SELECTOR_FOCUSED)
|
2019-11-15 21:28:34 +01:00
|
|
|
|
} else if sel.chooser {
|
2020-07-27 10:03:55 +02:00
|
|
|
|
style = sel.uiConfig.GetStyle(config.STYLE_SELECTOR_CHOOSER)
|
2019-11-15 21:28:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-07 05:29:43 +02:00
|
|
|
|
|
|
|
|
|
if space == 0 {
|
|
|
|
|
if sel.focus == i {
|
|
|
|
|
leftArrow, rightArrow := ' ', ' '
|
|
|
|
|
if i > 0 {
|
|
|
|
|
leftArrow = '❮'
|
|
|
|
|
}
|
|
|
|
|
if i < len(sel.options)-1 {
|
|
|
|
|
rightArrow = '❯'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s := runewidth.Truncate(option,
|
|
|
|
|
w-runewidth.RuneWidth(leftArrow)-runewidth.RuneWidth(rightArrow)-runewidth.StringWidth(fmt.Sprintf(format, "")),
|
|
|
|
|
"…")
|
|
|
|
|
|
|
|
|
|
nextPos := 0
|
|
|
|
|
nextPos += ctx.Printf(nextPos, y, defaultSelectorStyle, "%c", leftArrow)
|
|
|
|
|
nextPos += ctx.Printf(nextPos, y, style, format, s)
|
|
|
|
|
ctx.Printf(nextPos, y, defaultSelectorStyle, "%c", rightArrow)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
x += ctx.Printf(x, y, style, format, option)
|
|
|
|
|
x += space
|
|
|
|
|
}
|
2019-11-15 21:28:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 10:03:55 +02:00
|
|
|
|
func (sel *Selector) OnChoose(fn func(option string)) *Selector {
|
2019-11-15 21:28:34 +01:00
|
|
|
|
sel.onChoose = fn
|
|
|
|
|
return sel
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 10:03:55 +02:00
|
|
|
|
func (sel *Selector) OnSelect(fn func(option string)) *Selector {
|
2019-11-15 21:28:34 +01:00
|
|
|
|
sel.onSelect = fn
|
|
|
|
|
return sel
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 10:03:55 +02:00
|
|
|
|
func (sel *Selector) Selected() string {
|
2019-11-15 21:28:34 +01:00
|
|
|
|
return sel.options[sel.focus]
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 10:03:55 +02:00
|
|
|
|
func (sel *Selector) Focus(focus bool) {
|
2019-11-15 21:28:34 +01:00
|
|
|
|
sel.focused = focus
|
|
|
|
|
sel.Invalidate()
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 10:03:55 +02:00
|
|
|
|
func (sel *Selector) Event(event tcell.Event) bool {
|
2022-07-31 14:32:48 +02:00
|
|
|
|
if event, ok := event.(*tcell.EventKey); ok {
|
2019-11-15 21:28:34 +01:00
|
|
|
|
switch event.Key() {
|
|
|
|
|
case tcell.KeyCtrlH:
|
|
|
|
|
fallthrough
|
|
|
|
|
case tcell.KeyLeft:
|
|
|
|
|
if sel.focus > 0 {
|
|
|
|
|
sel.focus--
|
|
|
|
|
sel.Invalidate()
|
|
|
|
|
}
|
|
|
|
|
if sel.onSelect != nil {
|
|
|
|
|
sel.onSelect(sel.Selected())
|
|
|
|
|
}
|
|
|
|
|
case tcell.KeyCtrlL:
|
|
|
|
|
fallthrough
|
|
|
|
|
case tcell.KeyRight:
|
|
|
|
|
if sel.focus < len(sel.options)-1 {
|
|
|
|
|
sel.focus++
|
|
|
|
|
sel.Invalidate()
|
|
|
|
|
}
|
|
|
|
|
if sel.onSelect != nil {
|
|
|
|
|
sel.onSelect(sel.Selected())
|
|
|
|
|
}
|
|
|
|
|
case tcell.KeyEnter:
|
|
|
|
|
if sel.onChoose != nil {
|
|
|
|
|
sel.onChoose(sel.Selected())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
2022-05-24 19:12:36 +02:00
|
|
|
|
|
|
|
|
|
var ErrNoOptionSelected = fmt.Errorf("no option selected")
|
|
|
|
|
|
|
|
|
|
type SelectorDialog struct {
|
|
|
|
|
ui.Invalidatable
|
|
|
|
|
callback func(string, error)
|
|
|
|
|
title string
|
|
|
|
|
prompt string
|
2022-07-03 17:11:12 +02:00
|
|
|
|
uiConfig *config.UIConfig
|
2022-05-24 19:12:36 +02:00
|
|
|
|
selector *Selector
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewSelectorDialog(title string, prompt string, options []string, focus int,
|
2022-07-31 22:16:40 +02:00
|
|
|
|
uiConfig *config.UIConfig, cb func(string, error),
|
|
|
|
|
) *SelectorDialog {
|
2022-05-24 19:12:36 +02:00
|
|
|
|
sd := &SelectorDialog{
|
|
|
|
|
callback: cb,
|
|
|
|
|
title: title,
|
|
|
|
|
prompt: prompt,
|
|
|
|
|
uiConfig: uiConfig,
|
|
|
|
|
selector: NewSelector(options, focus, uiConfig).Chooser(true),
|
|
|
|
|
}
|
|
|
|
|
sd.selector.OnInvalidate(func(_ ui.Drawable) {
|
|
|
|
|
sd.Invalidate()
|
|
|
|
|
})
|
|
|
|
|
sd.selector.Focus(true)
|
|
|
|
|
return sd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (gp *SelectorDialog) Draw(ctx *ui.Context) {
|
|
|
|
|
defaultStyle := gp.uiConfig.GetStyle(config.STYLE_DEFAULT)
|
|
|
|
|
titleStyle := gp.uiConfig.GetStyle(config.STYLE_TITLE)
|
|
|
|
|
|
|
|
|
|
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', defaultStyle)
|
|
|
|
|
ctx.Fill(0, 0, ctx.Width(), 1, ' ', titleStyle)
|
|
|
|
|
ctx.Printf(1, 0, titleStyle, "%s", gp.title)
|
|
|
|
|
ctx.Printf(1, 1, defaultStyle, gp.prompt)
|
|
|
|
|
gp.selector.Draw(ctx.Subcontext(1, 3, ctx.Width()-2, 1))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (gp *SelectorDialog) Invalidate() {
|
|
|
|
|
gp.DoInvalidate(gp)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (gp *SelectorDialog) Event(event tcell.Event) bool {
|
|
|
|
|
switch event := event.(type) {
|
|
|
|
|
case *tcell.EventKey:
|
|
|
|
|
switch event.Key() {
|
|
|
|
|
case tcell.KeyEnter:
|
|
|
|
|
gp.selector.Focus(false)
|
|
|
|
|
gp.callback(gp.selector.Selected(), nil)
|
|
|
|
|
case tcell.KeyEsc:
|
|
|
|
|
gp.selector.Focus(false)
|
|
|
|
|
gp.callback("", ErrNoOptionSelected)
|
|
|
|
|
default:
|
|
|
|
|
gp.selector.Event(event)
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
gp.selector.Event(event)
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (gp *SelectorDialog) Focus(f bool) {
|
|
|
|
|
gp.selector.Focus(f)
|
|
|
|
|
}
|