Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
elkowar committed Aug 16, 2023
1 parent e52d8c0 commit 197e366
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 52 deletions.
4 changes: 2 additions & 2 deletions crates/eww/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl<B: DisplayBackend> App<B> {
self.close_window(id)
} else {
log::debug!("Config: {}, id: {}", config_name, id);
let window_args: Vec<(VarName, DynVal)> = args
let window_args = args
.iter()
.filter(|(win_id, ..)| win_id.is_empty() || win_id == id)
.map(|(_, n, v)| (n.clone(), v.clone()))
Expand Down Expand Up @@ -232,7 +232,7 @@ impl<B: DisplayBackend> App<B> {
size,
monitor,
anchor,
args: args.unwrap_or_default(),
args: args.unwrap_or_default().into_iter().collect(),
})
} else if should_toggle {
self.close_window(&instance_id)
Expand Down
7 changes: 1 addition & 6 deletions crates/eww/src/display_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,7 @@ mod platform_x11 {
Ok(X11BackendConnection { conn, root_window: screen.root, atoms })
}

fn set_xprops_for(
&self,
window: &gtk::Window,
monitor: Monitor,
window_init: &WindowInitiator,
) -> Result<()> {
fn set_xprops_for(&self, window: &gtk::Window, monitor: Monitor, window_init: &WindowInitiator) -> Result<()> {
let monitor_rect = monitor.geometry();
let scale_factor = monitor.scale_factor() as u32;
let gdk_window = window.window().context("Couldn't get gdk window from gtk window")?;
Expand Down
17 changes: 4 additions & 13 deletions crates/eww/src/window_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ pub struct WindowArguments {
/// Instance ID of the window
pub instance_id: String,
pub anchor: Option<AnchorPoint>,
pub args: Vec<(VarName, DynVal)>,
pub args: HashMap<VarName, DynVal>,
pub monitor: Option<MonitorIdentifier>,
pub pos: Option<Coords>,
pub size: Option<Coords>,
}

impl WindowArguments {
pub fn new_from_args(id: String, config_name: String, mut args: Vec<(VarName, DynVal)>) -> Result<Self> {
pub fn new_from_args(id: String, config_name: String, mut args: HashMap<VarName, DynVal>) -> Result<Self> {
let initiator = WindowArguments {
config_name,
instance_id: id,
Expand All @@ -42,17 +42,8 @@ impl WindowArguments {
Ok(initiator)
}

pub fn extract_value_from_args<T: FromStr>(name: &str, args: &mut Vec<(VarName, DynVal)>) -> Result<Option<T>, T::Err> {
let var_name = name.to_string();
let pos = args.iter().position(|(n, _)| n.0 == var_name);

if let Some(pos) = pos {
let (_, val) = args.remove(pos);
let converted_val = T::from_str(&val.0)?;
Ok(Some(converted_val))
} else {
Ok(None)
}
pub fn extract_value_from_args<T: FromStr>(name: &str, args: &mut HashMap<VarName, DynVal>) -> Result<Option<T>, T::Err> {
args.remove(&VarName(name.to_string())).map(|x| T::from_str(&x.0)).transpose()
}

pub fn get_local_window_variables(&self, window_def: &WindowDefinition) -> Result<HashMap<VarName, DynVal>> {
Expand Down
34 changes: 20 additions & 14 deletions crates/yuck/src/config/backend_window_options.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::{str::FromStr, collections::HashMap};
use std::{collections::HashMap, str::FromStr};

use anyhow::Result;
use simplexpr::{SimplExpr, dynval::{DynVal, FromDynVal}, eval::EvalError};
use simplexpr::{
dynval::{DynVal, FromDynVal},
eval::EvalError,
SimplExpr,
};

use crate::{
enum_parse,
Expand Down Expand Up @@ -34,10 +38,7 @@ pub struct BackendWindowOptionsDef {

impl BackendWindowOptionsDef {
pub fn eval(&self, local_variables: &HashMap<VarName, DynVal>) -> Result<BackendWindowOptions, Error> {
Ok(BackendWindowOptions {
wayland: self.wayland.eval(local_variables)?,
x11: self.x11.eval(local_variables)?,
})
Ok(BackendWindowOptions { wayland: self.wayland.eval(local_variables)?, x11: self.x11.eval(local_variables)? })
}

pub fn from_attrs(attrs: &mut Attributes) -> DiagResult<Self> {
Expand All @@ -55,7 +56,7 @@ impl BackendWindowOptionsDef {
namespace: attrs.ast_optional("namespace")?,
};

Ok(Self { wayland, x11 })
Ok(Self { wayland, x11 })
}
}

Expand Down Expand Up @@ -94,7 +95,11 @@ impl X11BackendWindowOptionsDef {
Some(expr) => X11WindowType::from_dynval(&expr.eval(local_variables)?)?,
None => X11WindowType::default(),
},
wm_ignore: eval_opt_expr_as_bool(&self.wm_ignore, self.window_type.is_none() && self.struts.is_none(), local_variables)?,
wm_ignore: eval_opt_expr_as_bool(
&self.wm_ignore,
self.window_type.is_none() && self.struts.is_none(),
local_variables,
)?,
})
}
}
Expand All @@ -121,12 +126,16 @@ impl WlBackendWindowOptionsDef {
namespace: match &self.namespace {
Some(expr) => Some(expr.eval(local_variables)?.as_string()?),
None => None,
}
},
})
}
}

fn eval_opt_expr_as_bool(opt_expr: &Option<SimplExpr>, default: bool, local_variables: &HashMap<VarName, DynVal>) -> Result<bool, EvalError> {
fn eval_opt_expr_as_bool(
opt_expr: &Option<SimplExpr>,
default: bool,
local_variables: &HashMap<VarName, DynVal>,
) -> Result<bool, EvalError> {
Ok(match opt_expr {
Some(expr) => expr.eval(local_variables)?.as_bool()?,
None => default,
Expand Down Expand Up @@ -207,10 +216,7 @@ impl FromAstElementContent for X11StrutDefinitionExpr {
fn from_tail<I: Iterator<Item = Ast>>(_span: Span, mut iter: AstIterator<I>) -> DiagResult<Self> {
let mut attrs = iter.expect_key_values()?;
iter.expect_done().map_err(DiagError::from).note("Check if you are missing a colon in front of a key")?;
Ok(X11StrutDefinitionExpr {
side: attrs.ast_optional("side")?,
dist: attrs.ast_required("distance")?
})
Ok(X11StrutDefinitionExpr { side: attrs.ast_optional("side")?, dist: attrs.ast_required("distance")? })
}
}

Expand Down
18 changes: 12 additions & 6 deletions crates/yuck/src/config/window_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ use crate::{
},
};
use eww_shared_util::{Span, VarName};
use simplexpr::{dynval::{DynVal, FromDynVal}, eval::EvalError, SimplExpr};
use simplexpr::{
dynval::{DynVal, FromDynVal},
eval::EvalError,
SimplExpr,
};

use super::{
attributes::AttrSpec, backend_window_options::BackendWindowOptionsDef, widget_use::WidgetUse, window_geometry::WindowGeometryDef,
attributes::AttrSpec, backend_window_options::BackendWindowOptionsDef, widget_use::WidgetUse,
window_geometry::WindowGeometryDef,
};

#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -52,12 +57,15 @@ impl WindowDefinition {
})
}

pub fn eval_stacking(&self, local_variables: &HashMap<VarName, DynVal>) -> Result<WindowStacking, WindowStackingConversionError> {
pub fn eval_stacking(
&self,
local_variables: &HashMap<VarName, DynVal>,
) -> Result<WindowStacking, WindowStackingConversionError> {
match &self.stacking {
Some(stacking_expr) => match stacking_expr.eval(local_variables) {
Ok(val) => Ok(WindowStacking::from_dynval(&val)?),
Err(err) => Err(WindowStackingConversionError::EvalError(err)),
}
},
None => Ok(WindowStacking::Foreground),
}
}
Expand Down Expand Up @@ -137,5 +145,3 @@ impl std::str::FromStr for WindowStacking {
}
}
}

static EXPECTED_WINDOW_DEF_FORMAT: &str = r#"Expected format: `(defwindow name [] (contained-widgets))`"#;
23 changes: 12 additions & 11 deletions crates/yuck/src/config/window_geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ use crate::{
error::DiagResult,
format_diagnostic::ToDiagnostic,
parser::{ast::Ast, ast_iterator::AstIterator, from_ast::FromAstElementContent},
value::{Coords, coords, NumWithUnit},
value::{coords, Coords, NumWithUnit},
};

use super::window_definition::EnumParseError;
use eww_shared_util::{Span, VarName};
use serde::{Deserialize, Serialize};
use simplexpr::{SimplExpr, dynval::{DynVal, FromDynVal}, eval::EvalError};
use simplexpr::{
dynval::{DynVal, FromDynVal},
eval::EvalError,
SimplExpr,
};

#[derive(Debug, Clone, Copy, Eq, PartialEq, smart_default::SmartDefault, Serialize, Deserialize, strum::Display)]
pub enum AnchorAlignment {
Expand Down Expand Up @@ -130,7 +134,10 @@ impl CoordsDef {
}
}

fn convert_to_num_with_unit(opt_expr: &Option<SimplExpr>, local_variables: &HashMap<VarName, DynVal>) -> Result<NumWithUnit, Error> {
fn convert_to_num_with_unit(
opt_expr: &Option<SimplExpr>,
local_variables: &HashMap<VarName, DynVal>,
) -> Result<NumWithUnit, Error> {
Ok(match opt_expr {
Some(expr) => NumWithUnit::from_dynval(&expr.eval(local_variables)?)?,
None => NumWithUnit::default(),
Expand All @@ -154,14 +161,8 @@ impl FromAstElementContent for WindowGeometryDef {

Ok(WindowGeometryDef {
anchor_point: attrs.ast_optional("anchor")?,
size: CoordsDef {
x: attrs.ast_optional("width")?,
y: attrs.ast_optional("height")?,
},
offset: CoordsDef {
x: attrs.ast_optional("x")?,
y: attrs.ast_optional("y")?,
},
size: CoordsDef { x: attrs.ast_optional("width")?, y: attrs.ast_optional("height")? },
offset: CoordsDef { x: attrs.ast_optional("x")?, y: attrs.ast_optional("y")? },
})
}
}
Expand Down

0 comments on commit 197e366

Please sign in to comment.