Skip to content

Commit

Permalink
Implement run_action
Browse files Browse the repository at this point in the history
  • Loading branch information
elkowar committed Apr 27, 2022
1 parent f89b317 commit 344ac32
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 70 deletions.
12 changes: 0 additions & 12 deletions .vimspector.json

This file was deleted.

7 changes: 7 additions & 0 deletions crates/eww/src/state/scope_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ impl ScopeIndex {
}
}

#[derive(Debug)]
pub enum ScopeGraphEvent {
RemoveScope(ScopeIndex),
UpdateValue(ScopeIndex, VarName, DynVal),
}

/// A graph structure of scopes where each scope may inherit from another scope,
Expand Down Expand Up @@ -87,6 +89,11 @@ impl ScopeGraph {
ScopeGraphEvent::RemoveScope(scope_index) => {
self.remove_scope(scope_index);
}
ScopeGraphEvent::UpdateValue(scope_index, name, value) => {
if let Err(e) = self.update_value(scope_index, &name, value) {
log::error!("{}", e);
}
}
}
}

Expand Down
14 changes: 13 additions & 1 deletion crates/eww/src/widgets/build_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,23 @@ fn build_let_special_widget(
custom_widget_invocation: Option<Rc<CustomWidgetInvocation>>,
) -> Result<gtk::Widget> {
let child = widget_use.body.first().expect("no child in let");

// Evaluate explicitly here, so we don't keep linking the state changes here.
// If that was desired, it'd suffice to just pass the simplexprs as attributes to register_new_scope,
// rather than converting them into literals explicitly.
let mut defined_vars = HashMap::new();
for (name, expr) in widget_use.defined_vars.into_iter() {
let mut needed_vars = graph.lookup_variables_in_scope(calling_scope, &expr.collect_var_refs())?;
needed_vars.extend(defined_vars.clone().into_iter());
let value = expr.eval(&needed_vars)?;
defined_vars.insert(name, value);
}

let let_scope = graph.register_new_scope(
"let-widget".to_string(),
Some(calling_scope),
calling_scope,
widget_use.defined_vars.into_iter().map(|(k, v)| (AttrName(k.to_string()), v)).collect(),
defined_vars.into_iter().map(|(k, v)| (AttrName(k.to_string()), SimplExpr::Literal(v))).collect(),
)?;
let gtk_widget = build_gtk_widget(graph, widget_defs, let_scope, child.clone(), custom_widget_invocation)?;
let scope_graph_sender = graph.event_sender.clone();
Expand Down
9 changes: 5 additions & 4 deletions crates/eww/src/widgets/def_widget_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ macro_rules! def_widget {
// Get all the variables that are referred to in any of the attributes expressions
let required_vars: Vec<eww_shared_util::VarName> = attr_map
.values()
.flat_map(|expr| expr.as_ref().and_then(|x| x.try_into_simplexpr()).map(|x| x.collect_var_refs()).unwrap_or_default())
.flat_map(|expr| expr.as_ref().map(|x| x.collect_var_refs()).unwrap_or_default())
.collect();

$args.scope_graph.register_listener(
$args.calling_scope,
crate::state::scope::Listener {
crate::state::scope::Listener {
needed_variables: required_vars,
f: Box::new({
let $gtk_widget = gdk::glib::clone::Downgrade::downgrade(&$gtk_widget);
move |$scope_graph, values| {
move |#[allow(unused)] $scope_graph, values| {
let $gtk_widget = gdk::glib::clone::Upgrade::upgrade(&$gtk_widget).expect("Failed to upgrade widget ref");
// values is a map of all the variables that are required to evaluate the
// attributes expression.
Expand Down Expand Up @@ -79,7 +79,8 @@ macro_rules! def_widget {

(@value_depending_on_type $values:expr, $attr_name:ident : as_action $(? $(@ $optional:tt @)?)? $(= $default:expr)?) => {
match $attr_name {
Some(yuck::config::attr_value::AttrValue::Action(action)) => Some(action),
Some(yuck::config::attr_value::AttrValue::Action(action)) => Some(action.eval_exprs(&$values)?),
Some(yuck::config::attr_value::AttrValue::SimplExpr(expr)) => Some(ExecutableAction::Shell(expr.eval(&$values)?.as_string()?)),
_ => None,
}
};
Expand Down
29 changes: 20 additions & 9 deletions crates/eww/src/widgets/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::process::Command;

use anyhow::Result;
use yuck::config::attr_value::Action;
use yuck::config::attr_value::ExecutableAction;

use crate::state::scope_graph::{ScopeGraph, ScopeIndex};
use crate::state::scope_graph::{ScopeGraphEvent, ScopeIndex};

pub mod build_widget;
pub mod circular_progressbar;
Expand Down Expand Up @@ -66,13 +65,25 @@ mod test {
}
}

pub(self) fn run_action(graph: &mut ScopeGraph, scope: ScopeIndex, action: &Action) -> Result<()> {
pub(self) fn run_action<T>(
sender: tokio::sync::mpsc::UnboundedSender<ScopeGraphEvent>,
scope: ScopeIndex,
timeout: std::time::Duration,
action: &ExecutableAction,
args: &[T],
) where
T: 'static + std::fmt::Display + Send + Sync + Clone,
{
match action {
Action::Update(varname, expr) => {
let value = graph.evaluate_simplexpr_in_scope(scope, expr)?;
graph.update_value(scope, varname, value)?;
ExecutableAction::Update(varname, value) => {
let res = sender.send(ScopeGraphEvent::UpdateValue(scope, varname.clone(), value.clone()));
if let Err(e) = res {
log::error!("{}", e);
}
}
ExecutableAction::Shell(command) => {
run_command(timeout, command, args);
}
Action::Noop => {}
ExecutableAction::Noop => {}
}
Ok(())
}
Loading

0 comments on commit 344ac32

Please sign in to comment.