[GH-ISSUE #311] Named primitives for fast access with a single widget instance #235

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

Originally created by @lenormf on GitHub (Jun 17, 2019).
Original GitHub issue: https://github.com/rivo/tview/issues/311

Hi,

It'd be nice to be able to get a widget instance from one of its ancestors. When creating an intricate layout with e.g. a Flex, code that needs to operate on the children widgets must have access to the variable that stores said widgets:

    widgetA := tview.NewXXX()
    layout := tview.NewFlex().
        AddItem(tview.NewFlex().
            AddItem(widgetA, 0, 1, false))

    layout.Callback(func() {
        widgetA.Foo()
    })

It would be more convenient (and scalable) to be able to assign a name/tag (as a string) to widgets, and be able to get their instance using only the parent:

    layout := tview.NewFlex().
        AddItem(tview.NewFlex().
            AddItem(tview.NewXXX().SetName("widgetA"), 0, 1, false))

    layout.Callback(func() {
        if widgetA, ok := layout.GetWidget("widgetA").(SomePrimitive); ok {
            widgetA.Foo()
        }
    })

HTH.

Originally created by @lenormf on GitHub (Jun 17, 2019). Original GitHub issue: https://github.com/rivo/tview/issues/311 Hi, It'd be nice to be able to get a widget instance from one of its ancestors. When creating an intricate layout with e.g. a `Flex`, code that needs to operate on the children widgets must have access to the variable that stores said widgets: ```go widgetA := tview.NewXXX() layout := tview.NewFlex(). AddItem(tview.NewFlex(). AddItem(widgetA, 0, 1, false)) layout.Callback(func() { widgetA.Foo() }) ``` It would be more convenient (and scalable) to be able to assign a name/tag (as a string) to widgets, and be able to get their instance using only the parent: ```go layout := tview.NewFlex(). AddItem(tview.NewFlex(). AddItem(tview.NewXXX().SetName("widgetA"), 0, 1, false)) layout.Callback(func() { if widgetA, ok := layout.GetWidget("widgetA").(SomePrimitive); ok { widgetA.Foo() } }) ``` HTH.
kerem closed this issue 2026-03-04 01:03:13 +03:00
Author
Owner

@rivo commented on GitHub (Jun 18, 2019):

I'm not going to rule this out completely for the far future but I find that how users organize widgets is very individual and I'm not sure your suggestion (or any other in that vein) will cover most users' needs. I also don't like that SetName() would need to be added to all primitives (likely conflicting with page names) and GetWidget() would always require a cast to do anything useful.

In your specific case, it probably won't require much code to come up with a structure like this. A simple map could do the trick already:

widgets := make(map[string]tview.Primitive)
widgets["widgetA"] = tview.NewXXX()
widgets["widgetA"].(XXX).Foo()
<!-- gh-comment-id:503027702 --> @rivo commented on GitHub (Jun 18, 2019): I'm not going to rule this out completely for the far future but I find that how users organize widgets is very individual and I'm not sure your suggestion (or any other in that vein) will cover most users' needs. I also don't like that `SetName()` would need to be added to *all* primitives (likely conflicting with [page names](https://godoc.org/github.com/rivo/tview#Pages.AddPage)) and `GetWidget()` would always require a cast to do anything useful. In your specific case, it probably won't require much code to come up with a structure like this. A simple `map` could do the trick already: ```go widgets := make(map[string]tview.Primitive) widgets["widgetA"] = tview.NewXXX() widgets["widgetA"].(XXX).Foo() ```
Author
Owner

@lenormf commented on GitHub (Jun 19, 2019):

I came up with the function names off the top of my head to describe the problem as well as possible, they can be changed.

Since widgets can have different types, a cast is inevitable.

As for storing the instances in a global object, it would be redundant with the framework itself, as the widgets are already stored by it, all we need is a function that returns a given instance.

<!-- gh-comment-id:503459117 --> @lenormf commented on GitHub (Jun 19, 2019): I came up with the function names off the top of my head to describe the problem as well as possible, they can be changed. Since widgets can have different types, a cast is inevitable. As for storing the instances in a global object, it would be redundant with the framework itself, as the widgets are already stored by it, all we need is a function that returns a given instance.
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#235
No description provided.