logo
pub struct RelmMsgHandler<Data, ParentModel>where
    Data: MessageHandler<ParentModel> + 'static,
    ParentModel: Model,
{ /* private fields */ }
Expand description

RelmMsgHandlers are usually used to run expansive tasks on different threads and report back when they are finished so that their parent components can keep handling UI events in the meantime. For example you could use a RelmMsgHandler for sending a HTTP request or for copying files.

Multiple RelmMsgHandlers that have the same parent are usually bundled along with RelmComponents and RelmWorkers in a struct that implements Components.

Implementations

Create a new RelmMsgHandler.

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

Examples found in repository?
relm4-examples/libadwaita/examples/calc-trainer.rs (line 267)
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 message handler.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
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 resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
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.