From c4caf1270653ca51b4becbb72801836946a441cd Mon Sep 17 00:00:00 2001 From: Ralf Zerres Date: Sun, 8 Nov 2020 18:47:43 +0100 Subject: [PATCH] widget/policycheck: update logic * remove handling of the menu logic (isolated in dedicated widget) * reference styling via theme * update documentation strings Signed-off-by: Ralf Zerres --- .../widgets/policycheck/policycheck_state.rs | 289 ++---------------- .../widgets/policycheck/policycheck_view.rs | 35 +-- 2 files changed, 35 insertions(+), 289 deletions(-) diff --git a/advotracker/src/widgets/policycheck/policycheck_state.rs b/advotracker/src/widgets/policycheck/policycheck_state.rs index 6223dd2..04f6fb5 100644 --- a/advotracker/src/widgets/policycheck/policycheck_state.rs +++ b/advotracker/src/widgets/policycheck/policycheck_state.rs @@ -5,10 +5,8 @@ * SPDX-License-Identifier: (0BSD or MIT) */ -use cfg_if::cfg_if; use locales::t; use orbtk::prelude::*; -use orbtk::shell::event::Key; use serde::Deserialize; use std::process; @@ -17,12 +15,12 @@ use std::time::{Duration, SystemTime}; use tracing::{error, info, trace}; use crate::{ - widgets::global_state::GlobalState, - widgets::policycheck::policycheck_view::PolicycheckView, data::{ structures::{PolicyCode, PolicyDataList, PolicyList}, constants::*, }, + widgets::global_state::GlobalState, + //widgets::menu::menu_view::MenuView, //services::imports::allianzdirectcall::import, services::imports::allianzdirectcall, }; @@ -35,13 +33,10 @@ pub enum PolicycheckAction { ChangeTheme(), InputTextChanged(Entity), ImportData, - OpenMenu(Entity), ParseEntry(Entity), RemoveFocus(Entity), - RemoveMenu(Entity), RemovePopup(Entity), ResetProgress, - SetMenuPopup(Entity), SetProgress(f64), SetProgressPopup(Entity), SetToggleTheme(Entity), @@ -66,12 +61,9 @@ pub struct PolicycheckState { duration: Duration, label_result: Entity, lang: String, - //last_focused: Option, button_menu: Entity, policy_data_count: u64, policy_numbers: HashMap, - popup_menu: Entity, - popup_menu_toggle_theme: Entity, progress_bar: Entity, progress_count: f64, progress_popup: Entity @@ -79,7 +71,7 @@ pub struct PolicycheckState { impl GlobalState for PolicycheckState {} -/// method definitions, that react on any given state change inside the `Policycheck` widget. +/// Method definitions, that react on any given state change inside the `Policycheck` widget. impl PolicycheckState { /// Create a hashmap (key: policy number, value: policy type). pub fn create_hashmap(&mut self, _ctx: &mut Context<'_>) -> Result<(), Box> { @@ -176,13 +168,6 @@ impl PolicycheckState { Ok(()) } - /// Open menu. - pub fn open_menu(&mut self, _entity: Entity, ctx: &mut Context<'_>) { - //let menu_string = ctx.get_widget(entity).get::("text"); - //.child(policycheck_menu); - self.set_popup_menu(ctx); - } - /// Parse validity of the given policy number. fn parse_entry(&mut self, policy_check_policy_number: Entity, ctx: &mut Context<'_>) { @@ -322,12 +307,6 @@ impl PolicycheckState { trace!(target: "advotracker", parse_entry = "finished"); } - /// Remove the menu popup box - fn remove_menu(&mut self, id: Entity, ctx: &mut Context<'_>) { - ctx.remove_child(self.popup_menu); - println!("Popup {:?} removed !", id); - } - /// Remove the popup box fn remove_popup(&mut self, id: Entity, ctx: &mut Context<'_>) { ctx.remove_child(self.progress_popup); @@ -363,61 +342,29 @@ impl PolicycheckState { } } - /// Set a menu - fn set_popup_menu(&mut self, ctx: &mut Context<'_>) { - let stack = ctx - .entity_of_child(ID_POLICY_CHECK_BUTTON_MENU) - .expect("PolicycheckState: Can't find entity of resource 'ID_POLICY_CHECK_POPUP_MENU'."); - let current_entity = ctx.entity(); - let build_context = &mut ctx.build_context(); - - // create a popup menu overlay - self.popup_menu = create_menu(current_entity, build_context); - - // create a menu_popup widget as a child of entity "ID_POLICY_CHECK_BUTTON_MENU" - build_context.append_child(stack, self.popup_menu); - - println!("Popup Menu created: {:?}", self.popup_menu); - } - /// Set a progress popup that updates the import status in a progress bar fn set_popup_progress(&mut self, ctx: &mut Context<'_>) { - //println!("Set up Progress popup: {:?}", text_box); + // create a stack as a child of entity "ID_POLICY_CHECK_POLICY_NUMBER" let stack = ctx - .entity_of_child(ID_POLICY_CHECK_RESULT) - .expect("PolicycheckState: Can't find entity of resource 'ID_POLICY_CHECK_RESULT'."); + .entity_of_child(ID_POLICY_CHECK_POLICY_NUMBER) + .expect("PolicycheckState: Can't find entity of resource 'ID_POLICY_CHECK_POLICY_NUMBER'."); let current_entity = ctx.entity(); let build_context = &mut ctx.build_context(); + // create the progress_popup widget self.progress_popup = create_popup_progress(current_entity, build_context); + println!("New entity `progress_popup` created: {:?}", self.progress_popup); - // create a progress_popup widget as a child of entity "ID_POLICY_CHECK_POLICY_NUMBER" + // append the stack inside the progress_popup build_context.append_child(stack, self.progress_popup); + // make sure we have a self.progress_bar = ctx .entity_of_child(ID_POLICY_CHECK_PROGRESS_BAR) .expect("PolicycheckState.init: Can't find entity of resource 'ID_POLICY_CHECK_PROGRESS_BAR'."); + println!("New entity `progress_bar` created: {:?}", self.progress_bar); - println!("PopupProgress created: {:?}", self.progress_popup); - } - - /// Set a toggle_theme menu - fn set_menu_toggle_theme(&mut self, ctx: &mut Context<'_>) { - let stack = ctx - .entity_of_child(ID_POLICY_CHECK_MENU_LABEL_TOGGLE_THEME) - .expect("PolicycheckState: Can't find entity of resource 'ID_POLICY_CHECK_MENU_LABEL_TOGGLE_THEME'."); - //.entity_of_child(ID_POLICY_CHECK_BUTTON_MENU) - //.expect("PolicycheckState: Can't find entity of resource 'ID_POLICY_CHECK_BUTTON_MENU'."); - let current_entity = ctx.entity(); - let build_context = &mut ctx.build_context(); - - // create a menu overlay - self.popup_menu_toggle_theme = create_menu_toggle_theme(current_entity, build_context); - - // create a menu_popup widget as a child of entity "ID_POLICY_CHECK_POPUP_MENU" - build_context.append_child(stack, self.popup_menu_toggle_theme); - - println!("Popup Menu Toggle Theme created: {:?}", self.popup_menu_toggle_theme); + println!("function `set_popup_progress()` finished!"); } /// Change visibility of the result label. @@ -450,15 +397,15 @@ impl PolicycheckState { } } -/// upported states for our view +/// Supported methods handled inside the `PolicycheckState` impl State for PolicycheckState { - /// Initialize the state of widget PolicyState + /// Initialize the state of widgets inside `PolicycheckState` fn init(&mut self, _: &mut Registry, ctx: &mut Context<'_>) { let time_start= SystemTime::now(); trace!(target: "advotracker", policycheck_state = "init", status = "started"); - // Entities + // Initialize required entities self.button_menu = ctx .entity_of_child(ID_POLICY_CHECK_BUTTON_MENU) .expect("PolicycheckState.init: Can't find resource entity 'ID_POLICY_CHECK_BUTTON_MENU'."); @@ -495,7 +442,7 @@ impl State for PolicycheckState { trace!(target: "advotracker", policycheck_state = "init", status = "finished", duration = ?duration); } - /// Handle messages inside the state of widget Policycheck. + /// Handle messages for `PolicycheckState`. fn messages( &mut self, mut messages: MessageReader, @@ -504,29 +451,6 @@ impl State for PolicycheckState { ) { for message in messages.read::() { match message { - PolicycheckAction::ChangeTheme() => { - let theme_index = *PolicycheckView::selected_index_ref(&ctx.widget()); - - cfg_if! { - if #[cfg(windows)] { - match theme_index { - 0 => ctx.switch_theme(theme_default_dark()), - 1 => ctx.switch_theme(theme_default_light()), - 2 => ctx.switch_theme(theme_redox()), - 3 => ctx.switch_theme(theme_fluent_dark()), - 4 => ctx.switch_theme(theme_fluent_light()), - _ => {} - } - } else { - match theme_index { - 0 => ctx.switch_theme(theme_default_dark()), - 1 => ctx.switch_theme(theme_default_light()), - 2 => ctx.switch_theme(theme_redox()), - _ => {} - } - } - } - }, PolicycheckAction::AddProgress(increment) => { let old_width = ProgressBar::val_clone(&mut ctx.child(ID_POLICY_CHECK_PROGRESS_BAR)); @@ -539,12 +463,12 @@ impl State for PolicycheckState { ProgressBar::val_set(&mut ctx.child(ID_POLICY_CHECK_PROGRESS_BAR), 1.); } } - _ => (), + _ => (), } } } - /// Update the state of widget Policycheck. + /// Update the state of widgets inside the `Policycheck` view. 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 { @@ -575,9 +499,6 @@ impl State for PolicycheckState { } } } - PolicycheckAction::OpenMenu(entity) => { - self.open_menu(entity, ctx); - } PolicycheckAction::ParseEntry(text_box) => { self.parse_entry(text_box, ctx); } @@ -585,10 +506,8 @@ impl State for PolicycheckState { ctx.get_widget(policy_check_policy_number).set("enabled", false); //ctx.EventAdapter(FocusEvent::RemoveFocus(policy_check_policy_number)); } - PolicycheckAction::RemoveMenu(entity) => { - self.remove_menu(entity, ctx); - } PolicycheckAction::RemovePopup(entity) => { + //println!("WIP: remove popup"); self.remove_popup(entity, ctx); } PolicycheckAction::ResetProgress => { @@ -598,9 +517,6 @@ impl State for PolicycheckState { //self.last_focused = Some(); self.set_entry(policy_check_policy_number, ctx); } - PolicycheckAction::SetMenuPopup(_entity) => { - self.set_popup_menu(ctx); - } PolicycheckAction::SetProgress(value) => { if value >= 0. || value <= 1. { ProgressBar::val_set(&mut ctx.child(ID_POLICY_CHECK_PROGRESS_BAR), value); @@ -610,10 +526,6 @@ impl State for PolicycheckState { PolicycheckAction::SetProgressPopup(_entity) => { self.set_popup_progress(ctx); } - PolicycheckAction::SetToggleTheme(_entity) => { - self.set_menu_toggle_theme(ctx); - //self.remove_popup(_entity, ctx); - } PolicycheckAction::SetVisibility(_entity) => { TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT), Visibility::Collapsed); } @@ -632,163 +544,19 @@ impl State for PolicycheckState { // } } -/// Create a menu popup -fn create_menu(menu: Entity, ctx: &mut BuildContext<'_>) -> Entity { - Popup::new() - .id(ID_POLICY_CHECK_POPUP_MENU) - .style("container_menu") - .target(menu) - .open(true) - .width(280) - .height(140) - .on_key_down(move | _ctx, key_event | { - match key_event.key { - Key::Q(..) => { - //if is_ctrl_home_down(ctx) { - println!("got: Ctrl+Q"); - process::exit(0); - //} - }, - Key::Escape => { - println!("got: Escape"); - //ctx.get::(menu) - // .set_action(PolicycheckAction::RemoveMenu(menu)); - }, - _ => (), - }; - true - }) - .child( - Grid::new() - .id(ID_POLICY_CHECK_MENU) - .columns( - Columns::create() - .push("80") // Menu Button - .push("1") // Seperator - .push("*") // Keyboard Shortcut - .build(), - ) - .rows( - Rows::create() - .push("auto") - .push("auto") - .push("auto") - .build(), - ) - .child( - Button::new() - .id(ID_POLICY_CHECK_MENU_LABEL_ACCOUNT) - .style("button_menu") - .attach(Grid::row(0)) - .attach(Grid::column(0)) - .attach(Grid::column_span(2)) - .icon(material_icons_font::MD_PERSON) - .text("Account") - .build(ctx), - ) - .child( - Button::new() - .id(ID_POLICY_CHECK_MENU_LABEL_TOGGLE_THEME) - .style("button_menu") - .attach(Grid::row(1)) - .attach(Grid::column(0)) - .attach(Grid::column_span(2)) - .icon(material_icons_font::MD_EDIT) - .text("Toggle theme") - .on_click(move |states, _| { - states.get_mut::(menu) - .set_action(PolicycheckAction::SetToggleTheme(menu)); - true - }) - .build(ctx), - ) - .child( - Button::new() - .id(ID_POLICY_CHECK_MENU_LABEL_QUIT) - .style("button_menu") - .attach(Grid::row(2)) - .attach(Grid::column(0)) - .attach(Grid::column_span(2)) - .icon(material_icons_font::MD_SETTINGS_POWER) - .text("Quit") - .on_mouse_down(move |_states, _| { - process::exit(0); - }) - .build(ctx), - ) - .child( - TextBlock::new() - .id(ID_POLICY_CHECK_MENU_SHORTCUT_QUIT) - .style("button_menu") - .attach(Grid::row(2)) - .attach(Grid::column(2)) - .margin((0, 0, 16, 0)) - .h_align("end") - .v_align("center") - .text("CTRL+Q") - .build(ctx), - ) - .build(ctx), - ) - .build(ctx) -} - -/// Create a popup submenu to toogle the active theme -fn create_menu_toggle_theme(menu_toggle_theme: Entity, ctx: &mut BuildContext<'_>) -> Entity { - // define the list of supported themes - let mut themes = vec![ - "default_dark".to_string(), - "default_light".to_string(), - "redox".to_string(), - ]; - cfg_if! { - if #[cfg(windows)] { - themes.push("fluent_dark".to_string()); - themes.push("fluent_light".to_string()); - } - } - let themes_count = themes.len(); - - Popup::new() - .id(ID_POLICY_CHECK_POPUP_MENU_TOGGLE_THEME) - .style("container_menu") - .target(menu_toggle_theme) - .open(true) - .width(280) - .height(140) - .child( - ComboBox::new() - .attach(Grid::column(2)) - .attach(Grid::row(6)) - .count(themes_count) - .items_builder(move |bc, index| { - let theme_name = - PolicycheckView::themes_ref(&bc.get_widget(menu_toggle_theme))[index].clone(); - TextBlock::new().v_align("center").text(theme_name).build(bc) - }) - .on_changed("selected_index", move |ctx, _| { - ctx.send_message(PolicycheckAction::ChangeTheme, menu_toggle_theme); - println!("changed theme."); - }) - .selected_index(menu_toggle_theme) - .build(ctx), - ) - .build(ctx) -} - /// Create a progress popup with update status of an onging data import -fn create_popup_progress(popup_progress: Entity, ctx: &mut BuildContext<'_>) -> Entity { +fn create_popup_progress(id: Entity, ctx: &mut BuildContext<'_>) -> Entity { Popup::new() .id(ID_POLICY_CHECK_POPUP_PROGRESS) - .target(popup_progress) + .target(id) .open(true) - //.style("popup_progress") + .style("popup_progress") .width(280) .height(100) - .on_mouse_down(move |ctx, _| { + .on_click(move |ctx, _| { println!("create_popup_progress: on_click -> remove_popup(popup_progress)"); - ctx.get_mut::(popup_progress) - .set_action(PolicycheckAction::RemovePopup(popup_progress)); + ctx.get_mut::(id) + .set_action(PolicycheckAction::RemovePopup(id)); true }) .child( @@ -800,14 +568,14 @@ fn create_popup_progress(popup_progress: Entity, ctx: &mut BuildContext<'_>) -> .child( TextBlock::new() .id(ID_POLICY_CHECK_PROGRESS_TEXT) - //.style("textblock_progress") - .font_size(12) + .style("textblock_progress") .text("Importing data") .build(ctx) ) .child( ProgressBar::new() .id(ID_POLICY_CHECK_PROGRESS_BAR) + .style("progress_bar") .val(0) //.width(250) .build(ctx) @@ -815,9 +583,8 @@ fn create_popup_progress(popup_progress: Entity, ctx: &mut BuildContext<'_>) -> .child( TextBlock::new() .id(ID_POLICY_CHECK_PROGRESS_TIME) - //.style("textblock_progress") + .style("textblock_progress") .h_align("end") - .font_size(12) .text("Processing time") .build(ctx) ) diff --git a/advotracker/src/widgets/policycheck/policycheck_view.rs b/advotracker/src/widgets/policycheck/policycheck_view.rs index fceb64b..f409560 100644 --- a/advotracker/src/widgets/policycheck/policycheck_view.rs +++ b/advotracker/src/widgets/policycheck/policycheck_view.rs @@ -13,12 +13,11 @@ use crate::{ constants::*, structures::PolicyCheck, }, + //widgets::menu::menu_state::{MenuAction, MenuState}, widgets::policycheck::policycheck_state::{PolicycheckAction, PolicycheckState}, }; -type List = Vec; - -// Macro that initializes the widget structures/variables for our view +// Macro that initializes the widget structures/variables for the policy check view widget!( /// Dialog to enter a policy identifier/number. /// This identifier is checked agains a map of valid policy codes. @@ -27,10 +26,7 @@ widget!( lang: String, policy_check: PolicyCheck, policy_check_title: String, - policy_data_count: u32, - selected_index: i32, - theme_name: String, - themes: List + policy_data_count: u32 } ); @@ -39,14 +35,6 @@ widget!( impl Template for PolicycheckView { //fn template(self, policycheck_view: Entity, ctx: &mut BuildContext<'_>) -> Self { fn template(self, id: Entity, ctx: &mut BuildContext<'_>) -> Self { - let themes = vec![ - "default_dark".to_string(), - "default_light".to_string(), - "redox".to_string(), - "fluent_dark".to_string(), - "fluent_light".to_string(), - ]; - let policy_check_bottom_bar = Container::new() //.style(STYLE_BOTTOM_BAR) .attach(Grid::row(4)) @@ -83,14 +71,6 @@ impl Template for PolicycheckView { ) .build(ctx); - let policy_check_label_menu = TextBlock::new() - .id(ID_POLICY_CHECK_LABEL_MENU) - .style("menu") - .text("Help Menu") - .v_align("center") - .h_align("center") - .build(ctx); - let policy_check_button_menu = Button::new() .id(ID_POLICY_CHECK_BUTTON_MENU) .style("button_single_content") @@ -98,10 +78,10 @@ impl Template for PolicycheckView { .attach(Grid::column(2)) //.min_size(16, 16) .h_align("end") - .on_click(move |ctx, _| { - //ctx.get_mut::(policycheck_state) - ctx.get_mut::(id) - .set_action(PolicycheckAction::OpenMenu(policy_check_label_menu)); + .on_click(move |_ctx, _| { + println!("WIP: open menu popup from MenuView"); + // ctx.get_mut::(id) + // .set_action(MenuAction::CreateMenu(ID_MENU_STACK)); true }) .build(ctx); @@ -372,7 +352,6 @@ impl Template for PolicycheckView { self.name("PolicycheckView") // initialize struct (derived default macro) .policy_check(PolicyCheck::default()) - .themes(themes) .child( Grid::new() .id(ID_POLICY_CHECK_WIDGET)