pub trait ComponentController<C: Component> {
// Required methods
fn sender(&self) -> &Sender<C::Input>;
fn state(&self) -> &StateWatcher<C>;
fn widget(&self) -> &C::Root;
fn detach_runtime(&mut self);
// Provided methods
fn emit(&self, event: C::Input) { ... }
fn model(&self) -> Ref<'_, C> { ... }
fn widgets(&self) -> Ref<'_, C::Widgets> { ... }
}Expand description
Shared behavior of component controller types.
Required Methods§
Sourcefn state(&self) -> &StateWatcher<C>
fn state(&self) -> &StateWatcher<C>
Provides access to the state of a component.
Sourcefn detach_runtime(&mut self)
fn detach_runtime(&mut self)
Dropping this type will usually stop the runtime of the component. With this method you can give the runtime a static lifetime. In other words, dropping the controller or connector will not stop the runtime anymore, instead it will run until the app is closed.
Provided Methods§
Sourcefn emit(&self, event: C::Input)
fn emit(&self, event: C::Input)
Emits an input to the component.
Examples found in repository?
More examples
relm4/examples/components.rs (line 211)
205 fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
206 match msg {
207 AppMsg::SetMode(mode) => {
208 self.mode = mode;
209 }
210 AppMsg::CloseRequest => {
211 self.dialog.emit(DialogMsg::Show);
212 }
213 AppMsg::Close => {
214 relm4::main_application().quit();
215 }
216 }
217 }relm4-components/examples/alert.rs (line 82)
68 fn update(&mut self, msg: AppMsg, _sender: ComponentSender<Self>) {
69 match msg {
70 AppMsg::Increment => {
71 self.counter = self.counter.wrapping_add(1);
72 }
73 AppMsg::Decrement => {
74 self.counter = self.counter.wrapping_sub(1);
75 }
76 AppMsg::CloseRequest => {
77 if self.counter == 42 {
78 relm4::main_application().quit();
79 } else {
80 self.alert_toggle = !self.alert_toggle;
81 if self.alert_toggle {
82 self.dialog.emit(AlertMsg::Show);
83 } else {
84 self.second_dialog.emit(AlertMsg::Show);
85 }
86 }
87 }
88 AppMsg::Save => {
89 println!("* Open save dialog here *");
90 }
91 AppMsg::Close => {
92 relm4::main_application().quit();
93 }
94 AppMsg::Ignore => (),
95 }
96 }relm4-components/examples/file_dialogs.rs (line 133)
131 fn update(&mut self, message: Self::Input, sender: ComponentSender<Self>) {
132 match message {
133 Input::OpenRequest => self.open_dialog.emit(OpenDialogMsg::Open),
134 Input::OpenResponse(path) => match std::fs::read_to_string(&path) {
135 Ok(contents) => {
136 self.buffer.set_text(&contents);
137 self.file_name = Some(
138 path.file_name()
139 .expect("The path has no file name")
140 .to_str()
141 .expect("Cannot convert file name to string")
142 .to_string(),
143 );
144 }
145 Err(e) => sender.input(Input::ShowMessage(e.to_string())),
146 },
147 Input::SaveRequest => self
148 .save_dialog
149 .emit(SaveDialogMsg::SaveAs(self.file_name.clone().unwrap())),
150 Input::SaveResponse(path) => match std::fs::write(
151 &path,
152 self.buffer
153 .text(&self.buffer.start_iter(), &self.buffer.end_iter(), false),
154 ) {
155 Ok(_) => {
156 sender.input(Input::ShowMessage(format!(
157 "File saved successfully at {path:?}"
158 )));
159 self.buffer.set_text("");
160 self.file_name = None;
161 }
162 Err(e) => sender.input(Input::ShowMessage(e.to_string())),
163 },
164 Input::ShowMessage(message) => {
165 self.message = Some(message);
166 }
167 Input::ResetMessage => {
168 self.message = None;
169 }
170 Input::Ignore => {}
171 }
172 }relm4/examples/state_management.rs (line 487)
484 fn update(&mut self, msg: AppInput, _sender: ComponentSender<Self>) {
485 match msg {
486 AppInput::Clear => {
487 self.document.emit(DocumentInput::Clear);
488 }
489 AppInput::Cleared => {
490 self.view.guard().clear();
491 }
492 AppInput::AddTask => {
493 self.document.emit(DocumentInput::AddTask);
494 }
495 AppInput::AddedTask => {
496 self.view.guard().push_back(());
497 }
498 AppInput::DeleteTask(index) => {
499 self.document.emit(DocumentInput::DeleteTask(index));
500 }
501 AppInput::DeletedTask(index) => {
502 self.view.guard().remove(index);
503 }
504 AppInput::ChangeTaskName(index, name) => {
505 self.document
506 .emit(DocumentInput::ChangeTaskName(index, name));
507 }
508 AppInput::ChangedTaskName(index, name) => {
509 self.view.guard().send(index, TaskInput::ChangedName(name));
510 }
511 AppInput::AddTag(index, name) => {
512 self.document.emit(DocumentInput::AddTag(index, name));
513 }
514 AppInput::AddedTag(index, name) => {
515 self.view.guard().send(index, TaskInput::AddedTag(name));
516 }
517 AppInput::DeleteTag(task_index, tag_index) => {
518 self.document
519 .emit(DocumentInput::DeleteTag(task_index, tag_index));
520 }
521 AppInput::DeletedTag(task_index, tag_index) => {
522 self.view
523 .guard()
524 .send(task_index, TaskInput::DeletedTag(tag_index));
525 }
526 AppInput::None => {}
527 AppInput::Save => {
528 let name = "tasks.json".into();
529 self.save_dialog.emit(SaveDialogMsg::SaveAs(name));
530 }
531 AppInput::SaveResponse(path) => {
532 self.document.emit(DocumentInput::Save(path));
533 }
534 AppInput::Open => {
535 self.open_dialog.emit(OpenDialogMsg::Open);
536 }
537 AppInput::OpenResponse(path) => {
538 self.document.emit(DocumentInput::Open(path));
539 }
540 }
541 }Sourcefn widgets(&self) -> Ref<'_, C::Widgets>
fn widgets(&self) -> Ref<'_, C::Widgets>
Returns a reference to the Component::Widgets.