antoyo / relm

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

Conditionally display GTK objects in declerative style? #173

Open simvux opened 5 years ago

simvux commented 5 years ago

Let's say I have this view

view! {
  gtk::Window {

  }
}

And i want to append a GTK object based on what it's in my model, how would i do it? I'd like to do something similar to this.

view! {
  gtk::Window {
    model.active_box
  }
}

where model.active_box is of type gtk::Box that gets swapped at runtime

or something like

view! {
  gtk::Window {
    match model.active_box {
        This -> gtk::Box {}
        That -> gtk::Box {}
    }
  }
}

where model.active_box is an enum. Coming from Elm, this would feels like the natural way to do things in declerative style.

antoyo commented 5 years ago

Good idea. I wonder if this could be generalized to support loops, but I guess this is where it becomes more complicated and might require a virtual "DOM" which does not seem to be the case for a match.

simvux commented 5 years ago

Ah okay, wasn't sure if this was something that was implemented or not. If anyone's looking to do what the title of this issue describes, check out the "widget-list.rs" example and observe how you can dynamically switch widgets in your update function using the .add_widget and .remove_widget methods.