[GH-ISSUE #1108] SetFocus not working before app starts for primitives inside Flex #805

Closed
opened 2026-03-04 01:07:53 +03:00 by kerem · 2 comments
Owner

Originally created by @ivoGanev on GitHub (Aug 2, 2025).
Original GitHub issue: https://github.com/rivo/tview/issues/1108

Not sure if this is a bug, a feature, or something else, but when trying to set focus on the middle pannel in the code bellow - before the app starts - nothing happens; on the other side after the app starts running, and I press F1, the middle pannel is set on focus without a problem.

package main

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

func main() {
	app := tview.NewApplication()

	left := tview.NewBox().
		SetBorder(true).
		SetTitle("Left Panel")

	middle := tview.NewBox().
		SetBorder(true).
		SetTitle("Middle Panel")

	right := tview.NewBox().
		SetBorder(true).
		SetTitle("Right Panel")

	top := tview.NewBox().
		SetBorder(true).
		SetTitle("Top Panel")

	bottom := tview.NewBox().
		SetBorder(true).
		SetTitle("Bottom Panel")

	middleRow := tview.NewFlex().
		AddItem(left, 0, 1, false).
		AddItem(middle, 0, 2, false).
		AddItem(right, 0, 1, false)

	layout := tview.NewFlex().
		SetDirection(tview.FlexRow).
		AddItem(top, 3, 0, false).
		AddItem(middleRow, 0, 1, false).
		AddItem(bottom, 3, 0, false)

	app.SetFocus(middle) // not working
	
	app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		if event.Key() == tcell.KeyF1 {
			app.SetFocus(middle) // this works
			return nil
		}
		return event
	})

	if err := app.SetRoot(layout, true).EnableMouse(true).Run(); err != nil {
		panic(err)
	}
}

Also, setting the middle to true like this does not work

	middleRow := tview.NewFlex().
		AddItem(left, 0, 1, false).
		AddItem(middle, 0, 2, true). // not working
		AddItem(right, 0, 1, false)

I have a use case where I want to select a default panel before the app start. I don't see any callbacks to set a focus after the app starts either. Is this a bug?

Originally created by @ivoGanev on GitHub (Aug 2, 2025). Original GitHub issue: https://github.com/rivo/tview/issues/1108 Not sure if this is a bug, a feature, or something else, but when trying to set focus on the middle pannel in the code bellow - before the app starts - nothing happens; on the other side after the app starts running, and I press F1, the middle pannel is set on focus without a problem. ``` package main import ( "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) func main() { app := tview.NewApplication() left := tview.NewBox(). SetBorder(true). SetTitle("Left Panel") middle := tview.NewBox(). SetBorder(true). SetTitle("Middle Panel") right := tview.NewBox(). SetBorder(true). SetTitle("Right Panel") top := tview.NewBox(). SetBorder(true). SetTitle("Top Panel") bottom := tview.NewBox(). SetBorder(true). SetTitle("Bottom Panel") middleRow := tview.NewFlex(). AddItem(left, 0, 1, false). AddItem(middle, 0, 2, false). AddItem(right, 0, 1, false) layout := tview.NewFlex(). SetDirection(tview.FlexRow). AddItem(top, 3, 0, false). AddItem(middleRow, 0, 1, false). AddItem(bottom, 3, 0, false) app.SetFocus(middle) // not working app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { if event.Key() == tcell.KeyF1 { app.SetFocus(middle) // this works return nil } return event }) if err := app.SetRoot(layout, true).EnableMouse(true).Run(); err != nil { panic(err) } } ``` Also, setting the middle to `true` like this does not work ``` middleRow := tview.NewFlex(). AddItem(left, 0, 1, false). AddItem(middle, 0, 2, true). // not working AddItem(right, 0, 1, false) ``` I have a use case where I want to select a default panel before the app start. I don't see any callbacks to set a focus after the app starts either. Is this a bug?
kerem closed this issue 2026-03-04 01:07:53 +03:00
Author
Owner

@MonkeyMatrix commented on GitHub (Aug 9, 2025):

I have not worked much with the project, but I believe this is due to SetRoot focusing your layout. This is found below:

github.com/rivo/tview@a4a78f1e05/application.go (L808-L820)

I believe you can just add .SetFocus(middle) to the provided code.

if err := app.SetRoot(layout, true).SetFocus(middle).EnableMouse(true).Run(); err != nil {
		panic(err)
}
<!-- gh-comment-id:3169552392 --> @MonkeyMatrix commented on GitHub (Aug 9, 2025): I have not worked much with the project, but I believe this is due to SetRoot focusing your `layout`. This is found below: https://github.com/rivo/tview/blob/a4a78f1e05cbdedca1e2a5f2a1120fea713674db/application.go#L808-L820 I believe you can just add `.SetFocus(middle)` to the provided code. ``` go if err := app.SetRoot(layout, true).SetFocus(middle).EnableMouse(true).Run(); err != nil { panic(err) } ```
Author
Owner

@ivoGanev commented on GitHub (Aug 9, 2025):

@MonkeyMatrix, thanks, that worked. I should have been a bit more curious about what’s going on in the codebase.

<!-- gh-comment-id:3170429329 --> @ivoGanev commented on GitHub (Aug 9, 2025): @MonkeyMatrix, thanks, that worked. I should have been a bit more curious about what’s going on in the codebase.
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#805
No description provided.