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:
136
frontend/src/policycheck_state.rs
Normal file
136
frontend/src/policycheck_state.rs
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user