[GH-ISSUE #95] Trouble with the implicit SetFocus() #73

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

Originally created by @snabb on GitHub (Mar 30, 2018).
Original GitHub issue: https://github.com/rivo/tview/issues/95

I had trouble setting focus when using a Table within a Flex. The following code does not work:

package main

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

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

        w1 := tview.NewTextView()
        w1.Write([]byte("testing 123"))

        w2 := tview.NewTable()
        w2.SetSelectable(true, false)
        w2.SetCellSimple(0, 0, "foo")
        w2.SetCellSimple(1, 0, "bar")

        layout := tview.NewFlex().
                SetDirection(tview.FlexRow).
                AddItem(w1, 0, 1, false).
                AddItem(w2, 0, 1, false)

        app.SetFocus(w2)

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

The focus is not in the table. I can not move between the table rows.

If I change the app.SetFocus(w2) call to the following it works:

        go func() {
                time.Sleep(time.Second)
                app.SetFocus(w2)
        }()

I browsed through all the closed issues and in one of them it was mentioned that app.SetRoot(foo, false) implicitly calls app.SetFocus(foo). How can I work around this? Would it make sense to eliminate the implicit app.SetFocus() call and let the user set the focus? (Or perhaps I am missing something obvious?)

PS. Great stuff!

Originally created by @snabb on GitHub (Mar 30, 2018). Original GitHub issue: https://github.com/rivo/tview/issues/95 I had trouble setting focus when using a Table within a Flex. The following code does not work: ``` package main import ( "github.com/rivo/tview" ) func main() { app := tview.NewApplication() w1 := tview.NewTextView() w1.Write([]byte("testing 123")) w2 := tview.NewTable() w2.SetSelectable(true, false) w2.SetCellSimple(0, 0, "foo") w2.SetCellSimple(1, 0, "bar") layout := tview.NewFlex(). SetDirection(tview.FlexRow). AddItem(w1, 0, 1, false). AddItem(w2, 0, 1, false) app.SetFocus(w2) if err := app.SetRoot(layout, false).Run(); err != nil { panic(err) } } ``` The focus is not in the table. I can not move between the table rows. If I change the `app.SetFocus(w2)` call to the following it works: ``` go func() { time.Sleep(time.Second) app.SetFocus(w2) }() ``` I browsed through all the closed issues and in one of them it was mentioned that `app.SetRoot(foo, false)` implicitly calls `app.SetFocus(foo)`. How can I work around this? Would it make sense to eliminate the implicit `app.SetFocus()` call and let the user set the focus? (Or perhaps I am missing something obvious?) PS. Great stuff!
kerem closed this issue 2026-03-04 01:01:41 +03:00
Author
Owner

@rivo commented on GitHub (Apr 1, 2018):

You'll want to pass true in the AddItem() call for your table:

AddItem(w2, 0, 1, true)

The application will initially set the focus to the Flex element. But then the Flex element will pass the focus on to your table. See the documentation for details.

Alternatively, you can also call SetFocus() between SetRoot() and Run():

if err := app.SetRoot(layout, false).SetFocus(w2).Run(); err != nil {
    panic(err)
}

Let me know if this helps.

<!-- gh-comment-id:377769942 --> @rivo commented on GitHub (Apr 1, 2018): You'll want to pass `true` in the `AddItem()` call for your table: ```go AddItem(w2, 0, 1, true) ``` The application will initially set the focus to the `Flex` element. But then the `Flex` element will pass the focus on to your table. See the [documentation](https://godoc.org/github.com/rivo/tview#Flex.AddItem) for details. Alternatively, you can also call `SetFocus()` between `SetRoot()` and `Run()`: ```go if err := app.SetRoot(layout, false).SetFocus(w2).Run(); err != nil { panic(err) } ``` Let me know if this helps.
Author
Owner

@snabb commented on GitHub (Apr 1, 2018):

I figured out the AddItem(x, x, x, true) method. It feels troublesome, because I need to define the full "path" through several Flex'es (if the application has many Flex'es within each other) instead of just telling which widget should have focus initially.

However calling app.SetFocus() after app.SetRoot() is easy and straight-forward. I apologize for not figuring this out.

Again: Great stuff! Thanks for making this package available!

<!-- gh-comment-id:377776299 --> @snabb commented on GitHub (Apr 1, 2018): I figured out the `AddItem(x, x, x, true)` method. It feels troublesome, because I need to define the full "path" through several Flex'es (if the application has many Flex'es within each other) instead of just telling which widget should have focus initially. However calling `app.SetFocus()` after `app.SetRoot()` is easy and straight-forward. I apologize for not figuring this out. Again: Great stuff! Thanks for making this package available!
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#73
No description provided.