Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Example of getting Notebook content in a type safe manner #152

Open
ZelphirKaltstahl opened this issue Oct 3, 2017 · 4 comments
Open

Example of getting Notebook content in a type safe manner #152

ZelphirKaltstahl opened this issue Oct 3, 2017 · 4 comments

Comments

@ZelphirKaltstahl
Copy link

ZelphirKaltstahl commented Oct 3, 2017

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.

@EPashkin
Copy link
Member

EPashkin commented Oct 4, 2017

As I understand you get None only if you try access to page that not exist,
so reaction only depend on what you wan't,
IMHO as this normally don't happened,
you can use .unwrap() or better .expect("message") instead match.
Same with result of .downcast::<Label>():
or you know what type you inserted here and use .expect,
or you need decide that to do.
If you adding different types to pages,
then you can check type with http://gtk-rs.org/docs/gtk/trait.Cast.html#method.is beforehand.

@ZelphirKaltstahl
Copy link
Author

ZelphirKaltstahl commented Oct 5, 2017

@EPashkin Thanks for mentioning the Cast is thingy. I'll probably use that somewhere and use expect at other times.

An idea is to give the notebook a generic type, which tells it what stuff one is adding as pages and then return that as pages, but for now the API does not have that.

@EPashkin
Copy link
Member

EPashkin commented Oct 5, 2017

You can add generic type that contains Notebook (and deref) and add your variant append_page and get_nth_page for it,
but IMHO when all pages same type is rare and other cases can be statically checked.

IMHO better can add function like https://github.com/gtk-rs/gtk/blob/master/src/notebook.rs#L102
that generic over type T:IsA<Widget>, that do check and return Option<T>.

@ZelphirKaltstahl
Copy link
Author

@EPashkin Thanks for the ideas. I have to think about this, as it is not obvious to me what the best way to go about it is.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants