Skip to content

Instantly share code, notes, and snippets.

@kivimango
Created July 12, 2020 12:21
Show Gist options
  • Select an option

  • Save kivimango/ad1a44bdaa3c331ec57e657cba4e1755 to your computer and use it in GitHub Desktop.

Select an option

Save kivimango/ad1a44bdaa3c331ec57e657cba4e1755 to your computer and use it in GitHub Desktop.
use orbtk::prelude::*;
use std::collections::HashMap;
use orbtk::shell::WindowRequest;
static ID_CHECK_POLICY_NUMBER: &'static str = "ID_CHECK_POLICY_NUMBER";
static PGBAR_ID: &'static str = "PGBAR_ID";
enum Action {
ParsePolicyNumber
}
#[derive(Default, AsAny)]
struct MainViewState {
action: Option<Action>,
progress_bar: Entity,
text_box: Entity,
current_progress: 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(PGBAR_ID).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) {
let max_value = 1.0;
// your code to import csv file into a hashmap
self.current_progress += 0.1;
self.update_progress(ctx);
}
fn update_progress(&self, ctx: &mut Context) {
let mut pgbar = ctx.get_widget(self.progress_bar);
pgbar.set::<f64>("val", self.current_progress);
let sender = ctx.window_sender();
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)
.on_activate(move |states, _entity| {
// you have to fire a new event to able to get in the update() with access to Context
states.get_mut::<MainViewState>(id).action(Action::ParsePolicyNumber);
})
.build(ctx)
)
.child(
ProgressBar::new()
.id(PGBAR_ID)
.build(ctx)
)
.build(ctx)
)
}
}
fn main() {
Application::new()
.window(|ctx| {
Window::new()
.title("rzerres skeleton")
.position((100.0, 100.0))
.size(420.0, 730.0)
.resizeable(true)
.child(MainView::new().build(ctx))
.build(ctx)
})
.run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment