logo
pub struct RelmComponent<Model, ParentModel>where
    Model: ComponentUpdate<ParentModel> + 'static,
    ParentModel: ModelTrait,
    Model::Widgets: WidgetsTrait<Model, ParentModel> + 'static,
{ /* private fields */ }
Expand description

A component that can be part of the main application or other components.

A RelmComponent has its own widget, model and message type and can send messages to its parent and its children components.

Multiple RelmComponents that have the same parent are usually bundled in a struct that implements Components.

Implementations

Create a new RelmComponent.

Examples found in repository?
relm4-examples/examples/components_old.rs (line 146)
144
145
146
147
148
149
    fn init_components(parent_model: &AppModel, parent_sender: Sender<AppMsg>) -> Self {
        AppComponents {
            comp1: RelmComponent::new(parent_model, parent_sender.clone()),
            comp2: RelmComponent::new(parent_model, parent_sender),
        }
    }

Connect the widgets to the widgets of the parent widget.

Examples found in repository?
relm4-examples/examples/components_old.rs (line 152)
151
152
153
154
    fn connect_parent(&mut self, parent_widgets: &AppWidgets) {
        self.comp1.connect_parent(parent_widgets);
        self.comp2.connect_parent(parent_widgets);
    }

Send a message to this component. This can be used by the parent to send messages to this component.

Examples found in repository?
relm4-examples/examples/components.rs (line 199)
193
194
195
196
197
198
199
200
201
202
203
204
205
206
    fn update(&mut self, msg: AppMsg, components: &AppComponents, _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::SetMode(mode) => {
                self.mode = mode;
            }
            AppMsg::CloseRequest => {
                components.dialog.send(DialogMsg::Show).unwrap();
            }
            AppMsg::Close => {
                return false;
            }
        }
        true
    }
More examples
Hide additional examples
relm4-examples/examples/components_old.rs (line 234)
229
230
231
232
233
234
235
236
237
238
239
240
241
242
    fn update(&mut self, msg: AppMsg, components: &AppComponents, _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Increment => self.counter = self.counter.saturating_add(1),
            AppMsg::Decrement => self.counter = self.counter.saturating_sub(1),
            AppMsg::ShowComp1 => {
                components.comp1.send(CompMsg::Show).unwrap();
            }
            AppMsg::ShowComp2 => {
                components.comp2.send(CompMsg::Show).unwrap();
            }
        }
        println!("counter: {}", self.counter);
        true
    }
relm4-examples/examples/save_dialog.rs (line 40)
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    fn update(&mut self, msg: AppMsg, components: &AppComponents, _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Increment => {
                self.counter = self.counter.wrapping_add(1);
            }
            AppMsg::Decrement => {
                self.counter = self.counter.wrapping_sub(1);
            }
            AppMsg::SaveRequest => {
                components
                    .dialog
                    .send(SaveDialogMsg::SaveAs(format!("Counter_{}", self.counter)))
                    .unwrap();
            }
            AppMsg::SaveResponse(path) => {
                println!("File would have been saved at {:?}", path);
            }
        }
        true
    }
relm4-examples/examples/alert.rs (line 42)
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    fn update(&mut self, msg: AppMsg, components: &AppComponents, _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Increment => {
                self.counter = self.counter.wrapping_add(1);
            }
            AppMsg::Decrement => {
                self.counter = self.counter.wrapping_sub(1);
            }
            AppMsg::CloseRequest => {
                if self.counter == 42 {
                    return false;
                } else {
                    self.alert_toggle = !self.alert_toggle;
                    if self.alert_toggle {
                        components.dialog.send(AlertMsg::Show).unwrap();
                    } else {
                        components.second_dialog.send(AlertMsg::Show).unwrap();
                    }
                }
            }
            AppMsg::Save => {
                println!("* Open save dialog here *");
            }
            AppMsg::Close => {
                return false;
            }
            AppMsg::Ignore => (),
        }

        true
    }
relm4-examples/libadwaita/examples/calc-trainer.rs (lines 290-293)
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
    fn update(&mut self, msg: AppMsg, components: &AppComponents, _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Plus(v) => {
                if self.minus || self.multiply {
                    self.plus = v;
                }
            }
            AppMsg::Multiply(v) => {
                if self.minus || self.plus {
                    self.multiply = v;
                }
            }
            AppMsg::Minus(v) => {
                if self.plus || self.multiply {
                    self.minus = v;
                }
            }
            AppMsg::MaxValue(v) => {
                self.range = v;
                self.calculate_task();
            }
            AppMsg::Entry(v) => {
                if self.correct_value == v as u32 {
                    self.feedback = "<big>😀 That was right!! 💓</big>".to_string();
                    self.pick_random_task_type();
                    self.calculate_task();
                    self.correct_calculations += 1;
                } else {
                    self.feedback = "<big>😕 Unfortunately wrong. 😓</big>".to_string();
                    self.incorrect_calculations += 1;
                }
            }
            AppMsg::EntryError => {
                self.feedback = "<big>Please enter a valid number.</big>".to_string();
            }
            AppMsg::SetTimer(v) => {
                self.timer_init_value = v;
            }
            AppMsg::StartTimer => {
                // if timer is running a press on the button will reset button
                // otherwise initialize the timer with the selected init value
                components.timer_handler.send(TimerMsg::StartStopTimer);
                if self.timer.is_some() {
                    self.timer = None;
                } else {
                    self.pick_random_task_type();
                    self.calculate_task();
                    self.timer = Some(self.timer_init_value);
                    self.correct_calculations = 0;
                    self.incorrect_calculations = 0;
                }
            }
            AppMsg::CountDown => {
                if let Some(t) = &mut self.timer {
                    *t -= 1;

                    // check if time is up
                    if *t == 0 {
                        self.timer = None;
                        // stop timer
                        components.timer_handler.send(TimerMsg::StartStopTimer);
                        // display results
                        components
                            .dialog
                            .send(DialogMsg::Show(
                                self.correct_calculations,
                                self.incorrect_calculations,
                            ))
                            .unwrap();
                    }
                }
            }
        }
        true
    }

Get a sender to send messages to this component.

Returns the root widget of this component’s widgets. Can be used by the parent to connect the root widget to the parent’s widgets.

Examples found in repository?
relm4-examples/examples/components_old.rs (line 213)
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
    fn init_view(model: &AppModel, components: &AppComponents, sender: Sender<AppMsg>) -> Self {
        let main = gtk::ApplicationWindow::default();
        let vbox = gtk::Box::builder()
            .orientation(gtk::Orientation::Vertical)
            .spacing(10)
            .build();
        vbox.set_margin_all(5);

        let text = gtk::Label::new(Some(&model.counter.to_string()));

        let inc_button = gtk::Button::with_label("Increment");
        let dec_button = gtk::Button::with_label("Decrement");

        vbox.append(&text);
        vbox.append(&inc_button);
        vbox.append(&dec_button);

        main.set_child(Some(&vbox));

        let sender2 = sender.clone();

        inc_button.connect_clicked(move |_button| {
            sender.send(AppMsg::Increment).unwrap();
        });

        dec_button.connect_clicked(move |_button| {
            sender2.send(AppMsg::Decrement).unwrap();
        });

        vbox.append(components.comp1.root_widget());
        vbox.append(components.comp2.root_widget());

        AppWidgets { main, text, vbox }
    }

Returns a mutable reference to the widgets of this component or None if you already have a reference to the widgets.

Use this carefully and make sure the reference to the widgets is dropped after use because otherwise the view function can’t be called as long you own the widgets (it uses RefCell internally).

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.