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§

source

fn sender(&self) -> &Sender<C::Input>

Provides access to the component’s sender.

source

fn state(&self) -> &StateWatcher<C>

Provides access to the state of a component.

source

fn widget(&self) -> &C::Root

Returns the root widget of the component.

source

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§

source

fn emit(&self, event: C::Input)

Emits an input to the component.

Examples found in repository?
relm4/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
relm4-components/examples/web_image.rs (line 60)
55
56
57
58
59
60
61
62
63
64
    fn update(&mut self, msg: Self::Input, _: ComponentSender<Self>) {
        match msg {
            AppMsg::Next => {
                self.idx = (self.idx + 1) % IMAGES.len();
                self.image
                    .emit(WebImageMsg::LoadImage(IMAGES[self.idx].to_owned()));
            }
            AppMsg::Unload => self.image.emit(WebImageMsg::Unload),
        }
    }
relm4/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();
            }
        }
    }
relm4-components/examples/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 => (),
        }
    }
relm4-components/examples/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
    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 => {}
        }
    }
source

fn model(&self) -> Ref<'_, C>

Returns a reference to the Component.

source

fn widgets(&self) -> Ref<'_, C::Widgets>

Returns a reference to the Component::Widgets.

Implementors§