From 13d56e2e05da49fb642f03f0be15750566497f2a Mon Sep 17 00:00:00 2001 From: Ralf Zerres Date: Fri, 28 Aug 2020 11:55:42 +0200 Subject: [PATCH] examples: new increment_progress_bar source Signed-off-by: Ralf Zerres --- .../examples/increment_progress_bar.rs | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 advotracker/examples/increment_progress_bar.rs diff --git a/advotracker/examples/increment_progress_bar.rs b/advotracker/examples/increment_progress_bar.rs new file mode 100644 index 0000000..55b9e4c --- /dev/null +++ b/advotracker/examples/increment_progress_bar.rs @@ -0,0 +1,124 @@ +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, + progress_bar: Entity, + text_box: Entity, + progress_counter: f64 + //records: HashMap::, + //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::("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 object + let sender = ctx.window_sender(); + let mut pgbar = ctx.get_widget(self.progress_bar); + pgbar.set::("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); + +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 ") + .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::(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(); +}