cview/demos/focusmanager/main.go

67 lines
1.5 KiB
Go
Raw Normal View History

2020-10-16 04:28:54 +00:00
// Demo code for the FocusManager utility.
package main
import (
"log"
2021-04-02 00:35:06 +00:00
"code.rocketnine.space/tslocum/cbind"
"code.rocketnine.space/tslocum/cview"
"github.com/gdamore/tcell/v2"
2020-10-16 04:28:54 +00:00
)
2020-10-16 18:01:25 +00:00
func wrap(f func()) func(ev *tcell.EventKey) *tcell.EventKey {
return func(ev *tcell.EventKey) *tcell.EventKey {
f()
return nil
}
}
2020-10-16 04:28:54 +00:00
func main() {
app := cview.NewApplication()
app.EnableMouse(true)
input1 := cview.NewInputField()
input1.SetLabel("InputField 1")
input2 := cview.NewInputField()
input2.SetLabel("InputField 2")
input3 := cview.NewInputField()
input3.SetLabel("InputField 3")
input4 := cview.NewInputField()
input4.SetLabel("InputField 4")
grid := cview.NewGrid()
grid.SetBorder(true)
grid.SetTitle(" Press Tab to advance focus ")
grid.AddItem(input1, 0, 0, 1, 1, 0, 0, true)
grid.AddItem(input2, 0, 1, 1, 1, 0, 0, false)
grid.AddItem(input3, 1, 1, 1, 1, 0, 0, false)
grid.AddItem(input4, 1, 0, 1, 1, 0, 0, false)
focusManager := cview.NewFocusManager(app.SetFocus)
focusManager.SetWrapAround(true)
focusManager.Add(input1, input2, input3, input4)
inputHandler := cbind.NewConfiguration()
for _, key := range cview.Keys.MovePreviousField {
2020-10-16 18:01:25 +00:00
err := inputHandler.Set(key, wrap(focusManager.FocusPrevious))
2020-10-16 04:28:54 +00:00
if err != nil {
log.Fatal(err)
}
}
for _, key := range cview.Keys.MoveNextField {
2020-10-16 18:01:25 +00:00
err := inputHandler.Set(key, wrap(focusManager.FocusNext))
2020-10-16 04:28:54 +00:00
if err != nil {
log.Fatal(err)
}
}
app.SetInputCapture(inputHandler.Capture)
app.SetRoot(grid, true)
if err := app.Run(); err != nil {
panic(err)
}
}