policycheck: wip: view and state handling updates

Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
This commit is contained in:
2020-06-22 14:27:30 +02:00
parent 717dc9ba03
commit 62c786b671
2 changed files with 346 additions and 167 deletions

View File

@@ -9,6 +9,7 @@ use crate::{
/// Actions that can execute on the task view.
#[derive(Debug, Clone, Copy)]
pub enum Action {
ClearEntry(Entity),
InputTextChanged(Entity),
ParseEntry(Entity),
RemoveFocus(Entity),
@@ -23,7 +24,7 @@ pub struct PolicyCheckState {
last_focused: Option<Entity>,
pub text_box: Entity,
menu_button: Entity,
policy_code_valid: bool,
policy_number_valid: bool,
}
impl BaseState for PolicyCheckState {}
@@ -34,29 +35,59 @@ impl PolicyCheckState {
self.action = action.into();
}
/// Parse and verify given policy code to match a valid policy data element.
fn parse_entry(&self, text_box: Entity, ctx: &mut Context) {
// Old style
//let length = ctx.get_widget(text_box).get::<String>("policy_code").len();
// New style
let text_box = TextBox::get(ctx.widget());
println!("parsing policy code: {}", text_box.text());
// Parse policy code: "AS-123456789"
if let policy_code = text_box.text() {
match policy_code {
_ => {
println!("policycode: {} is valid!", policy_code);
}
}
}
/// 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);
}
/// If TextBox 'policy_code' is empty, disable button "check"
/// Parse and verify given policy code to match a valid policy data element.
//fn parse_entry(&self, text_box: Entity, ctx: &mut Context) -> Result<(), Box<dyn std::error::Error>> {
fn parse_entry(&self, text_box: Entity, ctx: &mut Context) {
// Old style
//let length = ctx.get_widget(text_box).get::<String>("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::<String>("policy_code_check").is_empty() {
if ctx.get_widget(text_box).get::<String>("policy_number_check").is_empty() {
ctx.get_widget(self.text_box).set("enabled", false);
} else {
ctx.get_widget(self.text_box).set("enabled", true);
@@ -85,7 +116,7 @@ impl PolicyCheckState {
.start_index = 0;
ctx.get_widget(text_box)
.get_mut::<TextSelection>("text_selection")
.length = ctx.get_widget(text_box).get::<String>("policy_code").len();
.length = ctx.get_widget(text_box).get::<String>("policy_number").len();
ctx.push_event_by_window(FocusEvent::RequestFocus(text_box));
}
}
@@ -96,11 +127,11 @@ impl State for PolicyCheckState {
.entity_of_child(ID_POLICY_CHECK_MENU_BUTTON)
.expect("PolicyState.init: Can't find child 'Menu button'.");
self.text_box = ctx
.entity_of_child(ID_POLICY_CHECK_TEXT_BOX)
.expect("PolicyState.init: Can't find child 'Text Box'.");
.entity_of_child(ID_POLICY_CHECK_POLICY_NUMBER)
.expect("PolicyState.init: Can't find child 'Text Box'.");
}
fn update(&mut self, registry: &mut Registry, ctx: &mut Context) {
fn update(&mut self, _registry: &mut Registry, ctx: &mut Context) {
// clear focus on focus moved
if self.last_focused != ctx.window().get::<Global>("global").focused_widget {
if let Some(last_focused) = self.last_focused {
@@ -112,6 +143,9 @@ impl State for PolicyCheckState {
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);
}
@@ -126,7 +160,7 @@ impl State for PolicyCheckState {
self.last_focused = Some(text_box);
self.set_entry(text_box, ctx);
}
Action::TextChanged(entity, index) => {
Action::TextChanged(entity, _index) => {
self.set_entry(entity, ctx);
}
}