compose: make headerEditor focusing more reliable

Focusing header editors was hardcoded as integers which only worked with
the default ui. If a user changed the UI to, for example, put CC as a
field below "to", FocusSubject would focus the CC field instead of the
subject. This commit reuses and modifies the function FocusEditor to
generalize the focusing of header editors - which can now be called by
name via FocusEditor(name string)

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
Acked-by: Robin Jarry <robin@jarry.cc>
This commit is contained in:
Tim Culverhouse 2022-03-21 18:23:47 -05:00 committed by Robin Jarry
parent 93c160ab66
commit e56648029f
3 changed files with 10 additions and 22 deletions

View File

@ -94,7 +94,7 @@ func (forward) Execute(aerc *widgets.Aerc, args []string) error {
tab := aerc.NewTab(composer, subject) tab := aerc.NewTab(composer, subject)
if !h.Has("to") { if !h.Has("to") {
composer.FocusRecipient() composer.FocusEditor("to")
} else { } else {
composer.FocusTerminal() composer.FocusTerminal()
} }

View File

@ -575,14 +575,14 @@ func (aerc *Aerc) Mailto(addr *url.URL) error {
return nil return nil
} }
composer.SetContents(strings.NewReader(body)) composer.SetContents(strings.NewReader(body))
composer.FocusSubject() composer.FocusEditor("subject")
title := "New email" title := "New email"
if subject != "" { if subject != "" {
title = subject title = subject
composer.FocusTerminal() composer.FocusTerminal()
} }
if to == nil { if to == nil {
composer.FocusRecipient() composer.FocusEditor("to")
} }
tab := aerc.NewTab(composer, title) tab := aerc.NewTab(composer, title)
composer.OnHeaderChange("Subject", func(subject string) { composer.OnHeaderChange("Subject", func(subject string) {

View File

@ -294,20 +294,6 @@ func (c *Composer) FocusTerminal() *Composer {
return c return c
} }
func (c *Composer) FocusSubject() *Composer {
c.focusable[c.focused].Focus(false)
c.focused = 2
c.focusable[c.focused].Focus(true)
return c
}
func (c *Composer) FocusRecipient() *Composer {
c.focusable[c.focused].Focus(false)
c.focused = 1
c.focusable[c.focused].Focus(true)
return c
}
// OnHeaderChange registers an OnChange callback for the specified header. // OnHeaderChange registers an OnChange callback for the specified header.
func (c *Composer) OnHeaderChange(header string, fn func(subject string)) { func (c *Composer) OnHeaderChange(header string, fn func(subject string)) {
if editor, ok := c.editors[strings.ToLower(header)]; ok { if editor, ok := c.editors[strings.ToLower(header)]; ok {
@ -374,7 +360,7 @@ func (c *Composer) MouseEvent(localX int, localY int, event tcell.Event) {
for _, e := range c.focusable { for _, e := range c.focusable {
he, ok := e.(*headerEditor) he, ok := e.(*headerEditor)
if ok && he.focused { if ok && he.focused {
c.FocusEditor(he) c.FocusEditor(he.name)
} }
} }
} }
@ -722,10 +708,12 @@ func (c *Composer) NextField() {
c.focusable[c.focused].Focus(true) c.focusable[c.focused].Focus(true)
} }
func (c *Composer) FocusEditor(editor *headerEditor) { func (c *Composer) FocusEditor(editor string) {
editor = strings.ToLower(editor)
c.focusable[c.focused].Focus(false) c.focusable[c.focused].Focus(false)
for i, e := range c.focusable { for i, f := range c.focusable {
if e == editor { e := f.(*headerEditor)
if strings.ToLower(e.name) == editor {
c.focused = i c.focused = i
break break
} }
@ -768,7 +756,7 @@ func (c *Composer) AddEditor(header string, value string, appendHeader bool) {
editor.storeValue() editor.storeValue()
} }
if value == "" { if value == "" {
c.FocusEditor(c.editors[header]) c.FocusEditor(c.editors[header].name)
} }
c.updateGrid() c.updateGrid()
} }