[GH-ISSUE #770] Question, how to SetFocus from a function #562

Closed
opened 2026-03-04 01:06:02 +03:00 by kerem · 0 comments
Owner

Originally created by @Thijxx on GitHub (Nov 15, 2022).
Original GitHub issue: https://github.com/rivo/tview/issues/770

I'm sorry if this is not the right place to ask this.
After I SwitchToPage and on the page I have a grid with two lists, I cannot get focus to interact with the lists.
I have tried to call app.SetFocus(menu) but it does not work this way.

var (
	app         *tview.Application
)

func ViewProfiles() *tview.Grid {
	menu := tview.NewList().
		AddItem("New", "", 'n', nil).
		AddItem("Exit", "", 'x', nil).
		ShowSecondaryText(false)

	data := tview.NewList().
		AddItem("Thijs", "seomthing about a phenomeneu", 0, nil).
		ShowSecondaryText(true)

	grid := tview.NewGrid().
		SetRows(0).
		SetColumns(21, 0).
		AddItem(menu, 0, 0, 1, 1, 0, 0, false).
		AddItem(data, 0, 1, 1, 1, 0, 0, false)

	app.SetFocus(menu)
	
	return grid
}

func main() {
	log.SetOutput(os.Stdout)
	log.SetLevel(logrus.DebugLevel)
	log.Info("Gorim start")

	conf.NewConfig()

	app = tview.NewApplication()

	gorimversion := tview.NewTextView().SetTextAlign(tview.AlignCenter).SetText("Gorim v0.0.1")
	activeinfo := tview.NewTextView().SetTextAlign(tview.AlignCenter).SetText("Active Profile: Thijs")
	whoknows := tview.NewTextView().SetTextAlign(tview.AlignCenter).SetText("Who knows")
	mainbody := tview.NewPages()
	footer := tview.NewTextView().SetTextAlign(tview.AlignCenter).SetText("footer")
	pageMods := tview.NewTextView().SetTextAlign(tview.AlignCenter).SetText("mods")
	pageConfigurations := tview.NewTextView().SetTextAlign(tview.AlignCenter).SetText("configs")
	pageSettings := tview.NewTextView().SetTextAlign(tview.AlignCenter).SetText("settings")

	grid := tview.NewGrid().
		SetRows(1, 0, 1).
		SetColumns(20, 0, 20).
		SetBorders(true).
		AddItem(gorimversion, 0, 0, 1, 1, 0, 0, false).
		AddItem(activeinfo, 0, 1, 1, 1, 0, 0, false).
		AddItem(whoknows, 0, 2, 1, 1, 0, 0, false).
		AddItem(mainbody, 1, 0, 1, 3, 0, 0, false).
		AddItem(footer, 2, 0, 1, 3, 0, 0, false)

	mainbody.AddPage("profiles", ViewProfiles(), true, true)
	mainbody.AddPage("mods", pageMods, true, true)
	mainbody.AddPage("configurations", pageConfigurations, true, true)
	mainbody.AddPage("settings", pageSettings, true, true)
	grid.SetInputCapture(
		func(event *tcell.EventKey) *tcell.EventKey {
			if event.Rune() == 'p' {
				mainbody.SwitchToPage("profiles")
			}
			if event.Rune() == 'm' {
				mainbody.SwitchToPage("mods")
			}
			return event
		})

	app.SetRoot(grid, true).SetFocus(mainbody)

	mainbody.SwitchToPage("mods")

	if err := app.Run(); err != nil {
		fmt.Printf("Error running application: %s\n", err)
	}
Originally created by @Thijxx on GitHub (Nov 15, 2022). Original GitHub issue: https://github.com/rivo/tview/issues/770 I'm sorry if this is not the right place to ask this. After I SwitchToPage and on the page I have a grid with two lists, I cannot get focus to interact with the lists. I have tried to call `app.SetFocus(menu)` but it does not work this way. ``` var ( app *tview.Application ) func ViewProfiles() *tview.Grid { menu := tview.NewList(). AddItem("New", "", 'n', nil). AddItem("Exit", "", 'x', nil). ShowSecondaryText(false) data := tview.NewList(). AddItem("Thijs", "seomthing about a phenomeneu", 0, nil). ShowSecondaryText(true) grid := tview.NewGrid(). SetRows(0). SetColumns(21, 0). AddItem(menu, 0, 0, 1, 1, 0, 0, false). AddItem(data, 0, 1, 1, 1, 0, 0, false) app.SetFocus(menu) return grid } func main() { log.SetOutput(os.Stdout) log.SetLevel(logrus.DebugLevel) log.Info("Gorim start") conf.NewConfig() app = tview.NewApplication() gorimversion := tview.NewTextView().SetTextAlign(tview.AlignCenter).SetText("Gorim v0.0.1") activeinfo := tview.NewTextView().SetTextAlign(tview.AlignCenter).SetText("Active Profile: Thijs") whoknows := tview.NewTextView().SetTextAlign(tview.AlignCenter).SetText("Who knows") mainbody := tview.NewPages() footer := tview.NewTextView().SetTextAlign(tview.AlignCenter).SetText("footer") pageMods := tview.NewTextView().SetTextAlign(tview.AlignCenter).SetText("mods") pageConfigurations := tview.NewTextView().SetTextAlign(tview.AlignCenter).SetText("configs") pageSettings := tview.NewTextView().SetTextAlign(tview.AlignCenter).SetText("settings") grid := tview.NewGrid(). SetRows(1, 0, 1). SetColumns(20, 0, 20). SetBorders(true). AddItem(gorimversion, 0, 0, 1, 1, 0, 0, false). AddItem(activeinfo, 0, 1, 1, 1, 0, 0, false). AddItem(whoknows, 0, 2, 1, 1, 0, 0, false). AddItem(mainbody, 1, 0, 1, 3, 0, 0, false). AddItem(footer, 2, 0, 1, 3, 0, 0, false) mainbody.AddPage("profiles", ViewProfiles(), true, true) mainbody.AddPage("mods", pageMods, true, true) mainbody.AddPage("configurations", pageConfigurations, true, true) mainbody.AddPage("settings", pageSettings, true, true) grid.SetInputCapture( func(event *tcell.EventKey) *tcell.EventKey { if event.Rune() == 'p' { mainbody.SwitchToPage("profiles") } if event.Rune() == 'm' { mainbody.SwitchToPage("mods") } return event }) app.SetRoot(grid, true).SetFocus(mainbody) mainbody.SwitchToPage("mods") if err := app.Run(); err != nil { fmt.Printf("Error running application: %s\n", err) } ```
kerem closed this issue 2026-03-04 01:06:03 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#562
No description provided.