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 <ralf.zerres@networkx.de>
This commit is contained in:
2020-11-02 13:04:39 +01:00
parent 76e6e7503f
commit 89a6842106
2 changed files with 90 additions and 38 deletions

View File

@@ -9,6 +9,7 @@ use tracing::{error, info, trace};
use crate::{ use crate::{
widgets::global_state::GlobalState, widgets::global_state::GlobalState,
widgets::policycheck::policycheck_view::PolicycheckView,
data::{ data::{
structures::{PolicyCode, PolicyDataList, PolicyList}, structures::{PolicyCode, PolicyDataList, PolicyList},
constants::*, constants::*,
@@ -19,24 +20,24 @@ use crate::{
/// Actions that can execute on the task view. /// Actions that can execute on the task view.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum PolicycheckAction { pub enum PolicycheckAction {
AddProgress(f64),
ClearEntry(Entity), ClearEntry(Entity),
ChangeTheme(), ChangeTheme(),
InputTextChanged(Entity), InputTextChanged(Entity),
ImportData, ImportData,
OpenMenu(Entity), OpenMenu(Entity),
ParseEntry(Entity), ParseEntry(Entity),
AddProgress(f64),
ResetProgress,
SetMenu(Entity),
SetProgress(f64),
SetProgressPopup(Entity),
RemoveFocus(Entity), RemoveFocus(Entity),
RemoveMenu(Entity), RemoveMenu(Entity),
RemovePopup(Entity), RemovePopup(Entity),
ResetProgress,
SetMenuPopup(Entity),
SetProgress(f64),
SetProgressPopup(Entity),
SetToggleTheme(Entity),
SetEntry(Entity), SetEntry(Entity),
SetVisibility(Entity), SetVisibility(Entity),
TextChanged(Entity, usize), TextChanged(Entity, usize)
ToggleTheme(Entity)
} }
/// define valid environment variables provided via .env files /// define valid environment variables provided via .env files
@@ -60,6 +61,7 @@ pub struct PolicycheckState {
policy_data_count: u64, policy_data_count: u64,
policy_numbers: HashMap<u64, PolicyCode>, policy_numbers: HashMap<u64, PolicyCode>,
popup_menu: Entity, popup_menu: Entity,
popup_menu_toggle_theme: Entity,
progress_bar: Entity, progress_bar: Entity,
progress_count: f64, progress_count: f64,
progress_popup: Entity progress_popup: Entity
@@ -393,8 +395,10 @@ impl PolicycheckState {
/// Set a toggle_theme menu /// Set a toggle_theme menu
fn set_menu_toggle_theme(&mut self, ctx: &mut Context<'_>) { fn set_menu_toggle_theme(&mut self, ctx: &mut Context<'_>) {
let stack = ctx let stack = ctx
.entity_of_child(ID_POLICY_CHECK_POPUP_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_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 current_entity = ctx.entity();
let build_context = &mut ctx.build_context(); 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); 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::<PolicycheckAction>() {
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. /// Update the widget state.
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 // // 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.); ProgressBar::val_set(&mut ctx.child(ID_POLICY_CHECK_PROGRESS_BAR), 1.);
} }
} }
PolicycheckAction::ChangeTheme() => {
println!("Changing active theme.");
}
PolicycheckAction::ClearEntry(policy_check_policy_number) => { PolicycheckAction::ClearEntry(policy_check_policy_number) => {
ctx.get_widget(policy_check_policy_number).set("enabled", false); ctx.get_widget(policy_check_policy_number).set("enabled", false);
} }
@@ -551,7 +578,7 @@ impl State for PolicycheckState {
//self.last_focused = Some(); //self.last_focused = Some();
self.set_entry(policy_check_policy_number, ctx); self.set_entry(policy_check_policy_number, ctx);
} }
PolicycheckAction::SetMenu(_entity) => { PolicycheckAction::SetMenuPopup(_entity) => {
self.set_popup_menu(ctx); self.set_popup_menu(ctx);
} }
PolicycheckAction::SetProgress(value) => { PolicycheckAction::SetProgress(value) => {
@@ -563,16 +590,17 @@ impl State for PolicycheckState {
PolicycheckAction::SetProgressPopup(_entity) => { PolicycheckAction::SetProgressPopup(_entity) => {
self.set_popup_progress(ctx); self.set_popup_progress(ctx);
} }
PolicycheckAction::SetToggleTheme(_entity) => {
self.set_menu_toggle_theme(ctx);
self.remove_popup(_entity, ctx);
}
PolicycheckAction::SetVisibility(_entity) => { PolicycheckAction::SetVisibility(_entity) => {
TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT), Visibility::Collapsed); TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT), Visibility::Collapsed);
} }
PolicycheckAction::TextChanged(entity, _index) => { PolicycheckAction::TextChanged(entity, _index) => {
self.set_entry(entity, ctx); self.set_entry(entity, ctx);
} }
PolicycheckAction::ToggleTheme(_entity) => { _ => (),
println!("TODO: toggle active theme");
//self.toggle_theme(entity, ctx);
}
} }
} }
// Reset action // Reset action
@@ -585,18 +613,20 @@ impl State for PolicycheckState {
} }
/// Create a menu popup /// Create a menu popup
fn create_menu(target: Entity, ctx: &mut BuildContext<'_>) -> Entity { fn create_menu(menu: Entity, ctx: &mut BuildContext<'_>) -> Entity {
Popup::new() Popup::new()
.id(ID_POLICY_CHECK_POPUP_MENU) .id(ID_POLICY_CHECK_POPUP_MENU)
.style("container_menu") .style("container_menu")
.target(target) .target(menu)
.open(true) .open(true)
.width(280) .width(280)
.height(140) .height(140)
.on_mouse_down(move |ctx, _| { .on_mouse_down(move |ctx, _| {
println!("on_click -> remove_menu()"); println!("create_menu: on_click -> remove_popup(menu)");
ctx.get_mut::<PolicycheckState>(target) ctx.get_mut::<PolicycheckState>(menu)
.action(PolicycheckAction::RemoveMenu(target)); .action(PolicycheckAction::RemoveMenu(menu));
// ToDo: print the entity id!
//.action(PolicycheckAction::RemovePopup(menu));
true true
}) })
.child( .child(
@@ -640,8 +670,8 @@ fn create_menu(target: Entity, ctx: &mut BuildContext<'_>) -> Entity {
.icon(material_icons_font::MD_EDIT) .icon(material_icons_font::MD_EDIT)
.text("Toggle theme") .text("Toggle theme")
.on_click(move |states, _| { .on_click(move |states, _| {
states.get_mut::<PolicycheckState>(target) states.get_mut::<PolicycheckState>(menu)
.action(PolicycheckAction::ToggleTheme(target)); .action(PolicycheckAction::SetToggleTheme(menu));
true true
}) })
.build(ctx), .build(ctx),
@@ -678,11 +708,21 @@ fn create_menu(target: Entity, ctx: &mut BuildContext<'_>) -> Entity {
} }
/// Create a popup submenu to toogle the active theme /// 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() Popup::new()
.id(ID_POLICY_CHECK_POPUP_MENU_TOGGLE_THEME) .id(ID_POLICY_CHECK_POPUP_MENU_TOGGLE_THEME)
.style("container_menu") .style("container_menu")
.target(target) .target(menu_toggle_theme)
.open(true) .open(true)
.width(280) .width(280)
.height(140) .height(140)
@@ -690,35 +730,36 @@ fn create_menu_toggle_theme(target: Entity, ctx: &mut BuildContext<'_>) -> Entit
ComboBox::new() ComboBox::new()
.attach(Grid::column(2)) .attach(Grid::column(2))
.attach(Grid::row(6)) .attach(Grid::row(6))
//.count(themes_count) .count(themes_count)
.items_builder(move |bc, index| { .items_builder(move |bc, index| {
let theme_name = let theme_name =
PolicycheckView::themes_ref(&bc.get_widget(target))[index].clone(); PolicycheckView::themes_ref(&bc.get_widget(menu_toggle_theme))[index].clone();
//TextBlock::new().v_align("center").text(theme_name).build(bc) TextBlock::new().v_align("center").text(theme_name).build(bc)
TextBlock::new().v_align("center").text("redox").build(bc)
}) })
.on_changed("selected_index", move |ctx, _| { .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),
) )
.build(ctx) .build(ctx)
} }
/// Create a progress popup with update status of an onging data import /// 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() Popup::new()
.id(ID_POLICY_CHECK_POPUP_PROGRESS) .id(ID_POLICY_CHECK_POPUP_PROGRESS)
.target(target) .target(popup_progress)
.open(true) .open(true)
//.style("popup_progress") //.style("popup_progress")
.width(280) .width(280)
.height(100) .height(100)
.on_mouse_down(move |ctx, _| { .on_mouse_down(move |ctx, _| {
println!("on_click -> remove_popup_progress()"); println!("create_popup_progress: on_click -> remove_popup(popup_progress)");
ctx.get_mut::<PolicycheckState>(target) ctx.get_mut::<PolicycheckState>(popup_progress)
.action(PolicycheckAction::RemovePopup(target)); .action(PolicycheckAction::RemovePopup(popup_progress));
true true
}) })
.child( .child(

View File

@@ -25,7 +25,10 @@ widget!(
lang: String, lang: String,
policy_check: PolicyCheck, policy_check: PolicyCheck,
policy_check_title: String, 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 { impl Template for PolicycheckView {
//fn template(self, policycheck_view: Entity, ctx: &mut BuildContext<'_>) -> Self { //fn template(self, policycheck_view: Entity, ctx: &mut BuildContext<'_>) -> Self {
fn template(self, id: 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() let policy_check_bottom_bar = Container::new()
//.style(STYLE_BOTTOM_BAR) //.style(STYLE_BOTTOM_BAR)
@@ -355,6 +365,7 @@ impl Template for PolicycheckView {
self.name("PolicycheckView") self.name("PolicycheckView")
// initialize struct (derived default macro) // initialize struct (derived default macro)
.policy_check(PolicyCheck::default()) .policy_check(PolicyCheck::default())
.themes(themes)
.child( .child(
Grid::new() Grid::new()
.id(ID_POLICY_CHECK_WIDGET) .id(ID_POLICY_CHECK_WIDGET)