71 lines
2.1 KiB
Rust
71 lines
2.1 KiB
Rust
/*
|
|
* 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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|