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::*;
|
use orbtk::prelude::*;
|
||||||
|
|
||||||
|
mod data;
|
||||||
mod main_view;
|
mod main_view;
|
||||||
mod receiver;
|
mod receiver;
|
||||||
mod sender;
|
mod sender;
|
||||||
|
|||||||
@@ -12,10 +12,6 @@ use crate::{
|
|||||||
sender::sender_view::SenderView,
|
sender::sender_view::SenderView,
|
||||||
};
|
};
|
||||||
|
|
||||||
// constants
|
|
||||||
pub static ID_SENDER_VIEW: &str = "sender_view";
|
|
||||||
pub static ID_RECEIVER_VIEW: &str = "receiver_view";
|
|
||||||
|
|
||||||
widget!(MainView {
|
widget!(MainView {
|
||||||
//sender_view: ,
|
//sender_view: ,
|
||||||
//receiver_view:
|
//receiver_view:
|
||||||
@@ -38,11 +34,5 @@ impl Template for MainView {
|
|||||||
.child(receiver_view)
|
.child(receiver_view)
|
||||||
.build(ctx)
|
.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 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.
|
/// state changes for the `SenderView` widget.
|
||||||
pub enum TestMessageAction {
|
pub enum TestMessageAction {
|
||||||
// Toggle visibility of a message TextBox.
|
// Toggle visibility of a message TextBlock.
|
||||||
ToggleMessageBox
|
ToggleMessageBlock
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Valid `structure members` of the `ReceiverState` used to react on
|
/// Valid `structure members` of the `ReceiverState` used to react on
|
||||||
/// state changes inside the `ReceiverView` widget.
|
/// state changes inside the `ReceiverView` widget.
|
||||||
#[derive(Default, AsAny)]
|
#[derive(Default, AsAny)]
|
||||||
pub struct ReceiverState {
|
pub struct ReceiverState {
|
||||||
message_box: Option<Entity>,
|
message_block: Option<Entity>,
|
||||||
progress_bar: Entity
|
progress_bar: Entity
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Method definitions, we provide inside the `ReceiverState`.
|
/// Method definitions, we provide inside the `ReceiverState`.
|
||||||
impl ReceiverState {
|
impl ReceiverState {
|
||||||
fn toggle_message_box(&self, ctx: &mut Context) {
|
fn toggle_message_block(&self, ctx: &mut Context) {
|
||||||
if let Some(message_box) = self.message_box {
|
if let Some(message_block) = self.message_block {
|
||||||
ctx.get_widget(message_box)
|
ctx.get_widget(message_block)
|
||||||
.set("visibility", Visibility::Visible);
|
.set("visibility", Visibility::Visible);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Trait methods provided for the `SenderState`
|
/// Trait methods provided for the `ReceiverState`
|
||||||
impl State for ReceiverState {
|
impl State for ReceiverState {
|
||||||
// initialize the view entities
|
// initialize the view entities
|
||||||
fn init(&mut self, _: &mut Registry, ctx: &mut Context) {
|
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!");
|
.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
|
// handle messages targeting the view
|
||||||
@@ -52,17 +59,26 @@ impl State for ReceiverState {
|
|||||||
for message in messages.read::<SenderAction>() {
|
for message in messages.read::<SenderAction>() {
|
||||||
match message {
|
match message {
|
||||||
SenderAction::UpdateProgress(amount) => {
|
SenderAction::UpdateProgress(amount) => {
|
||||||
println!("Message received");
|
println!("UpdateProgress message received");
|
||||||
let mut progress_bar = ctx.get_widget(self.progress_bar);
|
let mut progress_bar = ctx.get_widget(self.progress_bar);
|
||||||
let current_progress = progress_bar.clone::<f64>("val");
|
let current_progress = progress_bar.clone::<f64>("val");
|
||||||
progress_bar.set::<f64>("val", current_progress + amount);
|
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>() {
|
for action in messages.read::<TestMessageAction>() {
|
||||||
match action {
|
match action {
|
||||||
TestMessageAction::ToggleMessageBox => {
|
TestMessageAction::ToggleMessageBlock => {
|
||||||
self.toggle_message_box(ctx);
|
self.toggle_message_block(ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,31 +7,50 @@
|
|||||||
|
|
||||||
use orbtk::prelude::*;
|
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>);
|
widget!(ReceiverView<ReceiverState>);
|
||||||
|
|
||||||
impl Template for ReceiverView {
|
impl Template for ReceiverView {
|
||||||
fn template(self, _id: Entity, bc: &mut BuildContext) -> Self {
|
fn template(self, _id: Entity, build_context: &mut BuildContext) -> Self {
|
||||||
self.name("ReceiverView")
|
self.id(ID_RECEIVER)
|
||||||
|
.name(ID_RECEIVER)
|
||||||
.child(
|
.child(
|
||||||
Stack::new()
|
Container::new()
|
||||||
.orientation("vertical")
|
.id(ID_RECEIVER_CONTAINER)
|
||||||
.spacing(16)
|
.border_brush(COLOR_BOMBAY)
|
||||||
|
.border_width(2)
|
||||||
|
.name(ID_RECEIVER_CONTAINER)
|
||||||
|
.padding(16)
|
||||||
.child(
|
.child(
|
||||||
ProgressBar::new()
|
Stack::new()
|
||||||
.id("progress_bar")
|
.orientation("vertical")
|
||||||
.build(bc)
|
.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(
|
.build(build_context)
|
||||||
TextBox::new()
|
|
||||||
.id("message_box")
|
|
||||||
.h_align("center")
|
|
||||||
.text("message received. Box toggled!")
|
|
||||||
.visibility("hidden")
|
|
||||||
.build(bc)
|
|
||||||
)
|
|
||||||
.build(bc)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,11 +7,14 @@
|
|||||||
|
|
||||||
use orbtk::prelude::*;
|
use orbtk::prelude::*;
|
||||||
|
|
||||||
|
//use crate::data::constants::*;
|
||||||
|
|
||||||
/// 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.
|
/// state changes for the `SenderView` widget.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum SenderAction {
|
pub enum SenderAction {
|
||||||
UpdateProgress(f64),
|
UpdateProgress(f64),
|
||||||
|
UpdateMessage(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Valid `structure members` of the `SenderState` used to react on
|
/// Valid `structure members` of the `SenderState` used to react on
|
||||||
@@ -26,11 +29,17 @@ pub struct SenderState {
|
|||||||
|
|
||||||
/// Method definitions, we provide inside the `SenderState`.
|
/// Method definitions, we provide inside the `SenderState`.
|
||||||
impl SenderState {
|
impl SenderState {
|
||||||
/// Sending message 'UpdateProgress'
|
/// Sending message with type 'UpdateProgress'
|
||||||
pub fn send_message(&mut self) {
|
pub fn send_update_progress(&mut self) {
|
||||||
println!("Sender: push 'UpdateProgress' action");
|
println!("Sender: push 'UpdateProgress' action");
|
||||||
self.actions.push(SenderAction::UpdateProgress(0.1));
|
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`
|
/// Trait methods provided for the `SenderState`
|
||||||
@@ -46,12 +55,23 @@ impl State for SenderState {
|
|||||||
fn update(&mut self, _: &mut Registry, ctx: &mut Context) {
|
fn update(&mut self, _: &mut Registry, ctx: &mut Context) {
|
||||||
let actions: Vec<SenderAction> = self.actions.drain(..).collect();
|
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 {
|
for action in actions {
|
||||||
match action {
|
match action {
|
||||||
SenderAction::UpdateProgress(amount) => {
|
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'");
|
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)
|
* 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> {
|
widget!(SenderView<SenderState> {
|
||||||
// the Entity of the widget that will receive the messages
|
// the Entity of the widget that will receive the messages
|
||||||
@@ -15,20 +22,79 @@ widget!(SenderView<SenderState> {
|
|||||||
});
|
});
|
||||||
|
|
||||||
impl Template for SenderView {
|
impl Template for SenderView {
|
||||||
fn template(self, id: Entity, bc: &mut BuildContext) -> Self {
|
fn template(self, id: Entity, build_context: &mut BuildContext) -> Self {
|
||||||
self.name("SenderView")
|
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(
|
.child(
|
||||||
Button::new()
|
Container::new()
|
||||||
.text("Click me to send a message!")
|
.id(ID_SENDER_CONTAINER)
|
||||||
.v_align("center")
|
.border_brush(COLOR_BOMBAY)
|
||||||
.h_align("center")
|
.border_width(2)
|
||||||
.on_click(move |states, _entity| {
|
.padding(16)
|
||||||
states.get_mut::<SenderState>(id).send_message();
|
.child(
|
||||||
//states.send_message(SenderAction::UpdateProgress, id);
|
Grid::new()
|
||||||
//ctx.send_message(TestMessageAction::ToggleMessageBox, id);
|
.id(ID_SENDER_GRID)
|
||||||
false
|
.columns(
|
||||||
})
|
Columns::create()
|
||||||
.build(bc)
|
.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