logo
pub struct MicroComponent<Model: MicroModel> { /* private fields */ }
Expand description

MicroComponent is a small component that lives in their parents model, can be modified from their parents model but at the same time have their own widgets and update function

Implementations

Creates new MicroComponent

Examples found in repository
relm4-examples/examples/micro_components_manual.rs (line 110)
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Increment => {
                self.counter = self.counter.wrapping_add(1);

                // Also increment the numbers.
                for num in &self.numbers {
                    let mut model = num.model_mut().unwrap();
                    model.num = model.num.wrapping_add(1);

                    // Make sure to drop the mutable reference before updating the view
                    drop(model);
                    num.update_view().unwrap();
                }
            }
            AppMsg::Decrement => {
                self.counter = self.counter.wrapping_sub(1);
            }
            AppMsg::AddNumber => {
                self.numbers
                    .push(MicroComponent::new(NumberModel { num: self.counter }, ()));
            }
        }
        true
    }
More examples
relm4-examples/examples/micro_components.rs (line 96)
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Increment => {
                self.counter = self.counter.wrapping_add(1);

                // Also increment the numbers.
                for num in &self.numbers {
                    let mut model = num.model_mut().unwrap();
                    model.num = model.num.wrapping_add(1);

                    // Make sure to drop the mutable reference before updating the view
                    drop(model);
                    num.update_view().unwrap();
                }
            }
            AppMsg::Decrement => {
                self.counter = self.counter.wrapping_sub(1);
            }
            AppMsg::AddNumber => {
                self.numbers
                    .push(MicroComponent::new(NumberModel { num: self.counter }, ()));
            }
        }
        true
    }

Updates a view of this MicroComponent

Examples found in repository
relm4-examples/examples/micro_components_manual.rs (line 102)
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Increment => {
                self.counter = self.counter.wrapping_add(1);

                // Also increment the numbers.
                for num in &self.numbers {
                    let mut model = num.model_mut().unwrap();
                    model.num = model.num.wrapping_add(1);

                    // Make sure to drop the mutable reference before updating the view
                    drop(model);
                    num.update_view().unwrap();
                }
            }
            AppMsg::Decrement => {
                self.counter = self.counter.wrapping_sub(1);
            }
            AppMsg::AddNumber => {
                self.numbers
                    .push(MicroComponent::new(NumberModel { num: self.counter }, ()));
            }
        }
        true
    }
More examples
relm4-examples/examples/micro_components.rs (line 88)
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Increment => {
                self.counter = self.counter.wrapping_add(1);

                // Also increment the numbers.
                for num in &self.numbers {
                    let mut model = num.model_mut().unwrap();
                    model.num = model.num.wrapping_add(1);

                    // Make sure to drop the mutable reference before updating the view
                    drop(model);
                    num.update_view().unwrap();
                }
            }
            AppMsg::Decrement => {
                self.counter = self.counter.wrapping_sub(1);
            }
            AppMsg::AddNumber => {
                self.numbers
                    .push(MicroComponent::new(NumberModel { num: self.counter }, ()));
            }
        }
        true
    }

Returns model for this MicroComponent

Use this carefully and make sure reference is dropped. It’s using RefCell internally.

Returns mutable reference to model for this MicroComponent

Use this carefully and make sure reference is dropped. It’s using RefCell internally. If you don’t drop the reference any call to MicroComponent::update_view will fail.

Examples found in repository
relm4-examples/examples/micro_components_manual.rs (line 97)
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Increment => {
                self.counter = self.counter.wrapping_add(1);

                // Also increment the numbers.
                for num in &self.numbers {
                    let mut model = num.model_mut().unwrap();
                    model.num = model.num.wrapping_add(1);

                    // Make sure to drop the mutable reference before updating the view
                    drop(model);
                    num.update_view().unwrap();
                }
            }
            AppMsg::Decrement => {
                self.counter = self.counter.wrapping_sub(1);
            }
            AppMsg::AddNumber => {
                self.numbers
                    .push(MicroComponent::new(NumberModel { num: self.counter }, ()));
            }
        }
        true
    }
More examples
relm4-examples/examples/micro_components.rs (line 83)
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Increment => {
                self.counter = self.counter.wrapping_add(1);

                // Also increment the numbers.
                for num in &self.numbers {
                    let mut model = num.model_mut().unwrap();
                    model.num = model.num.wrapping_add(1);

                    // Make sure to drop the mutable reference before updating the view
                    drop(model);
                    num.update_view().unwrap();
                }
            }
            AppMsg::Decrement => {
                self.counter = self.counter.wrapping_sub(1);
            }
            AppMsg::AddNumber => {
                self.numbers
                    .push(MicroComponent::new(NumberModel { num: self.counter }, ()));
            }
        }
        true
    }

Returns a mutable reference to the widgets of this MicroComponent or will fail when 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).

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

Get a sender to send messages to this MicroComponent.

Returns the root widget of this component’s widgets.

Returns true of the root widget is connected to a parent widget.

Tries to disconnect the root widget from its parent widget.

Returns true of the root widget was disconnected from the parent widget and false if nothing was done.

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.