antoyo / relm

Idiomatic, GTK+-based, GUI library, inspired by Elm, written in Rust
MIT License
2.43k stars 78 forks source link

Sending dynamic data to the view #204

Closed MGlolenstine closed 4 years ago

MGlolenstine commented 4 years ago

I want to implement my own ListView, and I'd like to send elements (String) into the view on the init of the View.

So far, I've tried the following, but it just results in some errors

        gtk::Window {
            property_default_width: 480,
            property_default_height: 272,
            border_width: 0,
            position: gtk::WindowPosition::Center,
            resizable: false,
            decorated: false,
            gtk::Box {
                orientation: Vertical,

                ListView{
                    Values(vec!["Test #1", "Test #2", "Test #3", "Test #4", "Test #5", "Test #6"]) => {},
                    Index(0) => {}
                },
            },
            delete_event(_, _) => (Msg::Quit, Inhibit(false)),
        }

and this is my list_view.rs, that has model, messages and the struct declaration

`list_view.rs` code ```Rust pub struct ListModel { pub values: Vec, pub index: u32, } #[derive(Msg)] pub enum ListMsg { Increment, Decrement, Index(u32), Values(Vec), } #[widget] impl Widget for ListView { fn model() -> ListModel { ListModel { values: vec![], index: 0 } } fn update(&mut self, event: ListMsg) { match event { Decrement => { if self.model.index > 0 { self.model.index -= 1; } // cur_number(self.model.counter); } Increment => { if self.model.index < self.model.values.len() as u32 { self.model.index += 1; } // cur_number(self.model.counter); }, Index(i) => self.model.index = i, Values(v) => self.model.values = v, } println!("Current index: {}", self.model.index); } view! { gtk::Box { orientation: Horizontal, gtk::Button { label: "^", property_height_request: 272, property_width_request: 50, clicked => Increment, }, gtk::Label { label: "0", hexpand: true, vexpand: true, text: &self.model.values.get(self.model.index as usize).unwrap(), }, gtk::Button { label: "v", property_height_request: 100, property_width_request: 50, clicked => Decrement, }, } } } ```

The error I'm getting is

unexpected `(` after qualified path

unexpected `(` after qualified path

help: for more information, see https://doc.rust-lang.org/edition-guide/rust-2018/slice-patterns.html
antoyo commented 4 years ago

A widget can have a parameter to its model() method. And you can set the parameter like this.

The other way is to create a binding for a relm widget message like you were trying to do, but the syntax is:

Index: 0

A capital letter indicates it's a relm widget message (i.e. will send a message), while a lowercase letter (index: 0) is a gtk property (i.e. will call set_index(0)).

(By the way, thanks for all your questions, I will make sure to write tutorials about this stuff.)

MGlolenstine commented 4 years ago

Thanks for all the help!

(and you're welcome ;) I know how hard it is to write a good documentation, so I'll keep on asking if I don't find something!)