antoyo / relm

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

Create a new instance of a Relm widget #288

Closed MGlolenstine closed 3 years ago

MGlolenstine commented 3 years ago

How does one create a new instance of a Relm widget? If I want to add GTK widgets to a FlowBox, we'd do it from inside the init_view and just use gtk::Container::add(), but I'm unsure on how to do it with Relm widgets, as they don't seem to have new() method and the only method that returns the widget itself is view, which requires Relm<WidgetModel>.

Is there a better way to do this, maybe just linking a vector to the FlowBox?

MGlolenstine commented 3 years ago

I've solved the problem. You can use relm::create_component, provide arguments for the model method and just call widget() on it to get the widget you're looking for.

let item_card = relm::create_component::<ItemCard>(d);
let item_card = item_card.widget();
self.widgets.search_result_box.add(item_card);
antoyo commented 3 years ago

You can also import the trait ContainerWidget and call the method add_widget().