use orbtk::prelude::*; //|---------------------------------------------------------------------------| //|------------------------------SENDER---------------------------------------| //|---------------------------------------------------------------------------| // View widget!(SenderWidget { // the Entity of the widget that will receive the messages target: u32 }); impl Template for SenderWidget { fn template(self, id: Entity, bc: &mut BuildContext) -> Self { self.name("SenderWidget").child( Button::new() .text("Click me to send a message!") .v_align("center") .h_align("center") .on_click(move |states, _entity| { states.get_mut::(id).send_message(); false }) .build(bc), ) } } // States enum Action { UpdateProgress(f64), } #[derive(Default, AsAny)] struct SenderState { actions: Vec, target: Entity, } impl SenderState { fn send_message(&mut self) { self.actions.push(Action::UpdateProgress(0.1)); } } impl State for SenderState { fn init(&mut self, _: &mut Registry, ctx: &mut Context) { self.target = Entity::from( ctx.widget() .try_clone::("target") .expect("ERROR: SenderState::init(): target entity not found!"), ); } fn update(&mut self, _: &mut Registry, ctx: &mut Context) { let actions: Vec = self.actions.drain(..).collect(); for action in actions { match action { Action::UpdateProgress(amount) => { ctx.send_message(Action::UpdateProgress(amount), self.target); println!("Message sent!"); } } } } } //|---------------------------------------------------------------------------| //|------------------------------RECEIVER-------------------------------------| //|---------------------------------------------------------------------------| // View widget!(ReceiverWidget); impl Template for ReceiverWidget { fn template(self, _id: Entity, bc: &mut BuildContext) -> Self { self.name("ReceiverWidget") .child(ProgressBar::new().id("progress_bar").build(bc)) } } // States #[derive(Default, AsAny)] struct ReceiverState { progress_bar: Entity, } impl State for ReceiverState { fn init(&mut self, _: &mut Registry, ctx: &mut Context) { self.progress_bar = ctx .entity_of_child("progress_bar") .expect("Cannot find ProgressBar!"); } fn messages( &mut self, mut messages: MessageReader, _registry: &mut Registry, ctx: &mut Context, ) { for action in messages.read::() { match action { Action::UpdateProgress(amount) => { println!("Message received"); let mut progress_bar = ctx.get_widget(self.progress_bar); let current_progress = progress_bar.clone::("val"); progress_bar.set::("val", current_progress + amount); } } } } } //|---------------------------------------------------------------------------| //|--------------------------------MAIN---------------------------------------| //|---------------------------------------------------------------------------| pub fn main() { Application::new() .window(|ctx| { let receiver = ReceiverWidget::new().build(ctx); let sender = SenderWidget::new() // the entity of the target (receiver) .target(receiver.0) .build(ctx); Window::new() .title("Messages example") .position((100.0, 100.0)) .resizeable(true) .size(450.0, 500.0) .child( Stack::new() .orientation("vertical") .child(sender) .child(receiver) .build(ctx), ) .build(ctx) }) .run(); }