Struct relm4::RelmComponent
source · [−]pub struct RelmComponent<Model, ParentModel> where
Model: ComponentUpdate<ParentModel> + 'static,
ParentModel: ModelTrait,
Model::Widgets: WidgetsTrait<Model, ParentModel> + 'static, { /* private fields */ }
Expand description
A component that can be part of the main application or other components.
A RelmComponent
has its own widget, model and message type
and can send messages to its parent and its children components.
Multiple RelmComponent
s that have the same parent are usually bundled in a struct that implements Components
.
Implementations
sourceimpl<Model, ParentModel> RelmComponent<Model, ParentModel> where
Model::Widgets: WidgetsTrait<Model, ParentModel> + 'static,
ParentModel: ModelTrait,
Model: ComponentUpdate<ParentModel> + 'static,
impl<Model, ParentModel> RelmComponent<Model, ParentModel> where
Model::Widgets: WidgetsTrait<Model, ParentModel> + 'static,
ParentModel: ModelTrait,
Model: ComponentUpdate<ParentModel> + 'static,
sourcepub fn new(
parent_model: &ParentModel,
parent_sender: Sender<ParentModel::Msg>
) -> Self
pub fn new(
parent_model: &ParentModel,
parent_sender: Sender<ParentModel::Msg>
) -> Self
Create a new RelmComponent
.
sourcepub fn connect_parent(&mut self, parent_widgets: &ParentModel::Widgets)
pub fn connect_parent(&mut self, parent_widgets: &ParentModel::Widgets)
Connect the widgets to the widgets of the parent widget.
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 component. This can be used by the parent to send messages to this component.
Examples found in repository
193 194 195 196 197 198 199 200 201 202 203 204 205 206
fn update(&mut self, msg: AppMsg, components: &AppComponents, _sender: Sender<AppMsg>) -> bool {
match msg {
AppMsg::SetMode(mode) => {
self.mode = mode;
}
AppMsg::CloseRequest => {
components.dialog.send(DialogMsg::Show).unwrap();
}
AppMsg::Close => {
return false;
}
}
true
}
More examples
229 230 231 232 233 234 235 236 237 238 239 240 241 242
fn update(&mut self, msg: AppMsg, components: &AppComponents, _sender: Sender<AppMsg>) -> bool {
match msg {
AppMsg::Increment => self.counter = self.counter.saturating_add(1),
AppMsg::Decrement => self.counter = self.counter.saturating_sub(1),
AppMsg::ShowComp1 => {
components.comp1.send(CompMsg::Show).unwrap();
}
AppMsg::ShowComp2 => {
components.comp2.send(CompMsg::Show).unwrap();
}
}
println!("counter: {}", self.counter);
true
}
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
fn update(&mut self, msg: AppMsg, components: &AppComponents, _sender: Sender<AppMsg>) -> bool {
match msg {
AppMsg::Increment => {
self.counter = self.counter.wrapping_add(1);
}
AppMsg::Decrement => {
self.counter = self.counter.wrapping_sub(1);
}
AppMsg::SaveRequest => {
components
.dialog
.send(SaveDialogMsg::SaveAs(format!("Counter_{}", self.counter)))
.unwrap();
}
AppMsg::SaveResponse(path) => {
println!("File would have been saved at {:?}", path);
}
}
true
}
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
fn update(&mut self, msg: AppMsg, components: &AppComponents, _sender: Sender<AppMsg>) -> bool {
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 {
return false;
} else {
self.alert_toggle = !self.alert_toggle;
if self.alert_toggle {
components.dialog.send(AlertMsg::Show).unwrap();
} else {
components.second_dialog.send(AlertMsg::Show).unwrap();
}
}
}
AppMsg::Save => {
println!("* Open save dialog here *");
}
AppMsg::Close => {
return false;
}
AppMsg::Ignore => (),
}
true
}
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
fn update(&mut self, msg: AppMsg, components: &AppComponents, _sender: Sender<AppMsg>) -> bool {
match msg {
AppMsg::Plus(v) => {
if self.minus || self.multiply {
self.plus = v;
}
}
AppMsg::Multiply(v) => {
if self.minus || self.plus {
self.multiply = v;
}
}
AppMsg::Minus(v) => {
if self.plus || self.multiply {
self.minus = v;
}
}
AppMsg::MaxValue(v) => {
self.range = v;
self.calculate_task();
}
AppMsg::Entry(v) => {
if self.correct_value == v as u32 {
self.feedback = "<big>😀 That was right!! 💓</big>".to_string();
self.pick_random_task_type();
self.calculate_task();
self.correct_calculations += 1;
} else {
self.feedback = "<big>😕 Unfortunately wrong. 😓</big>".to_string();
self.incorrect_calculations += 1;
}
}
AppMsg::EntryError => {
self.feedback = "<big>Please enter a valid number.</big>".to_string();
}
AppMsg::SetTimer(v) => {
self.timer_init_value = v;
}
AppMsg::StartTimer => {
// if timer is running a press on the button will reset button
// otherwise initialize the timer with the selected init value
components.timer_handler.send(TimerMsg::StartStopTimer);
if self.timer.is_some() {
self.timer = None;
} else {
self.pick_random_task_type();
self.calculate_task();
self.timer = Some(self.timer_init_value);
self.correct_calculations = 0;
self.incorrect_calculations = 0;
}
}
AppMsg::CountDown => {
if let Some(t) = &mut self.timer {
*t -= 1;
// check if time is up
if *t == 0 {
self.timer = None;
// stop timer
components.timer_handler.send(TimerMsg::StartStopTimer);
// display results
components
.dialog
.send(DialogMsg::Show(
self.correct_calculations,
self.incorrect_calculations,
))
.unwrap();
}
}
}
}
true
}
sourcepub fn root_widget(
&self
) -> &<Model::Widgets as WidgetsTrait<Model, ParentModel>>::Root
pub fn root_widget(
&self
) -> &<Model::Widgets as WidgetsTrait<Model, ParentModel>>::Root
Returns the root widget of this component’s widgets. Can be used by the parent to connect the root widget to the parent’s widgets.
Examples found in repository
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
fn init_view(model: &AppModel, components: &AppComponents, sender: Sender<AppMsg>) -> Self {
let main = gtk::ApplicationWindow::default();
let vbox = gtk::Box::builder()
.orientation(gtk::Orientation::Vertical)
.spacing(10)
.build();
vbox.set_margin_all(5);
let text = gtk::Label::new(Some(&model.counter.to_string()));
let inc_button = gtk::Button::with_label("Increment");
let dec_button = gtk::Button::with_label("Decrement");
vbox.append(&text);
vbox.append(&inc_button);
vbox.append(&dec_button);
main.set_child(Some(&vbox));
let sender2 = sender.clone();
inc_button.connect_clicked(move |_button| {
sender.send(AppMsg::Increment).unwrap();
});
dec_button.connect_clicked(move |_button| {
sender2.send(AppMsg::Decrement).unwrap();
});
vbox.append(components.comp1.root_widget());
vbox.append(components.comp2.root_widget());
AppWidgets { main, text, vbox }
}
sourcepub fn widgets(&self) -> Option<RefMut<'_, Model::Widgets>>
pub fn widgets(&self) -> Option<RefMut<'_, Model::Widgets>>
Returns a mutable reference to the widgets of this component or None
if
you already have a refernce 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).
Trait Implementations
sourceimpl<Model: Debug, ParentModel: Debug> Debug for RelmComponent<Model, ParentModel> where
Model: ComponentUpdate<ParentModel> + 'static,
ParentModel: ModelTrait,
Model::Widgets: WidgetsTrait<Model, ParentModel> + 'static,
Model::Components: Debug,
Model::Msg: Debug,
Model::Widgets: Debug,
Model::Widgets: Debug,
impl<Model: Debug, ParentModel: Debug> Debug for RelmComponent<Model, ParentModel> where
Model: ComponentUpdate<ParentModel> + 'static,
ParentModel: ModelTrait,
Model::Widgets: WidgetsTrait<Model, ParentModel> + 'static,
Model::Components: Debug,
Model::Msg: Debug,
Model::Widgets: Debug,
Model::Widgets: Debug,
Auto Trait Implementations
impl<Model, ParentModel> !RefUnwindSafe for RelmComponent<Model, ParentModel>
impl<Model, ParentModel> !Send for RelmComponent<Model, ParentModel>
impl<Model, ParentModel> !Sync for RelmComponent<Model, ParentModel>
impl<Model, ParentModel> Unpin for RelmComponent<Model, ParentModel> where
Model: Unpin,
ParentModel: Unpin,
<<Model as Model>::Widgets as Widgets<Model, ParentModel>>::Root: Unpin,
impl<Model, ParentModel> !UnwindSafe for RelmComponent<Model, ParentModel>
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