Skip to content

Commit

Permalink
Refactor local var logic + add default vars
Browse files Browse the repository at this point in the history
  • Loading branch information
WilfSilver committed May 14, 2022
1 parent e53cc20 commit 6a59835
Showing 1 changed file with 34 additions and 22 deletions.
56 changes: 34 additions & 22 deletions crates/eww/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HashMap<VarName, DynVal>> {
let mut local_variables: HashMap<VarName, DynVal> = 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<VarName> = 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)]
Expand Down Expand Up @@ -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<VarName, DynVal> = 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<VarName> = 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;

Expand Down

0 comments on commit 6a59835

Please sign in to comment.