pub trait ComponentController<C: Component> {
    fn sender(&self) -> &Sender<C::Input>;
    fn state(&self) -> &StateWatcher<C>;
    fn widget(&self) -> &C::Root;
    fn detach_runtime(&mut self);

    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§

Provides access to the component’s sender.

Provides access to the state of a component.

Returns the root widget of the component.

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§

Emits an input to the component.

Examples found in repository?
examples/message_broker.rs (line 212)
206
207
208
209
210
211
212
213
214
215
    fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
        match msg {
            AppMsg::SetMode(mode) => {
                self.mode = mode;
            }
            AppMsg::ShowDialog => {
                self.dialog.emit(DialogMsg::Show);
            }
        }
    }
More examples
Hide additional examples
examples/components.rs (line 207)
201
202
203
204
205
206
207
208
209
210
211
212
213
    fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
        match msg {
            AppMsg::SetMode(mode) => {
                self.mode = mode;
            }
            AppMsg::CloseRequest => {
                self.dialog.emit(DialogMsg::Show);
            }
            AppMsg::Close => {
                relm4::main_application().quit();
            }
        }
    }
examples/relm4-components/alert.rs (line 81)
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    fn update(&mut self, msg: AppMsg, _sender: ComponentSender<Self>) {
        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 {
                    relm4::main_application().quit();
                } else {
                    self.alert_toggle = !self.alert_toggle;
                    if self.alert_toggle {
                        self.dialog.emit(AlertMsg::Show);
                    } else {
                        self.second_dialog.emit(AlertMsg::Show);
                    }
                }
            }
            AppMsg::Save => {
                println!("* Open save dialog here *");
            }
            AppMsg::Close => {
                relm4::main_application().quit();
            }
            AppMsg::Ignore => (),
        }
    }
examples/relm4-components/file_dialogs.rs (line 131)
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    fn update(&mut self, message: Self::Input, sender: ComponentSender<Self>) {
        match message {
            Input::OpenRequest => self.open_dialog.emit(OpenDialogMsg::Open),
            Input::OpenResponse(path) => match std::fs::read_to_string(&path) {
                Ok(contents) => {
                    self.buffer.set_text(&contents);
                    self.file_name = Some(
                        path.file_name()
                            .expect("The path has no file name")
                            .to_str()
                            .expect("Cannot convert file name to string")
                            .to_string(),
                    );
                }
                Err(e) => sender.input(Input::ShowMessage(e.to_string())),
            },
            Input::SaveRequest => self
                .save_dialog
                .emit(SaveDialogMsg::SaveAs(self.file_name.clone().unwrap())),
            Input::SaveResponse(path) => match std::fs::write(
                &path,
                self.buffer
                    .text(&self.buffer.start_iter(), &self.buffer.end_iter(), false),
            ) {
                Ok(_) => {
                    sender.input(Input::ShowMessage(format!(
                        "File saved successfully at {:?}",
                        path
                    )));
                    self.buffer.set_text("");
                    self.file_name = None;
                }
                Err(e) => sender.input(Input::ShowMessage(e.to_string())),
            },
            Input::ShowMessage(message) => {
                self.message = Some(message);
            }
            Input::ResetMessage => {
                self.message = None;
            }
            Input::Ignore => {}
        }
    }

Returns a reference to the Component.

Returns a reference to the Component::Widgets.

Implementors§