tview icon indicating copy to clipboard operation
tview copied to clipboard

SetDoneFunc doesn't seem to work when the inputField is inside a Form

Open radisb opened this issue 8 years ago • 6 comments

(Sorry if it is something very obvious, but I am very new to golang so maybe I miss something. )

package main

import (
	"github.com/rivo/tview"
	"github.com/gdamore/tcell"
)

func main() {
	app := tview.NewApplication()
	field := tview.NewInputField().SetDoneFunc(func (key tcell.Key) { app.Stop() })

	//container := tview.NewForm().AddFormItem(field)
	// OR
        //container := tview.NewFlex().AddItem(field, 0, 1, true)

	if err := app.SetRoot(container, true).SetFocus(field).Run(); err != nil {
		panic(err)
	}
}

In the code above, when I press Enter, the app stops only with the second container line (inside a Flex). If I use the first container (the Form) nothing is happening. Is this by design?

radisb avatar Jan 21 '18 14:01 radisb

Actually, this is by design. Check out the documentation for AddFormItem():

Note, however, that the Form class will override some of its attributes to make it work in the form context.

I don't specify exactly what's being overridden because it's different depending on the type of form element. But the done handler is definitely overridden by the Form class (your own handler will be dropped).

The reason for this is that if such a field is embedded in a form, the desired behaviour is to move on to the next field when the user presses Enter (or Tab). If you insist on quitting the application (or anything else) when user presses Enter, you can use SetInputCapture() on your input field to override key events.

rivo avatar Jan 21 '18 20:01 rivo

Got it. Thanks for your answer.

On Sunday, January 21, 2018, rivo [email protected] wrote:

Actually, this is by design. Check out the documentation for AddFormItem() https://godoc.org/github.com/rivo/tview#Form.AddFormItem:

Note, however, that the Form class will override some of its attributes to make it work in the form context.

I don't specify exactly what's being overridden because it's different depending on the type of form element. But the done handler is definitely overridden by the Form class (your own handler will be dropped).

The reason for this is that if such a field is embedded in a form, the desired behaviour is to move on to the next field when the user presses Enter (or Tab). If you insist on quitting the application (or anything else) when user presses Enter, you can use SetInputCapture() https://godoc.org/github.com/rivo/tview#Box.SetInputCapture on your input field to override key events.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rivo/tview/issues/39#issuecomment-359275972, or mute the thread https://github.com/notifications/unsubscribe-auth/ABU66pbf50NmdH7fgB_vG0c8ureyPZMAks5tM5dTgaJpZM4Rl123 .

radisb avatar Jan 22 '18 08:01 radisb

@rivo I just discovered this as well. It would be nice if the Docs for SetDoneFunc on the form items called this out as well.

joegrasse avatar Apr 18 '18 14:04 joegrasse

FYI, I've separated the "done" function from the "finished" function so if your primitive is part of a form, your own "done" callback won't be overwritten anymore.

rivo avatar Apr 19 '18 19:04 rivo

Hi, If I have this

inputerField.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		if event.Key() == tcell.KeyTAB {
			app.SetFocus(textView)
		}
		return event
	})

the behaviour is to go to the next item in the form, not to set focus where i want. Is there any reason for this? How can i override the "Done" keys in a input field?

In my application i have several forms, ( because i couldn't figure out how to organize the input fields the way i want)

I like the cycle behaviour, but i would like to go to the next form when I'm done with the latest inputfield.

Thanks

felipeluna avatar Feb 25 '19 10:02 felipeluna

Have you tried returning nil from within your if statement?

rivo avatar Mar 09 '19 12:03 rivo