frontend: policycheck: component to check the validity of a given policy code

* policycheck_view: user GUI to interact with (orbtk Widget)
* policycheck_state: rust code with helper methods

Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
This commit is contained in:
2020-06-19 17:21:23 +02:00
parent e69e4c082c
commit c4b992f41a
2 changed files with 295 additions and 0 deletions

View File

@@ -0,0 +1,136 @@
use orbtk::prelude::*;
use crate::{
base_state::BaseState,
//data::{PolicyData, PolicyList},
keys::*,
};
/// Actions that can execute on the task view.
#[derive(Debug, Clone, Copy)]
pub enum Action {
InputTextChanged(Entity),
ParseEntry(Entity),
RemoveFocus(Entity),
SetEntry(Entity),
TextChanged(Entity, usize),
}
/// Handles the requests of the `OverviewView`.
#[derive(Default, AsAny)]
pub struct PolicyCheckState {
action:Option<Action>,
last_focused: Option<Entity>,
pub text_box: Entity,
menu_button: Entity,
policy_code_valid: bool,
}
impl BaseState for PolicyCheckState {}
impl PolicyCheckState {
/// Sets a new action.
pub fn action(&mut self, action: Action) {
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);
}
}
}
}
/// If TextBox 'policy_code' 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() {
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::<bool>("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>("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::<TextSelection>("text_selection")
.start_index = 0;
ctx.get_widget(text_box)
.get_mut::<TextSelection>("text_selection")
.length = ctx.get_widget(text_box).get::<String>("policy_code").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("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'.");
}
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 {
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::InputTextChanged(text_box) => {
self.set_check_button(text_box, 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;
}
}

View File

@@ -0,0 +1,159 @@
use orbtk::prelude::*;
use crate::{
//data::PolicyList,
keys::*,
policycheck_state::{Action, PolicyCheckState},
};
type ListIndex = Option<usize>;
widget!(
/// Starter page that offers the dialog to enter an identifier of a policy.
/// This identifier is checked agains a map of valid policy codes.
PolicyCheckView<PolicyCheckState> {
//is_valid: bool,
title: String,
policy_check_text_box: String
}
);
impl Template for PolicyCheckView {
fn template(self, id: Entity, ctx: &mut BuildContext) -> Self {
let policy_code_text_box = TextBox::new()
.id(ID_POLICY_CHECK_TEXT_BOX)
.attach(Grid::row(4))
.v_align("center")
.margin((4.0, 0.0, 0.0, 0.0))
.lost_focus_on_activation(false)
.on_activate(move |ctx, entity| {
ctx.get_mut::<PolicyCheckState>(id)
.action(Action::ParseEntry(entity));
})
.on_changed(move |ctx, entity| {
ctx.get_mut::<PolicyCheckState>(id)
.action(Action::InputTextChanged(entity));
})
.build(ctx);
// Check policy identifier page
self.name("Policy check")
.child(
Grid::new()
.id(ID_POLICY_CHECK_WIDGET)
.v_align("start")
.background("#fafafa")
.columns(
Columns::new()
.add(150.0)
.add("*")
.add(150.0)
.build(),
)
.rows(
Rows::new()
.add(52.0)
.add(1.0)
.add("*")
.add(1.0)
.add("*")
.build(),
)
// Top Bar
.child(
Container::new()
.class(CLASS_TOP_BAR)
.attach(Grid::row(0))
.attach(Grid::column(0))
.attach(Grid::column_span(3))
.child(
Grid::new()
.margin((4.0, 24.0, 24.0, 4.0))
.child(
TextBlock::new()
.class(CLASS_HEADER)
.v_align("center")
.h_align("center")
//.text("Validation number policyholder"))
.text("Validierung Versicherungsnummer")
.build(ctx),
)
.build(ctx),
)
.build(ctx),
)
.child(
Container::new()
.class("separator")
.attach(Grid::row(1))
.attach(Grid::column_span(3))
.build(ctx),
)
.child(
Stack::new()
.spacing(8.0)
.orientation("vertical")
.h_align("center")
.child(policy_code_text_box)
.child(
TextBlock::new()
.class("text_box")
.element("text-block")
.id("result")
.min_width(80.0)
.max_width(180.0)
//.text("Result:")
.text("Ergebnis:")
.build(ctx),
)
.build(ctx),
)
.child(
Container::new()
.class("separator")
.attach(Grid::row(3))
.attach(Grid::column_span(3))
.build(ctx),
)
// Bottom bar
.child(
Container::new()
.class(CLASS_BOTTOM_BAR)
.attach(Grid::row(4))
.attach(Grid::column(0))
.attach(Grid::column_span(3))
.child(
Grid::new()
.element("logo_customer")
.margin((9.0, 16.0, 16.0, 9.0))
.attach(Grid::column(0))
.attach(Grid::row(1))
.v_align("end")
.child(
ImageWidget::new()
.image("../resources/images/hiedemann_logo.png")
.build(ctx),
)
.build(ctx),
)
.child(
Grid::new()
.element("logo_vendor")
.margin((9.0, 16.0, 16.0, 9.0))
.attach(Grid::column(2))
.attach(Grid::row(1))
.v_align("end")
.child(
ImageWidget::new()
.image("../resources/images/networkx_logo.png")
.build(ctx),
)
.build(ctx),
)
.build(ctx),
)
.build(ctx),
)
}
}