Struct relm4::MicroComponent
source · [−]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
sourceimpl<Model> MicroComponent<Model> where
Model::Widgets: MicroWidgets<Model> + 'static,
Model::Msg: 'static,
Model::Data: 'static,
Model: MicroModel + 'static,
impl<Model> MicroComponent<Model> where
Model::Widgets: MicroWidgets<Model> + 'static,
Model::Msg: 'static,
Model::Data: 'static,
Model: MicroModel + 'static,
sourcepub fn new(model: Model, data: Model::Data) -> Self
pub fn new(model: Model, data: Model::Data) -> Self
Creates new MicroComponent
Examples found in repository
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
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
}
sourcepub fn update_view(&self) -> Result<(), MicroComponentError>
pub fn update_view(&self) -> Result<(), MicroComponentError>
Updates a view of this MicroComponent
Examples found in repository
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
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
}
sourcepub fn model(&self) -> Result<Ref<'_, Model>, BorrowError>
pub fn model(&self) -> Result<Ref<'_, Model>, BorrowError>
Returns model for this MicroComponent
Use this carefully and make sure reference is dropped. It’s using RefCell
internally.
sourcepub fn model_mut(&self) -> Result<RefMut<'_, Model>, BorrowMutError>
pub fn model_mut(&self) -> Result<RefMut<'_, Model>, BorrowMutError>
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
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
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
}
sourcepub fn widgets(&self) -> Result<RefMut<'_, Model::Widgets>, BorrowMutError>
pub fn widgets(&self) -> Result<RefMut<'_, Model::Widgets>, BorrowMutError>
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).
sourcepub fn send(&self, msg: Model::Msg) -> Result<(), SendError<Model::Msg>>
pub fn send(&self, msg: Model::Msg) -> Result<(), SendError<Model::Msg>>
Send a message to this MicroComponent
.
This can be used by the parent to send messages to this.
sourcepub fn sender(&self) -> Sender<Model::Msg>
pub fn sender(&self) -> Sender<Model::Msg>
Get a sender to send messages to this MicroComponent
.
sourcepub fn root_widget(&self) -> &<Model::Widgets as MicroWidgets<Model>>::Root
pub fn root_widget(&self) -> &<Model::Widgets as MicroWidgets<Model>>::Root
Returns the root widget of this component’s widgets.
sourceimpl<Model> MicroComponent<Model> where
Model::Widgets: MicroWidgets<Model> + 'static,
Model::Msg: 'static,
Model::Data: 'static,
Model: MicroModel + 'static,
<Model::Widgets as MicroWidgets<Model>>::Root: IsA<Widget>,
impl<Model> MicroComponent<Model> where
Model::Widgets: MicroWidgets<Model> + 'static,
Model::Msg: 'static,
Model::Data: 'static,
Model: MicroModel + 'static,
<Model::Widgets as MicroWidgets<Model>>::Root: IsA<Widget>,
sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Returns true
of the root widget is connected to a parent widget.
sourcepub fn try_diconnect_root(&self) -> bool
pub fn try_diconnect_root(&self) -> bool
Trait Implementations
Auto Trait Implementations
impl<Model> !RefUnwindSafe for MicroComponent<Model>
impl<Model> !Send for MicroComponent<Model>
impl<Model> !Sync for MicroComponent<Model>
impl<Model> Unpin for MicroComponent<Model> where
<<Model as MicroModel>::Widgets as MicroWidgets<Model>>::Root: Unpin,
impl<Model> !UnwindSafe for MicroComponent<Model>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more