Skip to content

Commit

Permalink
Improve window argument system
Browse files Browse the repository at this point in the history
  • Loading branch information
WilfSilver committed Apr 28, 2022
1 parent d6b11eb commit 17c930d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 37 deletions.
63 changes: 27 additions & 36 deletions crates/eww/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub enum DaemonCommand {
screen: Option<i32>,
should_toggle: bool,
sender: DaemonResponseSender,
args: Option<Vec<(VarName, DynVal)>>,
},
CloseWindows {
windows: Vec<String>,
Expand Down Expand Up @@ -75,6 +76,7 @@ pub struct WindowInitiator {
pub size: Option<Coords>,
pub monitor: Option<i32>,
pub anchor: Option<AnchorPoint>,
pub args: Vec<(VarName, DynVal)>,
}

impl WindowInitiator {
Expand All @@ -84,8 +86,9 @@ impl WindowInitiator {
size: Option<Coords>,
monitor: Option<i32>,
anchor: Option<AnchorPoint>,
args: Vec<(VarName, DynVal)>,
) -> Self {
WindowInitiator { config_name, pos, size, monitor, anchor }
WindowInitiator { config_name, pos, size, monitor, anchor, args }
}
}

Expand Down Expand Up @@ -192,7 +195,7 @@ impl App {
if should_toggle && self.open_windows.contains_key(w) {
self.close_window(w)
} else {
self.open_window(w, &WindowInitiator::new(w.clone(), None, None, None, None))
self.open_window(w, &WindowInitiator::new(w.clone(), None, None, None, None, Vec::new()))
}
})
.filter_map(Result::err);
Expand All @@ -207,6 +210,7 @@ impl App {
screen: monitor,
should_toggle,
sender,
args,
} => {
let id = instance_id.unwrap_or(window_name.clone());

Expand All @@ -220,7 +224,7 @@ impl App {
Ok(())
}
} else {
self.open_window(&id, &WindowInitiator::new(window_name, pos, size, monitor, anchor))
self.open_window(&id, &WindowInitiator::new(window_name, pos, size, monitor, anchor, args.unwrap_or(Vec::new())))
};
sender.respond_with_result(result)?;
}
Expand Down Expand Up @@ -346,21 +350,33 @@ impl App {
self.window_initiators.insert(instance_id.to_string(), initiator.clone());

let open_result: Result<_> = try {
let (window_name, params) = extract_window_info(&initiator.config_name);
let window_name: &str = &initiator.config_name;

let mut window_def = self.eww_config.get_window(window_name)?.clone();
window_def.geometry =
window_def.geometry.map(|x| x.override_if_given(initiator.anchor, initiator.pos, initiator.size));

if params.len() != window_def.expected_args.len() {
// Throw error
return Err(anyhow!("Error, {} arguments given but expected {}", params.len(), window_def.expected_args.len()));
}
// We will remove these parameters as we go through the required
let mut local_variables: HashMap<VarName, DynVal> = initiator.args.clone().into_iter().collect();

let mut local_variables: HashMap<VarName, DynVal> = HashMap::new();
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));
}
}
}

for i in 0..window_def.expected_args.len() {
local_variables.insert(VarName::from(window_def.expected_args[i].name.clone()), params[i].clone());
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 root_index = self.scope_graph.borrow().root_index;
Expand Down Expand Up @@ -556,28 +572,3 @@ pub fn get_window_rectangle(geometry: WindowGeometry, screen_rect: gdk::Rectangl
let y = screen_rect.y + offset_y + geometry.anchor_point.y.alignment_to_coordinate(height, screen_rect.height);
gdk::Rectangle { x, y, width, height }
}

fn extract_window_info(window_call: &str) -> (&str, Vec<DynVal>) {
if window_call.chars().last().unwrap() != ')' {
return (window_call, Vec::new());
} else {
match window_call.chars().rev().position(|c| c == '(') {
Some(param_start_index_from_end) => {
let param_start_index = window_call.len() - param_start_index_from_end;
let window_name = &window_call[..param_start_index - 1];
// Empty brackets e.g. window_name()
if param_start_index_from_end == 1 {
return (window_name, Vec::new());
} else {
// We want to get everything but the brackets
// | param_start_index | len() - 1
// window_name(param1,param2,param3)
// ^ start ^end
let param_str = &window_call[param_start_index..window_call.len() - 1];
return (window_name, param_str.split(',').map(|x| DynVal::from(x)).collect());
}
}
None => (window_call, Vec::new()),
}
}
}
6 changes: 5 additions & 1 deletion crates/eww/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ pub enum ActionWithServer {
/// If the window is already open, close it instead
#[structopt(long = "toggle")]
should_toggle: bool,

#[structopt(long, parse(try_from_str = parse_var_update_arg))]
args: Option<Vec<(VarName, DynVal)>>,
},

/// Open multiple windows at once.
Expand Down Expand Up @@ -215,7 +218,7 @@ impl ActionWithServer {
ActionWithServer::OpenMany { windows, should_toggle } => {
return with_response_channel(|sender| app::DaemonCommand::OpenMany { windows, should_toggle, sender });
}
ActionWithServer::OpenWindow { window_name, id, pos, size, screen, anchor, should_toggle } => {
ActionWithServer::OpenWindow { window_name, id, pos, size, screen, anchor, should_toggle, args } => {
return with_response_channel(|sender| app::DaemonCommand::OpenWindow {
window_name,
instance_id: id,
Expand All @@ -225,6 +228,7 @@ impl ActionWithServer {
screen,
should_toggle,
sender,
args,
})
}
ActionWithServer::CloseWindows { windows } => {
Expand Down

0 comments on commit 17c930d

Please sign in to comment.