diff --git a/niri-config/src/animations.rs b/niri-config/src/animations.rs index 346b6251..d50f4495 100644 --- a/niri-config/src/animations.rs +++ b/niri-config/src/animations.rs @@ -1,8 +1,13 @@ +use std::cell::RefCell; +use std::fs; +use std::rc::Rc; + +use knuffel::ast::SpannedNode; use knuffel::errors::DecodeError; use knuffel::Decode as _; -use crate::utils::{expect_only_children, parse_arg_node, MergeWith}; -use crate::FloatOrInt; +use crate::utils::{expand_home_path, expect_only_children, parse_arg_node, MergeWith}; +use crate::{FloatOrInt, Includes}; #[derive(Debug, Clone, PartialEq)] pub struct Animations { @@ -151,6 +156,57 @@ impl Default for WorkspaceSwitchAnim { } } +fn parse_custom_shader_path( + spanned: &SpannedNode, + ctx: &mut knuffel::decode::Context, +) -> Result> { + let mut shader_text = None; + + for (name, val) in &spanned.properties { + if &***name == "path" { + let path_val = knuffel::traits::DecodeScalar::decode(val, ctx)?; + let Some(path) = expand_home_path(path_val, spanned, ctx) else { + continue; + }; + + let includes = ctx.get::>>().unwrap(); + includes.borrow_mut().0.push(path.to_path_buf()); + + match fs::read_to_string(&path) { + Ok(text) => shader_text = Some(text), + Err(e) => ctx.emit_error(DecodeError::missing( + spanned, + format!("failed to read custom shader from {path:?}: {e}"), + )), + } + } else { + ctx.emit_error(DecodeError::unexpected( + &val.literal, + "property", + format!("unexpected property `{}`", name.escape_default()), + )); + } + } + + for arg in &spanned.arguments { + ctx.emit_error(DecodeError::unexpected( + &arg.literal, + "argument", + "unexpected argument", + )); + } + + for child in spanned.children() { + ctx.emit_error(DecodeError::unexpected( + child, + "child", + "unexpected children", + )); + } + + shader_text.ok_or_else(|| DecodeError::missing(spanned, "custom shader path missing")) +} + #[derive(Debug, Clone, PartialEq)] pub struct WindowOpenAnim { pub anim: Animation, @@ -383,7 +439,11 @@ where let mut custom_shader = None; let anim = Animation::decode_node(node, ctx, default, |child, ctx| { if &**child.node_name == "custom-shader" { - custom_shader = parse_arg_node("custom-shader", child, ctx)?; + custom_shader = if child.properties.contains_key("path") { + Some(parse_custom_shader_path(child, ctx)?) + } else { + Some(parse_arg_node("custom-shader", child, ctx)?) + }; Ok(true) } else { Ok(false) @@ -409,7 +469,11 @@ where let mut custom_shader = None; let anim = Animation::decode_node(node, ctx, default, |child, ctx| { if &**child.node_name == "custom-shader" { - custom_shader = parse_arg_node("custom-shader", child, ctx)?; + custom_shader = if child.properties.contains_key("path") { + Some(parse_custom_shader_path(child, ctx)?) + } else { + Some(parse_arg_node("custom-shader", child, ctx)?) + }; Ok(true) } else { Ok(false) @@ -435,7 +499,11 @@ where let mut custom_shader = None; let anim = Animation::decode_node(node, ctx, default, |child, ctx| { if &**child.node_name == "custom-shader" { - custom_shader = parse_arg_node("custom-shader", child, ctx)?; + custom_shader = if child.properties.contains_key("path") { + Some(parse_custom_shader_path(child, ctx)?) + } else { + Some(parse_arg_node("custom-shader", child, ctx)?) + }; Ok(true) } else { Ok(false)