From 89a68421068e701e7bc4d8dc2e8f7d71acb7da30 Mon Sep 17 00:00:00 2001 From: Ralf Zerres Date: Mon, 2 Nov 2020 13:04:39 +0100 Subject: [PATCH] policycheck: introduce toggle theme * new functions: - popup_menu_toggle_theme - set_menu_toggle_theme - create_menu_toggle_theme - messages * rename function: menu -> popup_menu * reorder enum: PolicycheckAction * new actions: SetMenuPopup, SetToggleTheme Signed-off-by: Ralf Zerres --- .../widgets/policycheck/policycheck_state.rs | 115 ++++++++++++------ .../widgets/policycheck/policycheck_view.rs | 13 +- 2 files changed, 90 insertions(+), 38 deletions(-) diff --git a/advotracker/src/widgets/policycheck/policycheck_state.rs b/advotracker/src/widgets/policycheck/policycheck_state.rs index fe9b173..b14a912 100644 --- a/advotracker/src/widgets/policycheck/policycheck_state.rs +++ b/advotracker/src/widgets/policycheck/policycheck_state.rs @@ -9,6 +9,7 @@ use tracing::{error, info, trace}; use crate::{ widgets::global_state::GlobalState, + widgets::policycheck::policycheck_view::PolicycheckView, data::{ structures::{PolicyCode, PolicyDataList, PolicyList}, constants::*, @@ -19,24 +20,24 @@ use crate::{ /// Actions that can execute on the task view. #[derive(Debug, Clone, Copy)] pub enum PolicycheckAction { + AddProgress(f64), ClearEntry(Entity), ChangeTheme(), InputTextChanged(Entity), ImportData, OpenMenu(Entity), ParseEntry(Entity), - AddProgress(f64), - ResetProgress, - SetMenu(Entity), - SetProgress(f64), - SetProgressPopup(Entity), RemoveFocus(Entity), RemoveMenu(Entity), RemovePopup(Entity), + ResetProgress, + SetMenuPopup(Entity), + SetProgress(f64), + SetProgressPopup(Entity), + SetToggleTheme(Entity), SetEntry(Entity), SetVisibility(Entity), - TextChanged(Entity, usize), - ToggleTheme(Entity) + TextChanged(Entity, usize) } /// define valid environment variables provided via .env files @@ -60,6 +61,7 @@ pub struct PolicycheckState { policy_data_count: u64, policy_numbers: HashMap, popup_menu: Entity, + popup_menu_toggle_theme: Entity, progress_bar: Entity, progress_count: f64, progress_popup: Entity @@ -393,8 +395,10 @@ impl PolicycheckState { /// Set a toggle_theme menu fn set_menu_toggle_theme(&mut self, ctx: &mut Context<'_>) { let stack = ctx - .entity_of_child(ID_POLICY_CHECK_POPUP_MENU_TOGGLE_THEME) - .expect("PolicycheckState: Can't find entity of resource 'ID_POLICY_CHECK_MENU_TOGGLE_THEME'."); + .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(); @@ -482,6 +486,32 @@ impl State for PolicycheckState { trace!(target: "advotracker", policycheck_state = "init", status = "finished", duration = ?duration); } + /// Handle messages inside the widget state. + fn messages( + &mut self, + mut messages: MessageReader, + _registry: &mut Registry, + ctx: &mut Context<'_>, + ) { + for message in messages.read::() { + match message { + PolicycheckAction::ChangeTheme() => { + let theme_index = *PolicycheckView::selected_index_ref(&ctx.widget()); + + 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()), + _ => {} + } + }, + _ => (), + } + } + } + /// Update the widget state. fn update(&mut self, _registry: &mut Registry, ctx: &mut Context<'_>) { // // clear focus on focus moved @@ -508,9 +538,6 @@ impl State for PolicycheckState { ProgressBar::val_set(&mut ctx.child(ID_POLICY_CHECK_PROGRESS_BAR), 1.); } } - PolicycheckAction::ChangeTheme() => { - println!("Changing active theme."); - } PolicycheckAction::ClearEntry(policy_check_policy_number) => { ctx.get_widget(policy_check_policy_number).set("enabled", false); } @@ -551,7 +578,7 @@ impl State for PolicycheckState { //self.last_focused = Some(); self.set_entry(policy_check_policy_number, ctx); } - PolicycheckAction::SetMenu(_entity) => { + PolicycheckAction::SetMenuPopup(_entity) => { self.set_popup_menu(ctx); } PolicycheckAction::SetProgress(value) => { @@ -563,16 +590,17 @@ 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); } PolicycheckAction::TextChanged(entity, _index) => { self.set_entry(entity, ctx); } - PolicycheckAction::ToggleTheme(_entity) => { - println!("TODO: toggle active theme"); - //self.toggle_theme(entity, ctx); - } + _ => (), } } // Reset action @@ -585,18 +613,20 @@ impl State for PolicycheckState { } /// Create a menu popup -fn create_menu(target: Entity, ctx: &mut BuildContext<'_>) -> Entity { +fn create_menu(menu: Entity, ctx: &mut BuildContext<'_>) -> Entity { Popup::new() .id(ID_POLICY_CHECK_POPUP_MENU) .style("container_menu") - .target(target) + .target(menu) .open(true) .width(280) .height(140) .on_mouse_down(move |ctx, _| { - println!("on_click -> remove_menu()"); - ctx.get_mut::(target) - .action(PolicycheckAction::RemoveMenu(target)); + println!("create_menu: on_click -> remove_popup(menu)"); + ctx.get_mut::(menu) + .action(PolicycheckAction::RemoveMenu(menu)); + // ToDo: print the entity id! + //.action(PolicycheckAction::RemovePopup(menu)); true }) .child( @@ -640,8 +670,8 @@ fn create_menu(target: Entity, ctx: &mut BuildContext<'_>) -> Entity { .icon(material_icons_font::MD_EDIT) .text("Toggle theme") .on_click(move |states, _| { - states.get_mut::(target) - .action(PolicycheckAction::ToggleTheme(target)); + states.get_mut::(menu) + .action(PolicycheckAction::SetToggleTheme(menu)); true }) .build(ctx), @@ -678,11 +708,21 @@ fn create_menu(target: Entity, ctx: &mut BuildContext<'_>) -> Entity { } /// Create a popup submenu to toogle the active theme -fn create_menu_toggle_theme(target: Entity, ctx: &mut BuildContext<'_>) -> Entity { +fn create_menu_toggle_theme(menu_toggle_theme: Entity, ctx: &mut BuildContext<'_>) -> Entity { + // define the list of supported themes + let themes = vec![ + "default_dark".to_string(), + "default_light".to_string(), + "redox".to_string(), + "fluent_dark".to_string(), + "fluent_light".to_string(), + ]; + let themes_count = themes.len(); + Popup::new() .id(ID_POLICY_CHECK_POPUP_MENU_TOGGLE_THEME) .style("container_menu") - .target(target) + .target(menu_toggle_theme) .open(true) .width(280) .height(140) @@ -690,35 +730,36 @@ fn create_menu_toggle_theme(target: Entity, ctx: &mut BuildContext<'_>) -> Entit ComboBox::new() .attach(Grid::column(2)) .attach(Grid::row(6)) - //.count(themes_count) + .count(themes_count) .items_builder(move |bc, index| { let theme_name = - PolicycheckView::themes_ref(&bc.get_widget(target))[index].clone(); - //TextBlock::new().v_align("center").text(theme_name).build(bc) - TextBlock::new().v_align("center").text("redox").build(bc) + 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, target); + ctx.send_message(PolicycheckAction::ChangeTheme, menu_toggle_theme); + println!("changed theme."); + }) - .selected_index(target) + .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(target: Entity, ctx: &mut BuildContext<'_>) -> Entity { +fn create_popup_progress(popup_progress: Entity, ctx: &mut BuildContext<'_>) -> Entity { Popup::new() .id(ID_POLICY_CHECK_POPUP_PROGRESS) - .target(target) + .target(popup_progress) .open(true) //.style("popup_progress") .width(280) .height(100) .on_mouse_down(move |ctx, _| { - println!("on_click -> remove_popup_progress()"); - ctx.get_mut::(target) - .action(PolicycheckAction::RemovePopup(target)); + println!("create_popup_progress: on_click -> remove_popup(popup_progress)"); + ctx.get_mut::(popup_progress) + .action(PolicycheckAction::RemovePopup(popup_progress)); true }) .child( diff --git a/advotracker/src/widgets/policycheck/policycheck_view.rs b/advotracker/src/widgets/policycheck/policycheck_view.rs index 706c3dd..4d4ec38 100644 --- a/advotracker/src/widgets/policycheck/policycheck_view.rs +++ b/advotracker/src/widgets/policycheck/policycheck_view.rs @@ -25,7 +25,10 @@ widget!( lang: String, policy_check: PolicyCheck, policy_check_title: String, - policy_data_count: u32 + policy_data_count: u32, + selected_index: i32, + theme_name: String, + themes: List } ); @@ -34,6 +37,13 @@ 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) @@ -355,6 +365,7 @@ 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)