Example of getting Notebook content in a type safe manner #152
Description
I saw the example at https://github.com/gtk-rs/examples/blob/master/src/bin/notebook.rs and it is great. I am using it in my own project and it showed me how to customize "Types" of Widgets.
Now I would like to see an example for getting the content of a notebook page, changing it and adding it back to the notebook all in a type safe manner. The problem is that I get back a Option<Widget>
, instead of whatever type I say I'll add to the Notebook. When I try to get back what I put in, obviously I know what it should be, but Rust seems not to know:
let the_tab: Option<Widget> = notebook.get_nth_page(4); // just get the widget which was added in the fifth tab of the notebook
Then I could to the following:
let label = match the_tab {
Some(widget) => widget.downcast::<Label>(),
None => // what to do here?
};
I found downcast
in the autogenerated docs from cargo doc
in the Cast
trait, after searching for an hour or more how to cast something, but still the problem is not solved, because I don't know how to handle the None
case. The label
must have some simple type like Label
, so that I can call all the functions which are implemented for that type and work with what I got from the Notebook
.
I am still a beginner in Rust, but I don't know how to proceed here. I think I would have to pass a type at creation time of the Notebook
, in order to let Rust know what comes back, but that would not change the interface to suddenly give me not only Option<Widget>
, but the actual more specific contained type.
An example of how to deal with such things in gtk-rs
would be of great use.