examples: introduce messages_test
* showcase demenstrating messages passing from a sender (widget1) to a receiver (widget 2) * use modular source tree structure Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
This commit is contained in:
32
advotracker/examples/messages_test/main.rs
Normal file
32
advotracker/examples/messages_test/main.rs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* OrbTK - The Orbital Widget Toolkit
|
||||||
|
*
|
||||||
|
* Copyright 2021 Ralf Zerres <ralf.zerres@networkx.de>
|
||||||
|
* SPDX-License-Identifier: (0BSD or MIT)
|
||||||
|
*/
|
||||||
|
|
||||||
|
use orbtk::prelude::*;
|
||||||
|
|
||||||
|
mod main_view;
|
||||||
|
mod receiver;
|
||||||
|
mod sender;
|
||||||
|
|
||||||
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
// use this only if you want to run it as web application.
|
||||||
|
orbtk::initialize();
|
||||||
|
|
||||||
|
Application::new()
|
||||||
|
.window(|ctx| {
|
||||||
|
Window::new()
|
||||||
|
.name("Main")
|
||||||
|
.title("OrbTK: example send/receive messages")
|
||||||
|
.position((100.0, 100.0))
|
||||||
|
.resizeable(true)
|
||||||
|
.size(450.0, 500.0)
|
||||||
|
.child(main_view::MainView::new().build(ctx))
|
||||||
|
.build(ctx)
|
||||||
|
})
|
||||||
|
.run();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
48
advotracker/examples/messages_test/main_view.rs
Normal file
48
advotracker/examples/messages_test/main_view.rs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* OrbTK - The Orbital Widget Toolkit
|
||||||
|
*
|
||||||
|
* Copyright 2021 Ralf Zerres <ralf.zerres@networkx.de>
|
||||||
|
* SPDX-License-Identifier: (0BSD or MIT)
|
||||||
|
*/
|
||||||
|
|
||||||
|
use orbtk::prelude::*;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
receiver::receiver_view::ReceiverView,
|
||||||
|
sender::sender_view::SenderView,
|
||||||
|
};
|
||||||
|
|
||||||
|
// constants
|
||||||
|
pub static ID_SENDER_VIEW: &str = "sender_view";
|
||||||
|
pub static ID_RECEIVER_VIEW: &str = "receiver_view";
|
||||||
|
|
||||||
|
widget!(MainView {
|
||||||
|
//sender_view: ,
|
||||||
|
//receiver_view:
|
||||||
|
});
|
||||||
|
|
||||||
|
impl Template for MainView {
|
||||||
|
fn template(self, _id: Entity, ctx: &mut BuildContext<'_>) -> Self {
|
||||||
|
let receiver_view = ReceiverView::new()
|
||||||
|
.build(ctx);
|
||||||
|
|
||||||
|
let sender_view = SenderView::new()
|
||||||
|
.target(receiver_view.0) // entity of the target
|
||||||
|
.build(ctx);
|
||||||
|
|
||||||
|
self.name("MainView")
|
||||||
|
.child(
|
||||||
|
Stack::new()
|
||||||
|
.orientation("vertical")
|
||||||
|
.child(sender_view)
|
||||||
|
.child(receiver_view)
|
||||||
|
.build(ctx)
|
||||||
|
)
|
||||||
|
// .child(
|
||||||
|
// TabWidget::new()
|
||||||
|
// .tab(ID_SENDER_VIEW, SenderView::new().build(ctx))
|
||||||
|
// .tab(ID_RECEIVER_VIEW, ReceiverView::new().build(ctx))
|
||||||
|
// .build(ctx),
|
||||||
|
// )
|
||||||
|
}
|
||||||
|
}
|
||||||
8
advotracker/examples/messages_test/mod.rs
Normal file
8
advotracker/examples/messages_test/mod.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
// The starting point (Main View).
|
||||||
|
pub mod main_view;
|
||||||
|
|
||||||
|
/// Receiver widget
|
||||||
|
pub mod receiver;
|
||||||
|
|
||||||
|
/// Sender widget.
|
||||||
|
pub mod sender;
|
||||||
5
advotracker/examples/messages_test/receiver/mod.rs
Normal file
5
advotracker/examples/messages_test/receiver/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
/// The reciever state
|
||||||
|
pub mod receiver_state;
|
||||||
|
|
||||||
|
/// The receiver view
|
||||||
|
pub mod receiver_view;
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* OrbTK - The Orbital Widget Toolkit
|
||||||
|
*
|
||||||
|
* Copyright 2021 Ralf Zerres <ralf.zerres@networkx.de>
|
||||||
|
* SPDX-License-Identifier: (0BSD or MIT)
|
||||||
|
*/
|
||||||
|
|
||||||
|
use orbtk::prelude::*;
|
||||||
|
|
||||||
|
use crate::sender::sender_state::{SenderAction, SenderState};
|
||||||
|
|
||||||
|
/// Enumeration of valid `action variants` that need to be handled as
|
||||||
|
/// state changes for the `SenderView` widget.
|
||||||
|
pub enum TestMessageAction {
|
||||||
|
// Toggle visibility of a message TextBox.
|
||||||
|
ToggleMessageBox
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Valid `structure members` of the `ReceiverState` used to react on
|
||||||
|
/// state changes inside the `ReceiverView` widget.
|
||||||
|
#[derive(Default, AsAny)]
|
||||||
|
pub struct ReceiverState {
|
||||||
|
message_box: Option<Entity>,
|
||||||
|
progress_bar: Entity
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Method definitions, we provide inside the `ReceiverState`.
|
||||||
|
impl ReceiverState {
|
||||||
|
fn toggle_message_box(&self, ctx: &mut Context) {
|
||||||
|
if let Some(message_box) = self.message_box {
|
||||||
|
ctx.get_widget(message_box)
|
||||||
|
.set("visibility", Visibility::Visible);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Trait methods provided for the `SenderState`
|
||||||
|
impl State for ReceiverState {
|
||||||
|
// initialize the view entities
|
||||||
|
fn init(&mut self, _: &mut Registry, ctx: &mut Context) {
|
||||||
|
self.progress_bar = ctx.entity_of_child("progress_bar")
|
||||||
|
.expect("Cannot find ProgressBar!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle messages targeting the view
|
||||||
|
fn messages(
|
||||||
|
&mut self,
|
||||||
|
mut messages: MessageReader,
|
||||||
|
_registry: &mut Registry,
|
||||||
|
ctx: &mut Context
|
||||||
|
) {
|
||||||
|
for message in messages.read::<SenderAction>() {
|
||||||
|
match message {
|
||||||
|
SenderAction::UpdateProgress(amount) => {
|
||||||
|
println!("Message received");
|
||||||
|
let mut progress_bar = ctx.get_widget(self.progress_bar);
|
||||||
|
let current_progress = progress_bar.clone::<f64>("val");
|
||||||
|
progress_bar.set::<f64>("val", current_progress + amount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for action in messages.read::<TestMessageAction>() {
|
||||||
|
match action {
|
||||||
|
TestMessageAction::ToggleMessageBox => {
|
||||||
|
self.toggle_message_box(ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
37
advotracker/examples/messages_test/receiver/receiver_view.rs
Normal file
37
advotracker/examples/messages_test/receiver/receiver_view.rs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* OrbTK - The Orbital Widget Toolkit
|
||||||
|
*
|
||||||
|
* Copyright 2021 Ralf Zerres <ralf.zerres@networkx.de>
|
||||||
|
* SPDX-License-Identifier: (0BSD or MIT)
|
||||||
|
*/
|
||||||
|
|
||||||
|
use orbtk::prelude::*;
|
||||||
|
|
||||||
|
use crate::receiver::receiver_state::{TestMessageAction, ReceiverState};
|
||||||
|
|
||||||
|
widget!(ReceiverView<ReceiverState>);
|
||||||
|
|
||||||
|
impl Template for ReceiverView {
|
||||||
|
fn template(self, _id: Entity, bc: &mut BuildContext) -> Self {
|
||||||
|
self.name("ReceiverView")
|
||||||
|
.child(
|
||||||
|
Stack::new()
|
||||||
|
.orientation("vertical")
|
||||||
|
.spacing(16)
|
||||||
|
.child(
|
||||||
|
ProgressBar::new()
|
||||||
|
.id("progress_bar")
|
||||||
|
.build(bc)
|
||||||
|
)
|
||||||
|
.child(
|
||||||
|
TextBox::new()
|
||||||
|
.id("message_box")
|
||||||
|
.h_align("center")
|
||||||
|
.text("message received. Box toggled!")
|
||||||
|
.visibility("hidden")
|
||||||
|
.build(bc)
|
||||||
|
)
|
||||||
|
.build(bc)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
5
advotracker/examples/messages_test/sender/mod.rs
Normal file
5
advotracker/examples/messages_test/sender/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
/// The sender state
|
||||||
|
pub mod sender_state;
|
||||||
|
|
||||||
|
/// The sender view
|
||||||
|
pub mod sender_view;
|
||||||
58
advotracker/examples/messages_test/sender/sender_state.rs
Normal file
58
advotracker/examples/messages_test/sender/sender_state.rs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* OrbTK - The Orbital Widget Toolkit
|
||||||
|
*
|
||||||
|
* Copyright 2021 Ralf Zerres <ralf.zerres@networkx.de>
|
||||||
|
* SPDX-License-Identifier: (0BSD or MIT)
|
||||||
|
*/
|
||||||
|
|
||||||
|
use orbtk::prelude::*;
|
||||||
|
|
||||||
|
/// Enumeration of valid `action variants` that need to be handled as
|
||||||
|
/// state changes for the `SenderView` widget.
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub enum SenderAction {
|
||||||
|
UpdateProgress(f64),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Valid `structure members` of the `SenderState` used to react on
|
||||||
|
/// state changes inside the `SenderView` widget.
|
||||||
|
#[derive(AsAny, Default)]
|
||||||
|
pub struct SenderState {
|
||||||
|
// actions
|
||||||
|
actions: Vec<SenderAction>,
|
||||||
|
// entity that will receive the message
|
||||||
|
target: Entity
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Method definitions, we provide inside the `SenderState`.
|
||||||
|
impl SenderState {
|
||||||
|
/// Sending message 'UpdateProgress'
|
||||||
|
pub fn send_message(&mut self) {
|
||||||
|
println!("Sender: push 'UpdateProgress' action");
|
||||||
|
self.actions.push(SenderAction::UpdateProgress(0.1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Trait methods provided for the `SenderState`
|
||||||
|
impl State for SenderState {
|
||||||
|
// initialize the view entities
|
||||||
|
fn init(&mut self, _registry: &mut Registry, ctx: &mut Context) {
|
||||||
|
// create the target entity, that receives the Sender messages
|
||||||
|
self.target = Entity::from(ctx.widget().try_clone::<u32>("target")
|
||||||
|
.expect("ERROR: SenderState::init(): target entity not found!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// update entities, before we render the view
|
||||||
|
fn update(&mut self, _: &mut Registry, ctx: &mut Context) {
|
||||||
|
let actions: Vec<SenderAction> = self.actions.drain(..).collect();
|
||||||
|
|
||||||
|
for action in actions {
|
||||||
|
match action {
|
||||||
|
SenderAction::UpdateProgress(amount) => {
|
||||||
|
ctx.send_message(SenderAction::UpdateProgress(amount), self.target);
|
||||||
|
println!("Sender: send message 'SenderAction::UpdateProgress'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
34
advotracker/examples/messages_test/sender/sender_view.rs
Normal file
34
advotracker/examples/messages_test/sender/sender_view.rs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* OrbTK - The Orbital Widget Toolkit
|
||||||
|
*
|
||||||
|
* Copyright 2021 Ralf Zerres <ralf.zerres@networkx.de>
|
||||||
|
* SPDX-License-Identifier: (0BSD or MIT)
|
||||||
|
*/
|
||||||
|
|
||||||
|
use orbtk::prelude::*;
|
||||||
|
|
||||||
|
use crate::sender::sender_state::{SenderAction, SenderState};
|
||||||
|
|
||||||
|
widget!(SenderView<SenderState> {
|
||||||
|
// the Entity of the widget that will receive the messages
|
||||||
|
target: u32
|
||||||
|
});
|
||||||
|
|
||||||
|
impl Template for SenderView {
|
||||||
|
fn template(self, id: Entity, bc: &mut BuildContext) -> Self {
|
||||||
|
self.name("SenderView")
|
||||||
|
.child(
|
||||||
|
Button::new()
|
||||||
|
.text("Click me to send a message!")
|
||||||
|
.v_align("center")
|
||||||
|
.h_align("center")
|
||||||
|
.on_click(move |states, _entity| {
|
||||||
|
states.get_mut::<SenderState>(id).send_message();
|
||||||
|
//states.send_message(SenderAction::UpdateProgress, id);
|
||||||
|
//ctx.send_message(TestMessageAction::ToggleMessageBox, id);
|
||||||
|
false
|
||||||
|
})
|
||||||
|
.build(bc)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user