Search code examples
gowidgettabcontainerfyne

Navigating and adding to what's in a TabItem


I have a specific and perhaps unusual problem involving building a GUI in Fyne. My application is Tabbed; aka first thing in the window is a TabContainer. All other widgets to be placed in the application are placed dynamically, at the direction of a server, which sends down a stream of instructions like "in tab 2, row 4, column 3, place an Entry widget" and so on. To be clear, the application is going to make every TabItem a set of vertical Box widgets, each of which will contain a set of horizontal boxes. Think of it as a ragged grid, with no attempt to line up columns, in each tab.

My confusion is this: when I get an instruction from the server, I may (often will!) have to extend the VBoxes on the TabItem, and/or the HBoxes in the chosen VBox. So I want to go to the TabItem and ask it how many VBoxes it already has and then append a few more as needed, and ditto for the horizontal Boxes in that vertical Box. But I can't figure out how to ask that.

Given the tab number, it's not hard to get to the TabItem: tc.Items[tabNumber] gets me that, and I can get to the Content with tc.Items[tabNumber].Content. The problem is, I know that content is a Box, and I (generally) want to add to it. But the compiler isn't putting up with my attempt to cast:

aBox := widget.Box(tc.Items[thing.about.page].Content) //Trust me, it is one. Really.
//and now I can append as needed to this aBox. Except the line above doesn't compile.

The compile error makes sense; this presumably is not a reasonable cast. In C++ I'd break out dynamic_cast and be fine. Being new to Go I don't know how to manage the equivalent.

I know I can arrange a set of maps of maps of maps on the side to hold my Boxes; I don't strictly speaking need to ask tab containers what they contain since I can keep track of it all separately. But that feels wrong; I'm duplicating knowledge that Fyne already has, if only I knew how to dig it out. And when I get commands from the server to start deleting some of the widgets, I'm going to be doing a lot of parallel effort, managing both the Tab container and my maps.

What's the elegant solution?


Solution

  • You need to assert the type this way instead: myBox := tc.Items[thing.about.page].Content.(*widget.Box)