use orbtk::prelude::*; use crate::{ base_state::BaseState, data::{PolicyData, PolicyDataList}, keys::*, }; /// Actions that can execute on the task view. #[derive(Debug, Clone, Copy)] pub enum Action { ClearEntry(Entity), InputTextChanged(Entity), OpenMenu(Entity), ParseEntry(Entity), RemoveFocus(Entity), SetEntry(Entity), TextChanged(Entity, usize), } /// Handles the requests of the `PolicyCheckView`. #[derive(Default, AsAny)] pub struct PolicyCheckState { action:Option, last_focused: Option, pub text_box: Entity, menu_button: Entity, policy_number_valid: bool, } impl BaseState for PolicyCheckState {} impl PolicyCheckState { /// Sets a new action. pub fn action(&mut self, action: Action) { self.action = action.into(); } /// Clear text in text box. pub fn clear_entry(&mut self, text_box: Entity, ctx: &mut Context) { let mut text_box = TextBox::get(ctx.get_widget(text_box)); let text = text_box.text_mut(); println!("reset {}", text); } /// Open menu. pub fn open_menu(&mut self, text_block: Entity, ctx: &mut Context) { let mut text_block = TextBlock::get(ctx.get_widget(text_block)); let text = text_block.text_mut(); println!("Menu text: {}", text); } fn parse_entry(&self, text_box: Entity, ctx: &mut Context) { // Old style //let length = ctx.get_widget(text_box).get::("policy_number").len(); // New style let mut text_box = TextBox::get(ctx.get_widget(text_box)); let policy_number = text_box.text_mut(); println!("parsing policy Number: {}", policy_number); // Parse policy code: "AS-123456789" // DION VERS POLLFNR // 1 AS 1515735810 let policy_number_length = policy_number.len(); if policy_number_length == 10 { println!("Verify {} ...", policy_number); // check against hash-table //ctx.get_widget(text_box).set("policy_number_valid", true); //TextBox::get(ctx.child("text_box")).set_foreground(colors::LINK_WATER_COLOR); } if policy_number_length < 10 { println!("Policy number is to short!"); //TextBox::get(ctx.child("text_box")).set_foreground("#ffffff"); //TextBox::get(ctx.child("text_box")).set_background("#5b0f22"); } if policy_number_length > 10 { println!("Policy number is to big!"); //TextBox::get(ctx.child("text_box")).set_foreground("#ffffff"); //TextBox::get(ctx.child("text_box")).set_background("#5b0f22"); } // if let policy_number = text_box.text() { // match policy_number { // _ => { // println!("policynumber: {} is valid!", policy_number); // } // } // } } /// If TextBox 'policy_number' is empty, disable button "check" /// otherwise enabled it. fn set_check_button(&self, text_box: Entity, ctx: &mut Context) { if ctx.get_widget(text_box).get::("policy_number_check").is_empty() { ctx.get_widget(self.text_box).set("enabled", false); } else { ctx.get_widget(self.text_box).set("enabled", true); } ctx.get_widget(self.text_box).update_theme_by_state(true); } /// Change status of given text box to edit mode. fn set_entry(&self, text_box: Entity, ctx: &mut Context) { if *ctx.get_widget(text_box).get::("focused") { ctx.get_widget(text_box).set("enabled", false); ctx.push_event_by_window(FocusEvent::RemoveFocus(text_box)); return; } if let Some(old_focused_element) = ctx.window().get::("global").focused_widget { ctx.push_event_by_window(FocusEvent::RemoveFocus(old_focused_element)); } ctx.get_widget(text_box).set("enabled", true); // select all ctx.get_widget(text_box) .get_mut::("text_selection") .start_index = 0; ctx.get_widget(text_box) .get_mut::("text_selection") .length = ctx.get_widget(text_box).get::("policy_number").len(); ctx.push_event_by_window(FocusEvent::RequestFocus(text_box)); } } impl State for PolicyCheckState { fn init(&mut self, _: &mut Registry, ctx: &mut Context) { self.menu_button = ctx .entity_of_child(ID_POLICY_CHECK_MENU_BUTTON) .expect("PolicyCheckState.init: Can't find child 'Menu button'."); self.text_box = ctx .entity_of_child(ID_POLICY_CHECK_POLICY_NUMBER) .expect("PolicyCheckState.init: Can't find child 'Text Box'."); } fn update(&mut self, _registry: &mut Registry, ctx: &mut Context) { // clear focus on focus moved if self.last_focused != ctx.window().get::("global").focused_widget { if let Some(last_focused) = self.last_focused { ctx.get_widget(last_focused).set("focused", false); ctx.get_widget(last_focused) .set("visibility", Visibility::Collapsed); } } if let Some(action) = self.action { match action { Action::ClearEntry(text_box) => { self.clear_entry(text_box, ctx); } Action::InputTextChanged(text_box) => { self.set_check_button(text_box, ctx); } Action::OpenMenu(text_block) => { self.open_menu(text_block, ctx); } Action::ParseEntry(text_box) => { self.parse_entry(text_box, ctx); } Action::RemoveFocus(text_box) => { ctx.get_widget(text_box).set("enabled", false); ctx.push_event_by_window(FocusEvent::RemoveFocus(text_box)); } Action::SetEntry(text_box) => { self.last_focused = Some(text_box); self.set_entry(text_box, ctx); } Action::TextChanged(entity, _index) => { self.set_entry(entity, ctx); } } } self.action = None; } }