From 6a59835e10e619606dd4260854cbf43d3c91ba2a Mon Sep 17 00:00:00 2001 From: Wilf Silver Date: Thu, 28 Apr 2022 21:08:37 +0100 Subject: [PATCH] Refactor local var logic + add default vars --- crates/eww/src/app.rs | 56 ++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/crates/eww/src/app.rs b/crates/eww/src/app.rs index 89a50277..9e0c7fc7 100644 --- a/crates/eww/src/app.rs +++ b/crates/eww/src/app.rs @@ -90,6 +90,39 @@ impl WindowInitiator { ) -> Self { WindowInitiator { config_name, pos, size, monitor, anchor, args } } + + pub fn get_local_window_variables(&self, id: &str, window_def: &WindowDefinition) -> Result> { + let mut local_variables: HashMap = HashMap::new(); + + local_variables.insert(VarName::from("id"), DynVal::from(id)); + if self.monitor.is_some() { + local_variables.insert(VarName::from("screen"), DynVal::from(self.monitor.unwrap())); + } + + local_variables.extend(self.args.clone().into_iter()); + + for attr in &window_def.expected_args { + let name = VarName::from(attr.name.clone()); + if !local_variables.contains_key(&name) { + if attr.optional { + local_variables.insert(name, DynVal::from(String::new())); + } else { + return Err(anyhow!("Error, {} was required when creating {} but was not given", attr.name, self.config_name)); + } + } + } + + if local_variables.len() != window_def.expected_args.len() { + let expected_args: HashSet<&String> = window_def.expected_args.iter().map(|x| &x.name.0).collect(); + let unexpected_vars: Vec = local_variables + .iter() + .filter_map(|(n,_)| if !expected_args.contains(&n.0) { Some(n.clone()) } else { None }) + .collect(); + return Err(anyhow!("{} were unexpectedly defined when creating window {}", unexpected_vars.join(","), self.config_name)); + } + + Ok(local_variables) + } } #[derive(Debug, Clone)] @@ -356,28 +389,7 @@ impl App { window_def.geometry = window_def.geometry.map(|x| x.override_if_given(initiator.anchor, initiator.pos, initiator.size)); - // We will remove these parameters as we go through the required - let mut local_variables: HashMap = initiator.args.clone().into_iter().collect(); - - for attr in &window_def.expected_args { - let name = VarName::from(attr.name.clone()); - if !local_variables.contains_key(&name) { - if attr.optional { - local_variables.insert(name, DynVal::from(String::new())); - } else { - return Err(anyhow!("Error, {} was required when creating {} but was not given", attr.name, window_name)); - } - } - } - - if local_variables.len() != window_def.expected_args.len() { - let expected_args: HashSet<&String> = window_def.expected_args.iter().map(|x| &x.name.0).collect(); - let unexpected_vars: Vec = local_variables - .iter() - .filter_map(|(n,_)| if !expected_args.contains(&n.0) { Some(n.clone()) } else { None }) - .collect(); - return Err(anyhow!("{} were unexpectedly defined when creating window {}", unexpected_vars.join(","), window_name)); - } + let local_variables = initiator.get_local_window_variables(instance_id, &window_def)?; let root_index = self.scope_graph.borrow().root_index;