Files
advotracker/crates/advotracker_client/examples/increment_progress_bar.rs
Ralf Zerres 5a9965751a advotracker: reorganize the project using a crate based structure
* advotracker: the framework project
* crate/advotrackerdb: implementation of the database backend
* crate/advotrackerd: implementation of the backend (daemon)
* crate/adovtracker: implementaton of the application (CLI and GUI)

Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
2021-03-08 04:32:11 +01:00

125 lines
3.8 KiB
Rust

use std::collections::HashMap;
//use std::sync::mpsc;
use orbtk::prelude::*;
use orbtk::shell::WindowRequest;
static ID_CHECK_POLICY_NUMBER: &'static str = "ID_CHECK_POLICY_NUMBER";
static ID_PROGRESS_BAR: &'static str = "ID_PROGRESS_BAR";
enum Action {
ParsePolicyNumber
}
#[derive(Default, AsAny)]
struct MainViewState {
action: Option<Action>,
progress_bar: Entity,
text_box: Entity,
progress_counter: f64
//records: HashMap::<String, String>,
//record_counter: u64
}
impl State for MainViewState {
fn init(&mut self, _: &mut Registry, ctx: &mut Context) {
self.text_box = ctx.entity_of_child(ID_CHECK_POLICY_NUMBER).expect("Cannot get TextBox!");
self.progress_bar = ctx.entity_of_child(ID_PROGRESS_BAR).expect("Cannot get progress bar !");
}
fn update(&mut self, _: &mut Registry, ctx: &mut Context) {
// if there is an action, process it
if let Some(action) = &self.action {
match action {
Action::ParsePolicyNumber => {
let value_to_parse = ctx.get_widget(self.text_box).get::<String16>("text").clone();
self.parse_policy_number(value_to_parse, ctx);
}
}
// Reset action
self.action = None;
}
}
}
impl MainViewState {
fn action(&mut self, action: Action) {
self.action = Some(action);
}
fn parse_policy_number(&mut self, _value: String16, ctx: &mut Context) {
self.import_csv(ctx);
}
fn import_csv(&mut self, ctx: &mut Context) {
// code to import csv file into a hashmap
// will read in number_of_records = 100%
// progress_counter should be incremented, if
// read_in_loop will reach next 10% -> self.progress_counter += 0.1
// now fire an event to update the widget
// question: how to fire up the event inside import_csv function,
// without the need to mute "ID_CHECK_POLICY_NUMBER" ?
// given code just increments, if you change "ID_CHECK_POLICY_NUMBER"
self.progress_counter += 0.1;
self.update_progress(ctx);
}
fn update_progress(&self, ctx: &mut Context) {
// create a mpsc::Sender<WindowRequest> object
let sender = ctx.window_sender();
let mut pgbar = ctx.get_widget(self.progress_bar);
pgbar.set::<f64>("val", self.progress_counter);
// redraw screen if sender has changed
// only way to trigger a redraw: create an event
sender.send(WindowRequest::Redraw).unwrap()
}
}
widget!(MainView<MainViewState>);
impl Template for MainView {
fn template(self, id: Entity, ctx: &mut BuildContext) -> Self {
self
.margin(32.0)
.child(
Stack::new()
.orientation("vertical")
.h_align("center")
.v_align("top")
.spacing(8.0)
.child(
TextBox::new()
.id(ID_CHECK_POLICY_NUMBER)
.water_mark("Mut value and type <Return>")
.on_activate(move |states, _entity| {
// you have to fire a new event to be able to get in the update() with access to Context
states.get_mut::<MainViewState>(id).action(Action::ParsePolicyNumber);
})
.build(ctx)
)
.child(
ProgressBar::new()
.id(ID_PROGRESS_BAR)
.build(ctx)
)
.build(ctx)
)
}
}
fn main() {
Application::new()
.window(|ctx| {
Window::new()
.title("incroment_progress_bar skeleton")
.position((100.0, 100.0))
.size(420.0, 730.0)
.resizeable(true)
.child(MainView::new().build(ctx))
.build(ctx)
})
.run();
}