examples: initial version of messages_test
* destinct sender and receiver widgets * update PopupBox in receiver view, if send button on sender view is pressed * sending of inserted message text of sender view is initiated, if you hit 'Ctrl+S' inside the text box. Text block in receiver view is updated with sending Text Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
|
||||
use orbtk::prelude::*;
|
||||
|
||||
mod data;
|
||||
mod main_view;
|
||||
mod receiver;
|
||||
mod sender;
|
||||
|
||||
@@ -12,10 +12,6 @@ use crate::{
|
||||
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:
|
||||
@@ -38,11 +34,5 @@ impl Template for MainView {
|
||||
.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),
|
||||
// )
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,39 +7,46 @@
|
||||
|
||||
use orbtk::prelude::*;
|
||||
|
||||
use crate::sender::sender_state::{SenderAction, SenderState};
|
||||
use crate::{
|
||||
data::constants::*,
|
||||
sender::sender_state::SenderAction,
|
||||
};
|
||||
|
||||
/// Enumeration of valid `action variants` that need to be handled as
|
||||
// /// 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
|
||||
// Toggle visibility of a message TextBlock.
|
||||
ToggleMessageBlock
|
||||
}
|
||||
|
||||
/// 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>,
|
||||
message_block: 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)
|
||||
fn toggle_message_block(&self, ctx: &mut Context) {
|
||||
if let Some(message_block) = self.message_block {
|
||||
ctx.get_widget(message_block)
|
||||
.set("visibility", Visibility::Visible);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait methods provided for the `SenderState`
|
||||
/// Trait methods provided for the `ReceiverState`
|
||||
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")
|
||||
self.progress_bar = ctx
|
||||
.entity_of_child(ID_RECEIVER_PROGRESS_BAR)
|
||||
.expect("Cannot find ProgressBar!");
|
||||
//self.message_box = ctx
|
||||
// .entity_of_child(ID_RECEIVER_MESSAGE)
|
||||
// .expect("Cannot find 'ID_RECEIVER_MESSAGE'!");
|
||||
}
|
||||
|
||||
// handle messages targeting the view
|
||||
@@ -52,17 +59,26 @@ impl State for ReceiverState {
|
||||
for message in messages.read::<SenderAction>() {
|
||||
match message {
|
||||
SenderAction::UpdateProgress(amount) => {
|
||||
println!("Message received");
|
||||
println!("UpdateProgress 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);
|
||||
}
|
||||
SenderAction::UpdateMessage(message) => {
|
||||
println!("UpdateMessage message received");
|
||||
//Reciever::text_set(&mut ctx.widget(), self.message.to_string());
|
||||
//Reciever::text_set(&mut ctx.widget(), self.message.to_string());
|
||||
TextBlock::text_set(&mut ctx.child(ID_RECEIVER_MESSAGE_BLOCK), String::from(message));
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
for action in messages.read::<TestMessageAction>() {
|
||||
match action {
|
||||
TestMessageAction::ToggleMessageBox => {
|
||||
self.toggle_message_box(ctx);
|
||||
TestMessageAction::ToggleMessageBlock => {
|
||||
self.toggle_message_block(ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,31 +7,50 @@
|
||||
|
||||
use orbtk::prelude::*;
|
||||
|
||||
use crate::receiver::receiver_state::{TestMessageAction, ReceiverState};
|
||||
use crate::{
|
||||
data::constants::*,
|
||||
//receiver::receiver_state::{TestMessageAction, ReceiverState},
|
||||
receiver::receiver_state::ReceiverState,
|
||||
};
|
||||
|
||||
widget!(ReceiverView<ReceiverState>);
|
||||
|
||||
impl Template for ReceiverView {
|
||||
fn template(self, _id: Entity, bc: &mut BuildContext) -> Self {
|
||||
self.name("ReceiverView")
|
||||
fn template(self, _id: Entity, build_context: &mut BuildContext) -> Self {
|
||||
self.id(ID_RECEIVER)
|
||||
.name(ID_RECEIVER)
|
||||
.child(
|
||||
Stack::new()
|
||||
.orientation("vertical")
|
||||
.spacing(16)
|
||||
Container::new()
|
||||
.id(ID_RECEIVER_CONTAINER)
|
||||
.border_brush(COLOR_BOMBAY)
|
||||
.border_width(2)
|
||||
.name(ID_RECEIVER_CONTAINER)
|
||||
.padding(16)
|
||||
.child(
|
||||
ProgressBar::new()
|
||||
.id("progress_bar")
|
||||
.build(bc)
|
||||
Stack::new()
|
||||
.orientation("vertical")
|
||||
.spacing(8)
|
||||
.child(
|
||||
TextBlock::new()
|
||||
.id(ID_RECEIVER_TEXT_BLOCK)
|
||||
.text(ID_RECEIVER)
|
||||
.build(build_context)
|
||||
)
|
||||
.child(
|
||||
ProgressBar::new()
|
||||
.id(ID_RECEIVER_PROGRESS_BAR)
|
||||
.build(build_context)
|
||||
)
|
||||
.child(
|
||||
TextBlock::new()
|
||||
.id(ID_RECEIVER_MESSAGE_BLOCK)
|
||||
.text("")
|
||||
.water_mark("here we expect the message updates.")
|
||||
.build(build_context)
|
||||
)
|
||||
.build(build_context)
|
||||
)
|
||||
.child(
|
||||
TextBox::new()
|
||||
.id("message_box")
|
||||
.h_align("center")
|
||||
.text("message received. Box toggled!")
|
||||
.visibility("hidden")
|
||||
.build(bc)
|
||||
)
|
||||
.build(bc)
|
||||
.build(build_context)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,11 +7,14 @@
|
||||
|
||||
use orbtk::prelude::*;
|
||||
|
||||
//use crate::data::constants::*;
|
||||
|
||||
/// 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),
|
||||
UpdateMessage(String),
|
||||
}
|
||||
|
||||
/// Valid `structure members` of the `SenderState` used to react on
|
||||
@@ -26,11 +29,17 @@ pub struct SenderState {
|
||||
|
||||
/// Method definitions, we provide inside the `SenderState`.
|
||||
impl SenderState {
|
||||
/// Sending message 'UpdateProgress'
|
||||
pub fn send_message(&mut self) {
|
||||
/// Sending message with type 'UpdateProgress'
|
||||
pub fn send_update_progress(&mut self) {
|
||||
println!("Sender: push 'UpdateProgress' action");
|
||||
self.actions.push(SenderAction::UpdateProgress(0.1));
|
||||
}
|
||||
/// Sending message with type 'UpdateMessage'
|
||||
pub fn send_update_message(&mut self) {
|
||||
println!("Sender: push 'UpdateMessage' action");
|
||||
self.actions.push(SenderAction::UpdateMessage(String::from("Hey sender. I got your message. All fine.")));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// Trait methods provided for the `SenderState`
|
||||
@@ -46,12 +55,23 @@ impl State for SenderState {
|
||||
fn update(&mut self, _: &mut Registry, ctx: &mut Context) {
|
||||
let actions: Vec<SenderAction> = self.actions.drain(..).collect();
|
||||
|
||||
//let message = ctx.child(ID_SENDER_TEXT_BOX).get::<String>("text").to_string();
|
||||
//println!("Child text: {:?}", message);
|
||||
|
||||
// create an explicit message_adapter context
|
||||
let message_adapter = ctx.message_adapter();
|
||||
|
||||
for action in actions {
|
||||
match action {
|
||||
SenderAction::UpdateProgress(amount) => {
|
||||
ctx.send_message(SenderAction::UpdateProgress(amount), self.target);
|
||||
message_adapter.send_message(SenderAction::UpdateProgress(amount), self.target);
|
||||
println!("Sender: send message 'SenderAction::UpdateProgress'");
|
||||
}
|
||||
SenderAction::UpdateMessage(message) => {
|
||||
//let message = ctx.child(ID_SENDER_TEXT_BOX).get::<String>("text").to_string();
|
||||
message_adapter.send_message(SenderAction::UpdateMessage(message), self.target);
|
||||
println!("Sender: send message 'SenderAction::UpdateMessage'");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,9 +5,16 @@
|
||||
* SPDX-License-Identifier: (0BSD or MIT)
|
||||
*/
|
||||
|
||||
use orbtk::prelude::*;
|
||||
use orbtk::{
|
||||
prelude::*,
|
||||
shell::event::Key,
|
||||
};
|
||||
|
||||
use crate::sender::sender_state::{SenderAction, SenderState};
|
||||
use crate::{
|
||||
data::constants::*,
|
||||
sender::sender_state::SenderState,
|
||||
//sender::sender_state::{SenderAction, SenderState},
|
||||
};
|
||||
|
||||
widget!(SenderView<SenderState> {
|
||||
// the Entity of the widget that will receive the messages
|
||||
@@ -15,20 +22,79 @@ widget!(SenderView<SenderState> {
|
||||
});
|
||||
|
||||
impl Template for SenderView {
|
||||
fn template(self, id: Entity, bc: &mut BuildContext) -> Self {
|
||||
self.name("SenderView")
|
||||
fn template(self, id: Entity, build_context: &mut BuildContext) -> Self {
|
||||
let sender_header = TextBlock::new()
|
||||
.attach(Grid::row(0))
|
||||
.text(ID_SENDER)
|
||||
.build(build_context);
|
||||
|
||||
let sender_message = TextBox::new()
|
||||
.attach(Grid::row(2))
|
||||
.attach(Grid::column(0))
|
||||
.name(ID_SENDER_TEXT_BOX)
|
||||
.text("")
|
||||
.water_mark("Message text to be send via MessageHandler.")
|
||||
.on_key_down(move | states, key_event | {
|
||||
match key_event.key {
|
||||
Key::Tab | Key::S(..) => {
|
||||
println!("KeyHandler: got Tab");
|
||||
states.get_mut::<SenderState>(id).send_update_progress();
|
||||
states.get_mut::<SenderState>(id).send_update_message();
|
||||
},
|
||||
_ => {
|
||||
println!("KeyHandler: got {:?}", key_event.key);
|
||||
},
|
||||
};
|
||||
true
|
||||
})
|
||||
.build(build_context);
|
||||
|
||||
let sender_button = Button::new()
|
||||
.attach(Grid::row(2))
|
||||
.attach(Grid::column(2))
|
||||
.name(ID_SENDER_BUTTON)
|
||||
.text("send")
|
||||
.icon(material_icons_font::MD_SEND)
|
||||
.on_click(move |states, _entity| {
|
||||
states.get_mut::<SenderState>(id).send_update_progress();
|
||||
false
|
||||
})
|
||||
.build(build_context);
|
||||
|
||||
//let sender_content =
|
||||
|
||||
self.id(ID_SENDER)
|
||||
.name(ID_SENDER)
|
||||
.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)
|
||||
Container::new()
|
||||
.id(ID_SENDER_CONTAINER)
|
||||
.border_brush(COLOR_BOMBAY)
|
||||
.border_width(2)
|
||||
.padding(16)
|
||||
.child(
|
||||
Grid::new()
|
||||
.id(ID_SENDER_GRID)
|
||||
.columns(
|
||||
Columns::create()
|
||||
.push("stretch") // Message
|
||||
.push(16) // Delimiter
|
||||
.push("auto") // Button
|
||||
)
|
||||
.rows(
|
||||
Rows::create()
|
||||
.push("auto") // Header
|
||||
.push(6) // Delimiter
|
||||
.push("auto") // Message
|
||||
.push(6) // Delimiter
|
||||
)
|
||||
|
||||
.child(sender_header) // row 0
|
||||
.child(sender_message) // row 2
|
||||
.child(sender_button) // row 3
|
||||
.build(build_context)
|
||||
)
|
||||
.build(build_context)
|
||||
)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user